Chromium Code Reviews| 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..deff89e220d831f83598eddcddde59f5c2e65efa |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/input/BaseDatePicker.java |
| @@ -0,0 +1,293 @@ |
| +// 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 android.widget.NumberPicker; |
| + |
| +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 { |
|
Miguel Garcia
2013/05/23 18:38:49
DatePicker suggest its a date, let's go with TwoFi
keishi
2013/06/07 11:08:52
Done.
|
| + |
|
Miguel Garcia
2013/05/23 18:38:49
I've made a significant simplification of the mont
keishi
2013/06/07 11:08:52
Done.
|
| + 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; |
| + |
| + protected NumberPicker mPositionInYearSpinner; |
| + |
| + protected NumberPicker mYearSpinner; |
| + |
| + protected OnMonthOrWeekChangedListener mMonthChangedListener; |
| + |
| + /** |
| + * The callback used to indicate the user changes\d the date. |
| + */ |
| + public interface OnMonthOrWeekChangedListener { |
| + |
| + /** |
| + * Called upon a date change. |
| + * |
| + * @param view The view associated with this listener. |
| + * @param year The year that was set. |
| + * @param weekOfYear The week in year. |
| + */ |
| + void onWeekChanged(WeekPicker view, int year, int weekOfYear); |
| + |
| + /** |
| + * Called upon a date change. |
| + * |
| + * @param view The view associated with this listener. |
| + * @param year The year that was set. |
| + * @param monthOfYear The month that was set (0-11) for compatibility |
| + * with {@link java.util.Calendar}. |
| + */ |
| + void onMonthChanged(MonthPicker view, int year, int monthOfYear); |
| + } |
| + |
| + 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()); |
| + } |
| + |
| + public void init(int year, int weekOfYear, |
| + OnMonthOrWeekChangedListener onDateChangedListener) { |
| + } |
| + |
| + /** |
| + * Updates the current date. |
| + * |
| + * @param year The year. |
| + * @param positionInYear The month or week in year. |
| + */ |
| + public void updateDate(int year, int positionInYear) { |
| + if (!isNewDate(year, positionInYear)) { |
| + return; |
| + } |
| + setDate(year, positionInYear); |
| + updateSpinners(); |
| + notifyDateChanged(); |
| + } |
| + |
| + protected boolean isNewDate(int year, int positionInYear) { |
| + return true; |
| + } |
| + |
| + protected void setDate(int year, int positionInYear) { |
| + } |
| + |
| + /** |
| + * 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); |
| + } |
| + |
| + /** |
| + * @return The selected year. |
| + */ |
| + public int getYear() { |
| + return mCurrentDate.get(Calendar.YEAR); |
| + } |
| + |
| + /** |
| + * @return The selected month or week. |
| + */ |
| + public int getMonthOrWeek() { |
| + return 0; |
| + } |
| + |
| + /** |
| + * 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); |
| + } |
| +} |