/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.tr4android.support.extension.picker.date; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.text.format.DateUtils; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import com.tr4android.appcompat.extension.R; import java.util.Calendar; /** * A simple dialog containing an {@link android.widget.DatePicker}. * * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> * guide.</p> */ public class AppCompatDatePickerDialog extends AlertDialog implements OnClickListener, AppCompatDatePicker.OnDateChangedListener { private static final String YEAR = "year"; private static final String MONTH = "month"; private static final String DAY = "day"; private final AppCompatDatePicker mDatePicker; private final OnDateSetListener mDateSetListener; /** * The callback used to indicate the user is done filling in the date. */ public interface OnDateSetListener { /** * @param view The view associated with this listener. * @param year The year that was set. * @param monthOfYear The month that was set (0-11) for compatibility * with {@link java.util.Calendar}. * @param dayOfMonth The day of the month that was set. */ void onDateSet(AppCompatDatePicker view, int year, int monthOfYear, int dayOfMonth); } /** * @param context The context the dialog is to run in. * @param listener How the parent is notified that the date is set. * @param year The initial year of the dialog. * @param monthOfYear The initial month of the dialog. * @param dayOfMonth The initial day of the dialog. */ public AppCompatDatePickerDialog(Context context, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { this(context, 0, listener, year, monthOfYear, dayOfMonth); } static int resolveDialogTheme(Context context, int resid) { if (resid == 0) { final TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(R.attr.datePickerDialogTheme, outValue, true); return outValue.resourceId; } else { return resid; } } /** * @param context The context the dialog is to run in. * @param theme the theme to apply to this dialog * @param listener How the parent is notified that the date is set. * @param year The initial year of the dialog. * @param monthOfYear The initial month of the dialog. * @param dayOfMonth The initial day of the dialog. */ public AppCompatDatePickerDialog(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { super(context, resolveDialogTheme(context, theme)); mDateSetListener = listener; final Context themeContext = getContext(); final LayoutInflater inflater = LayoutInflater.from(themeContext); final View view = inflater.inflate(R.layout.date_picker_dialog, null); setView(view); setButton(BUTTON_POSITIVE, themeContext.getString(android.R.string.ok), this); setButton(BUTTON_NEGATIVE, themeContext.getString(android.R.string.cancel), this); //setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); mDatePicker = (AppCompatDatePicker) view.findViewById(R.id.datePicker); mDatePicker.init(year, monthOfYear, dayOfMonth, this); mDatePicker.setValidationCallback(mValidationCallback); } @Override public void onDateChanged(AppCompatDatePicker view, int year, int month, int day) { mDatePicker.init(year, month, day, this); } @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case BUTTON_POSITIVE: if (mDateSetListener != null) { // Clearing focus forces the dialog to commit any pending // changes, e.g. typed text in a NumberPicker. mDatePicker.clearFocus(); mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); } break; case BUTTON_NEGATIVE: cancel(); break; } } /** * Gets the {@link AppCompatDatePicker} contained in this dialog. * * @return The calendar view. */ public AppCompatDatePicker getDatePicker() { return mDatePicker; } /** * Sets the current date. * * @param year The date year. * @param monthOfYear The date month. * @param dayOfMonth The date day of month. */ public void updateDate(int year, int monthOfYear, int dayOfMonth) { mDatePicker.updateDate(year, monthOfYear, dayOfMonth); } @Override public Bundle onSaveInstanceState() { final Bundle state = super.onSaveInstanceState(); state.putInt(YEAR, mDatePicker.getYear()); state.putInt(MONTH, mDatePicker.getMonth()); state.putInt(DAY, mDatePicker.getDayOfMonth()); return state; } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); final int year = savedInstanceState.getInt(YEAR); final int month = savedInstanceState.getInt(MONTH); final int day = savedInstanceState.getInt(DAY); mDatePicker.init(year, month, day, this); } private final AppCompatDatePicker.ValidationCallback mValidationCallback = new AppCompatDatePicker.ValidationCallback() { @Override public void onValidationChanged(boolean valid) { final Button positive = getButton(BUTTON_POSITIVE); if (positive != null) { positive.setEnabled(valid); } } }; }