TimePickerClockDelegate.java revision 4420ae875de711a91dc10f7f4dd5a9cc62221ac8
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.res.ColorStateList;
22import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.text.SpannableStringBuilder;
27import android.text.format.DateFormat;
28import android.text.format.DateUtils;
29import android.text.style.TtsSpan;
30import android.util.AttributeSet;
31import android.util.StateSet;
32import android.view.HapticFeedbackConstants;
33import android.view.LayoutInflater;
34import android.view.MotionEvent;
35import android.view.View;
36import android.view.View.AccessibilityDelegate;
37import android.view.View.MeasureSpec;
38import android.view.ViewGroup;
39import android.view.accessibility.AccessibilityEvent;
40import android.view.accessibility.AccessibilityNodeInfo;
41import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
42
43import com.android.internal.R;
44import com.android.internal.widget.NumericTextView;
45import com.android.internal.widget.NumericTextView.OnValueChangedListener;
46
47import java.util.Calendar;
48import java.util.Locale;
49
50/**
51 * A delegate implementing the radial clock-based TimePicker.
52 */
53class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate implements
54        RadialTimePickerView.OnValueSelectedListener {
55    /**
56     * Delay in milliseconds before valid but potentially incomplete, for
57     * example "1" but not "12", keyboard edits are propagated from the
58     * hour / minute fields to the radial picker.
59     */
60    private static final long DELAY_COMMIT_MILLIS = 2000;
61
62    // Index used by RadialPickerLayout
63    private static final int HOUR_INDEX = 0;
64    private static final int MINUTE_INDEX = 1;
65
66    // NOT a real index for the purpose of what's showing.
67    private static final int AMPM_INDEX = 2;
68
69    private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor};
70    private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha};
71
72    // LayoutLib relies on these constants. Change TimePickerClockDelegate_Delegate if
73    // modifying these.
74    static final int AM = 0;
75    static final int PM = 1;
76
77    private static final int HOURS_IN_HALF_DAY = 12;
78
79    private final NumericTextView mHourView;
80    private final NumericTextView mMinuteView;
81    private final View mAmPmLayout;
82    private final RadioButton mAmLabel;
83    private final RadioButton mPmLabel;
84    private final RadialTimePickerView mRadialTimePickerView;
85    private final TextView mSeparatorView;
86
87    private boolean mIsEnabled = true;
88    private boolean mAllowAutoAdvance;
89    private int mInitialHourOfDay;
90    private int mInitialMinute;
91    private boolean mIs24Hour;
92    private boolean mIsAmPmAtStart;
93
94    // Accessibility strings.
95    private String mSelectHours;
96    private String mSelectMinutes;
97
98    // Localization data.
99    private boolean mHourFormatShowLeadingZero;
100    private boolean mHourFormatStartsAtZero;
101
102    // Most recent time announcement values for accessibility.
103    private CharSequence mLastAnnouncedText;
104    private boolean mLastAnnouncedIsHour;
105
106    private Calendar mTempCalendar;
107
108    public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs,
109            int defStyleAttr, int defStyleRes) {
110        super(delegator, context);
111
112        // process style attributes
113        final TypedArray a = mContext.obtainStyledAttributes(attrs,
114                R.styleable.TimePicker, defStyleAttr, defStyleRes);
115        final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
116                Context.LAYOUT_INFLATER_SERVICE);
117        final Resources res = mContext.getResources();
118
119        mSelectHours = res.getString(R.string.select_hours);
120        mSelectMinutes = res.getString(R.string.select_minutes);
121
122        final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout,
123                R.layout.time_picker_material);
124        final View mainView = inflater.inflate(layoutResourceId, delegator);
125        final View headerView = mainView.findViewById(R.id.time_header);
126        headerView.setOnTouchListener(new NearestTouchDelegate());
127
128        // Set up hour/minute labels.
129        mHourView = (NumericTextView) mainView.findViewById(R.id.hours);
130        mHourView.setOnClickListener(mClickListener);
131        mHourView.setOnFocusChangeListener(mFocusListener);
132        mHourView.setOnDigitEnteredListener(mDigitEnteredListener);
133        mHourView.setAccessibilityDelegate(
134                new ClickActionDelegate(context, R.string.select_hours));
135        mSeparatorView = (TextView) mainView.findViewById(R.id.separator);
136        mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes);
137        mMinuteView.setOnClickListener(mClickListener);
138        mMinuteView.setOnFocusChangeListener(mFocusListener);
139        mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener);
140        mMinuteView.setAccessibilityDelegate(
141                new ClickActionDelegate(context, R.string.select_minutes));
142        mMinuteView.setRange(0, 59);
143
144        // Set up AM/PM labels.
145        mAmPmLayout = mainView.findViewById(R.id.ampm_layout);
146        mAmPmLayout.setOnTouchListener(new NearestTouchDelegate());
147
148        final String[] amPmStrings = TimePicker.getAmPmStrings(context);
149        mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label);
150        mAmLabel.setText(obtainVerbatim(amPmStrings[0]));
151        mAmLabel.setOnClickListener(mClickListener);
152        ensureMinimumTextWidth(mAmLabel);
153
154        mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label);
155        mPmLabel.setText(obtainVerbatim(amPmStrings[1]));
156        mPmLabel.setOnClickListener(mClickListener);
157        ensureMinimumTextWidth(mPmLabel);
158
159        // For the sake of backwards compatibility, attempt to extract the text
160        // color from the header time text appearance. If it's set, we'll let
161        // that override the "real" header text color.
162        ColorStateList headerTextColor = null;
163
164        @SuppressWarnings("deprecation")
165        final int timeHeaderTextAppearance = a.getResourceId(
166                R.styleable.TimePicker_headerTimeTextAppearance, 0);
167        if (timeHeaderTextAppearance != 0) {
168            final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
169                    ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance);
170            final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
171            headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
172            textAppearance.recycle();
173        }
174
175        if (headerTextColor == null) {
176            headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor);
177        }
178
179        if (headerTextColor != null) {
180            mHourView.setTextColor(headerTextColor);
181            mSeparatorView.setTextColor(headerTextColor);
182            mMinuteView.setTextColor(headerTextColor);
183            mAmLabel.setTextColor(headerTextColor);
184            mPmLabel.setTextColor(headerTextColor);
185        }
186
187        // Set up header background, if available.
188        if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) {
189            headerView.setBackground(a.getDrawable(R.styleable.TimePicker_headerBackground));
190        }
191
192        a.recycle();
193
194        mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker);
195        mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes);
196
197        setupListeners();
198
199        mAllowAutoAdvance = true;
200
201        // Updates mHourFormat variables used below.
202        updateHourFormat(mLocale, mIs24Hour);
203
204        // Update hour text field.
205        final int minHour = mHourFormatStartsAtZero ? 0 : 1;
206        final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
207        mHourView.setRange(minHour, maxHour);
208        mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero);
209
210        // Initialize with current time.
211        mTempCalendar = Calendar.getInstance(mLocale);
212        final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY);
213        final int currentMinute = mTempCalendar.get(Calendar.MINUTE);
214        initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX);
215    }
216
217    /**
218     * Ensures that a TextView is wide enough to contain its text without
219     * wrapping or clipping. Measures the specified view and sets the minimum
220     * width to the view's desired width.
221     *
222     * @param v the text view to measure
223     */
224    private static void ensureMinimumTextWidth(TextView v) {
225        v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
226
227        // Set both the TextView and the View version of minimum
228        // width because they are subtly different.
229        final int minWidth = v.getMeasuredWidth();
230        v.setMinWidth(minWidth);
231        v.setMinimumWidth(minWidth);
232    }
233
234    /**
235     * Determines how the hour should be formatted and updates member variables
236     * related to hour formatting.
237     *
238     * @param locale the locale in which the view is displayed
239     * @param is24Hour whether the view is in 24-hour (hour-of-day) mode
240     */
241    private void updateHourFormat(Locale locale, boolean is24Hour) {
242        final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
243                locale, is24Hour ? "Hm" : "hm");
244        final int lengthPattern = bestDateTimePattern.length();
245        boolean showLeadingZero = false;
246        char hourFormat = '\0';
247
248        for (int i = 0; i < lengthPattern; i++) {
249            final char c = bestDateTimePattern.charAt(i);
250            if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
251                hourFormat = c;
252                if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
253                    showLeadingZero = true;
254                }
255                break;
256            }
257        }
258
259        mHourFormatShowLeadingZero = showLeadingZero;
260        mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H';
261    }
262
263    private static final CharSequence obtainVerbatim(String text) {
264        return new SpannableStringBuilder().append(text,
265                new TtsSpan.VerbatimBuilder(text).build(), 0);
266    }
267
268    /**
269     * The legacy text color might have been poorly defined. Ensures that it
270     * has an appropriate activated state, using the selected state if one
271     * exists or modifying the default text color otherwise.
272     *
273     * @param color a legacy text color, or {@code null}
274     * @return a color state list with an appropriate activated state, or
275     *         {@code null} if a valid activated state could not be generated
276     */
277    @Nullable
278    private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
279        if (color == null || color.hasState(R.attr.state_activated)) {
280            return color;
281        }
282
283        final int activatedColor;
284        final int defaultColor;
285        if (color.hasState(R.attr.state_selected)) {
286            activatedColor = color.getColorForState(StateSet.get(
287                    StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
288            defaultColor = color.getColorForState(StateSet.get(
289                    StateSet.VIEW_STATE_ENABLED), 0);
290        } else {
291            activatedColor = color.getDefaultColor();
292
293            // Generate a non-activated color using the disabled alpha.
294            final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
295            final float disabledAlpha = ta.getFloat(0, 0.30f);
296            defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
297        }
298
299        if (activatedColor == 0 || defaultColor == 0) {
300            // We somehow failed to obtain the colors.
301            return null;
302        }
303
304        final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
305        final int[] colors = new int[] { activatedColor, defaultColor };
306        return new ColorStateList(stateSet, colors);
307    }
308
309    private int multiplyAlphaComponent(int color, float alphaMod) {
310        final int srcRgb = color & 0xFFFFFF;
311        final int srcAlpha = (color >> 24) & 0xFF;
312        final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f);
313        return srcRgb | (dstAlpha << 24);
314    }
315
316    private static class ClickActionDelegate extends AccessibilityDelegate {
317        private final AccessibilityAction mClickAction;
318
319        public ClickActionDelegate(Context context, int resId) {
320            mClickAction = new AccessibilityAction(
321                    AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId));
322        }
323
324        @Override
325        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
326            super.onInitializeAccessibilityNodeInfo(host, info);
327
328            info.addAction(mClickAction);
329        }
330    }
331
332    private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) {
333        mInitialHourOfDay = hourOfDay;
334        mInitialMinute = minute;
335        mIs24Hour = is24HourView;
336        updateUI(index);
337    }
338
339    private void setupListeners() {
340        mRadialTimePickerView.setOnValueSelectedListener(this);
341    }
342
343    private void updateUI(int index) {
344        updateHeaderAmPm();
345        updateHeaderHour(mInitialHourOfDay, false);
346        updateHeaderSeparator();
347        updateHeaderMinute(mInitialMinute, false);
348        updateRadialPicker(index);
349
350        mDelegator.invalidate();
351    }
352
353    private void updateRadialPicker(int index) {
354        mRadialTimePickerView.initialize(mInitialHourOfDay, mInitialMinute, mIs24Hour);
355        setCurrentItemShowing(index, false, true);
356    }
357
358    private void updateHeaderAmPm() {
359        if (mIs24Hour) {
360            mAmPmLayout.setVisibility(View.GONE);
361        } else {
362            // Ensure that AM/PM layout is in the correct position.
363            final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm");
364            final boolean isAmPmAtStart = dateTimePattern.startsWith("a");
365            setAmPmAtStart(isAmPmAtStart);
366
367            updateAmPmLabelStates(mInitialHourOfDay < 12 ? AM : PM);
368        }
369    }
370
371    private void setAmPmAtStart(boolean isAmPmAtStart) {
372        if (mIsAmPmAtStart != isAmPmAtStart) {
373            mIsAmPmAtStart = isAmPmAtStart;
374
375            final RelativeLayout.LayoutParams params =
376                    (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
377            if (params.getRule(RelativeLayout.RIGHT_OF) != 0 ||
378                    params.getRule(RelativeLayout.LEFT_OF) != 0) {
379                if (isAmPmAtStart) {
380                    params.removeRule(RelativeLayout.RIGHT_OF);
381                    params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
382                } else {
383                    params.removeRule(RelativeLayout.LEFT_OF);
384                    params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
385                }
386            }
387
388            mAmPmLayout.setLayoutParams(params);
389        }
390    }
391
392    /**
393     * Set the current hour.
394     */
395    @Override
396    public void setHour(int hour) {
397        if (mInitialHourOfDay != hour) {
398            mInitialHourOfDay = hour;
399            updateHeaderHour(hour, true);
400            updateHeaderAmPm();
401            mRadialTimePickerView.setCurrentHour(hour);
402            mRadialTimePickerView.setAmOrPm(mInitialHourOfDay < 12 ? AM : PM);
403            mDelegator.invalidate();
404            onTimeChanged();
405        }
406    }
407
408    /**
409     * @return the current hour in the range (0-23)
410     */
411    @Override
412    public int getHour() {
413        final int currentHour = mRadialTimePickerView.getCurrentHour();
414        if (mIs24Hour) {
415            return currentHour;
416        }
417
418        if (mRadialTimePickerView.getAmOrPm() == PM) {
419            return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY;
420        } else {
421            return currentHour % HOURS_IN_HALF_DAY;
422        }
423    }
424
425    /**
426     * Set the current minute (0-59).
427     */
428    @Override
429    public void setMinute(int minute) {
430        if (mInitialMinute != minute) {
431            mInitialMinute = minute;
432            updateHeaderMinute(minute, true);
433            mRadialTimePickerView.setCurrentMinute(minute);
434            mDelegator.invalidate();
435            onTimeChanged();
436        }
437    }
438
439    /**
440     * @return The current minute.
441     */
442    @Override
443    public int getMinute() {
444        return mRadialTimePickerView.getCurrentMinute();
445    }
446
447    /**
448     * Sets whether time is displayed in 24-hour mode or 12-hour mode with
449     * AM/PM indicators.
450     *
451     * @param is24Hour {@code true} to display time in 24-hour mode or
452     *        {@code false} for 12-hour mode with AM/PM
453     */
454    public void setIs24Hour(boolean is24Hour) {
455        if (mIs24Hour != is24Hour) {
456            mIs24Hour = is24Hour;
457            mInitialHourOfDay = getHour();
458
459            updateUI(mRadialTimePickerView.getCurrentItemShowing());
460        }
461    }
462
463    /**
464     * @return {@code true} if time is displayed in 24-hour mode, or
465     *         {@code false} if time is displayed in 12-hour mode with AM/PM
466     *         indicators
467     */
468    @Override
469    public boolean is24Hour() {
470        return mIs24Hour;
471    }
472
473    @Override
474    public void setOnTimeChangedListener(TimePicker.OnTimeChangedListener callback) {
475        mOnTimeChangedListener = callback;
476    }
477
478    @Override
479    public void setEnabled(boolean enabled) {
480        mHourView.setEnabled(enabled);
481        mMinuteView.setEnabled(enabled);
482        mAmLabel.setEnabled(enabled);
483        mPmLabel.setEnabled(enabled);
484        mRadialTimePickerView.setEnabled(enabled);
485        mIsEnabled = enabled;
486    }
487
488    @Override
489    public boolean isEnabled() {
490        return mIsEnabled;
491    }
492
493    @Override
494    public int getBaseline() {
495        // does not support baseline alignment
496        return -1;
497    }
498
499    @Override
500    public Parcelable onSaveInstanceState(Parcelable superState) {
501        return new SavedState(superState, getHour(), getMinute(),
502                is24Hour(), getCurrentItemShowing());
503    }
504
505    @Override
506    public void onRestoreInstanceState(Parcelable state) {
507        final SavedState ss = (SavedState) state;
508        initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing());
509        mRadialTimePickerView.invalidate();
510    }
511
512    @Override
513    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
514        onPopulateAccessibilityEvent(event);
515        return true;
516    }
517
518    @Override
519    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
520        int flags = DateUtils.FORMAT_SHOW_TIME;
521        if (mIs24Hour) {
522            flags |= DateUtils.FORMAT_24HOUR;
523        } else {
524            flags |= DateUtils.FORMAT_12HOUR;
525        }
526        mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour());
527        mTempCalendar.set(Calendar.MINUTE, getMinute());
528        String selectedDate = DateUtils.formatDateTime(mContext,
529                mTempCalendar.getTimeInMillis(), flags);
530        event.getText().add(selectedDate);
531    }
532
533    /**
534     * @return the index of the current item showing
535     */
536    private int getCurrentItemShowing() {
537        return mRadialTimePickerView.getCurrentItemShowing();
538    }
539
540    /**
541     * Propagate the time change
542     */
543    private void onTimeChanged() {
544        mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
545        if (mOnTimeChangedListener != null) {
546            mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
547        }
548    }
549
550    /**
551     * Used to save / restore state of time picker
552     */
553    private static class SavedState extends View.BaseSavedState {
554
555        private final int mHour;
556        private final int mMinute;
557        private final boolean mIs24HourMode;
558        private final int mCurrentItemShowing;
559
560        private SavedState(Parcelable superState, int hour, int minute, boolean is24HourMode,
561                int currentItemShowing) {
562            super(superState);
563            mHour = hour;
564            mMinute = minute;
565            mIs24HourMode = is24HourMode;
566            mCurrentItemShowing = currentItemShowing;
567        }
568
569        private SavedState(Parcel in) {
570            super(in);
571            mHour = in.readInt();
572            mMinute = in.readInt();
573            mIs24HourMode = (in.readInt() == 1);
574            mCurrentItemShowing = in.readInt();
575        }
576
577        public int getHour() {
578            return mHour;
579        }
580
581        public int getMinute() {
582            return mMinute;
583        }
584
585        public boolean is24HourMode() {
586            return mIs24HourMode;
587        }
588
589        public int getCurrentItemShowing() {
590            return mCurrentItemShowing;
591        }
592
593        @Override
594        public void writeToParcel(Parcel dest, int flags) {
595            super.writeToParcel(dest, flags);
596            dest.writeInt(mHour);
597            dest.writeInt(mMinute);
598            dest.writeInt(mIs24HourMode ? 1 : 0);
599            dest.writeInt(mCurrentItemShowing);
600        }
601
602        @SuppressWarnings({"unused", "hiding"})
603        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
604            public SavedState createFromParcel(Parcel in) {
605                return new SavedState(in);
606            }
607
608            public SavedState[] newArray(int size) {
609                return new SavedState[size];
610            }
611        };
612    }
613
614    private void tryVibrate() {
615        mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
616    }
617
618    private void updateAmPmLabelStates(int amOrPm) {
619        final boolean isAm = amOrPm == AM;
620        mAmLabel.setActivated(isAm);
621        mAmLabel.setChecked(isAm);
622
623        final boolean isPm = amOrPm == PM;
624        mPmLabel.setActivated(isPm);
625        mPmLabel.setChecked(isPm);
626    }
627
628    /**
629     * Called by the picker for updating the header display.
630     */
631    @Override
632    public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) {
633        switch (pickerIndex) {
634            case HOUR_INDEX:
635                if (mAllowAutoAdvance && autoAdvance) {
636                    updateHeaderHour(newValue, false);
637                    setCurrentItemShowing(MINUTE_INDEX, true, false);
638                    mDelegator.announceForAccessibility(newValue + ". " + mSelectMinutes);
639                } else {
640                    updateHeaderHour(newValue, true);
641                }
642                break;
643            case MINUTE_INDEX:
644                updateHeaderMinute(newValue, true);
645                break;
646            case AMPM_INDEX:
647                updateAmPmLabelStates(newValue);
648                break;
649        }
650
651        if (mOnTimeChangedListener != null) {
652            mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
653        }
654    }
655
656    /**
657     * Converts hour-of-day (0-23) time into a localized hour number.
658     *
659     * @param hourOfDay the hour-of-day (0-23)
660     * @return a localized hour number
661     */
662    private int getLocalizedHour(int hourOfDay) {
663        if (!mIs24Hour) {
664            // Convert to hour-of-am-pm.
665            hourOfDay %= 12;
666        }
667
668        if (!mHourFormatStartsAtZero && hourOfDay == 0) {
669            // Convert to clock-hour (either of-day or of-am-pm).
670            hourOfDay = mIs24Hour ? 24 : 12;
671        }
672
673        return hourOfDay;
674    }
675
676    private void updateHeaderHour(int hourOfDay, boolean announce) {
677        final int localizedHour = getLocalizedHour(hourOfDay);
678        mHourView.setValue(localizedHour);
679
680        if (announce) {
681            tryAnnounceForAccessibility(mHourView.getText(), true);
682        }
683    }
684
685    private void updateHeaderMinute(int minuteOfHour, boolean announce) {
686        mMinuteView.setValue(minuteOfHour);
687
688        if (announce) {
689            tryAnnounceForAccessibility(mMinuteView.getText(), false);
690        }
691    }
692
693    /**
694     * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
695     *
696     * See http://unicode.org/cldr/trac/browser/trunk/common/main
697     *
698     * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
699     * separator as the character which is just after the hour marker in the returned pattern.
700     */
701    private void updateHeaderSeparator() {
702        final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
703                (mIs24Hour) ? "Hm" : "hm");
704        final String separatorText;
705        // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats
706        final char[] hourFormats = {'H', 'h', 'K', 'k'};
707        int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats);
708        if (hIndex == -1) {
709            // Default case
710            separatorText = ":";
711        } else {
712            separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1));
713        }
714        mSeparatorView.setText(separatorText);
715    }
716
717    static private int lastIndexOfAny(String str, char[] any) {
718        final int lengthAny = any.length;
719        if (lengthAny > 0) {
720            for (int i = str.length() - 1; i >= 0; i--) {
721                char c = str.charAt(i);
722                for (int j = 0; j < lengthAny; j++) {
723                    if (c == any[j]) {
724                        return i;
725                    }
726                }
727            }
728        }
729        return -1;
730    }
731
732    private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) {
733        if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) {
734            // TODO: Find a better solution, potentially live regions?
735            mDelegator.announceForAccessibility(text);
736            mLastAnnouncedText = text;
737            mLastAnnouncedIsHour = isHour;
738        }
739    }
740
741    /**
742     * Show either Hours or Minutes.
743     */
744    private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) {
745        mRadialTimePickerView.setCurrentItemShowing(index, animateCircle);
746
747        if (index == HOUR_INDEX) {
748            if (announce) {
749                mDelegator.announceForAccessibility(mSelectHours);
750            }
751        } else {
752            if (announce) {
753                mDelegator.announceForAccessibility(mSelectMinutes);
754            }
755        }
756
757        mHourView.setActivated(index == HOUR_INDEX);
758        mMinuteView.setActivated(index == MINUTE_INDEX);
759    }
760
761    private void setAmOrPm(int amOrPm) {
762        updateAmPmLabelStates(amOrPm);
763        mRadialTimePickerView.setAmOrPm(amOrPm);
764    }
765
766    private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() {
767        @Override
768        public void onValueChanged(NumericTextView view, int value,
769                boolean isValid, boolean isFinished) {
770            final Runnable commitCallback;
771            final View nextFocusTarget;
772            if (view == mHourView) {
773                commitCallback = mCommitHour;
774                nextFocusTarget = view.isFocused() ? mMinuteView : null;
775            } else if (view == mMinuteView) {
776                commitCallback = mCommitMinute;
777                nextFocusTarget = null;
778            } else {
779                return;
780            }
781
782            view.removeCallbacks(commitCallback);
783
784            if (isValid) {
785                if (isFinished) {
786                    // Done with hours entry, make visual updates
787                    // immediately and move to next focus if needed.
788                    commitCallback.run();
789
790                    if (nextFocusTarget != null) {
791                        nextFocusTarget.requestFocus();
792                    }
793                } else {
794                    // May still be making changes. Postpone visual
795                    // updates to prevent distracting the user.
796                    view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS);
797                }
798            }
799        }
800    };
801
802    private final Runnable mCommitHour = new Runnable() {
803        @Override
804        public void run() {
805            setHour(mHourView.getValue());
806        }
807    };
808
809    private final Runnable mCommitMinute = new Runnable() {
810        @Override
811        public void run() {
812            setMinute(mMinuteView.getValue());
813        }
814    };
815
816    private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() {
817        @Override
818        public void onFocusChange(View v, boolean focused) {
819            if (focused) {
820                switch (v.getId()) {
821                    case R.id.am_label:
822                        setAmOrPm(AM);
823                        break;
824                    case R.id.pm_label:
825                        setAmOrPm(PM);
826                        break;
827                    case R.id.hours:
828                        setCurrentItemShowing(HOUR_INDEX, true, true);
829                        break;
830                    case R.id.minutes:
831                        setCurrentItemShowing(MINUTE_INDEX, true, true);
832                        break;
833                    default:
834                        // Failed to handle this click, don't vibrate.
835                        return;
836                }
837
838                tryVibrate();
839            }
840        }
841    };
842
843    private final View.OnClickListener mClickListener = new View.OnClickListener() {
844        @Override
845        public void onClick(View v) {
846
847            final int amOrPm;
848            switch (v.getId()) {
849                case R.id.am_label:
850                    setAmOrPm(AM);
851                    break;
852                case R.id.pm_label:
853                    setAmOrPm(PM);
854                    break;
855                case R.id.hours:
856                    setCurrentItemShowing(HOUR_INDEX, true, true);
857                    break;
858                case R.id.minutes:
859                    setCurrentItemShowing(MINUTE_INDEX, true, true);
860                    break;
861                default:
862                    // Failed to handle this click, don't vibrate.
863                    return;
864            }
865
866            tryVibrate();
867        }
868    };
869
870    /**
871     * Delegates unhandled touches in a view group to the nearest child view.
872     */
873    private static class NearestTouchDelegate implements View.OnTouchListener {
874            private View mInitialTouchTarget;
875
876            @Override
877            public boolean onTouch(View view, MotionEvent motionEvent) {
878                final int actionMasked = motionEvent.getActionMasked();
879                if (actionMasked == MotionEvent.ACTION_DOWN) {
880                    mInitialTouchTarget = findNearestChild((ViewGroup) view,
881                            (int) motionEvent.getX(), (int) motionEvent.getY());
882                }
883
884                final View child = mInitialTouchTarget;
885                if (child == null) {
886                    return false;
887                }
888
889                final float offsetX = view.getScrollX() - child.getLeft();
890                final float offsetY = view.getScrollY() - child.getTop();
891                motionEvent.offsetLocation(offsetX, offsetY);
892                final boolean handled = child.dispatchTouchEvent(motionEvent);
893                motionEvent.offsetLocation(-offsetX, -offsetY);
894
895                if (actionMasked == MotionEvent.ACTION_UP
896                        || actionMasked == MotionEvent.ACTION_CANCEL) {
897                    mInitialTouchTarget = null;
898                }
899
900                return handled;
901            }
902
903        private View findNearestChild(ViewGroup v, int x, int y) {
904            View bestChild = null;
905            int bestDist = Integer.MAX_VALUE;
906
907            for (int i = 0, count = v.getChildCount(); i < count; i++) {
908                final View child = v.getChildAt(i);
909                final int dX = x - (child.getLeft() + child.getWidth() / 2);
910                final int dY = y - (child.getTop() + child.getHeight() / 2);
911                final int dist = dX * dX + dY * dY;
912                if (bestDist > dist) {
913                    bestChild = child;
914                    bestDist = dist;
915                }
916            }
917
918            return bestChild;
919        }
920    }
921}
922