Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/input/DateDialogNormalizer.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/input/DateDialogNormalizer.java b/content/public/android/java/src/org/chromium/content/browser/input/DateDialogNormalizer.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0e171ca4e6af14d1d4b105461a720d03c549603e |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/input/DateDialogNormalizer.java |
| @@ -0,0 +1,72 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser.input; |
| + |
| +import android.widget.DatePicker; |
| +import android.widget.DatePicker.OnDateChangedListener; |
| + |
| +import java.util.Calendar; |
| + |
| +/** |
| + * Normalize a date dialog so that it respect min and max. |
| + */ |
| +class DateDialogNormalizer { |
| + private static final long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; |
| + |
| + private static void setLimits(DatePicker picker, long min, long max) { |
| + // DatePicker intervals are non inclusive |
|
bulach
2013/05/30 14:16:30
nit: end with .
Miguel Garcia
2013/06/03 13:08:58
Done.
|
| + long minTime = min > 0 ? min - 1 : 0; |
| + long maxTime = (max - ONE_DAY_MILLIS) >= (Long.MAX_VALUE - ONE_DAY_MILLIS) ? |
| + Long.MAX_VALUE : max + ONE_DAY_MILLIS; |
| + |
| + // 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.
|
| + // display dates it can fail if the min and max time are in the same date as the |
| + // min max accepted date but a different times. |
| + Calendar cal = Calendar.getInstance(); |
| + cal.clear(); |
| + cal.setTimeInMillis(max); |
| + |
| + picker.setMaxDate(trimToDate(maxTime)); |
| + picker.setMinDate(trimToDate(minTime)); |
| + } |
| + |
| + /** |
| + * Resets the hour, minute, second piece of a time stamp to 0, maintaining the remaining |
| + * components (year, month, day). |
| + */ |
| + private static long trimToDate(long time) { |
| + Calendar cal = Calendar.getInstance(); |
| + cal.clear(); |
| + cal.setTimeInMillis(time); |
| + Calendar result = Calendar.getInstance(); |
| + result.clear(); |
| + result.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), |
| + 0, 0, 0); |
| + return result.getTimeInMillis(); |
| + } |
| + |
| + /** |
| + * Normalizes an existing DateDialogPicker changing the default date if |
| + * needed to comply with the {@code min} and {@code max} attributes. |
| + */ |
| + static void normalize(DatePicker picker, OnDateChangedListener listener, |
| + int year, int month, int day, int hour, int minute, long min, long max) { |
| + Calendar calendar = Calendar.getInstance(); |
| + calendar.clear(); |
| + calendar.set(year, month, day, hour, minute, 0); |
| + if (calendar.getTimeInMillis() < min) { |
| + calendar.clear(); |
| + calendar.setTimeInMillis(min); |
| + } else if (calendar.getTimeInMillis() > max) { |
| + calendar.clear(); |
| + calendar.setTimeInMillis(max); |
| + } |
| + picker.init( |
| + calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), |
| + calendar.get(Calendar.DAY_OF_MONTH), listener); |
| + |
| + setLimits(picker, min, max); |
| + } |
| +} |