Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / content / public / android / java / src / org / chromium / content / browser / input / DateDialogNormalizer.java
1 // Copyright 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 import java.util.TimeZone;
12
13 /**
14  * Normalize a date dialog so that it respect min and max.
15  */
16 class DateDialogNormalizer {
17
18     private static void setLimits(DatePicker picker, long minMillis, long maxMillis) {
19         // DatePicker intervals are non inclusive, the DatePicker will throw an
20         // exception when setting the min/max attribute to the current date
21         // so make sure this never happens
22         if (maxMillis <= minMillis) {
23             return;
24         }
25         Calendar minCal = trimToDate(minMillis);
26         Calendar maxCal = trimToDate(maxMillis);
27         int currentYear = picker.getYear();
28         int currentMonth = picker.getMonth();
29         int currentDayOfMonth = picker.getDayOfMonth();
30         picker.updateDate(maxCal.get(Calendar.YEAR),
31                 maxCal.get(Calendar.MONTH),
32                 maxCal.get(Calendar.DAY_OF_MONTH));
33         picker.setMinDate(minCal.getTimeInMillis());
34         picker.updateDate(minCal.get(Calendar.YEAR),
35                 minCal.get(Calendar.MONTH),
36                 minCal.get(Calendar.DAY_OF_MONTH));
37         picker.setMaxDate(maxCal.getTimeInMillis());
38
39         // Restore the current date, this will keep the min/max settings
40         // previously set into account.
41         picker.updateDate(currentYear, currentMonth, currentDayOfMonth);
42     }
43
44     private static Calendar trimToDate(long time) {
45         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
46         cal.clear();
47         cal.setTimeInMillis(time);
48         Calendar result = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
49         result.clear();
50         result.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
51                 0, 0, 0);
52         return result;
53     }
54
55     /**
56      * Normalizes an existing DateDialogPicker changing the default date if
57      * needed to comply with the {@code min} and {@code max} attributes.
58      */
59     static void normalize(DatePicker picker, OnDateChangedListener listener,
60             int year, int month, int day, int hour, int minute, long minMillis, long maxMillis) {
61         Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
62         calendar.clear();
63         calendar.set(year, month, day, hour, minute, 0);
64         if (calendar.getTimeInMillis() < minMillis) {
65             calendar.clear();
66             calendar.setTimeInMillis(minMillis);
67         } else if (calendar.getTimeInMillis() > maxMillis) {
68             calendar.clear();
69             calendar.setTimeInMillis(maxMillis);
70         }
71         picker.init(
72                 calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
73                 calendar.get(Calendar.DAY_OF_MONTH), listener);
74
75         setLimits(picker, minMillis, maxMillis);
76     }
77 }