org.akop.crosswords.fragment.DatePickerFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.akop.crosswords.fragment.DatePickerFragment.java

Source

// Copyright (c) 2014-2015 Akop Karapetyan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package org.akop.crosswords.fragment;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import org.akop.crosswords.R;
import org.joda.time.DateTime;

public class DatePickerFragment extends DialogFragment {
    public interface OnDateSelectedListener {
        void onDateSelected(DateTime dateTime);
    }

    public static Bundle createArgs(DateTime date) {
        Bundle bundle = new Bundle();
        bundle.putLong("dateMillis", date.getMillis());

        return bundle;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        DateTime dateTime = null;
        Bundle args = getArguments();
        if (args != null) {
            long dateMillis = args.getLong("dateMillis", -1);
            if (dateMillis != -1) {
                dateTime = new DateTime(dateMillis);
            }
        }

        if (dateTime == null) {
            dateTime = DateTime.now();
        }

        final DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(),
                R.style.Theme_Crosswords_Default_Dialog, null, dateTime.getYear(), dateTime.getMonthOfYear() - 1,
                dateTime.getDayOfMonth());

        // Workaround for the JellyBean bug
        // http://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel
        pickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        notifyListener(pickerDialog.getDatePicker());
                    }
                });
        pickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(android.R.string.cancel),
                (DialogInterface.OnClickListener) null);

        return pickerDialog;
    }

    private void notifyListener(DatePicker picker) {
        int year = picker.getYear();
        int month = picker.getMonth() + 1;
        int day = picker.getDayOfMonth();

        Activity activity = getActivity();
        if (activity instanceof OnDateSelectedListener) {
            OnDateSelectedListener listener = (OnDateSelectedListener) activity;
            listener.onDateSelected(new DateTime(year, month, day, 0, 0, 0));
        }
    }
}