blob: 82eed24be35f7e342710704b7dc3a10ff0133916 [file] [log] [blame]
Daniel Lehmann392ccec2010-11-01 20:50:03 -07001/*
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
17package com.android.contacts.datepicker;
18
19// This is a fork of the standard Android DatePicker that additionally allows toggling the year
Brian Attwellee891642014-06-16 17:46:36 -070020// on/off.
Daniel Lehmann392ccec2010-11-01 20:50:03 -070021
22import android.app.AlertDialog;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.content.DialogInterface.OnClickListener;
Daniel Lehmann392ccec2010-11-01 20:50:03 -070026import android.os.Bundle;
Daniel Lehmann392ccec2010-11-01 20:50:03 -070027import android.view.LayoutInflater;
28import android.view.View;
Daniel Lehmann392ccec2010-11-01 20:50:03 -070029
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070030import com.android.contacts.R;
31import com.android.contacts.datepicker.DatePicker.OnDateChangedListener;
Gary Mai0a49afa2016-12-05 15:53:58 -080032import com.android.contacts.util.DateUtils;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070033
Daniel Lehmann11812c52012-03-29 17:50:29 -070034import java.text.DateFormat;
Daniel Lehmann392ccec2010-11-01 20:50:03 -070035import java.util.Calendar;
36
37/**
38 * A simple dialog containing an {@link DatePicker}.
39 *
40 * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-datepicker.html">Date Picker
41 * tutorial</a>.</p>
42 */
43public class DatePickerDialog extends AlertDialog implements OnClickListener,
44 OnDateChangedListener {
45
Daniel Lehmann5a7a2692012-08-03 21:39:01 -070046 /** Magic year that represents "no year" */
47 public static int NO_YEAR = DatePicker.NO_YEAR;
48
Daniel Lehmann392ccec2010-11-01 20:50:03 -070049 private static final String YEAR = "year";
50 private static final String MONTH = "month";
51 private static final String DAY = "day";
52 private static final String YEAR_OPTIONAL = "year_optional";
53
54 private final DatePicker mDatePicker;
55 private final OnDateSetListener mCallBack;
Daniel Lehmann11812c52012-03-29 17:50:29 -070056 private final DateFormat mTitleDateFormat;
57 private final DateFormat mTitleNoYearDateFormat;
Daniel Lehmann392ccec2010-11-01 20:50:03 -070058
59 private int mInitialYear;
60 private int mInitialMonth;
61 private int mInitialDay;
62
63 /**
64 * The callback used to indicate the user is done filling in the date.
65 */
66 public interface OnDateSetListener {
67 /**
68 * @param view The view associated with this listener.
Daniel Lehmann5a7a2692012-08-03 21:39:01 -070069 * @param year The year that was set or {@link DatePickerDialog#NO_YEAR} if the user has
70 * not specified a year
Daniel Lehmann392ccec2010-11-01 20:50:03 -070071 * @param monthOfYear The month that was set (0-11) for compatibility
72 * with {@link java.util.Calendar}.
73 * @param dayOfMonth The day of the month that was set.
74 */
75 void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
76 }
77
78 /**
79 * @param context The context the dialog is to run in.
80 * @param callBack How the parent is notified that the date is set.
81 * @param year The initial year of the dialog
82 * @param monthOfYear The initial month of the dialog.
83 * @param dayOfMonth The initial day of the dialog.
84 */
85 public DatePickerDialog(Context context,
86 OnDateSetListener callBack,
87 int year,
88 int monthOfYear,
89 int dayOfMonth) {
90 this(context, callBack, year, monthOfYear, dayOfMonth, false);
91 }
92
93 /**
94 * @param context The context the dialog is to run in.
95 * @param callBack How the parent is notified that the date is set.
Daniel Lehmann5a7a2692012-08-03 21:39:01 -070096 * @param year The initial year of the dialog or {@link DatePickerDialog#NO_YEAR} if no year
97 * has been specified
Daniel Lehmann392ccec2010-11-01 20:50:03 -070098 * @param monthOfYear The initial month of the dialog.
99 * @param dayOfMonth The initial day of the dialog.
100 * @param yearOptional Whether the year can be toggled by the user
101 */
102 public DatePickerDialog(Context context,
103 OnDateSetListener callBack,
104 int year,
105 int monthOfYear,
106 int dayOfMonth,
107 boolean yearOptional) {
Brian Attwellec0e0962015-05-21 15:01:09 -0700108 // Don't pass a theme id. Instead use the default alert dialog theme.
109 this(context, /* themeId = */ -1, callBack, year, monthOfYear, dayOfMonth,
Brian Attwellee891642014-06-16 17:46:36 -0700110 yearOptional);
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700111 }
112
113 /**
114 * @param context The context the dialog is to run in.
115 * @param theme the theme to apply to this dialog
116 * @param callBack How the parent is notified that the date is set.
Daniel Lehmann5a7a2692012-08-03 21:39:01 -0700117 * @param year The initial year of the dialog or {@link DatePickerDialog#NO_YEAR} if no year
118 * has been specified
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700119 * @param monthOfYear The initial month of the dialog.
120 * @param dayOfMonth The initial day of the dialog.
121 */
122 public DatePickerDialog(Context context,
123 int theme,
124 OnDateSetListener callBack,
125 int year,
126 int monthOfYear,
127 int dayOfMonth) {
128 this(context, theme, callBack, year, monthOfYear, dayOfMonth, false);
129 }
130
131 /**
132 * @param context The context the dialog is to run in.
133 * @param theme the theme to apply to this dialog
134 * @param callBack How the parent is notified that the date is set.
Daniel Lehmann5a7a2692012-08-03 21:39:01 -0700135 * @param year The initial year of the dialog or {@link DatePickerDialog#NO_YEAR} if no
136 * year has been specified.
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700137 * @param monthOfYear The initial month of the dialog.
138 * @param dayOfMonth The initial day of the dialog.
139 * @param yearOptional Whether the year can be toggled by the user
140 */
141 public DatePickerDialog(Context context,
142 int theme,
143 OnDateSetListener callBack,
144 int year,
145 int monthOfYear,
146 int dayOfMonth,
147 boolean yearOptional) {
148 super(context, theme);
149
150 mCallBack = callBack;
151 mInitialYear = year;
152 mInitialMonth = monthOfYear;
153 mInitialDay = dayOfMonth;
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700154
Daniel Lehmann11812c52012-03-29 17:50:29 -0700155 mTitleDateFormat = DateFormat.getDateInstance(DateFormat.FULL);
Yorke Leec7ea6e92012-12-11 14:55:41 -0800156 mTitleNoYearDateFormat = DateUtils.getLocalizedDateFormatWithoutYear(getContext());
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700157 updateTitle(mInitialYear, mInitialMonth, mInitialDay);
158
Brian Attwellee891642014-06-16 17:46:36 -0700159 setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set),
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700160 this);
161 setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel),
162 (OnClickListener) null);
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700163
164 LayoutInflater inflater =
165 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
166 View view = inflater.inflate(R.layout.date_picker_dialog, null);
167 setView(view);
168 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
169 mDatePicker.init(mInitialYear, mInitialMonth, mInitialDay, yearOptional, this);
170 }
171
172 @Override
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700173 public void onClick(DialogInterface dialog, int which) {
174 if (mCallBack != null) {
175 mDatePicker.clearFocus();
176 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
177 mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
178 }
179 }
180
Daniel Lehmann11812c52012-03-29 17:50:29 -0700181 @Override
182 public void onDateChanged(DatePicker view, int year, int month, int day) {
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700183 updateTitle(year, month, day);
184 }
185
186 public void updateDate(int year, int monthOfYear, int dayOfMonth) {
187 mInitialYear = year;
188 mInitialMonth = monthOfYear;
189 mInitialDay = dayOfMonth;
190 mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
191 }
192
193 private void updateTitle(int year, int month, int day) {
Daniel Lehmann5a7a2692012-08-03 21:39:01 -0700194 final Calendar calendar = Calendar.getInstance();
195 calendar.set(Calendar.YEAR, year);
196 calendar.set(Calendar.MONTH, month);
197 calendar.set(Calendar.DAY_OF_MONTH, day);
Daniel Lehmann11812c52012-03-29 17:50:29 -0700198 final DateFormat dateFormat =
Daniel Lehmann5a7a2692012-08-03 21:39:01 -0700199 year == NO_YEAR ? mTitleNoYearDateFormat : mTitleDateFormat;
200 setTitle(dateFormat.format(calendar.getTime()));
Daniel Lehmann392ccec2010-11-01 20:50:03 -0700201 }
202
203 @Override
204 public Bundle onSaveInstanceState() {
205 Bundle state = super.onSaveInstanceState();
206 state.putInt(YEAR, mDatePicker.getYear());
207 state.putInt(MONTH, mDatePicker.getMonth());
208 state.putInt(DAY, mDatePicker.getDayOfMonth());
209 state.putBoolean(YEAR_OPTIONAL, mDatePicker.isYearOptional());
210 return state;
211 }
212
213 @Override
214 public void onRestoreInstanceState(Bundle savedInstanceState) {
215 super.onRestoreInstanceState(savedInstanceState);
216 int year = savedInstanceState.getInt(YEAR);
217 int month = savedInstanceState.getInt(MONTH);
218 int day = savedInstanceState.getInt(DAY);
219 boolean yearOptional = savedInstanceState.getBoolean(YEAR_OPTIONAL);
220 mDatePicker.init(year, month, day, yearOptional, this);
221 updateTitle(year, month, day);
222 }
223}