Index: content/public/android/java/src/org/chromium/content/browser/input/TwoFieldDatePicker.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/input/TwoFieldDatePicker.java b/content/public/android/java/src/org/chromium/content/browser/input/TwoFieldDatePicker.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4f637c68d1f37cf59fd5eeb09e55c0e2e1170a5c |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/input/TwoFieldDatePicker.java |
@@ -0,0 +1,155 @@ |
+// 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.text.format.DateUtils; |
+import android.view.accessibility.AccessibilityEvent; |
+import android.widget.FrameLayout; |
+import android.widget.NumberPicker; |
+ |
+import java.util.Calendar; |
+ |
+import org.chromium.content.R; |
+ |
+// This class is heavily based on android.widget.DatePicker. |
+public class TwoFieldDatePicker extends FrameLayout { |
+ |
+ protected NumberPicker mPositionInYearSpinner; |
+ |
+ protected NumberPicker mYearSpinner; |
+ |
+ protected OnMonthOrWeekChangedListener mMonthOrWeekChangedListener; |
+ |
+ // 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. |
Miguel Garcia
2013/06/11 13:02:52
no protected members please, make accessors if you
keishi
2013/06/12 13:23:07
Done. I had to add some accessors.
|
+ protected Calendar mMinDate; |
+ |
+ protected Calendar mMaxDate; |
+ |
+ protected Calendar mCurrentDate; |
+ |
+ /** |
+ * 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 positionInYear The month or week in year. |
+ */ |
+ void onMonthOrWeekChanged(TwoFieldDatePicker view, int year, int positionInYear); |
+ } |
+ |
+ public TwoFieldDatePicker(Context context) { |
+ super(context, null, android.R.attr.datePickerStyle); |
+ mCurrentDate = Calendar.getInstance(); |
+ } |
+ |
+ /** |
+ * 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 positionInYear The initial month <strong>starting from zero</strong> or week in year. |
+ * @param onMonthChangedListener How user is notified date is changed by |
+ * user, can be null. |
+ */ |
+ public void init(int year, int positionInYear, |
+ OnMonthOrWeekChangedListener onMonthOrWeekChangedListener) { |
+ setCurrentDate(year, positionInYear); |
+ updateSpinners(); |
+ mMonthOrWeekChangedListener = onMonthOrWeekChangedListener; |
+ } |
+ |
+ public boolean isNewDate(int year, int positionInYear) { |
+ return (getYear() != year || getMonthOrWeek() != positionInYear); |
+ } |
+ |
+ /** |
+ * 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; |
+ } |
+ setCurrentDate(year, positionInYear); |
+ updateSpinners(); |
+ notifyDateChanged(); |
+ } |
+ |
+ protected void setCurrentDate(int year, int positionInYear) { |
+ } |
+ |
+ @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); |
+ } |
+ |
+ /** |
+ * @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 bootstrapped with the value of a given calendar. |
+ * |
+ * @param oldCalendar The old calendar. |
+ */ |
+ protected Calendar getCalendar(Calendar oldCalendar) { |
+ Calendar newCalendar = Calendar.getInstance(); |
+ 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. |
Miguel Garcia
2013/06/11 13:02:52
make this abstract, it does require an implementat
keishi
2013/06/12 13:23:07
Done.
|
+ */ |
+ protected void updateSpinners() { |
+ } |
+ |
+ /** |
+ * Notifies the listener, if such, for a change in the selected date. |
+ */ |
+ protected void notifyDateChanged() { |
+ sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
+ if (mMonthOrWeekChangedListener != null) { |
+ mMonthOrWeekChangedListener.onMonthOrWeekChanged(this, getYear(), getMonthOrWeek()); |
+ } |
+ } |
+} |