OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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.widget.DatePicker; | |
8 import android.widget.DatePicker.OnDateChangedListener; | |
9 | |
10 import java.util.Calendar; | |
11 | |
12 /** | |
13 * Normalize a date dialog so that it respect min and max. | |
14 */ | |
15 class DateDialogNormalizer { | |
16 private static final long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; | |
17 | |
18 private static void setLimits(DatePicker picker, long min, long max) { | |
19 // DatePicker intervals are non inclusive | |
bulach
2013/05/30 14:16:30
nit: end with .
Miguel Garcia
2013/06/03 13:08:58
Done.
| |
20 long minTime = min > 0 ? min - 1 : 0; | |
21 long maxTime = (max - ONE_DAY_MILLIS) >= (Long.MAX_VALUE - ONE_DAY_MILLI S) ? | |
22 Long.MAX_VALUE : max + ONE_DAY_MILLIS; | |
23 | |
24 // set max/min Date accepts a time so while the widget is only able to | |
bulach
2013/05/30 14:16:30
this sentence is a bit confusing, how about:
// Wh
Miguel Garcia
2013/06/03 13:08:58
Done.
| |
25 // display dates it can fail if the min and max time are in the same dat e as the | |
26 // min max accepted date but a different times. | |
27 Calendar cal = Calendar.getInstance(); | |
28 cal.clear(); | |
29 cal.setTimeInMillis(max); | |
30 | |
31 picker.setMaxDate(trimToDate(maxTime)); | |
32 picker.setMinDate(trimToDate(minTime)); | |
33 } | |
34 | |
35 /** | |
36 * Resets the hour, minute, second piece of a time stamp to 0, maintaining t he remaining | |
37 * components (year, month, day). | |
38 */ | |
39 private static long trimToDate(long time) { | |
40 Calendar cal = Calendar.getInstance(); | |
41 cal.clear(); | |
42 cal.setTimeInMillis(time); | |
43 Calendar result = Calendar.getInstance(); | |
44 result.clear(); | |
45 result.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Cale ndar.DAY_OF_MONTH), | |
46 0, 0, 0); | |
47 return result.getTimeInMillis(); | |
48 } | |
49 | |
50 /** | |
51 * Normalizes an existing DateDialogPicker changing the default date if | |
52 * needed to comply with the {@code min} and {@code max} attributes. | |
53 */ | |
54 static void normalize(DatePicker picker, OnDateChangedListener listener, | |
55 int year, int month, int day, int hour, int minute, long min, long m ax) { | |
56 Calendar calendar = Calendar.getInstance(); | |
57 calendar.clear(); | |
58 calendar.set(year, month, day, hour, minute, 0); | |
59 if (calendar.getTimeInMillis() < min) { | |
60 calendar.clear(); | |
61 calendar.setTimeInMillis(min); | |
62 } else if (calendar.getTimeInMillis() > max) { | |
63 calendar.clear(); | |
64 calendar.setTimeInMillis(max); | |
65 } | |
66 picker.init( | |
67 calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), | |
68 calendar.get(Calendar.DAY_OF_MONTH), listener); | |
69 | |
70 setLimits(picker, min, max); | |
71 } | |
72 } | |
OLD | NEW |