| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.content.browser.input; |
| 6 | |
| 7 | import android.app.AlertDialog; |
| 8 | import android.content.Context; |
| 9 | import android.content.DialogInterface; |
| 10 | import android.content.DialogInterface.OnClickListener; |
| 11 | import android.os.Build; |
| 12 | import android.text.format.Time; |
| 13 | import android.view.LayoutInflater; |
| 14 | import android.view.View; |
| 15 | import android.widget.DatePicker; |
| 16 | import android.widget.DatePicker.OnDateChangedListener; |
| 17 | import android.widget.TimePicker; |
| 18 | import android.widget.TimePicker.OnTimeChangedListener; |
| 19 | |
| 20 | import org.chromium.content.R; |
| 21 | |
| 22 | class DateTimePickerDialog extends AlertDialog implements OnClickListener, |
| 23 | OnDateChangedListener, OnTimeChangedListener { |
| 24 | |
| 25 | private static final String YEAR = "year"; |
| 26 | private static final String MONTH = "month"; |
| 27 | private static final String DAY = "day"; |
| 28 | private static final String HOUR = "hour"; |
| 29 | private static final String MINUTE = "minute"; |
| 30 | private static final String IS_24_HOUR = "is24hour"; |
| 31 | |
| 32 | private final DatePicker mDatePicker; |
| 33 | private final TimePicker mTimePicker; |
| 34 | private final OnDateTimeSetListener mCallBack; |
| 35 | |
| 36 | private final long mMinTimeMillis; |
| 37 | private final long mMaxTimeMillis; |
| 38 | |
| 39 | /** |
| 40 | * The callback used to indicate the user is done filling in the date. |
| 41 | */ |
| 42 | public interface OnDateTimeSetListener { |
| 43 | |
| 44 | /** |
| 45 | * @param dateView The DatePicker view associated with this listener. |
| 46 | * @param timeView The TimePicker view associated with this listener. |
| 47 | * @param year The year that was set. |
| 48 | * @param monthOfYear The month that was set (0-11) for compatibility |
| 49 | * with {@link java.util.Calendar}. |
| 50 | * @param dayOfMonth The day of the month that was set. |
| 51 | * @param hourOfDay The hour that was set. |
| 52 | * @param minute The minute that was set. |
| 53 | */ |
| 54 | void onDateTimeSet(DatePicker dateView, TimePicker timeView, int year, int monthOfYear, |
| 55 | int dayOfMonth, int hourOfDay, int minute); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param context The context the dialog is to run in. |
| 60 | * @param callBack How the parent is notified that the date is set. |
| 61 | * @param year The initial year of the dialog. |
| 62 | * @param monthOfYear The initial month of the dialog. |
| 63 | * @param dayOfMonth The initial day of the dialog. |
| 64 | */ |
| 65 | public DateTimePickerDialog(Context context, |
| 66 | OnDateTimeSetListener callBack, |
| 67 | int year, |
| 68 | int monthOfYear, |
| 69 | int dayOfMonth, |
| 70 | int hourOfDay, int minute, boolean is24HourView, |
| 71 | long min, long max) { |
| 72 | super(context, 0); |
| 73 | |
| 74 | mMinTimeMillis = min; |
| 75 | mMaxTimeMillis = max; |
| 76 | |
| 77 | mCallBack = callBack; |
| 78 | |
| 79 | setButton(BUTTON_POSITIVE, context.getText( |
| 80 | R.string.date_picker_dialog_set), this); |
| 81 | setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel), |
| 82 | (OnClickListener) null); |
| 83 | setIcon(0); |
| 84 | setTitle(context.getText(R.string.date_time_picker_dialog_title)); |
| 85 | |
| 86 | LayoutInflater inflater = |
| 87 | (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 88 | View view = inflater.inflate(R.layout.date_time_picker_dialog, null); |
| 89 | setView(view); |
| 90 | mDatePicker = (DatePicker) view.findViewById(R.id.date_picker); |
| 91 | DateDialogNormalizer.normalize(mDatePicker, this, |
| 92 | year, monthOfYear, dayOfMonth, hourOfDay, minute, min, max); |
| 93 | |
| 94 | mTimePicker = (TimePicker) view.findViewById(R.id.time_picker); |
| 95 | mTimePicker.setIs24HourView(is24HourView); |
| 96 | mTimePicker.setCurrentHour(hourOfDay); |
| 97 | mTimePicker.setCurrentMinute(minute); |
| 98 | mTimePicker.setOnTimeChangedListener(this); |
| 99 | onTimeChanged(mTimePicker, mTimePicker.getCurrentHour(), |
| 100 | mTimePicker.getCurrentMinute()); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onClick(DialogInterface dialog, int which) { |
| 105 | tryNotifyDateTimeSet(); |
| 106 | } |
| 107 | |
| 108 | private void tryNotifyDateTimeSet() { |
| 109 | if (mCallBack != null) { |
| 110 | mDatePicker.clearFocus(); |
| 111 | mCallBack.onDateTimeSet(mDatePicker, mTimePicker, mDatePicker.getYear(), |
| 112 | mDatePicker.getMonth(), mDatePicker.getDayOfMonth(), |
| 113 | mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onStop() { |
| 119 | if (Build.VERSION.SDK_INT >= 16) { |
| 120 | // The default behavior of dialogs changed in JellyBean and onwards. |
| 121 | // Dismissing a dialog (by pressing back for example) |
| 122 | // applies the chosen date. This code is added here so that the custom |
| 123 | // pickers behave the same as the internal DatePickerDialog. |
| 124 | tryNotifyDateTimeSet(); |
| 125 | } |
| 126 | super.onStop(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void onDateChanged(DatePicker view, int year, |
| 131 | int month, int day) { |
| 132 | // Signal a time change so the max/min checks can be applied. |
| 133 | if (mTimePicker != null) { |
| 134 | onTimeChanged(mTimePicker, mTimePicker.getCurrentHour(), |
| 135 | mTimePicker.getCurrentMinute()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { |
| 141 | Time time = new Time(); |
| 142 | time.set(0, mTimePicker.getCurrentMinute(), |
| 143 | mTimePicker.getCurrentHour(), mDatePicker.getDayOfMonth(), |
| 144 | mDatePicker.getMonth(), mDatePicker.getYear()); |
| 145 | |
| 146 | if (time.toMillis(true) < mMinTimeMillis) { |
| 147 | time.set(mMinTimeMillis); |
| 148 | } else if (time.toMillis(true) > mMaxTimeMillis) { |
| 149 | time.set(mMaxTimeMillis); |
| 150 | } |
| 151 | mTimePicker.setCurrentHour(time.hour); |
| 152 | mTimePicker.setCurrentMinute(time.minute); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Sets the current date. |
| 157 | * |
| 158 | * @param year The date year. |
| 159 | * @param monthOfYear The date month. |
| 160 | * @param dayOfMonth The date day of month. |
| 161 | */ |
| 162 | public void updateDateTime(int year, int monthOfYear, int dayOfMonth, |
| 163 | int hourOfDay, int minutOfHour) { |
| 164 | mDatePicker.updateDate(year, monthOfYear, dayOfMonth); |
| 165 | mTimePicker.setCurrentHour(hourOfDay); |
| 166 | mTimePicker.setCurrentMinute(minutOfHour); |
| 167 | } |
| 168 | } |