summaryrefslogtreecommitdiff
path: root/chromium/content/public/android/java/src/org/chromium/content/browser/input/TwoFieldDatePicker.java
blob: c6dfd616c388138575db5a16a207d8b5a69c79ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 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.content.Context;
import android.view.LayoutInflater;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
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 abstract class TwoFieldDatePicker extends FrameLayout {

    private NumberPicker mPositionInYearSpinner;

    private NumberPicker mYearSpinner;

    private 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.
    private Calendar mMinDate;

    private Calendar mMaxDate;

    private 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, long minValue, long maxValue) {
        super(context, null, android.R.attr.datePickerStyle);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.two_field_date_picker, this, true);

        OnValueChangeListener onChangeListener = new OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int year = getYear();
                int positionInYear = getPositionInYear();
                // take care of wrapping of days and months to update greater fields
                if (picker == mPositionInYearSpinner) {
                    positionInYear = newVal;
                    if (oldVal == picker.getMaxValue() && newVal == picker.getMinValue()) {
                        year += 1;
                        positionInYear = getMinPositionInYear(year);
                    } else if (oldVal == picker.getMinValue() && newVal == picker.getMaxValue()) {
                        year -=1;
                        positionInYear = getMaxPositionInYear(year);
                    }
                } else if (picker == mYearSpinner) {
                    year = newVal;
                 } else {
                    throw new IllegalArgumentException();
                }

                // now set the date to the adjusted one
                setCurrentDate(year, positionInYear);
                updateSpinners();
                notifyDateChanged();
            }
        };

        mCurrentDate = Calendar.getInstance();
        if (minValue >= maxValue) {
            mMinDate = Calendar.getInstance();
            mMinDate.set(0, 0, 1);
            mMaxDate = Calendar.getInstance();
            mMaxDate.set(9999, 0, 1);
        } else {
            mMinDate = createDateFromValue(minValue);
            mMaxDate = createDateFromValue(maxValue);
        }

        // month
        mPositionInYearSpinner = (NumberPicker) findViewById(R.id.position_in_year);
        mPositionInYearSpinner.setOnLongPressUpdateInterval(200);
        mPositionInYearSpinner.setOnValueChangedListener(onChangeListener);

        // year
        mYearSpinner = (NumberPicker) findViewById(R.id.year);
        mYearSpinner.setOnLongPressUpdateInterval(100);
        mYearSpinner.setOnValueChangedListener(onChangeListener);
    }

    /**
     * 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 || getPositionInYear() != positionInYear);
    }

    /**
     * Subclasses know the semantics of @value, and need to return
     * a Calendar corresponding to it.
     */
    protected abstract Calendar createDateFromValue(long value);

    /**
     * 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();
    }

    /**
     * Subclasses know the semantics of @positionInYear, and need to update @mCurrentDate to the
     * appropriate date.
     */
    protected abstract void setCurrentDate(int year, int positionInYear);

    protected void setCurrentDate(Calendar date) {
        mCurrentDate = date;
    }

    @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 abstract int getPositionInYear();

    protected abstract int getMaxYear();

    protected abstract int getMinYear();

    protected abstract int getMaxPositionInYear(int year);

    protected abstract int getMinPositionInYear(int year);

    protected Calendar getMaxDate() {
        return mMaxDate;
    }

    protected Calendar getMinDate() {
        return mMinDate;
    }

    protected Calendar getCurrentDate() {
        return mCurrentDate;
    }

    protected NumberPicker getPositionInYearSpinner() {
        return mPositionInYearSpinner;
    }

    protected NumberPicker getYearSpinner() {
        return mYearSpinner;
    }

    /**
     * This method should be subclassed to update the spinners based on mCurrentDate.
     */
    protected void updateSpinners() {
        mPositionInYearSpinner.setDisplayedValues(null);

        // set the spinner ranges respecting the min and max dates
        mPositionInYearSpinner.setMinValue(getMinPositionInYear(getYear()));
        mPositionInYearSpinner.setMaxValue(getMaxPositionInYear(getYear()));
        mPositionInYearSpinner.setWrapSelectorWheel(
                !mCurrentDate.equals(mMinDate) && !mCurrentDate.equals(mMaxDate));

        // year spinner range does not change based on the current date
        mYearSpinner.setMinValue(getMinYear());
        mYearSpinner.setMaxValue(getMaxYear());
        mYearSpinner.setWrapSelectorWheel(false);

        // set the spinner values
        mYearSpinner.setValue(getYear());
        mPositionInYearSpinner.setValue(getPositionInYear());
    }

    /**
     * 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(), getPositionInYear());
        }
    }
}