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.TextUtils.TruncateAt;
     24 import android.text.format.DateFormat;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.widget.DatePicker;
     28 import android.widget.TextView;
     29 import android.widget.DatePicker.OnDateChangedListener;
     30 
     31 import com.android.internal.R;
     32 
     33 import java.text.DateFormatSymbols;
     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}resources/tutorials/views/hello-datepicker.html">Date Picker
     40  * tutorial</a>.</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 mCallBack;
     51     private final Calendar mCalendar;
     52     private final java.text.DateFormat mTitleDateFormat;
     53     private final String[] mWeekDays;
     54 
     55     private int mInitialYear;
     56     private int mInitialMonth;
     57     private int mInitialDay;
     58 
     59     /**
     60      * The callback used to indicate the user is done filling in the date.
     61      */
     62     public interface OnDateSetListener {
     63 
     64         /**
     65          * @param view The view associated with this listener.
     66          * @param year The year that was set.
     67          * @param monthOfYear The month that was set (0-11) for compatibility
     68          *  with {@link java.util.Calendar}.
     69          * @param dayOfMonth The day of the month that was set.
     70          */
     71         void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
     72     }
     73 
     74     /**
     75      * @param context The context the dialog is to run in.
     76      * @param callBack How the parent is notified that the date is set.
     77      * @param year The initial year of the dialog.
     78      * @param monthOfYear The initial month of the dialog.
     79      * @param dayOfMonth The initial day of the dialog.
     80      */
     81     public DatePickerDialog(Context context,
     82             OnDateSetListener callBack,
     83             int year,
     84             int monthOfYear,
     85             int dayOfMonth) {
     86         this(context, com.android.internal.R.style.Theme_Dialog_Alert,
     87                 callBack, year, monthOfYear, dayOfMonth);
     88     }
     89 
     90     /**
     91      * @param context The context the dialog is to run in.
     92      * @param theme the theme to apply to this dialog
     93      * @param callBack How the parent is notified that the date is set.
     94      * @param year The initial year of the dialog.
     95      * @param monthOfYear The initial month of the dialog.
     96      * @param dayOfMonth The initial day of the dialog.
     97      */
     98     public DatePickerDialog(Context context,
     99             int theme,
    100             OnDateSetListener callBack,
    101             int year,
    102             int monthOfYear,
    103             int dayOfMonth) {
    104         super(context, theme);
    105 
    106         mCallBack = callBack;
    107         mInitialYear = year;
    108         mInitialMonth = monthOfYear;
    109         mInitialDay = dayOfMonth;
    110         DateFormatSymbols symbols = new DateFormatSymbols();
    111         mWeekDays = symbols.getShortWeekdays();
    112 
    113         mTitleDateFormat = java.text.DateFormat.
    114                                 getDateInstance(java.text.DateFormat.FULL);
    115         mCalendar = Calendar.getInstance();
    116         updateTitle(mInitialYear, mInitialMonth, mInitialDay);
    117 
    118         setButton(context.getText(R.string.date_time_set), this);
    119         setButton2(context.getText(R.string.cancel), (OnClickListener) null);
    120         setIcon(R.drawable.ic_dialog_time);
    121 
    122         LayoutInflater inflater =
    123                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    124         View view = inflater.inflate(R.layout.date_picker_dialog, null);
    125         setView(view);
    126         mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
    127         mDatePicker.init(mInitialYear, mInitialMonth, mInitialDay, this);
    128     }
    129 
    130     @Override
    131     public void show() {
    132         super.show();
    133 
    134         /* Sometimes the full month is displayed causing the title
    135          * to be very long, in those cases ensure it doesn't wrap to
    136          * 2 lines (as that looks jumpy) and ensure we ellipsize the end.
    137          */
    138         TextView title = (TextView) findViewById(R.id.alertTitle);
    139         title.setSingleLine();
    140         title.setEllipsize(TruncateAt.END);
    141     }
    142 
    143     public void onClick(DialogInterface dialog, int which) {
    144         if (mCallBack != null) {
    145             mDatePicker.clearFocus();
    146             mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
    147                     mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
    148         }
    149     }
    150 
    151     public void onDateChanged(DatePicker view, int year,
    152             int month, int day) {
    153         updateTitle(year, month, day);
    154     }
    155 
    156     public void updateDate(int year, int monthOfYear, int dayOfMonth) {
    157         mInitialYear = year;
    158         mInitialMonth = monthOfYear;
    159         mInitialDay = dayOfMonth;
    160         mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    161     }
    162 
    163     private void updateTitle(int year, int month, int day) {
    164         mCalendar.set(Calendar.YEAR, year);
    165         mCalendar.set(Calendar.MONTH, month);
    166         mCalendar.set(Calendar.DAY_OF_MONTH, day);
    167         setTitle(mTitleDateFormat.format(mCalendar.getTime()));
    168     }
    169 
    170     @Override
    171     public Bundle onSaveInstanceState() {
    172         Bundle state = super.onSaveInstanceState();
    173         state.putInt(YEAR, mDatePicker.getYear());
    174         state.putInt(MONTH, mDatePicker.getMonth());
    175         state.putInt(DAY, mDatePicker.getDayOfMonth());
    176         return state;
    177     }
    178 
    179     @Override
    180     public void onRestoreInstanceState(Bundle savedInstanceState) {
    181         super.onRestoreInstanceState(savedInstanceState);
    182         int year = savedInstanceState.getInt(YEAR);
    183         int month = savedInstanceState.getInt(MONTH);
    184         int day = savedInstanceState.getInt(DAY);
    185         mDatePicker.init(year, month, day, this);
    186         updateTitle(year, month, day);
    187     }
    188 }
    189