Index: content/public/android/java/src/org/chromium/content/browser/input/MonthPicker.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/input/MonthPicker.java b/content/public/android/java/src/org/chromium/content/browser/input/MonthPicker.java |
index b668c3fc71e6eb6c7aa266273dac11cce61eae98..3e06d75cf903e53bd317c8500607d138fb822bb7 100644 |
--- a/content/public/android/java/src/org/chromium/content/browser/input/MonthPicker.java |
+++ b/content/public/android/java/src/org/chromium/content/browser/input/MonthPicker.java |
@@ -5,135 +5,38 @@ |
package org.chromium.content.browser.input; |
import android.content.Context; |
-import android.content.res.Configuration; |
-import android.text.format.DateUtils; |
-import android.view.LayoutInflater; |
-import android.view.accessibility.AccessibilityEvent; |
-import android.widget.FrameLayout; |
-import android.widget.NumberPicker; |
-import android.widget.NumberPicker.OnValueChangeListener; |
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; |
-public class MonthPicker extends FrameLayout { |
+public class MonthPicker extends TwoFieldDatePicker { |
private static final int MONTHS_NUMBER = 12; |
- private final NumberPicker mMonthSpinner; |
- |
- private final NumberPicker mYearSpinner; |
- |
- |
- private OnMonthChangedListener mMonthChangedListener; |
- |
private String[] mShortMonths; |
- // It'd be nice to use android.text.Time like in other Dialogs but |
- // it suffers from the 2038 effect so it would prevent us from |
- // having dates over 2038. |
- private Calendar mMinDate; |
+ public MonthPicker(Context context, long minValue, long maxValue) { |
+ super(context, minValue, maxValue); |
- private Calendar mMaxDate; |
- |
- private Calendar mCurrentDate; |
- |
- /** |
- * The callback used to indicate the user changes\d the date. |
- */ |
- public interface OnMonthChangedListener { |
- |
- /** |
- * Called upon a date change. |
- * |
- * @param view The view associated with this listener. |
- * @param year The year that was set. |
- * @param month The month that was set (0-11) for compatibility |
- * with {@link java.util.Calendar}. |
- */ |
- void onMonthChanged(MonthPicker view, int year, int month); |
- } |
- |
- public MonthPicker(Context context, long minMonth, long maxMonth) { |
- super(context, null, android.R.attr.datePickerStyle); |
+ getPositionInYearSpinner().setContentDescription( |
+ getResources().getString(R.string.accessibility_date_picker_month)); |
// initialization based on locale |
mShortMonths = |
DateFormatSymbols.getInstance(Locale.getDefault()).getShortMonths(); |
- LayoutInflater inflater = (LayoutInflater) context |
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
- inflater.inflate(R.layout.month_picker, this, true); |
- |
- OnValueChangeListener onChangeListener = new OnValueChangeListener() { |
- @Override |
- public void onValueChange(NumberPicker picker, int oldVal, int newVal) { |
- int month = mCurrentDate.get(Calendar.MONTH); |
- int year = mCurrentDate.get(Calendar.YEAR); |
- |
- // take care of wrapping of days and months to update greater fields |
- if (picker == mMonthSpinner) { |
- month = newVal; |
- if (oldVal == (MONTHS_NUMBER - 1) && newVal == 0) { |
- year += 1; |
- } else if (oldVal == 0 && newVal == 11) { |
- year -=1; |
- } |
- } else if (picker == mYearSpinner) { |
- year = newVal; |
- } else { |
- throw new IllegalArgumentException(); |
- } |
- |
- // now set the date to the adjusted one |
- setCurrentDate(year, month); |
- updateSpinners(); |
- notifyDateChanged(); |
- } |
- }; |
- |
- // month |
- mMonthSpinner = (NumberPicker) findViewById(R.id.month); |
- mMonthSpinner.setMinValue(0); |
- mMonthSpinner.setMaxValue(MONTHS_NUMBER - 1); |
- mMonthSpinner.setDisplayedValues(mShortMonths); |
- mMonthSpinner.setOnLongPressUpdateInterval(200); |
- mMonthSpinner.setOnValueChangedListener(onChangeListener); |
- |
- // year |
- mYearSpinner = (NumberPicker) findViewById(R.id.year); |
- mYearSpinner.setOnLongPressUpdateInterval(100); |
- mYearSpinner.setOnValueChangedListener(onChangeListener); |
- |
- mMinDate = monthsToCalendar(minMonth); |
- mMaxDate = monthsToCalendar(maxMonth); |
- |
// initialize to current date |
Calendar cal = Calendar.getInstance(); |
init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), null); |
} |
- private void setCurrentDate(int year, int month) { |
- if (mCurrentDate == null) { |
- mCurrentDate = Calendar.getInstance(); |
- } |
- mCurrentDate.clear(); |
- mCurrentDate.set(year, month, 1); |
- if (mCurrentDate.getTimeInMillis() < mMinDate.getTimeInMillis()) { |
- mCurrentDate = (Calendar) mMinDate.clone(); |
- } else if (mCurrentDate.getTimeInMillis() > mMaxDate.getTimeInMillis()) { |
- mCurrentDate = (Calendar) mMaxDate.clone(); |
- } |
- updateSpinners(); |
- } |
- |
- private static Calendar monthsToCalendar(long months) { |
- int year = (int)Math.min(months / 12 + 1970, Integer.MAX_VALUE); |
- int month = (int) (months % 12); |
+ @Override |
+ protected Calendar createDateFromValue(long value) { |
+ int year = (int)Math.min(value / 12 + 1970, Integer.MAX_VALUE); |
+ int month = (int) (value % 12); |
Calendar cal = Calendar.getInstance(); |
cal.clear(); |
cal.set(year, month, 1); |
@@ -141,98 +44,66 @@ public class MonthPicker extends FrameLayout { |
} |
@Override |
- public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
- onPopulateAccessibilityEvent(event); |
- return true; |
+ protected void setCurrentDate(int year, int month) { |
+ Calendar date = Calendar.getInstance(); |
+ date.set(year, month, 1); |
+ if (date.before(getMinDate())) { |
+ setCurrentDate(getMinDate()); |
+ } else if (date.after(getMaxDate())) { |
+ setCurrentDate(getMaxDate()); |
+ } else { |
+ setCurrentDate(date); |
+ } |
} |
@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); |
- } |
+ protected void updateSpinners() { |
+ super.updateSpinners(); |
- @Override |
- protected void onConfigurationChanged(Configuration newConfig) { |
- super.onConfigurationChanged(newConfig); |
+ // make sure the month names are a zero based array |
+ // with the months in the month spinner |
+ String[] displayedValues = Arrays.copyOfRange(mShortMonths, |
+ getPositionInYearSpinner().getMinValue(), |
+ getPositionInYearSpinner().getMaxValue() + 1); |
+ getPositionInYearSpinner().setDisplayedValues(displayedValues); |
} |
/** |
- * Initialize the state. If the provided values designate an inconsistent |
- * date the values are normalized before updating the spinners. |
- * |
- * @param year The initial year. |
- * @param month The initial month <strong>starting from zero</strong>. |
- * @param onMonthChangedListener How user is notified date is changed by |
- * user, can be null. |
+ * @return The selected month. |
*/ |
- public void init(int year, int month, OnMonthChangedListener onMonthChangedListener) { |
- setCurrentDate(year, month); |
- updateSpinners(); |
- mMonthChangedListener = onMonthChangedListener; |
+ public int getMonth() { |
+ return getCurrentDate().get(Calendar.MONTH); |
} |
- private boolean isNewDate(int year, int month) { |
- return (mCurrentDate.get(Calendar.YEAR) != year |
- || mCurrentDate.get(Calendar.MONTH) != month); |
+ @Override |
+ public int getPositionInYear() { |
+ return getMonth(); |
} |
- private void updateSpinners() { |
- // set the spinner ranges respecting the min and max dates |
- mMonthSpinner.setDisplayedValues(null); |
- mMonthSpinner.setMinValue(0); |
- mMonthSpinner.setMaxValue(MONTHS_NUMBER - 1); |
- if (mCurrentDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)) { |
- mMonthSpinner.setMinValue(mMinDate.get(Calendar.MONTH)); |
- mMonthSpinner.setWrapSelectorWheel(false); |
- } else if (mCurrentDate.get(Calendar.YEAR) == mMaxDate.get(Calendar.YEAR)) { |
- mMonthSpinner.setMaxValue(mMaxDate.get(Calendar.MONTH)); |
- mMonthSpinner.setWrapSelectorWheel(false); |
- } else { |
- mMonthSpinner.setWrapSelectorWheel(true); |
- } |
- |
- // make sure the month names are a zero based array |
- // with the months in the month spinner |
- String[] displayedValues = Arrays.copyOfRange(mShortMonths, |
- mMonthSpinner.getMinValue(), mMonthSpinner.getMaxValue() + 1); |
- mMonthSpinner.setDisplayedValues(displayedValues); |
- |
- // year spinner range does not change based on the current date |
- mYearSpinner.setMinValue(mMinDate.get(Calendar.YEAR)); |
- mYearSpinner.setMaxValue(mMaxDate.get(Calendar.YEAR)); |
- mYearSpinner.setWrapSelectorWheel(false); |
- |
- // set the spinner values |
- mYearSpinner.setValue(mCurrentDate.get(Calendar.YEAR)); |
- mMonthSpinner.setValue(mCurrentDate.get(Calendar.MONTH)); |
+ @Override |
+ protected int getMaxYear() { |
+ return getMaxDate().get(Calendar.YEAR); |
} |
- /** |
- * @return The selected year. |
- */ |
- public int getYear() { |
- return mCurrentDate.get(Calendar.YEAR); |
+ @Override |
+ protected int getMinYear() { |
+ return getMinDate().get(Calendar.YEAR); |
} |
- /** |
- * @return The selected month. |
- */ |
- public int getMonth() { |
- return mCurrentDate.get(Calendar.MONTH); |
+ |
+ @Override |
+ protected int getMaxPositionInYear() { |
+ if (getYear() == getMaxDate().get(Calendar.YEAR)) { |
+ return getMaxDate().get(Calendar.MONTH); |
+ } |
+ return MONTHS_NUMBER - 1; |
} |
- /** |
- * Notifies the listener, if such, for a change in the selected date. |
- */ |
- private void notifyDateChanged() { |
- sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
- if (mMonthChangedListener != null) { |
- mMonthChangedListener.onMonthChanged(this, getYear(), getMonth()); |
+ @Override |
+ protected int getMinPositionInYear() { |
+ if (getYear() == getMinDate().get(Calendar.YEAR)) { |
+ return getMinDate().get(Calendar.MONTH); |
} |
+ return 0; |
} |
} |