OLD | NEW |
| (Empty) |
1 // Copyright 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.text.format.Time; | |
12 import android.view.LayoutInflater; | |
13 import android.view.View; | |
14 import android.widget.DatePicker; | |
15 import android.widget.DatePicker.OnDateChangedListener; | |
16 import android.widget.TimePicker; | |
17 import android.widget.TimePicker.OnTimeChangedListener; | |
18 | |
19 import org.chromium.content.R; | |
20 | |
21 class DateTimePickerDialog extends AlertDialog implements OnClickListener, | |
22 OnDateChangedListener, OnTimeChangedListener { | |
23 private final DatePicker mDatePicker; | |
24 private final TimePicker mTimePicker; | |
25 private final OnDateTimeSetListener mCallBack; | |
26 | |
27 private final long mMinTimeMillis; | |
28 private final long mMaxTimeMillis; | |
29 | |
30 /** | |
31 * The callback used to indicate the user is done filling in the date. | |
32 */ | |
33 public interface OnDateTimeSetListener { | |
34 | |
35 /** | |
36 * @param dateView The DatePicker view associated with this listener. | |
37 * @param timeView The TimePicker view associated with this listener. | |
38 * @param year The year that was set. | |
39 * @param monthOfYear The month that was set (0-11) for compatibility | |
40 * with {@link java.util.Calendar}. | |
41 * @param dayOfMonth The day of the month that was set. | |
42 * @param hourOfDay The hour that was set. | |
43 * @param minute The minute that was set. | |
44 */ | |
45 void onDateTimeSet(DatePicker dateView, TimePicker timeView, int year, i
nt monthOfYear, | |
46 int dayOfMonth, int hourOfDay, int minute); | |
47 } | |
48 | |
49 /** | |
50 * @param context The context the dialog is to run in. | |
51 * @param callBack How the parent is notified that the date is set. | |
52 * @param year The initial year of the dialog. | |
53 * @param monthOfYear The initial month of the dialog. | |
54 * @param dayOfMonth The initial day of the dialog. | |
55 */ | |
56 public DateTimePickerDialog(Context context, | |
57 OnDateTimeSetListener callBack, | |
58 int year, | |
59 int monthOfYear, | |
60 int dayOfMonth, | |
61 int hourOfDay, int minute, boolean is24HourView, | |
62 double min, double max) { | |
63 super(context, 0); | |
64 | |
65 mMinTimeMillis = (long) min; | |
66 mMaxTimeMillis = (long) max; | |
67 | |
68 mCallBack = callBack; | |
69 | |
70 setButton(BUTTON_POSITIVE, context.getText( | |
71 R.string.date_picker_dialog_set), this); | |
72 setButton(BUTTON_NEGATIVE, context.getText(android.R.string.cancel), | |
73 (OnClickListener) null); | |
74 setIcon(0); | |
75 setTitle(context.getText(R.string.date_time_picker_dialog_title)); | |
76 | |
77 LayoutInflater inflater = | |
78 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATE
R_SERVICE); | |
79 View view = inflater.inflate(R.layout.date_time_picker_dialog, null); | |
80 setView(view); | |
81 mDatePicker = (DatePicker) view.findViewById(R.id.date_picker); | |
82 DateDialogNormalizer.normalize(mDatePicker, this, | |
83 year, monthOfYear, dayOfMonth, hourOfDay, minute, mMinTimeMillis
, mMaxTimeMillis); | |
84 | |
85 mTimePicker = (TimePicker) view.findViewById(R.id.time_picker); | |
86 mTimePicker.setIs24HourView(is24HourView); | |
87 mTimePicker.setCurrentHour(hourOfDay); | |
88 mTimePicker.setCurrentMinute(minute); | |
89 mTimePicker.setOnTimeChangedListener(this); | |
90 onTimeChanged(mTimePicker, mTimePicker.getCurrentHour(), | |
91 mTimePicker.getCurrentMinute()); | |
92 } | |
93 | |
94 @Override | |
95 public void onClick(DialogInterface dialog, int which) { | |
96 tryNotifyDateTimeSet(); | |
97 } | |
98 | |
99 private void tryNotifyDateTimeSet() { | |
100 if (mCallBack != null) { | |
101 mDatePicker.clearFocus(); | |
102 mCallBack.onDateTimeSet(mDatePicker, mTimePicker, mDatePicker.getYea
r(), | |
103 mDatePicker.getMonth(), mDatePicker.getDayOfMonth(), | |
104 mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute()
); | |
105 } | |
106 } | |
107 | |
108 @Override | |
109 public void onDateChanged(DatePicker view, int year, | |
110 int month, int day) { | |
111 // Signal a time change so the max/min checks can be applied. | |
112 if (mTimePicker != null) { | |
113 onTimeChanged(mTimePicker, mTimePicker.getCurrentHour(), | |
114 mTimePicker.getCurrentMinute()); | |
115 } | |
116 } | |
117 | |
118 @Override | |
119 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { | |
120 Time time = new Time(); | |
121 time.set(0, mTimePicker.getCurrentMinute(), | |
122 mTimePicker.getCurrentHour(), mDatePicker.getDayOfMonth(), | |
123 mDatePicker.getMonth(), mDatePicker.getYear()); | |
124 | |
125 if (time.toMillis(true) < mMinTimeMillis) { | |
126 time.set(mMinTimeMillis); | |
127 } else if (time.toMillis(true) > mMaxTimeMillis) { | |
128 time.set(mMaxTimeMillis); | |
129 } | |
130 mTimePicker.setCurrentHour(time.hour); | |
131 mTimePicker.setCurrentMinute(time.minute); | |
132 } | |
133 | |
134 /** | |
135 * Sets the current date. | |
136 * | |
137 * @param year The date year. | |
138 * @param monthOfYear The date month. | |
139 * @param dayOfMonth The date day of month. | |
140 */ | |
141 public void updateDateTime(int year, int monthOfYear, int dayOfMonth, | |
142 int hourOfDay, int minutOfHour) { | |
143 mDatePicker.updateDate(year, monthOfYear, dayOfMonth); | |
144 mTimePicker.setCurrentHour(hourOfDay); | |
145 mTimePicker.setCurrentMinute(minutOfHour); | |
146 } | |
147 } | |
OLD | NEW |