Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/input/InputDialogContainer.java

Issue 85643002: Transfer date/time value to chooser as double (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@doubledate2
Patch Set: used ui::TextInputType Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser.input; 5 package org.chromium.content.browser.input;
6 6
7 import android.app.AlertDialog; 7 import android.app.AlertDialog;
8 import android.app.DatePickerDialog; 8 import android.app.DatePickerDialog;
9 import android.app.DatePickerDialog.OnDateSetListener; 9 import android.app.DatePickerDialog.OnDateSetListener;
10 import android.app.TimePickerDialog.OnTimeSetListener; 10 import android.app.TimePickerDialog.OnTimeSetListener;
11 import android.content.Context; 11 import android.content.Context;
12 import android.content.DialogInterface; 12 import android.content.DialogInterface;
13 import android.content.DialogInterface.OnDismissListener; 13 import android.content.DialogInterface.OnDismissListener;
14 import android.text.format.DateFormat; 14 import android.text.format.DateFormat;
15 import android.text.format.Time; 15 import android.text.format.Time;
16 import android.widget.DatePicker; 16 import android.widget.DatePicker;
17 import android.widget.TimePicker; 17 import android.widget.TimePicker;
18 18
19 import org.chromium.content.R; 19 import org.chromium.content.R;
20 import org.chromium.content.browser.input.DateTimePickerDialog.OnDateTimeSetList ener; 20 import org.chromium.content.browser.input.DateTimePickerDialog.OnDateTimeSetList ener;
21 import org.chromium.content.browser.input.MultiFieldTimePickerDialog.OnMultiFiel dTimeSetListener; 21 import org.chromium.content.browser.input.MultiFieldTimePickerDialog.OnMultiFiel dTimeSetListener;
22 22
23 import java.util.Calendar; 23 import java.util.Calendar;
24 import java.util.TimeZone;
25 import java.util.concurrent.TimeUnit;
24 26
25 public class InputDialogContainer { 27 public class InputDialogContainer {
26 28
27 interface InputActionDelegate { 29 interface InputActionDelegate {
28 void cancelDateTimeDialog(); 30 void cancelDateTimeDialog();
29 void replaceDateTime(int dialogType, 31 void replaceDateTime(double value);
30 int year, int month, int day, int hour, int minute, int second, int milli, int week);
31 } 32 }
32 33
33 // Default values used in Time representations of selected date/time before formatting. 34 // Default values used in Time representations of selected date/time before formatting.
34 // They are never displayed to the user. 35 // They are never displayed to the user.
35 private static final int YEAR_DEFAULT = 1970; 36 private static final int YEAR_DEFAULT = 1970;
36 private static final int MONTH_DEFAULT = 0; 37 private static final int MONTH_DEFAULT = 0;
37 private static final int MONTHDAY_DEFAULT = 1; 38 private static final int MONTHDAY_DEFAULT = 1;
38 private static final int HOUR_DEFAULT = 0; 39 private static final int HOUR_DEFAULT = 0;
39 private static final int MINUTE_DEFAULT = 0; 40 private static final int MINUTE_DEFAULT = 0;
40 private static final int WEEK_DEFAULT = 0; 41 private static final int WEEK_DEFAULT = 0;
41 42
42 // Date formats as accepted by Time.format.
43 private static final String HTML_DATE_FORMAT = "%Y-%m-%d";
44 private static final String HTML_TIME_FORMAT = "%H:%M";
45 // For datetime we always send selected time as UTC, as we have no timezone selector.
46 // This is consistent with other browsers.
47 private static final String HTML_DATE_TIME_FORMAT = "%Y-%m-%dT%H:%MZ";
48 private static final String HTML_DATE_TIME_LOCAL_FORMAT = "%Y-%m-%dT%H:%M";
49 private static final String HTML_MONTH_FORMAT = "%Y-%m";
50 private static final String HTML_WEEK_FORMAT = "%Y-%w";
51
52 private static int sTextInputTypeDate; 43 private static int sTextInputTypeDate;
53 private static int sTextInputTypeDateTime; 44 private static int sTextInputTypeDateTime;
54 private static int sTextInputTypeDateTimeLocal; 45 private static int sTextInputTypeDateTimeLocal;
55 private static int sTextInputTypeMonth; 46 private static int sTextInputTypeMonth;
56 private static int sTextInputTypeTime; 47 private static int sTextInputTypeTime;
57 private static int sTextInputTypeWeek; 48 private static int sTextInputTypeWeek;
58 49
59 private final Context mContext; 50 private final Context mContext;
60 51
61 // Prevents sending two notifications (from onClick and from onDismiss) 52 // Prevents sending two notifications (from onClick and from onDismiss)
(...skipping 18 matching lines...) Expand all
80 return type == sTextInputTypeDate || type == sTextInputTypeTime 71 return type == sTextInputTypeDate || type == sTextInputTypeTime
81 || type == sTextInputTypeDateTime || type == sTextInputTypeDateT imeLocal 72 || type == sTextInputTypeDateTime || type == sTextInputTypeDateT imeLocal
82 || type == sTextInputTypeMonth || type == sTextInputTypeWeek; 73 || type == sTextInputTypeMonth || type == sTextInputTypeWeek;
83 } 74 }
84 75
85 InputDialogContainer(Context context, InputActionDelegate inputActionDelegat e) { 76 InputDialogContainer(Context context, InputActionDelegate inputActionDelegat e) {
86 mContext = context; 77 mContext = context;
87 mInputActionDelegate = inputActionDelegate; 78 mInputActionDelegate = inputActionDelegate;
88 } 79 }
89 80
90 private Time normalizeTime(int year, int month, int monthDay, 81 void showDialog(final int dialogType, double dialogValue,
91 int hour, int minute, int second) { 82 double min, double max, double step) {
92 Time result = new Time(); 83 Calendar cal;
93 if (year == 0 && month == 0 && monthDay == 0 && hour == 0 && 84 // |dialogValue|, |min|, |max| mean different things depending on the |d ialogType|.
94 minute == 0 && second == 0) { 85 // For input type=month is the number of months since 1970.
95 Calendar cal = Calendar.getInstance(); 86 // For input type=time it is milliseconds since midnight.
96 result.set(cal.get(Calendar.SECOND), cal.get(Calendar.MINUTE), 87 - // For other types they are just milliseconds since 1970.
97 cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.DATE), 88 // If |dialogValue| is NaN it means an empty value. We will show the cur rent time.
98 cal.get(Calendar.MONTH), cal.get(Calendar.YEAR)); 89 if (Double.isNaN(dialogValue)) {
90 cal = Calendar.getInstance();
91 cal.set(Calendar.MILLISECOND, 0);
99 } else { 92 } else {
100 result.set(second, minute, hour, monthDay, month, year); 93 cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
94 if (dialogType == sTextInputTypeMonth) {
95 cal.set(Calendar.YEAR, (int) (dialogValue / 12));
96 cal.set(Calendar.MONTH, (int) (dialogValue % 12));
97 } else {
98 cal.setTimeInMillis((long) dialogValue);
99 }
101 } 100 }
102 return result; 101 if (dialogType == sTextInputTypeDate) {
102 showDialog(dialogType,
103 cal.get(Calendar.YEAR),
104 cal.get(Calendar.MONTH),
105 cal.get(Calendar.DAY_OF_MONTH),
106 0, 0, 0, 0, 0, min, max, step);
107 } else if (dialogType == sTextInputTypeTime) {
108 showDialog(dialogType, 0, 0, 0,
109 cal.get(Calendar.HOUR_OF_DAY),
110 cal.get(Calendar.MINUTE),
111 0, 0, 0, min, max, step);
112 } else if (dialogType == sTextInputTypeDateTime ||
113 dialogType == sTextInputTypeDateTimeLocal) {
114 showDialog(dialogType,
115 cal.get(Calendar.YEAR),
116 cal.get(Calendar.MONTH),
117 cal.get(Calendar.DAY_OF_MONTH),
118 cal.get(Calendar.HOUR_OF_DAY),
119 cal.get(Calendar.MINUTE),
120 cal.get(Calendar.SECOND),
121 cal.get(Calendar.MILLISECOND),
122 0, min, max, step);
123 } else if (dialogType == sTextInputTypeMonth) {
124 showDialog(dialogType, cal.get(Calendar.YEAR), cal.get(Calendar.MONT H), 0,
125 0, 0, 0, 0, 0, min, max, step);
126 } else if (dialogType == sTextInputTypeWeek) {
127 int year = WeekPicker.getISOWeekYearForDate(cal);
128 int week = WeekPicker.getWeekForDate(cal);
129 showDialog(dialogType, year, 0, 0, 0, 0, 0, 0, week, min, max, step) ;
130 }
103 } 131 }
104 132
105 void showDialog(final int dialogType, int year, int month, int monthDay, 133 void showDialog(final int dialogType,
106 int hour, int minute, int second, int milli, int week, 134 int year, int month, int monthDay,
107 double min, double max, double step) { 135 int hourOfDay, int minute, int second, int millis, int week,
136 double min, double max, double step) {
108 if (isDialogShowing()) mDialog.dismiss(); 137 if (isDialogShowing()) mDialog.dismiss();
109 138
110 // Java Date dialogs like longs but Blink prefers doubles..
111 // Both parameters mean different things depending on the type
112 // For input type=month min and max come as number on months since 1970
113 // For other types (including type=time) they are just milliseconds sinc e 1970
114 // In any case the cast here is safe given the above restrictions.
115 long minTime = (long) min;
116 long maxTime = (long) max;
117 int stepTime = (int) step; 139 int stepTime = (int) step;
118 140
119 if (milli > 1000) {
120 second += milli / 1000;
121 milli %= 1000;
122 }
123 Time time = normalizeTime(year, month, monthDay, hour, minute, second);
124 if (dialogType == sTextInputTypeDate) { 141 if (dialogType == sTextInputTypeDate) {
125 DatePickerDialog dialog = new DatePickerDialog(mContext, 142 DatePickerDialog dialog = new DatePickerDialog(mContext,
126 new DateListener(dialogType), time.year, time.month, time.mo nthDay); 143 new DateListener(dialogType),
144 year, month, monthDay);
127 DateDialogNormalizer.normalize(dialog.getDatePicker(), dialog, 145 DateDialogNormalizer.normalize(dialog.getDatePicker(), dialog,
128 time.year, time.month, time.monthDay, 0, 0, minTime, maxTime ); 146 year, month, monthDay,
147 0, 0,
148 (long) min, (long) max);
129 149
130 dialog.setTitle(mContext.getText(R.string.date_picker_dialog_title)) ; 150 dialog.setTitle(mContext.getText(R.string.date_picker_dialog_title)) ;
131 mDialog = dialog; 151 mDialog = dialog;
132 } else if (dialogType == sTextInputTypeTime) { 152 } else if (dialogType == sTextInputTypeTime) {
133 mDialog = new MultiFieldTimePickerDialog( 153 mDialog = new MultiFieldTimePickerDialog(
134 mContext, 0 /* theme */ , 154 mContext, 0 /* theme */ ,
135 time.hour, time.minute, time.second, milli, 155 hourOfDay, minute, second, millis,
136 (int) minTime, (int) maxTime, stepTime, 156 (int) min, (int) max, stepTime,
137 DateFormat.is24HourFormat(mContext), 157 DateFormat.is24HourFormat(mContext),
138 new FullTimeListener(dialogType)); 158 new FullTimeListener(dialogType));
139 } else if (dialogType == sTextInputTypeDateTime || 159 } else if (dialogType == sTextInputTypeDateTime ||
140 dialogType == sTextInputTypeDateTimeLocal) { 160 dialogType == sTextInputTypeDateTimeLocal) {
141 mDialog = new DateTimePickerDialog(mContext, 161 mDialog = new DateTimePickerDialog(mContext,
142 new DateTimeListener(dialogType), 162 new DateTimeListener(dialogType),
143 time.year, time.month, time.monthDay, 163 year, month, monthDay,
144 time.hour, time.minute, DateFormat.is24HourFormat(mContext), 164 hourOfDay, minute,
145 minTime, maxTime); 165 DateFormat.is24HourFormat(mContext), min, max);
146 } else if (dialogType == sTextInputTypeMonth) { 166 } else if (dialogType == sTextInputTypeMonth) {
147 mDialog = new MonthPickerDialog(mContext, new MonthOrWeekListener(di alogType), 167 mDialog = new MonthPickerDialog(mContext, new MonthOrWeekListener(di alogType),
148 time.year, time.month, minTime, maxTime); 168 year, month, min, max);
149 } else if (dialogType == sTextInputTypeWeek) { 169 } else if (dialogType == sTextInputTypeWeek) {
150 if (week == 0) {
151 Calendar cal = Calendar.getInstance();
152 year = WeekPicker.getISOWeekYearForDate(cal);
153 week = WeekPicker.getWeekForDate(cal);
154 }
155 mDialog = new WeekPickerDialog(mContext, new MonthOrWeekListener(dia logType), 170 mDialog = new WeekPickerDialog(mContext, new MonthOrWeekListener(dia logType),
156 year, week, minTime, maxTime); 171 year, week, min, max);
157 } 172 }
158 173
159 mDialog.setButton(DialogInterface.BUTTON_POSITIVE, 174 mDialog.setButton(DialogInterface.BUTTON_POSITIVE,
160 mContext.getText(R.string.date_picker_dialog_set), 175 mContext.getText(R.string.date_picker_dialog_set),
161 (DialogInterface.OnClickListener) mDialog); 176 (DialogInterface.OnClickListener) mDialog);
162 177
163 mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, 178 mDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
164 mContext.getText(android.R.string.cancel), 179 mContext.getText(android.R.string.cancel),
165 (DialogInterface.OnClickListener) null); 180 (DialogInterface.OnClickListener) null);
166 181
167 mDialog.setButton(DialogInterface.BUTTON_NEUTRAL, 182 mDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
168 mContext.getText(R.string.date_picker_dialog_clear), 183 mContext.getText(R.string.date_picker_dialog_clear),
169 new DialogInterface.OnClickListener() { 184 new DialogInterface.OnClickListener() {
170 @Override 185 @Override
171 public void onClick(DialogInterface dialog, int which) { 186 public void onClick(DialogInterface dialog, int which) {
172 mDialogAlreadyDismissed = true; 187 mDialogAlreadyDismissed = true;
173 mInputActionDelegate.replaceDateTime(dialogType, 0, 0, 0 , 0, 0, 0, 0, 0); 188 mInputActionDelegate.replaceDateTime(Double.NaN);
174 } 189 }
175 }); 190 });
176 191
177 mDialog.setOnDismissListener( 192 mDialog.setOnDismissListener(
178 new OnDismissListener() { 193 new OnDismissListener() {
179 @Override 194 @Override
180 public void onDismiss(final DialogInterface dialog) { 195 public void onDismiss(final DialogInterface dialog) {
181 if (!mDialogAlreadyDismissed) { 196 if (!mDialogAlreadyDismissed) {
182 mDialogAlreadyDismissed = true; 197 mDialogAlreadyDismissed = true;
183 mInputActionDelegate.cancelDateTimeDialog(); 198 mInputActionDelegate.cancelDateTimeDialog();
(...skipping 15 matching lines...) Expand all
199 214
200 private class DateListener implements OnDateSetListener { 215 private class DateListener implements OnDateSetListener {
201 private final int mDialogType; 216 private final int mDialogType;
202 217
203 DateListener(int dialogType) { 218 DateListener(int dialogType) {
204 mDialogType = dialogType; 219 mDialogType = dialogType;
205 } 220 }
206 221
207 @Override 222 @Override
208 public void onDateSet(DatePicker view, int year, int month, int monthDay ) { 223 public void onDateSet(DatePicker view, int year, int month, int monthDay ) {
209 if (!mDialogAlreadyDismissed) { 224 setFieldDateTimeValue(mDialogType, year, month, monthDay, 0, 0, 0, 0 , 0);
210 setFieldDateTimeValue(mDialogType,
211 year, month, monthDay,
212 HOUR_DEFAULT, MINUTE_DEFAULT, WEEK_DEFAULT,
213 HTML_DATE_FORMAT);
214 }
215 } 225 }
216 } 226 }
217 227
218 private class TimeListener implements OnTimeSetListener {
219 private final int mDialogType;
220
221 TimeListener(int dialogType) {
222 mDialogType = dialogType;
223 }
224
225 @Override
226 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
227 if (!mDialogAlreadyDismissed) {
228 setFieldDateTimeValue(mDialogType,
229 YEAR_DEFAULT, MONTH_DEFAULT, MONTHDAY_DEFAULT,
230 hourOfDay, minute, WEEK_DEFAULT, HTML_TIME_FORMAT);
231 }
232 }
233 }
234
235 private class FullTimeListener implements OnMultiFieldTimeSetListener { 228 private class FullTimeListener implements OnMultiFieldTimeSetListener {
236 private final int mDialogType; 229 private final int mDialogType;
237 FullTimeListener(int dialogType) { 230 FullTimeListener(int dialogType) {
238 mDialogType = dialogType; 231 mDialogType = dialogType;
239 } 232 }
240 233
241 @Override 234 @Override
242 public void onTimeSet(int hourOfDay, int minute, int second, int milli) { 235 public void onTimeSet(int hourOfDay, int minute, int second, int milli) {
243 if (!mDialogAlreadyDismissed) { 236 setFieldDateTimeValue(mDialogType, 0, 0, 0, hourOfDay, minute, secon d, milli, 0);
244 setFieldDateTimeValue(mDialogType,
245 YEAR_DEFAULT, MONTH_DEFAULT, MONTHDAY_DEFAULT,
246 hourOfDay, minute, second, milli, WEEK_DEFAULT, HTML_TIM E_FORMAT);
247 }
248 } 237 }
249 } 238 }
250 239
251 private class DateTimeListener implements OnDateTimeSetListener { 240 private class DateTimeListener implements OnDateTimeSetListener {
252 private final boolean mLocal; 241 private final boolean mLocal;
253 private final int mDialogType; 242 private final int mDialogType;
254 243
255 public DateTimeListener(int dialogType) { 244 public DateTimeListener(int dialogType) {
256 mLocal = dialogType == sTextInputTypeDateTimeLocal; 245 mLocal = dialogType == sTextInputTypeDateTimeLocal;
257 mDialogType = dialogType; 246 mDialogType = dialogType;
258 } 247 }
259 248
260 @Override 249 @Override
261 public void onDateTimeSet(DatePicker dateView, TimePicker timeView, 250 public void onDateTimeSet(DatePicker dateView, TimePicker timeView,
262 int year, int month, int monthDay, 251 int year, int month, int monthDay,
263 int hourOfDay, int minute) { 252 int hourOfDay, int minute) {
264 if (!mDialogAlreadyDismissed) { 253 setFieldDateTimeValue(mDialogType, year, month, monthDay, hourOfDay, minute, 0, 0, 0);
265 setFieldDateTimeValue(mDialogType, year, month, monthDay,
266 hourOfDay, minute, WEEK_DEFAULT,
267 mLocal ? HTML_DATE_TIME_LOCAL_FORMAT : HTML_DATE_TIME_FO RMAT);
268 }
269 } 254 }
270 } 255 }
271 256
272 private class MonthOrWeekListener implements TwoFieldDatePickerDialog.OnValu eSetListener { 257 private class MonthOrWeekListener implements TwoFieldDatePickerDialog.OnValu eSetListener {
273 private final int mDialogType; 258 private final int mDialogType;
274 259
275 MonthOrWeekListener(int dialogType) { 260 MonthOrWeekListener(int dialogType) {
276 mDialogType = dialogType; 261 mDialogType = dialogType;
277 } 262 }
278 263
279 @Override 264 @Override
280 public void onValueSet(int year, int positionInYear) { 265 public void onValueSet(int year, int positionInYear) {
281 if (!mDialogAlreadyDismissed) { 266 if (mDialogType == sTextInputTypeMonth) {
282 if (mDialogType == sTextInputTypeMonth) { 267 setFieldDateTimeValue(mDialogType, year, positionInYear, 0, 0, 0 , 0, 0, 0);
283 setFieldDateTimeValue(mDialogType, year, positionInYear, MON THDAY_DEFAULT, 268 } else {
284 HOUR_DEFAULT, MINUTE_DEFAULT, WEEK_DEFAULT, 269 setFieldDateTimeValue(mDialogType, year, 0, 0, 0, 0, 0, 0, posit ionInYear);
285 HTML_MONTH_FORMAT);
286 } else {
287 setFieldDateTimeValue(mDialogType, year, MONTH_DEFAULT, MONT HDAY_DEFAULT,
288 HOUR_DEFAULT, MINUTE_DEFAULT, positionInYear, HTML_W EEK_FORMAT);
289 }
290 } 270 }
291 } 271 }
292 } 272 }
293 273
294 private void setFieldDateTimeValue(int dialogType, 274 private void setFieldDateTimeValue(int dialogType,
295 int year, int month, int monthDay, int hourOfDay, 275 int year, int month, int monthDay,
296 int minute, int week, String dateFormat) { 276 int hourOfDay, int minute, int second, in t millis,
277 int week) {
297 // Prevents more than one callback being sent to the native 278 // Prevents more than one callback being sent to the native
298 // side when the dialog triggers multiple events. 279 // side when the dialog triggers multiple events.
280 if (mDialogAlreadyDismissed)
281 return;
299 mDialogAlreadyDismissed = true; 282 mDialogAlreadyDismissed = true;
300 283
301 mInputActionDelegate.replaceDateTime(dialogType, 284 double value = 0;
302 year, month, monthDay, hourOfDay, minute, 0 /* second */, 0 /* milli */, week); 285 if (dialogType == sTextInputTypeMonth) {
303 } 286 mInputActionDelegate.replaceDateTime((year - 1970) * 12 + month);
304 287 } else if (dialogType == sTextInputTypeTime) {
305 private void setFieldDateTimeValue(int dialogType, 288 mInputActionDelegate.replaceDateTime(TimeUnit.HOURS.toMillis(hourOfD ay) +
306 int year, int month, int monthDay, int hourOfDay, 289 TimeUnit.MINUTES.toMillis(minut e) +
307 int minute, int second, int milli, int week, String dateFormat) { 290 TimeUnit.SECONDS.toMillis(secon d) +
308 // Prevents more than one callback being sent to the native 291 millis);
309 // side when the dialog triggers multiple events. 292 } else {
310 mDialogAlreadyDismissed = true; 293 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
311 294 cal.clear();
312 mInputActionDelegate.replaceDateTime( 295 cal.set(Calendar.YEAR, year);
313 dialogType, year, month, monthDay, hourOfDay, minute, second, milli, week); 296 cal.set(Calendar.MONTH, month);
297 cal.set(Calendar.DAY_OF_MONTH, monthDay);
298 cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
299 cal.set(Calendar.MINUTE, minute);
300 cal.set(Calendar.SECOND, second);
301 cal.set(Calendar.MILLISECOND, millis);
302 mInputActionDelegate.replaceDateTime((double) cal.getTimeInMillis()) ;
303 }
314 } 304 }
315 } 305 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698