blob: 05fd4c8ab69f371452bd03818885742a98e9ee1a [file] [log] [blame]
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -07001/*
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
Alan Viverettef2525f62015-03-24 18:03:38 -070019import android.annotation.Nullable;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070020import android.content.Context;
Alan Viverettef2525f62015-03-24 18:03:38 -070021import android.content.res.ColorStateList;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070022import android.content.res.Resources;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070023import android.content.res.TypedArray;
24import android.os.Parcel;
25import android.os.Parcelable;
Alan Viverettef63757b2015-04-01 17:14:45 -070026import android.text.SpannableStringBuilder;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070027import android.text.format.DateFormat;
28import android.text.format.DateUtils;
Alan Viverettef63757b2015-04-01 17:14:45 -070029import android.text.style.TtsSpan;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070030import android.util.AttributeSet;
Alan Viverettef2525f62015-03-24 18:03:38 -070031import android.util.StateSet;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070032import android.view.HapticFeedbackConstants;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070033import android.view.LayoutInflater;
Alan Viveretteb3f24632015-10-22 16:01:48 -040034import android.view.MotionEvent;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070035import android.view.View;
Alan Viverette3fc00e312014-12-10 09:46:49 -080036import android.view.View.AccessibilityDelegate;
Alan Viveretteb3f24632015-10-22 16:01:48 -040037import android.view.View.MeasureSpec;
38import android.view.ViewGroup;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070039import android.view.accessibility.AccessibilityEvent;
40import android.view.accessibility.AccessibilityNodeInfo;
Alan Viverette3fc00e312014-12-10 09:46:49 -080041import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070042
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070043import com.android.internal.R;
Alan Viveretteb3f24632015-10-22 16:01:48 -040044import com.android.internal.widget.NumericTextView;
45import com.android.internal.widget.NumericTextView.OnValueChangedListener;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070046
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070047import java.util.Calendar;
48import java.util.Locale;
49
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070050/**
Alan Viverettedaf33ed2014-10-23 13:34:17 -070051 * A delegate implementing the radial clock-based TimePicker.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070052 */
Alan Viverettedaf33ed2014-10-23 13:34:17 -070053class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate implements
54 RadialTimePickerView.OnValueSelectedListener {
Alan Viveretteb3f24632015-10-22 16:01:48 -040055 /**
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;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070061
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
Alan Viverettef86bbd02015-09-16 14:19:21 -040069 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};
Alan Viverettef2525f62015-03-24 18:03:38 -070071
Deepanshu Gupta491523d2015-10-06 17:56:37 -070072 private static final int AM = 0;
73 private static final int PM = 1;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070074
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070075 private static final int HOURS_IN_HALF_DAY = 12;
76
Alan Viveretteb3f24632015-10-22 16:01:48 -040077 private final NumericTextView mHourView;
78 private final NumericTextView mMinuteView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070079 private final View mAmPmLayout;
Alan Viveretteb3f24632015-10-22 16:01:48 -040080 private final RadioButton mAmLabel;
81 private final RadioButton mPmLabel;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070082 private final RadialTimePickerView mRadialTimePickerView;
83 private final TextView mSeparatorView;
84
Alan Viverette68016a62015-11-19 17:10:54 -050085 private final Calendar mTempCalendar;
86
Alan Viverettef2525f62015-03-24 18:03:38 -070087 private boolean mIsEnabled = true;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070088 private boolean mAllowAutoAdvance;
89 private int mInitialHourOfDay;
90 private int mInitialMinute;
Alan Viverette4420ae82015-11-16 16:10:56 -050091 private boolean mIs24Hour;
Alan Viveretteadbc95f2015-02-20 10:51:33 -080092 private boolean mIsAmPmAtStart;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070093
Alan Viverettedaf33ed2014-10-23 13:34:17 -070094 // Accessibility strings.
Alan Viverettedaf33ed2014-10-23 13:34:17 -070095 private String mSelectHours;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070096 private String mSelectMinutes;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070097
Alan Viveretteb3f24632015-10-22 16:01:48 -040098 // Localization data.
99 private boolean mHourFormatShowLeadingZero;
100 private boolean mHourFormatStartsAtZero;
101
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700102 // Most recent time announcement values for accessibility.
103 private CharSequence mLastAnnouncedText;
104 private boolean mLastAnnouncedIsHour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700105
Chet Haase3053b2f2014-08-06 07:51:50 -0700106 public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs,
107 int defStyleAttr, int defStyleRes) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700108 super(delegator, context);
109
110 // process style attributes
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700111 final TypedArray a = mContext.obtainStyledAttributes(attrs,
112 R.styleable.TimePicker, defStyleAttr, defStyleRes);
113 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
114 Context.LAYOUT_INFLATER_SERVICE);
115 final Resources res = mContext.getResources();
116
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700117 mSelectHours = res.getString(R.string.select_hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700118 mSelectMinutes = res.getString(R.string.select_minutes);
119
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700120 final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout,
Alan Viverette62c79e92015-02-26 09:47:10 -0800121 R.layout.time_picker_material);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700122 final View mainView = inflater.inflate(layoutResourceId, delegator);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400123 final View headerView = mainView.findViewById(R.id.time_header);
124 headerView.setOnTouchListener(new NearestTouchDelegate());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700125
126 // Set up hour/minute labels.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400127 mHourView = (NumericTextView) mainView.findViewById(R.id.hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700128 mHourView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400129 mHourView.setOnFocusChangeListener(mFocusListener);
130 mHourView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800131 mHourView.setAccessibilityDelegate(
132 new ClickActionDelegate(context, R.string.select_hours));
Alan Viverette62c79e92015-02-26 09:47:10 -0800133 mSeparatorView = (TextView) mainView.findViewById(R.id.separator);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400134 mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700135 mMinuteView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400136 mMinuteView.setOnFocusChangeListener(mFocusListener);
137 mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800138 mMinuteView.setAccessibilityDelegate(
139 new ClickActionDelegate(context, R.string.select_minutes));
Alan Viveretteb3f24632015-10-22 16:01:48 -0400140 mMinuteView.setRange(0, 59);
Alan Viverettef63757b2015-04-01 17:14:45 -0700141
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700142 // Set up AM/PM labels.
Alan Viverette62c79e92015-02-26 09:47:10 -0800143 mAmPmLayout = mainView.findViewById(R.id.ampm_layout);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400144 mAmPmLayout.setOnTouchListener(new NearestTouchDelegate());
145
146 final String[] amPmStrings = TimePicker.getAmPmStrings(context);
147 mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700148 mAmLabel.setText(obtainVerbatim(amPmStrings[0]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700149 mAmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400150 ensureMinimumTextWidth(mAmLabel);
151
152 mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700153 mPmLabel.setText(obtainVerbatim(amPmStrings[1]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700154 mPmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400155 ensureMinimumTextWidth(mPmLabel);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700156
Alan Viverettef2525f62015-03-24 18:03:38 -0700157 // For the sake of backwards compatibility, attempt to extract the text
158 // color from the header time text appearance. If it's set, we'll let
159 // that override the "real" header text color.
160 ColorStateList headerTextColor = null;
161
162 @SuppressWarnings("deprecation")
163 final int timeHeaderTextAppearance = a.getResourceId(
164 R.styleable.TimePicker_headerTimeTextAppearance, 0);
165 if (timeHeaderTextAppearance != 0) {
166 final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
167 ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance);
168 final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
169 headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
170 textAppearance.recycle();
171 }
172
173 if (headerTextColor == null) {
174 headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor);
175 }
176
177 if (headerTextColor != null) {
178 mHourView.setTextColor(headerTextColor);
179 mSeparatorView.setTextColor(headerTextColor);
180 mMinuteView.setTextColor(headerTextColor);
181 mAmLabel.setTextColor(headerTextColor);
182 mPmLabel.setTextColor(headerTextColor);
183 }
184
185 // Set up header background, if available.
186 if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400187 headerView.setBackground(a.getDrawable(R.styleable.TimePicker_headerBackground));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700188 }
189
Alan Viverette51344782014-07-16 17:39:27 -0700190 a.recycle();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700191
Alan Viverette2b4dc112015-10-02 15:29:43 -0400192 mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker);
193 mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700194
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700195 setupListeners();
196
197 mAllowAutoAdvance = true;
198
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500199 updateHourFormat();
Alan Viveretteb3f24632015-10-22 16:01:48 -0400200
201 // Initialize with current time.
Alan Viverette4420ae82015-11-16 16:10:56 -0500202 mTempCalendar = Calendar.getInstance(mLocale);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400203 final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY);
204 final int currentMinute = mTempCalendar.get(Calendar.MINUTE);
Alan Viverette4420ae82015-11-16 16:10:56 -0500205 initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400206 }
207
208 /**
209 * Ensures that a TextView is wide enough to contain its text without
210 * wrapping or clipping. Measures the specified view and sets the minimum
211 * width to the view's desired width.
212 *
213 * @param v the text view to measure
214 */
215 private static void ensureMinimumTextWidth(TextView v) {
216 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
217
218 // Set both the TextView and the View version of minimum
219 // width because they are subtly different.
220 final int minWidth = v.getMeasuredWidth();
221 v.setMinWidth(minWidth);
222 v.setMinimumWidth(minWidth);
223 }
224
225 /**
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500226 * Updates hour formatting based on the current locale and 24-hour mode.
227 * <p>
228 * Determines how the hour should be formatted, sets member variables for
229 * leading zero and starting hour, and sets the hour view's presentation.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400230 */
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500231 private void updateHourFormat() {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400232 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500233 mLocale, mIs24Hour ? "Hm" : "hm");
Alan Viveretteb3f24632015-10-22 16:01:48 -0400234 final int lengthPattern = bestDateTimePattern.length();
235 boolean showLeadingZero = false;
236 char hourFormat = '\0';
237
238 for (int i = 0; i < lengthPattern; i++) {
239 final char c = bestDateTimePattern.charAt(i);
240 if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
241 hourFormat = c;
242 if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
243 showLeadingZero = true;
244 }
245 break;
246 }
247 }
248
249 mHourFormatShowLeadingZero = showLeadingZero;
250 mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H';
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500251
252 // Update hour text field.
253 final int minHour = mHourFormatStartsAtZero ? 0 : 1;
254 final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
255 mHourView.setRange(minHour, maxHour);
256 mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700257 }
258
Alan Viverettef63757b2015-04-01 17:14:45 -0700259 private static final CharSequence obtainVerbatim(String text) {
260 return new SpannableStringBuilder().append(text,
261 new TtsSpan.VerbatimBuilder(text).build(), 0);
262 }
263
Alan Viverettef2525f62015-03-24 18:03:38 -0700264 /**
265 * The legacy text color might have been poorly defined. Ensures that it
266 * has an appropriate activated state, using the selected state if one
267 * exists or modifying the default text color otherwise.
268 *
269 * @param color a legacy text color, or {@code null}
270 * @return a color state list with an appropriate activated state, or
271 * {@code null} if a valid activated state could not be generated
272 */
273 @Nullable
274 private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
275 if (color == null || color.hasState(R.attr.state_activated)) {
276 return color;
277 }
278
279 final int activatedColor;
280 final int defaultColor;
281 if (color.hasState(R.attr.state_selected)) {
282 activatedColor = color.getColorForState(StateSet.get(
283 StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
284 defaultColor = color.getColorForState(StateSet.get(
285 StateSet.VIEW_STATE_ENABLED), 0);
286 } else {
287 activatedColor = color.getDefaultColor();
288
289 // Generate a non-activated color using the disabled alpha.
290 final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
291 final float disabledAlpha = ta.getFloat(0, 0.30f);
292 defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
293 }
294
295 if (activatedColor == 0 || defaultColor == 0) {
296 // We somehow failed to obtain the colors.
297 return null;
298 }
299
300 final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
301 final int[] colors = new int[] { activatedColor, defaultColor };
302 return new ColorStateList(stateSet, colors);
303 }
304
305 private int multiplyAlphaComponent(int color, float alphaMod) {
306 final int srcRgb = color & 0xFFFFFF;
307 final int srcAlpha = (color >> 24) & 0xFF;
308 final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f);
309 return srcRgb | (dstAlpha << 24);
310 }
311
Alan Viverette3fc00e312014-12-10 09:46:49 -0800312 private static class ClickActionDelegate extends AccessibilityDelegate {
313 private final AccessibilityAction mClickAction;
314
315 public ClickActionDelegate(Context context, int resId) {
316 mClickAction = new AccessibilityAction(
317 AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId));
318 }
319
320 @Override
321 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
322 super.onInitializeAccessibilityNodeInfo(host, info);
323
324 info.addAction(mClickAction);
325 }
326 }
327
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700328 private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) {
329 mInitialHourOfDay = hourOfDay;
330 mInitialMinute = minute;
Alan Viverette4420ae82015-11-16 16:10:56 -0500331 mIs24Hour = is24HourView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700332 updateUI(index);
333 }
334
335 private void setupListeners() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700336 mRadialTimePickerView.setOnValueSelectedListener(this);
337 }
338
339 private void updateUI(int index) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700340 updateHeaderAmPm();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700341 updateHeaderHour(mInitialHourOfDay, false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700342 updateHeaderSeparator();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700343 updateHeaderMinute(mInitialMinute, false);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400344 updateRadialPicker(index);
345
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700346 mDelegator.invalidate();
347 }
348
349 private void updateRadialPicker(int index) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500350 mRadialTimePickerView.initialize(mInitialHourOfDay, mInitialMinute, mIs24Hour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700351 setCurrentItemShowing(index, false, true);
352 }
353
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700354 private void updateHeaderAmPm() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500355 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700356 mAmPmLayout.setVisibility(View.GONE);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700357 } else {
Alan Viveretted9f3fdf2014-11-12 09:31:22 -0800358 // Ensure that AM/PM layout is in the correct position.
Alan Viverette4420ae82015-11-16 16:10:56 -0500359 final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm");
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800360 final boolean isAmPmAtStart = dateTimePattern.startsWith("a");
361 setAmPmAtStart(isAmPmAtStart);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700362
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700363 updateAmPmLabelStates(mInitialHourOfDay < 12 ? AM : PM);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700364 }
365 }
366
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800367 private void setAmPmAtStart(boolean isAmPmAtStart) {
368 if (mIsAmPmAtStart != isAmPmAtStart) {
369 mIsAmPmAtStart = isAmPmAtStart;
370
371 final RelativeLayout.LayoutParams params =
372 (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
Alan Viverette62c79e92015-02-26 09:47:10 -0800373 if (params.getRule(RelativeLayout.RIGHT_OF) != 0 ||
374 params.getRule(RelativeLayout.LEFT_OF) != 0) {
375 if (isAmPmAtStart) {
376 params.removeRule(RelativeLayout.RIGHT_OF);
377 params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
378 } else {
379 params.removeRule(RelativeLayout.LEFT_OF);
380 params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
381 }
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800382 }
383
384 mAmPmLayout.setLayoutParams(params);
385 }
386 }
387
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700388 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700389 * Set the current hour.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700390 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700391 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500392 public void setHour(int hour) {
393 if (mInitialHourOfDay != hour) {
394 mInitialHourOfDay = hour;
395 updateHeaderHour(hour, true);
396 updateHeaderAmPm();
397 mRadialTimePickerView.setCurrentHour(hour);
398 mRadialTimePickerView.setAmOrPm(mInitialHourOfDay < 12 ? AM : PM);
399 mDelegator.invalidate();
400 onTimeChanged();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700401 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700402 }
403
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700404 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500405 * @return the current hour in the range (0-23)
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700406 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700407 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500408 public int getHour() {
409 final int currentHour = mRadialTimePickerView.getCurrentHour();
410 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700411 return currentHour;
Alan Viverette4420ae82015-11-16 16:10:56 -0500412 }
413
414 if (mRadialTimePickerView.getAmOrPm() == PM) {
415 return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700416 } else {
Alan Viverette4420ae82015-11-16 16:10:56 -0500417 return currentHour % HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700418 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700419 }
420
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700421 /**
422 * Set the current minute (0-59).
423 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700424 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500425 public void setMinute(int minute) {
426 if (mInitialMinute != minute) {
427 mInitialMinute = minute;
428 updateHeaderMinute(minute, true);
429 mRadialTimePickerView.setCurrentMinute(minute);
430 mDelegator.invalidate();
431 onTimeChanged();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700432 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700433 }
434
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700435 /**
436 * @return The current minute.
437 */
438 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500439 public int getMinute() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700440 return mRadialTimePickerView.getCurrentMinute();
441 }
442
443 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500444 * Sets whether time is displayed in 24-hour mode or 12-hour mode with
445 * AM/PM indicators.
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700446 *
Alan Viverette4420ae82015-11-16 16:10:56 -0500447 * @param is24Hour {@code true} to display time in 24-hour mode or
448 * {@code false} for 12-hour mode with AM/PM
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700449 */
Alan Viverette4420ae82015-11-16 16:10:56 -0500450 public void setIs24Hour(boolean is24Hour) {
451 if (mIs24Hour != is24Hour) {
452 mIs24Hour = is24Hour;
453 mInitialHourOfDay = getHour();
454
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500455 updateHourFormat();
Alan Viverette4420ae82015-11-16 16:10:56 -0500456 updateUI(mRadialTimePickerView.getCurrentItemShowing());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700457 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700458 }
459
460 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500461 * @return {@code true} if time is displayed in 24-hour mode, or
462 * {@code false} if time is displayed in 12-hour mode with AM/PM
463 * indicators
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700464 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700465 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500466 public boolean is24Hour() {
467 return mIs24Hour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700468 }
469
470 @Override
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700471 public void setOnTimeChangedListener(TimePicker.OnTimeChangedListener callback) {
472 mOnTimeChangedListener = callback;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700473 }
474
475 @Override
476 public void setEnabled(boolean enabled) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700477 mHourView.setEnabled(enabled);
478 mMinuteView.setEnabled(enabled);
479 mAmLabel.setEnabled(enabled);
480 mPmLabel.setEnabled(enabled);
481 mRadialTimePickerView.setEnabled(enabled);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700482 mIsEnabled = enabled;
483 }
484
485 @Override
486 public boolean isEnabled() {
487 return mIsEnabled;
488 }
489
490 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700491 public int getBaseline() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700492 // does not support baseline alignment
493 return -1;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700494 }
495
496 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700497 public Parcelable onSaveInstanceState(Parcelable superState) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500498 return new SavedState(superState, getHour(), getMinute(),
499 is24Hour(), getCurrentItemShowing());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700500 }
501
502 @Override
503 public void onRestoreInstanceState(Parcelable state) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400504 final SavedState ss = (SavedState) state;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700505 initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing());
506 mRadialTimePickerView.invalidate();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700507 }
508
509 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700510 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
511 onPopulateAccessibilityEvent(event);
512 return true;
513 }
514
515 @Override
516 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
517 int flags = DateUtils.FORMAT_SHOW_TIME;
Alan Viverette4420ae82015-11-16 16:10:56 -0500518 if (mIs24Hour) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700519 flags |= DateUtils.FORMAT_24HOUR;
520 } else {
521 flags |= DateUtils.FORMAT_12HOUR;
522 }
Alan Viverette4420ae82015-11-16 16:10:56 -0500523 mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour());
524 mTempCalendar.set(Calendar.MINUTE, getMinute());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700525 String selectedDate = DateUtils.formatDateTime(mContext,
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700526 mTempCalendar.getTimeInMillis(), flags);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700527 event.getText().add(selectedDate);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700528 }
529
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700530 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700531 * @return the index of the current item showing
532 */
533 private int getCurrentItemShowing() {
534 return mRadialTimePickerView.getCurrentItemShowing();
535 }
536
537 /**
538 * Propagate the time change
539 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700540 private void onTimeChanged() {
541 mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
542 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500543 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700544 }
545 }
546
547 /**
548 * Used to save / restore state of time picker
549 */
550 private static class SavedState extends View.BaseSavedState {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700551
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700552 private final int mHour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700553 private final int mMinute;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700554 private final boolean mIs24HourMode;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700555 private final int mCurrentItemShowing;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700556
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700557 private SavedState(Parcelable superState, int hour, int minute, boolean is24HourMode,
Alan Viveretteb3f24632015-10-22 16:01:48 -0400558 int currentItemShowing) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700559 super(superState);
560 mHour = hour;
561 mMinute = minute;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700562 mIs24HourMode = is24HourMode;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700563 mCurrentItemShowing = currentItemShowing;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700564 }
565
566 private SavedState(Parcel in) {
567 super(in);
568 mHour = in.readInt();
569 mMinute = in.readInt();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700570 mIs24HourMode = (in.readInt() == 1);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700571 mCurrentItemShowing = in.readInt();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700572 }
573
574 public int getHour() {
575 return mHour;
576 }
577
578 public int getMinute() {
579 return mMinute;
580 }
581
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700582 public boolean is24HourMode() {
583 return mIs24HourMode;
584 }
585
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700586 public int getCurrentItemShowing() {
587 return mCurrentItemShowing;
588 }
589
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700590 @Override
591 public void writeToParcel(Parcel dest, int flags) {
592 super.writeToParcel(dest, flags);
593 dest.writeInt(mHour);
594 dest.writeInt(mMinute);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700595 dest.writeInt(mIs24HourMode ? 1 : 0);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700596 dest.writeInt(mCurrentItemShowing);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700597 }
598
599 @SuppressWarnings({"unused", "hiding"})
Alan Viveretteb3f24632015-10-22 16:01:48 -0400600 public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700601 public SavedState createFromParcel(Parcel in) {
602 return new SavedState(in);
603 }
604
605 public SavedState[] newArray(int size) {
606 return new SavedState[size];
607 }
608 };
609 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700610
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700611 private void tryVibrate() {
612 mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700613 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700614
615 private void updateAmPmLabelStates(int amOrPm) {
616 final boolean isAm = amOrPm == AM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700617 mAmLabel.setActivated(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700618 mAmLabel.setChecked(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700619
620 final boolean isPm = amOrPm == PM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700621 mPmLabel.setActivated(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700622 mPmLabel.setChecked(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700623 }
624
625 /**
626 * Called by the picker for updating the header display.
627 */
628 @Override
629 public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) {
Alan Viverette73c30682014-11-07 15:39:24 -0800630 switch (pickerIndex) {
631 case HOUR_INDEX:
632 if (mAllowAutoAdvance && autoAdvance) {
633 updateHeaderHour(newValue, false);
634 setCurrentItemShowing(MINUTE_INDEX, true, false);
635 mDelegator.announceForAccessibility(newValue + ". " + mSelectMinutes);
636 } else {
637 updateHeaderHour(newValue, true);
638 }
639 break;
640 case MINUTE_INDEX:
641 updateHeaderMinute(newValue, true);
642 break;
643 case AMPM_INDEX:
644 updateAmPmLabelStates(newValue);
645 break;
Alan Viverette73c30682014-11-07 15:39:24 -0800646 }
647
648 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500649 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700650 }
651 }
652
Alan Viveretteb3f24632015-10-22 16:01:48 -0400653 /**
654 * Converts hour-of-day (0-23) time into a localized hour number.
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500655 * <p>
656 * The localized value may be in the range (0-23), (1-24), (0-11), or
657 * (1-12) depending on the locale. This method does not handle leading
658 * zeroes.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400659 *
660 * @param hourOfDay the hour-of-day (0-23)
661 * @return a localized hour number
662 */
663 private int getLocalizedHour(int hourOfDay) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500664 if (!mIs24Hour) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400665 // Convert to hour-of-am-pm.
666 hourOfDay %= 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700667 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400668
669 if (!mHourFormatStartsAtZero && hourOfDay == 0) {
670 // Convert to clock-hour (either of-day or of-am-pm).
Alan Viverette4420ae82015-11-16 16:10:56 -0500671 hourOfDay = mIs24Hour ? 24 : 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700672 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400673
674 return hourOfDay;
675 }
676
677 private void updateHeaderHour(int hourOfDay, boolean announce) {
678 final int localizedHour = getLocalizedHour(hourOfDay);
679 mHourView.setValue(localizedHour);
680
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700681 if (announce) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400682 tryAnnounceForAccessibility(mHourView.getText(), true);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700683 }
684 }
685
Alan Viveretteb3f24632015-10-22 16:01:48 -0400686 private void updateHeaderMinute(int minuteOfHour, boolean announce) {
687 mMinuteView.setValue(minuteOfHour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700688
Alan Viveretteb3f24632015-10-22 16:01:48 -0400689 if (announce) {
690 tryAnnounceForAccessibility(mMinuteView.getText(), false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700691 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700692 }
693
694 /**
695 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
696 *
697 * See http://unicode.org/cldr/trac/browser/trunk/common/main
698 *
699 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
700 * separator as the character which is just after the hour marker in the returned pattern.
701 */
702 private void updateHeaderSeparator() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500703 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
704 (mIs24Hour) ? "Hm" : "hm");
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700705 final String separatorText;
706 // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats
707 final char[] hourFormats = {'H', 'h', 'K', 'k'};
708 int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats);
709 if (hIndex == -1) {
710 // Default case
711 separatorText = ":";
712 } else {
713 separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1));
714 }
715 mSeparatorView.setText(separatorText);
716 }
717
718 static private int lastIndexOfAny(String str, char[] any) {
719 final int lengthAny = any.length;
720 if (lengthAny > 0) {
721 for (int i = str.length() - 1; i >= 0; i--) {
722 char c = str.charAt(i);
723 for (int j = 0; j < lengthAny; j++) {
724 if (c == any[j]) {
725 return i;
726 }
727 }
728 }
729 }
730 return -1;
731 }
732
Alan Viveretteb3f24632015-10-22 16:01:48 -0400733 private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) {
734 if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) {
735 // TODO: Find a better solution, potentially live regions?
736 mDelegator.announceForAccessibility(text);
737 mLastAnnouncedText = text;
738 mLastAnnouncedIsHour = isHour;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700739 }
740 }
741
742 /**
743 * Show either Hours or Minutes.
744 */
745 private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) {
746 mRadialTimePickerView.setCurrentItemShowing(index, animateCircle);
747
748 if (index == HOUR_INDEX) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700749 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700750 mDelegator.announceForAccessibility(mSelectHours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700751 }
752 } else {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700753 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700754 mDelegator.announceForAccessibility(mSelectMinutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700755 }
756 }
757
Alan Viverettef2525f62015-03-24 18:03:38 -0700758 mHourView.setActivated(index == HOUR_INDEX);
759 mMinuteView.setActivated(index == MINUTE_INDEX);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700760 }
761
762 private void setAmOrPm(int amOrPm) {
763 updateAmPmLabelStates(amOrPm);
764 mRadialTimePickerView.setAmOrPm(amOrPm);
765 }
766
Alan Viveretteb3f24632015-10-22 16:01:48 -0400767 private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() {
768 @Override
769 public void onValueChanged(NumericTextView view, int value,
770 boolean isValid, boolean isFinished) {
771 final Runnable commitCallback;
772 final View nextFocusTarget;
773 if (view == mHourView) {
774 commitCallback = mCommitHour;
775 nextFocusTarget = view.isFocused() ? mMinuteView : null;
776 } else if (view == mMinuteView) {
777 commitCallback = mCommitMinute;
778 nextFocusTarget = null;
779 } else {
780 return;
781 }
782
783 view.removeCallbacks(commitCallback);
784
785 if (isValid) {
786 if (isFinished) {
787 // Done with hours entry, make visual updates
788 // immediately and move to next focus if needed.
789 commitCallback.run();
790
791 if (nextFocusTarget != null) {
792 nextFocusTarget.requestFocus();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700793 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400794 } else {
795 // May still be making changes. Postpone visual
796 // updates to prevent distracting the user.
797 view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700798 }
799 }
800 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400801 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700802
Alan Viveretteb3f24632015-10-22 16:01:48 -0400803 private final Runnable mCommitHour = new Runnable() {
804 @Override
805 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500806 setHour(mHourView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400807 }
808 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700809
Alan Viveretteb3f24632015-10-22 16:01:48 -0400810 private final Runnable mCommitMinute = new Runnable() {
811 @Override
812 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500813 setMinute(mMinuteView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400814 }
815 };
816
817 private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() {
818 @Override
819 public void onFocusChange(View v, boolean focused) {
820 if (focused) {
821 switch (v.getId()) {
822 case R.id.am_label:
823 setAmOrPm(AM);
824 break;
825 case R.id.pm_label:
826 setAmOrPm(PM);
827 break;
828 case R.id.hours:
829 setCurrentItemShowing(HOUR_INDEX, true, true);
830 break;
831 case R.id.minutes:
832 setCurrentItemShowing(MINUTE_INDEX, true, true);
833 break;
834 default:
835 // Failed to handle this click, don't vibrate.
836 return;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700837 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400838
839 tryVibrate();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700840 }
841 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400842 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700843
844 private final View.OnClickListener mClickListener = new View.OnClickListener() {
845 @Override
846 public void onClick(View v) {
847
848 final int amOrPm;
849 switch (v.getId()) {
850 case R.id.am_label:
851 setAmOrPm(AM);
852 break;
853 case R.id.pm_label:
854 setAmOrPm(PM);
855 break;
856 case R.id.hours:
857 setCurrentItemShowing(HOUR_INDEX, true, true);
858 break;
859 case R.id.minutes:
860 setCurrentItemShowing(MINUTE_INDEX, true, true);
861 break;
862 default:
863 // Failed to handle this click, don't vibrate.
864 return;
865 }
866
867 tryVibrate();
868 }
869 };
870
Alan Viveretteb3f24632015-10-22 16:01:48 -0400871 /**
872 * Delegates unhandled touches in a view group to the nearest child view.
873 */
874 private static class NearestTouchDelegate implements View.OnTouchListener {
875 private View mInitialTouchTarget;
876
877 @Override
878 public boolean onTouch(View view, MotionEvent motionEvent) {
879 final int actionMasked = motionEvent.getActionMasked();
880 if (actionMasked == MotionEvent.ACTION_DOWN) {
Alan Viverette7add7e02015-11-20 14:19:39 -0500881 if (view instanceof ViewGroup) {
882 mInitialTouchTarget = findNearestChild((ViewGroup) view,
883 (int) motionEvent.getX(), (int) motionEvent.getY());
884 } else {
885 mInitialTouchTarget = null;
886 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400887 }
888
889 final View child = mInitialTouchTarget;
890 if (child == null) {
891 return false;
892 }
893
894 final float offsetX = view.getScrollX() - child.getLeft();
895 final float offsetY = view.getScrollY() - child.getTop();
896 motionEvent.offsetLocation(offsetX, offsetY);
897 final boolean handled = child.dispatchTouchEvent(motionEvent);
898 motionEvent.offsetLocation(-offsetX, -offsetY);
899
900 if (actionMasked == MotionEvent.ACTION_UP
901 || actionMasked == MotionEvent.ACTION_CANCEL) {
902 mInitialTouchTarget = null;
903 }
904
905 return handled;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700906 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700907
Alan Viveretteb3f24632015-10-22 16:01:48 -0400908 private View findNearestChild(ViewGroup v, int x, int y) {
909 View bestChild = null;
910 int bestDist = Integer.MAX_VALUE;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700911
Alan Viveretteb3f24632015-10-22 16:01:48 -0400912 for (int i = 0, count = v.getChildCount(); i < count; i++) {
913 final View child = v.getChildAt(i);
914 final int dX = x - (child.getLeft() + child.getWidth() / 2);
915 final int dY = y - (child.getTop() + child.getHeight() / 2);
916 final int dist = dX * dX + dY * dY;
917 if (bestDist > dist) {
918 bestChild = child;
919 bestDist = dist;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700920 }
921 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400922
923 return bestChild;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700924 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400925 }
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700926}