OLD | NEW |
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.view.View; |
| 17 import android.widget.AdapterView; |
16 import android.widget.DatePicker; | 18 import android.widget.DatePicker; |
| 19 import android.widget.ListView; |
| 20 import android.widget.SimpleAdapter; |
| 21 import android.widget.TextView; |
17 import android.widget.TimePicker; | 22 import android.widget.TimePicker; |
18 | 23 |
19 import org.chromium.content.R; | 24 import org.chromium.content.R; |
20 import org.chromium.content.browser.input.DateTimePickerDialog.OnDateTimeSetList
ener; | 25 import org.chromium.content.browser.input.DateTimePickerDialog.OnDateTimeSetList
ener; |
21 import org.chromium.content.browser.input.MultiFieldTimePickerDialog.OnMultiFiel
dTimeSetListener; | 26 import org.chromium.content.browser.input.MultiFieldTimePickerDialog.OnMultiFiel
dTimeSetListener; |
22 | 27 |
| 28 import java.util.Arrays; |
23 import java.util.Calendar; | 29 import java.util.Calendar; |
24 import java.util.Date; | 30 import java.util.Date; |
25 import java.util.GregorianCalendar; | 31 import java.util.GregorianCalendar; |
26 import java.util.TimeZone; | 32 import java.util.TimeZone; |
27 import java.util.concurrent.TimeUnit; | 33 import java.util.concurrent.TimeUnit; |
28 | 34 |
29 public class InputDialogContainer { | 35 public class InputDialogContainer { |
30 | 36 |
31 interface InputActionDelegate { | 37 interface InputActionDelegate { |
32 void cancelDateTimeDialog(); | 38 void cancelDateTimeDialog(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 return type == sTextInputTypeDate || type == sTextInputTypeTime | 70 return type == sTextInputTypeDate || type == sTextInputTypeTime |
65 || type == sTextInputTypeDateTime || type == sTextInputTypeDateT
imeLocal | 71 || type == sTextInputTypeDateTime || type == sTextInputTypeDateT
imeLocal |
66 || type == sTextInputTypeMonth || type == sTextInputTypeWeek; | 72 || type == sTextInputTypeMonth || type == sTextInputTypeWeek; |
67 } | 73 } |
68 | 74 |
69 InputDialogContainer(Context context, InputActionDelegate inputActionDelegat
e) { | 75 InputDialogContainer(Context context, InputActionDelegate inputActionDelegat
e) { |
70 mContext = context; | 76 mContext = context; |
71 mInputActionDelegate = inputActionDelegate; | 77 mInputActionDelegate = inputActionDelegate; |
72 } | 78 } |
73 | 79 |
74 void showDialog(final int dialogType, double dialogValue, | 80 void showPickerDialog(final int dialogType, double dialogValue, |
75 double min, double max, double step) { | 81 double min, double max, double step) { |
76 Calendar cal; | 82 Calendar cal; |
77 // |dialogValue|, |min|, |max| mean different things depending on the |d
ialogType|. | 83 // |dialogValue|, |min|, |max| mean different things depending on the |d
ialogType|. |
78 // For input type=month is the number of months since 1970. | 84 // For input type=month is the number of months since 1970. |
79 // For input type=time it is milliseconds since midnight. | 85 // For input type=time it is milliseconds since midnight. |
80 // For other types they are just milliseconds since 1970. | 86 // For other types they are just milliseconds since 1970. |
81 // If |dialogValue| is NaN it means an empty value. We will show the cur
rent time. | 87 // If |dialogValue| is NaN it means an empty value. We will show the cur
rent time. |
82 if (Double.isNaN(dialogValue)) { | 88 if (Double.isNaN(dialogValue)) { |
83 cal = Calendar.getInstance(); | 89 cal = Calendar.getInstance(); |
84 cal.set(Calendar.MILLISECOND, 0); | 90 cal.set(Calendar.MILLISECOND, 0); |
85 } else { | 91 } else { |
86 if (dialogType == sTextInputTypeMonth) { | 92 if (dialogType == sTextInputTypeMonth) { |
87 cal = MonthPicker.createDateFromValue(dialogValue); | 93 cal = MonthPicker.createDateFromValue(dialogValue); |
88 } else if (dialogType == sTextInputTypeWeek) { | 94 } else if (dialogType == sTextInputTypeWeek) { |
89 cal = WeekPicker.createDateFromValue(dialogValue); | 95 cal = WeekPicker.createDateFromValue(dialogValue); |
90 } else { | 96 } else { |
91 GregorianCalendar gregorianCalendar = | 97 GregorianCalendar gregorianCalendar = |
92 new GregorianCalendar(TimeZone.getTimeZone("UTC")); | 98 new GregorianCalendar(TimeZone.getTimeZone("UTC")); |
93 // According to the HTML spec we only use the Gregorian calendar | 99 // According to the HTML spec we only use the Gregorian calendar |
94 // so we ignore the Julian/Gregorian transition. | 100 // so we ignore the Julian/Gregorian transition. |
95 gregorianCalendar.setGregorianChange(new Date(Long.MIN_VALUE)); | 101 gregorianCalendar.setGregorianChange(new Date(Long.MIN_VALUE)); |
96 gregorianCalendar.setTimeInMillis((long) dialogValue); | 102 gregorianCalendar.setTimeInMillis((long) dialogValue); |
97 cal = gregorianCalendar; | 103 cal = gregorianCalendar; |
98 } | 104 } |
99 } | 105 } |
100 if (dialogType == sTextInputTypeDate) { | 106 if (dialogType == sTextInputTypeDate) { |
101 showDialog(dialogType, | 107 showPickerDialog(dialogType, |
102 cal.get(Calendar.YEAR), | 108 cal.get(Calendar.YEAR), |
103 cal.get(Calendar.MONTH), | 109 cal.get(Calendar.MONTH), |
104 cal.get(Calendar.DAY_OF_MONTH), | 110 cal.get(Calendar.DAY_OF_MONTH), |
105 0, 0, 0, 0, 0, min, max, step); | 111 0, 0, 0, 0, 0, min, max, step); |
106 } else if (dialogType == sTextInputTypeTime) { | 112 } else if (dialogType == sTextInputTypeTime) { |
107 showDialog(dialogType, 0, 0, 0, | 113 showPickerDialog(dialogType, 0, 0, 0, |
108 cal.get(Calendar.HOUR_OF_DAY), | 114 cal.get(Calendar.HOUR_OF_DAY), |
109 cal.get(Calendar.MINUTE), | 115 cal.get(Calendar.MINUTE), |
110 0, 0, 0, min, max, step); | 116 0, 0, 0, min, max, step); |
111 } else if (dialogType == sTextInputTypeDateTime || | 117 } else if (dialogType == sTextInputTypeDateTime || |
112 dialogType == sTextInputTypeDateTimeLocal) { | 118 dialogType == sTextInputTypeDateTimeLocal) { |
113 showDialog(dialogType, | 119 showPickerDialog(dialogType, |
114 cal.get(Calendar.YEAR), | 120 cal.get(Calendar.YEAR), |
115 cal.get(Calendar.MONTH), | 121 cal.get(Calendar.MONTH), |
116 cal.get(Calendar.DAY_OF_MONTH), | 122 cal.get(Calendar.DAY_OF_MONTH), |
117 cal.get(Calendar.HOUR_OF_DAY), | 123 cal.get(Calendar.HOUR_OF_DAY), |
118 cal.get(Calendar.MINUTE), | 124 cal.get(Calendar.MINUTE), |
119 cal.get(Calendar.SECOND), | 125 cal.get(Calendar.SECOND), |
120 cal.get(Calendar.MILLISECOND), | 126 cal.get(Calendar.MILLISECOND), |
121 0, min, max, step); | 127 0, min, max, step); |
122 } else if (dialogType == sTextInputTypeMonth) { | 128 } else if (dialogType == sTextInputTypeMonth) { |
123 showDialog(dialogType, cal.get(Calendar.YEAR), cal.get(Calendar.MONT
H), 0, | 129 showPickerDialog(dialogType, cal.get(Calendar.YEAR), cal.get(Calenda
r.MONTH), 0, |
124 0, 0, 0, 0, 0, min, max, step); | 130 0, 0, 0, 0, 0, min, max, step); |
125 } else if (dialogType == sTextInputTypeWeek) { | 131 } else if (dialogType == sTextInputTypeWeek) { |
126 int year = WeekPicker.getISOWeekYearForDate(cal); | 132 int year = WeekPicker.getISOWeekYearForDate(cal); |
127 int week = WeekPicker.getWeekForDate(cal); | 133 int week = WeekPicker.getWeekForDate(cal); |
128 showDialog(dialogType, year, 0, 0, 0, 0, 0, 0, week, min, max, step)
; | 134 showPickerDialog(dialogType, year, 0, 0, 0, 0, 0, 0, week, min, max,
step); |
129 } | 135 } |
130 } | 136 } |
131 | 137 |
132 void showDialog(final int dialogType, | 138 void showSuggestionDialog(final int dialogType, |
| 139 final double dialogValue, |
| 140 final double min, final double max, final double step, |
| 141 DateTimeSuggestion[] suggestions) { |
| 142 ListView suggestionListView = new ListView(mContext); |
| 143 final DateTimeSuggestionListAdapter adapter = |
| 144 new DateTimeSuggestionListAdapter(mContext, Arrays.asList(suggestion
s)); |
| 145 suggestionListView.setAdapter(adapter); |
| 146 suggestionListView.setOnItemClickListener(new AdapterView.OnItemClickLis
tener() { |
| 147 @Override |
| 148 public void onItemClick(AdapterView<?> parent, View view, int positi
on, long id) { |
| 149 if (position == adapter.getCount() - 1) { |
| 150 dismissDialog(); |
| 151 showPickerDialog(dialogType, dialogValue, min, max, step); |
| 152 } else { |
| 153 double suggestionValue = adapter.getItem(position).value(); |
| 154 mInputActionDelegate.replaceDateTime(suggestionValue); |
| 155 dismissDialog(); |
| 156 mDialogAlreadyDismissed = true; |
| 157 } |
| 158 } |
| 159 }); |
| 160 |
| 161 int dialogTitleId = R.string.date_picker_dialog_title; |
| 162 if (dialogType == sTextInputTypeTime) { |
| 163 dialogTitleId = R.string.time_picker_dialog_title; |
| 164 } else if (dialogType == sTextInputTypeDateTime || |
| 165 dialogType == sTextInputTypeDateTimeLocal) { |
| 166 dialogTitleId = R.string.date_time_picker_dialog_title; |
| 167 } else if (dialogType == sTextInputTypeMonth) { |
| 168 dialogTitleId = R.string.month_picker_dialog_title; |
| 169 } else if (dialogType == sTextInputTypeWeek) { |
| 170 dialogTitleId = R.string.week_picker_dialog_title; |
| 171 } |
| 172 |
| 173 mDialog = new AlertDialog.Builder(mContext) |
| 174 .setTitle(dialogTitleId) |
| 175 .setView(suggestionListView) |
| 176 .setOnDismissListener(new DialogInterface.OnDismissListener() { |
| 177 @Override |
| 178 public void onDismiss(DialogInterface dialog) { |
| 179 if (mDialog == dialog && !mDialogAlreadyDismissed) { |
| 180 mDialogAlreadyDismissed = true; |
| 181 mInputActionDelegate.cancelDateTimeDialog(); |
| 182 } |
| 183 } |
| 184 }) |
| 185 .setNegativeButton(mContext.getText(android.R.string.cancel), |
| 186 new DialogInterface.OnClickListener() { |
| 187 @Override |
| 188 public void onClick(DialogInterface dialog, int which) { |
| 189 dismissDialog(); |
| 190 } |
| 191 }) |
| 192 .create(); |
| 193 |
| 194 mDialogAlreadyDismissed = false; |
| 195 mDialog.show(); |
| 196 } |
| 197 |
| 198 void showDialog(final int type, final double value, |
| 199 double min, double max, double step, |
| 200 DateTimeSuggestion[] suggestions) { |
| 201 // When the web page asks to show a dialog while there is one already op
en, |
| 202 // dismiss the old one. |
| 203 dismissDialog(); |
| 204 if (suggestions == null) { |
| 205 showPickerDialog(type, value, min, max, step); |
| 206 } else { |
| 207 showSuggestionDialog(type, value, min, max, step, suggestions); |
| 208 } |
| 209 } |
| 210 |
| 211 void showPickerDialog(final int dialogType, |
133 int year, int month, int monthDay, | 212 int year, int month, int monthDay, |
134 int hourOfDay, int minute, int second, int millis, int week, | 213 int hourOfDay, int minute, int second, int millis, int week, |
135 double min, double max, double step) { | 214 double min, double max, double step) { |
136 if (isDialogShowing()) mDialog.dismiss(); | 215 if (isDialogShowing()) mDialog.dismiss(); |
137 | 216 |
138 int stepTime = (int) step; | 217 int stepTime = (int) step; |
139 | 218 |
140 if (dialogType == sTextInputTypeDate) { | 219 if (dialogType == sTextInputTypeDate) { |
141 DatePickerDialog dialog = new DatePickerDialog(mContext, | 220 DatePickerDialog dialog = new DatePickerDialog(mContext, |
142 new DateListener(dialogType), | 221 new DateListener(dialogType), |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 } else if (dialogType == sTextInputTypeWeek) { | 365 } else if (dialogType == sTextInputTypeWeek) { |
287 mInputActionDelegate.replaceDateTime( | 366 mInputActionDelegate.replaceDateTime( |
288 WeekPicker.createDateFromWeek(year, week).getTimeInMillis()); | 367 WeekPicker.createDateFromWeek(year, week).getTimeInMillis()); |
289 } else if (dialogType == sTextInputTypeTime) { | 368 } else if (dialogType == sTextInputTypeTime) { |
290 mInputActionDelegate.replaceDateTime(TimeUnit.HOURS.toMillis(hourOfD
ay) + | 369 mInputActionDelegate.replaceDateTime(TimeUnit.HOURS.toMillis(hourOfD
ay) + |
291 TimeUnit.MINUTES.toMillis(minut
e) + | 370 TimeUnit.MINUTES.toMillis(minut
e) + |
292 TimeUnit.SECONDS.toMillis(secon
d) + | 371 TimeUnit.SECONDS.toMillis(secon
d) + |
293 millis); | 372 millis); |
294 } else { | 373 } else { |
295 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); | 374 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
296 cal.clear(); | 375 cal.clear(); |
297 cal.set(Calendar.YEAR, year); | 376 cal.set(Calendar.YEAR, year); |
298 cal.set(Calendar.MONTH, month); | 377 cal.set(Calendar.MONTH, month); |
299 cal.set(Calendar.DAY_OF_MONTH, monthDay); | 378 cal.set(Calendar.DAY_OF_MONTH, monthDay); |
300 cal.set(Calendar.HOUR_OF_DAY, hourOfDay); | 379 cal.set(Calendar.HOUR_OF_DAY, hourOfDay); |
301 cal.set(Calendar.MINUTE, minute); | 380 cal.set(Calendar.MINUTE, minute); |
302 cal.set(Calendar.SECOND, second); | 381 cal.set(Calendar.SECOND, second); |
303 cal.set(Calendar.MILLISECOND, millis); | 382 cal.set(Calendar.MILLISECOND, millis); |
304 mInputActionDelegate.replaceDateTime((double) cal.getTimeInMillis())
; | 383 mInputActionDelegate.replaceDateTime((double) cal.getTimeInMillis())
; |
305 } | 384 } |
306 } | 385 } |
307 } | 386 } |
OLD | NEW |