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