| Index: content/public/android/java/src/org/chromium/content/browser/input/BaseDatePicker.java
|
| diff --git a/content/public/android/java/src/org/chromium/content/browser/input/BaseDatePicker.java b/content/public/android/java/src/org/chromium/content/browser/input/BaseDatePicker.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d31a42020208c3307066d5dfe5aba6dd9b24fd43
|
| --- /dev/null
|
| +++ b/content/public/android/java/src/org/chromium/content/browser/input/BaseDatePicker.java
|
| @@ -0,0 +1,221 @@
|
| +// Copyright (c) 2012 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.content.Context;
|
| +import android.content.res.Configuration;
|
| +import android.os.Parcel;
|
| +import android.os.Parcelable;
|
| +import android.text.format.DateUtils;
|
| +import android.util.AttributeSet;
|
| +import android.view.LayoutInflater;
|
| +import android.view.accessibility.AccessibilityEvent;
|
| +import android.widget.DatePicker;
|
| +import android.widget.FrameLayout;
|
| +
|
| +import java.text.DateFormatSymbols;
|
| +import java.util.Arrays;
|
| +import java.util.Calendar;
|
| +import java.util.Locale;
|
| +import java.util.TimeZone;
|
| +
|
| +import org.chromium.content.R;
|
| +
|
| +// This class is heavily based on android.widget.DatePicker.
|
| +public class BaseDatePicker extends FrameLayout {
|
| +
|
| + private static final int DEFAULT_START_YEAR = 1900;
|
| +
|
| + private static final int DEFAULT_END_YEAR = 2100;
|
| +
|
| + private static final boolean DEFAULT_ENABLED_STATE = true;
|
| +
|
| + protected Locale mCurrentLocale;
|
| +
|
| + protected Calendar mMinDate;
|
| +
|
| + protected Calendar mMaxDate;
|
| +
|
| + protected boolean mIsEnabled = DEFAULT_ENABLED_STATE;
|
| +
|
| + protected Calendar mCurrentDate;
|
| +
|
| + public BaseDatePicker(Context context) {
|
| + this(context, null);
|
| + }
|
| +
|
| + public BaseDatePicker(Context context, AttributeSet attrs) {
|
| + this(context, attrs, android.R.attr.datePickerStyle);
|
| + }
|
| +
|
| + public BaseDatePicker(Context context, AttributeSet attrs, int defStyle) {
|
| + super(context, attrs, defStyle);
|
| +
|
| + // initialization based on locale
|
| + setCurrentLocale(Locale.getDefault());
|
| +
|
| + int startYear = DEFAULT_START_YEAR;
|
| + int endYear = DEFAULT_END_YEAR;
|
| +
|
| + Calendar tempDate = getCalendarForLocale(null, mCurrentLocale);
|
| + tempDate.set(startYear, 0, 1);
|
| + setMinDate(tempDate.getTimeInMillis());
|
| + tempDate.set(endYear, 11, 31);
|
| + setMaxDate(tempDate.getTimeInMillis());
|
| + }
|
| +
|
| + /**
|
| + * Gets the minimal date supported by this {@link DatePicker} in
|
| + * milliseconds since January 1, 1970 00:00:00 in
|
| + * {@link TimeZone#getDefault()} time zone.
|
| + * <p>
|
| + * Note: The default minimal date is 01/01/1900.
|
| + * <p>
|
| + *
|
| + * @return The minimal supported date.
|
| + */
|
| + public long getMinDate() {
|
| + return mMinDate.getTimeInMillis();
|
| + }
|
| +
|
| + /**
|
| + * Sets the minimal date supported by this {@link NumberPicker} in
|
| + * milliseconds since January 1, 1970 00:00:00 in
|
| + * {@link TimeZone#getDefault()} time zone.
|
| + *
|
| + * @param minDate The minimal supported date.
|
| + */
|
| + public void setMinDate(long minDate) {
|
| + Calendar tempDate = getCalendarForLocale(null, mCurrentLocale);
|
| + tempDate.setTimeInMillis(minDate);
|
| + if (tempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)
|
| + && tempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR)) {
|
| + return;
|
| + }
|
| + mMinDate.setTimeInMillis(minDate);
|
| + if (mCurrentDate.before(mMinDate)) {
|
| + mCurrentDate.setTimeInMillis(mMinDate.getTimeInMillis());
|
| + }
|
| + updateSpinners();
|
| + }
|
| +
|
| + /**
|
| + * Gets the maximal date supported by this {@link DatePicker} in
|
| + * milliseconds since January 1, 1970 00:00:00 in
|
| + * {@link TimeZone#getDefault()} time zone.
|
| + * <p>
|
| + * Note: The default maximal date is 12/31/2100.
|
| + * <p>
|
| + *
|
| + * @return The maximal supported date.
|
| + */
|
| + public long getMaxDate() {
|
| + return mMaxDate.getTimeInMillis();
|
| + }
|
| +
|
| + /**
|
| + * Sets the maximal date supported by this {@link DatePicker} in
|
| + * milliseconds since January 1, 1970 00:00:00 in
|
| + * {@link TimeZone#getDefault()} time zone.
|
| + *
|
| + * @param maxDate The maximal supported date.
|
| + */
|
| + public void setMaxDate(long maxDate) {
|
| + Calendar tempDate = getCalendarForLocale(null, mCurrentLocale);
|
| + tempDate.setTimeInMillis(maxDate);
|
| + if (tempDate.get(Calendar.YEAR) == mMaxDate.get(Calendar.YEAR)
|
| + && tempDate.get(Calendar.DAY_OF_YEAR) != mMaxDate.get(Calendar.DAY_OF_YEAR)) {
|
| + return;
|
| + }
|
| + mMaxDate.setTimeInMillis(maxDate);
|
| + if (mCurrentDate.after(mMaxDate)) {
|
| + mCurrentDate.setTimeInMillis(mMaxDate.getTimeInMillis());
|
| + }
|
| + updateSpinners();
|
| + }
|
| +
|
| + @Override
|
| + public void setEnabled(boolean enabled) {
|
| + if (mIsEnabled == enabled) {
|
| + return;
|
| + }
|
| + super.setEnabled(enabled);
|
| + mIsEnabled = enabled;
|
| + }
|
| +
|
| + @Override
|
| + public boolean isEnabled() {
|
| + return mIsEnabled;
|
| + }
|
| +
|
| + @Override
|
| + public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
|
| + onPopulateAccessibilityEvent(event);
|
| + return true;
|
| + }
|
| +
|
| + @Override
|
| + public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
|
| + super.onPopulateAccessibilityEvent(event);
|
| +
|
| + final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
|
| + String selectedDateUtterance = DateUtils.formatDateTime(getContext(),
|
| + mCurrentDate.getTimeInMillis(), flags);
|
| + event.getText().add(selectedDateUtterance);
|
| + }
|
| +
|
| + @Override
|
| + protected void onConfigurationChanged(Configuration newConfig) {
|
| + super.onConfigurationChanged(newConfig);
|
| + setCurrentLocale(newConfig.locale);
|
| + }
|
| +
|
| + /**
|
| + * Sets the current locale.
|
| + *
|
| + * @param locale The current locale.
|
| + */
|
| + protected void setCurrentLocale(Locale locale) {
|
| + if (locale.equals(mCurrentLocale)) {
|
| + return;
|
| + }
|
| +
|
| + mCurrentLocale = locale;
|
| + mMinDate = getCalendarForLocale(mMinDate, locale);
|
| + mMaxDate = getCalendarForLocale(mMaxDate, locale);
|
| + mCurrentDate = getCalendarForLocale(mCurrentDate, locale);
|
| + }
|
| +
|
| + /**
|
| + * Gets a calendar for locale bootstrapped with the value of a given calendar.
|
| + *
|
| + * @param oldCalendar The old calendar.
|
| + * @param locale The locale.
|
| + */
|
| + protected Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
|
| + Calendar newCalendar = Calendar.getInstance(locale);
|
| + newCalendar.setFirstDayOfWeek(Calendar.MONDAY);
|
| + newCalendar.setMinimalDaysInFirstWeek(4);
|
| + if (oldCalendar == null) {
|
| + newCalendar.setTimeInMillis(0);
|
| + } else {
|
| + newCalendar.setTimeInMillis(oldCalendar.getTimeInMillis());
|
| + }
|
| + return newCalendar;
|
| + }
|
| +
|
| + /**
|
| + * This method should be subclassed to update the spinners based on mCurrentDate.
|
| + */
|
| + private void updateSpinners() {
|
| + }
|
| +
|
| + /**
|
| + * Notifies the listener, if such, for a change in the selected date.
|
| + */
|
| + private void notifyDateChanged() {
|
| + sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
|
| + }
|
| +}
|
|
|