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 com.android.contacts.datepicker; 18 19 // This is a fork of the standard Android DatePicker that additionally allows toggling the year 20 // on/off. 21 22 import android.app.AlertDialog; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.DialogInterface.OnClickListener; 26 import android.os.Bundle; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 30 import com.android.contacts.R; 31 import com.android.contacts.common.util.DateUtils; 32 import com.android.contacts.datepicker.DatePicker.OnDateChangedListener; 33 34 import java.text.DateFormat; 35 import 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 */ 43 public class DatePickerDialog extends AlertDialog implements OnClickListener, 44 OnDateChangedListener { 45 46 /** Magic year that represents "no year" */ 47 public static int NO_YEAR = DatePicker.NO_YEAR; 48 49 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; 56 private final DateFormat mTitleDateFormat; 57 private final DateFormat mTitleNoYearDateFormat; 58 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. 69 * @param year The year that was set or {@link DatePickerDialog#NO_YEAR} if the user has 70 * not specified a year 71 * @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 */ onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)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 */ DatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)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. 96 * @param year The initial year of the dialog or {@link DatePickerDialog#NO_YEAR} if no year 97 * has been specified 98 * @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 */ DatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth, boolean yearOptional)102 public DatePickerDialog(Context context, 103 OnDateSetListener callBack, 104 int year, 105 int monthOfYear, 106 int dayOfMonth, 107 boolean yearOptional) { 108 this(context, THEME_DEVICE_DEFAULT_LIGHT, callBack, year, monthOfYear, dayOfMonth, 109 yearOptional); 110 } 111 112 /** 113 * @param context The context the dialog is to run in. 114 * @param theme the theme to apply to this dialog 115 * @param callBack How the parent is notified that the date is set. 116 * @param year The initial year of the dialog or {@link DatePickerDialog#NO_YEAR} if no year 117 * has been specified 118 * @param monthOfYear The initial month of the dialog. 119 * @param dayOfMonth The initial day of the dialog. 120 */ DatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)121 public DatePickerDialog(Context context, 122 int theme, 123 OnDateSetListener callBack, 124 int year, 125 int monthOfYear, 126 int dayOfMonth) { 127 this(context, theme, callBack, year, monthOfYear, dayOfMonth, false); 128 } 129 130 /** 131 * @param context The context the dialog is to run in. 132 * @param theme the theme to apply to this dialog 133 * @param callBack How the parent is notified that the date is set. 134 * @param year The initial year of the dialog or {@link DatePickerDialog#NO_YEAR} if no 135 * year has been specified. 136 * @param monthOfYear The initial month of the dialog. 137 * @param dayOfMonth The initial day of the dialog. 138 * @param yearOptional Whether the year can be toggled by the user 139 */ DatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth, boolean yearOptional)140 public DatePickerDialog(Context context, 141 int theme, 142 OnDateSetListener callBack, 143 int year, 144 int monthOfYear, 145 int dayOfMonth, 146 boolean yearOptional) { 147 super(context, theme); 148 149 mCallBack = callBack; 150 mInitialYear = year; 151 mInitialMonth = monthOfYear; 152 mInitialDay = dayOfMonth; 153 154 mTitleDateFormat = DateFormat.getDateInstance(DateFormat.FULL); 155 mTitleNoYearDateFormat = DateUtils.getLocalizedDateFormatWithoutYear(getContext()); 156 updateTitle(mInitialYear, mInitialMonth, mInitialDay); 157 158 setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set), 159 this); 160 setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel), 161 (OnClickListener) null); 162 163 LayoutInflater inflater = 164 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 165 View view = inflater.inflate(R.layout.date_picker_dialog, null); 166 setView(view); 167 mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); 168 mDatePicker.init(mInitialYear, mInitialMonth, mInitialDay, yearOptional, this); 169 } 170 171 @Override onClick(DialogInterface dialog, int which)172 public void onClick(DialogInterface dialog, int which) { 173 if (mCallBack != null) { 174 mDatePicker.clearFocus(); 175 mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), 176 mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); 177 } 178 } 179 180 @Override onDateChanged(DatePicker view, int year, int month, int day)181 public void onDateChanged(DatePicker view, int year, int month, int day) { 182 updateTitle(year, month, day); 183 } 184 updateDate(int year, int monthOfYear, int dayOfMonth)185 public void updateDate(int year, int monthOfYear, int dayOfMonth) { 186 mInitialYear = year; 187 mInitialMonth = monthOfYear; 188 mInitialDay = dayOfMonth; 189 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); 190 } 191 updateTitle(int year, int month, int day)192 private void updateTitle(int year, int month, int day) { 193 final Calendar calendar = Calendar.getInstance(); 194 calendar.set(Calendar.YEAR, year); 195 calendar.set(Calendar.MONTH, month); 196 calendar.set(Calendar.DAY_OF_MONTH, day); 197 final DateFormat dateFormat = 198 year == NO_YEAR ? mTitleNoYearDateFormat : mTitleDateFormat; 199 setTitle(dateFormat.format(calendar.getTime())); 200 } 201 202 @Override onSaveInstanceState()203 public Bundle onSaveInstanceState() { 204 Bundle state = super.onSaveInstanceState(); 205 state.putInt(YEAR, mDatePicker.getYear()); 206 state.putInt(MONTH, mDatePicker.getMonth()); 207 state.putInt(DAY, mDatePicker.getDayOfMonth()); 208 state.putBoolean(YEAR_OPTIONAL, mDatePicker.isYearOptional()); 209 return state; 210 } 211 212 @Override onRestoreInstanceState(Bundle savedInstanceState)213 public void onRestoreInstanceState(Bundle savedInstanceState) { 214 super.onRestoreInstanceState(savedInstanceState); 215 int year = savedInstanceState.getInt(YEAR); 216 int month = savedInstanceState.getInt(MONTH); 217 int day = savedInstanceState.getInt(DAY); 218 boolean yearOptional = savedInstanceState.getBoolean(YEAR_OPTIONAL); 219 mDatePicker.init(year, month, day, yearOptional, this); 220 updateTitle(year, month, day); 221 } 222 } 223