Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.app;
     18 
     19 import android.content.Context;
     20 import android.content.DialogInterface;
     21 import android.content.DialogInterface.OnClickListener;
     22 import android.os.Bundle;
     23 import android.text.format.DateUtils;
     24 import android.util.TypedValue;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.widget.Button;
     28 import android.widget.DatePicker;
     29 import android.widget.DatePicker.OnDateChangedListener;
     30 import android.widget.DatePicker.ValidationCallback;
     31 
     32 import com.android.internal.R;
     33 
     34 import java.util.Calendar;
     35 
     36 /**
     37  * A simple dialog containing an {@link android.widget.DatePicker}.
     38  *
     39  * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
     40  * guide.</p>
     41  */
     42 public class DatePickerDialog extends AlertDialog implements OnClickListener,
     43         OnDateChangedListener {
     44 
     45     private static final String YEAR = "year";
     46     private static final String MONTH = "month";
     47     private static final String DAY = "day";
     48 
     49     private final DatePicker mDatePicker;
     50     private final OnDateSetListener mDateSetListener;
     51     private final Calendar mCalendar;
     52 
     53     private boolean mTitleNeedsUpdate = true;
     54 
     55     /**
     56      * The callback used to indicate the user is done filling in the date.
     57      */
     58     public interface OnDateSetListener {
     59 
     60         /**
     61          * @param view The view associated with this listener.
     62          * @param year The year that was set.
     63          * @param monthOfYear The month that was set (0-11) for compatibility
     64          *  with {@link java.util.Calendar}.
     65          * @param dayOfMonth The day of the month that was set.
     66          */
     67         void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
     68     }
     69 
     70     /**
     71      * @param context The context the dialog is to run in.
     72      * @param callBack How the parent is notified that the date is set.
     73      * @param year The initial year of the dialog.
     74      * @param monthOfYear The initial month of the dialog.
     75      * @param dayOfMonth The initial day of the dialog.
     76      */
     77     public DatePickerDialog(Context context,
     78             OnDateSetListener callBack,
     79             int year,
     80             int monthOfYear,
     81             int dayOfMonth) {
     82         this(context, 0, callBack, year, monthOfYear, dayOfMonth);
     83     }
     84 
     85     static int resolveDialogTheme(Context context, int resid) {
     86         if (resid == 0) {
     87             final TypedValue outValue = new TypedValue();
     88             context.getTheme().resolveAttribute(R.attr.datePickerDialogTheme, outValue, true);
     89             return outValue.resourceId;
     90         } else {
     91             return resid;
     92         }
     93     }
     94 
     95     /**
     96      * @param context The context the dialog is to run in.
     97      * @param theme the theme to apply to this dialog
     98      * @param listener How the parent is notified that the date is set.
     99      * @param year The initial year of the dialog.
    100      * @param monthOfYear The initial month of the dialog.
    101      * @param dayOfMonth The initial day of the dialog.
    102      */
    103     public DatePickerDialog(Context context, int theme, OnDateSetListener listener, int year,
    104             int monthOfYear, int dayOfMonth) {
    105         super(context, resolveDialogTheme(context, theme));
    106 
    107         mDateSetListener = listener;
    108         mCalendar = Calendar.getInstance();
    109 
    110         final Context themeContext = getContext();
    111         final LayoutInflater inflater = LayoutInflater.from(themeContext);
    112         final View view = inflater.inflate(R.layout.date_picker_dialog, null);
    113         setView(view);
    114         setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
    115         setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
    116         setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
    117 
    118         mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
    119         mDatePicker.init(year, monthOfYear, dayOfMonth, this);
    120         mDatePicker.setValidationCallback(mValidationCallback);
    121     }
    122 
    123     @Override
    124     public void onDateChanged(DatePicker view, int year, int month, int day) {
    125         mDatePicker.init(year, month, day, this);
    126         updateTitle(year, month, day);
    127     }
    128 
    129     @Override
    130     public void onClick(DialogInterface dialog, int which) {
    131         switch (which) {
    132             case BUTTON_POSITIVE:
    133                 if (mDateSetListener != null) {
    134                     mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
    135                             mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
    136                 }
    137                 break;
    138             case BUTTON_NEGATIVE:
    139                 cancel();
    140                 break;
    141         }
    142     }
    143 
    144     /**
    145      * Gets the {@link DatePicker} contained in this dialog.
    146      *
    147      * @return The calendar view.
    148      */
    149     public DatePicker getDatePicker() {
    150         return mDatePicker;
    151     }
    152 
    153     /**
    154      * Sets the current date.
    155      *
    156      * @param year The date year.
    157      * @param monthOfYear The date month.
    158      * @param dayOfMonth The date day of month.
    159      */
    160     public void updateDate(int year, int monthOfYear, int dayOfMonth) {
    161         mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    162     }
    163 
    164     private void updateTitle(int year, int month, int day) {
    165         if (!mDatePicker.getCalendarViewShown()) {
    166             mCalendar.set(Calendar.YEAR, year);
    167             mCalendar.set(Calendar.MONTH, month);
    168             mCalendar.set(Calendar.DAY_OF_MONTH, day);
    169             String title = DateUtils.formatDateTime(mContext,
    170                     mCalendar.getTimeInMillis(),
    171                     DateUtils.FORMAT_SHOW_DATE
    172                     | DateUtils.FORMAT_SHOW_WEEKDAY
    173                     | DateUtils.FORMAT_SHOW_YEAR
    174                     | DateUtils.FORMAT_ABBREV_MONTH
    175                     | DateUtils.FORMAT_ABBREV_WEEKDAY);
    176             setTitle(title);
    177             mTitleNeedsUpdate = true;
    178         } else {
    179             if (mTitleNeedsUpdate) {
    180                 mTitleNeedsUpdate = false;
    181                 setTitle(R.string.date_picker_dialog_title);
    182             }
    183         }
    184     }
    185 
    186     @Override
    187     public Bundle onSaveInstanceState() {
    188         final Bundle state = super.onSaveInstanceState();
    189         state.putInt(YEAR, mDatePicker.getYear());
    190         state.putInt(MONTH, mDatePicker.getMonth());
    191         state.putInt(DAY, mDatePicker.getDayOfMonth());
    192         return state;
    193     }
    194 
    195     @Override
    196     public void onRestoreInstanceState(Bundle savedInstanceState) {
    197         super.onRestoreInstanceState(savedInstanceState);
    198         final int year = savedInstanceState.getInt(YEAR);
    199         final int month = savedInstanceState.getInt(MONTH);
    200         final int day = savedInstanceState.getInt(DAY);
    201         mDatePicker.init(year, month, day, this);
    202     }
    203 
    204     private final ValidationCallback mValidationCallback = new ValidationCallback() {
    205         @Override
    206         public void onValidationChanged(boolean valid) {
    207             final Button positive = getButton(BUTTON_POSITIVE);
    208             if (positive != null) {
    209                 positive.setEnabled(valid);
    210             }
    211         }
    212     };
    213 }
    214