blob: b6200a14abca098b478614106be6e6fe51d46b6f [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;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070024import android.os.Parcelable;
Alan Viverettef63757b2015-04-01 17:14:45 -070025import android.text.SpannableStringBuilder;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070026import android.text.format.DateFormat;
27import android.text.format.DateUtils;
Alan Viverettef63757b2015-04-01 17:14:45 -070028import android.text.style.TtsSpan;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070029import android.util.AttributeSet;
Alan Viverettef2525f62015-03-24 18:03:38 -070030import android.util.StateSet;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070031import android.view.HapticFeedbackConstants;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070032import android.view.LayoutInflater;
Alan Viveretteb3f24632015-10-22 16:01:48 -040033import android.view.MotionEvent;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070034import android.view.View;
Alan Viverette3fc00e312014-12-10 09:46:49 -080035import android.view.View.AccessibilityDelegate;
Alan Viveretteb3f24632015-10-22 16:01:48 -040036import android.view.View.MeasureSpec;
37import android.view.ViewGroup;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070038import android.view.accessibility.AccessibilityEvent;
39import android.view.accessibility.AccessibilityNodeInfo;
Alan Viverette3fc00e312014-12-10 09:46:49 -080040import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070041
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070042import com.android.internal.R;
Alan Viveretteb3f24632015-10-22 16:01:48 -040043import com.android.internal.widget.NumericTextView;
44import com.android.internal.widget.NumericTextView.OnValueChangedListener;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070045
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070046import java.util.Calendar;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070047
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070048/**
Alan Viverettedaf33ed2014-10-23 13:34:17 -070049 * A delegate implementing the radial clock-based TimePicker.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070050 */
Alan Viverettedaf33ed2014-10-23 13:34:17 -070051class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate implements
52 RadialTimePickerView.OnValueSelectedListener {
Alan Viveretteb3f24632015-10-22 16:01:48 -040053 /**
54 * Delay in milliseconds before valid but potentially incomplete, for
55 * example "1" but not "12", keyboard edits are propagated from the
56 * hour / minute fields to the radial picker.
57 */
58 private static final long DELAY_COMMIT_MILLIS = 2000;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070059
60 // Index used by RadialPickerLayout
Alan Viveretteb0f54612016-04-12 14:58:09 -040061 private static final int HOUR_INDEX = RadialTimePickerView.HOURS;
62 private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070063
64 // NOT a real index for the purpose of what's showing.
65 private static final int AMPM_INDEX = 2;
66
Alan Viverettef86bbd02015-09-16 14:19:21 -040067 private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor};
68 private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha};
Alan Viverettef2525f62015-03-24 18:03:38 -070069
Deepanshu Gupta491523d2015-10-06 17:56:37 -070070 private static final int AM = 0;
71 private static final int PM = 1;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070072
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070073 private static final int HOURS_IN_HALF_DAY = 12;
74
Alan Viveretteb3f24632015-10-22 16:01:48 -040075 private final NumericTextView mHourView;
76 private final NumericTextView mMinuteView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070077 private final View mAmPmLayout;
Alan Viveretteb3f24632015-10-22 16:01:48 -040078 private final RadioButton mAmLabel;
79 private final RadioButton mPmLabel;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070080 private final RadialTimePickerView mRadialTimePickerView;
81 private final TextView mSeparatorView;
82
Alan Viverette68016a62015-11-19 17:10:54 -050083 private final Calendar mTempCalendar;
84
Alan Viveretteb0f54612016-04-12 14:58:09 -040085 // Accessibility strings.
86 private final String mSelectHours;
87 private final String mSelectMinutes;
88
Alan Viverettef2525f62015-03-24 18:03:38 -070089 private boolean mIsEnabled = true;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070090 private boolean mAllowAutoAdvance;
91 private int mInitialHourOfDay;
92 private int mInitialMinute;
Alan Viverette4420ae82015-11-16 16:10:56 -050093 private boolean mIs24Hour;
Alan Viveretteadbc95f2015-02-20 10:51:33 -080094 private boolean mIsAmPmAtStart;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070095
Alan Viveretteb3f24632015-10-22 16:01:48 -040096 // Localization data.
97 private boolean mHourFormatShowLeadingZero;
98 private boolean mHourFormatStartsAtZero;
99
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700100 // Most recent time announcement values for accessibility.
101 private CharSequence mLastAnnouncedText;
102 private boolean mLastAnnouncedIsHour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700103
Chet Haase3053b2f2014-08-06 07:51:50 -0700104 public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs,
105 int defStyleAttr, int defStyleRes) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700106 super(delegator, context);
107
108 // process style attributes
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700109 final TypedArray a = mContext.obtainStyledAttributes(attrs,
110 R.styleable.TimePicker, defStyleAttr, defStyleRes);
111 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
112 Context.LAYOUT_INFLATER_SERVICE);
113 final Resources res = mContext.getResources();
114
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700115 mSelectHours = res.getString(R.string.select_hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700116 mSelectMinutes = res.getString(R.string.select_minutes);
117
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700118 final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout,
Alan Viverette62c79e92015-02-26 09:47:10 -0800119 R.layout.time_picker_material);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700120 final View mainView = inflater.inflate(layoutResourceId, delegator);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400121 final View headerView = mainView.findViewById(R.id.time_header);
122 headerView.setOnTouchListener(new NearestTouchDelegate());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700123
124 // Set up hour/minute labels.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400125 mHourView = (NumericTextView) mainView.findViewById(R.id.hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700126 mHourView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400127 mHourView.setOnFocusChangeListener(mFocusListener);
128 mHourView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800129 mHourView.setAccessibilityDelegate(
130 new ClickActionDelegate(context, R.string.select_hours));
Alan Viverette62c79e92015-02-26 09:47:10 -0800131 mSeparatorView = (TextView) mainView.findViewById(R.id.separator);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400132 mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700133 mMinuteView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400134 mMinuteView.setOnFocusChangeListener(mFocusListener);
135 mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800136 mMinuteView.setAccessibilityDelegate(
137 new ClickActionDelegate(context, R.string.select_minutes));
Alan Viveretteb3f24632015-10-22 16:01:48 -0400138 mMinuteView.setRange(0, 59);
Alan Viverettef63757b2015-04-01 17:14:45 -0700139
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700140 // Set up AM/PM labels.
Alan Viverette62c79e92015-02-26 09:47:10 -0800141 mAmPmLayout = mainView.findViewById(R.id.ampm_layout);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400142 mAmPmLayout.setOnTouchListener(new NearestTouchDelegate());
143
144 final String[] amPmStrings = TimePicker.getAmPmStrings(context);
145 mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700146 mAmLabel.setText(obtainVerbatim(amPmStrings[0]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700147 mAmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400148 ensureMinimumTextWidth(mAmLabel);
149
150 mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700151 mPmLabel.setText(obtainVerbatim(amPmStrings[1]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700152 mPmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400153 ensureMinimumTextWidth(mPmLabel);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700154
Alan Viverettef2525f62015-03-24 18:03:38 -0700155 // For the sake of backwards compatibility, attempt to extract the text
156 // color from the header time text appearance. If it's set, we'll let
157 // that override the "real" header text color.
158 ColorStateList headerTextColor = null;
159
160 @SuppressWarnings("deprecation")
161 final int timeHeaderTextAppearance = a.getResourceId(
162 R.styleable.TimePicker_headerTimeTextAppearance, 0);
163 if (timeHeaderTextAppearance != 0) {
164 final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
165 ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance);
166 final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
167 headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
168 textAppearance.recycle();
169 }
170
171 if (headerTextColor == null) {
172 headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor);
173 }
174
175 if (headerTextColor != null) {
176 mHourView.setTextColor(headerTextColor);
177 mSeparatorView.setTextColor(headerTextColor);
178 mMinuteView.setTextColor(headerTextColor);
179 mAmLabel.setTextColor(headerTextColor);
180 mPmLabel.setTextColor(headerTextColor);
181 }
182
183 // Set up header background, if available.
184 if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400185 headerView.setBackground(a.getDrawable(R.styleable.TimePicker_headerBackground));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700186 }
187
Alan Viverette51344782014-07-16 17:39:27 -0700188 a.recycle();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700189
Alan Viverette2b4dc112015-10-02 15:29:43 -0400190 mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker);
191 mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700192
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700193 setupListeners();
194
195 mAllowAutoAdvance = true;
196
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500197 updateHourFormat();
Alan Viveretteb3f24632015-10-22 16:01:48 -0400198
199 // Initialize with current time.
Alan Viverette4420ae82015-11-16 16:10:56 -0500200 mTempCalendar = Calendar.getInstance(mLocale);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400201 final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY);
202 final int currentMinute = mTempCalendar.get(Calendar.MINUTE);
Alan Viverette4420ae82015-11-16 16:10:56 -0500203 initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400204 }
205
206 /**
207 * Ensures that a TextView is wide enough to contain its text without
208 * wrapping or clipping. Measures the specified view and sets the minimum
209 * width to the view's desired width.
210 *
211 * @param v the text view to measure
212 */
213 private static void ensureMinimumTextWidth(TextView v) {
214 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
215
216 // Set both the TextView and the View version of minimum
217 // width because they are subtly different.
218 final int minWidth = v.getMeasuredWidth();
219 v.setMinWidth(minWidth);
220 v.setMinimumWidth(minWidth);
221 }
222
223 /**
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500224 * Updates hour formatting based on the current locale and 24-hour mode.
225 * <p>
226 * Determines how the hour should be formatted, sets member variables for
227 * leading zero and starting hour, and sets the hour view's presentation.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400228 */
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500229 private void updateHourFormat() {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400230 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500231 mLocale, mIs24Hour ? "Hm" : "hm");
Alan Viveretteb3f24632015-10-22 16:01:48 -0400232 final int lengthPattern = bestDateTimePattern.length();
233 boolean showLeadingZero = false;
234 char hourFormat = '\0';
235
236 for (int i = 0; i < lengthPattern; i++) {
237 final char c = bestDateTimePattern.charAt(i);
238 if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
239 hourFormat = c;
240 if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
241 showLeadingZero = true;
242 }
243 break;
244 }
245 }
246
247 mHourFormatShowLeadingZero = showLeadingZero;
248 mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H';
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500249
250 // Update hour text field.
251 final int minHour = mHourFormatStartsAtZero ? 0 : 1;
252 final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
253 mHourView.setRange(minHour, maxHour);
254 mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700255 }
256
Alan Viverettef63757b2015-04-01 17:14:45 -0700257 private static final CharSequence obtainVerbatim(String text) {
258 return new SpannableStringBuilder().append(text,
259 new TtsSpan.VerbatimBuilder(text).build(), 0);
260 }
261
Alan Viverettef2525f62015-03-24 18:03:38 -0700262 /**
263 * The legacy text color might have been poorly defined. Ensures that it
264 * has an appropriate activated state, using the selected state if one
265 * exists or modifying the default text color otherwise.
266 *
267 * @param color a legacy text color, or {@code null}
268 * @return a color state list with an appropriate activated state, or
269 * {@code null} if a valid activated state could not be generated
270 */
271 @Nullable
272 private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
273 if (color == null || color.hasState(R.attr.state_activated)) {
274 return color;
275 }
276
277 final int activatedColor;
278 final int defaultColor;
279 if (color.hasState(R.attr.state_selected)) {
280 activatedColor = color.getColorForState(StateSet.get(
281 StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
282 defaultColor = color.getColorForState(StateSet.get(
283 StateSet.VIEW_STATE_ENABLED), 0);
284 } else {
285 activatedColor = color.getDefaultColor();
286
287 // Generate a non-activated color using the disabled alpha.
288 final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
289 final float disabledAlpha = ta.getFloat(0, 0.30f);
290 defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
291 }
292
293 if (activatedColor == 0 || defaultColor == 0) {
294 // We somehow failed to obtain the colors.
295 return null;
296 }
297
298 final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
299 final int[] colors = new int[] { activatedColor, defaultColor };
300 return new ColorStateList(stateSet, colors);
301 }
302
303 private int multiplyAlphaComponent(int color, float alphaMod) {
304 final int srcRgb = color & 0xFFFFFF;
305 final int srcAlpha = (color >> 24) & 0xFF;
306 final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f);
307 return srcRgb | (dstAlpha << 24);
308 }
309
Alan Viverette3fc00e312014-12-10 09:46:49 -0800310 private static class ClickActionDelegate extends AccessibilityDelegate {
311 private final AccessibilityAction mClickAction;
312
313 public ClickActionDelegate(Context context, int resId) {
314 mClickAction = new AccessibilityAction(
315 AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId));
316 }
317
318 @Override
319 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
320 super.onInitializeAccessibilityNodeInfo(host, info);
321
322 info.addAction(mClickAction);
323 }
324 }
325
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700326 private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) {
327 mInitialHourOfDay = hourOfDay;
328 mInitialMinute = minute;
Alan Viverette4420ae82015-11-16 16:10:56 -0500329 mIs24Hour = is24HourView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700330 updateUI(index);
331 }
332
333 private void setupListeners() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700334 mRadialTimePickerView.setOnValueSelectedListener(this);
335 }
336
337 private void updateUI(int index) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700338 updateHeaderAmPm();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700339 updateHeaderHour(mInitialHourOfDay, false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700340 updateHeaderSeparator();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700341 updateHeaderMinute(mInitialMinute, false);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400342 updateRadialPicker(index);
343
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700344 mDelegator.invalidate();
345 }
346
347 private void updateRadialPicker(int index) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500348 mRadialTimePickerView.initialize(mInitialHourOfDay, mInitialMinute, mIs24Hour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700349 setCurrentItemShowing(index, false, true);
350 }
351
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700352 private void updateHeaderAmPm() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500353 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700354 mAmPmLayout.setVisibility(View.GONE);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700355 } else {
Alan Viveretted9f3fdf2014-11-12 09:31:22 -0800356 // Ensure that AM/PM layout is in the correct position.
Alan Viverette4420ae82015-11-16 16:10:56 -0500357 final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm");
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800358 final boolean isAmPmAtStart = dateTimePattern.startsWith("a");
359 setAmPmAtStart(isAmPmAtStart);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700360
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700361 updateAmPmLabelStates(mInitialHourOfDay < 12 ? AM : PM);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700362 }
363 }
364
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800365 private void setAmPmAtStart(boolean isAmPmAtStart) {
366 if (mIsAmPmAtStart != isAmPmAtStart) {
367 mIsAmPmAtStart = isAmPmAtStart;
368
369 final RelativeLayout.LayoutParams params =
370 (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
Alan Viverette62c79e92015-02-26 09:47:10 -0800371 if (params.getRule(RelativeLayout.RIGHT_OF) != 0 ||
372 params.getRule(RelativeLayout.LEFT_OF) != 0) {
373 if (isAmPmAtStart) {
374 params.removeRule(RelativeLayout.RIGHT_OF);
375 params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
376 } else {
377 params.removeRule(RelativeLayout.LEFT_OF);
378 params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
379 }
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800380 }
381
382 mAmPmLayout.setLayoutParams(params);
383 }
384 }
385
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700386 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700387 * Set the current hour.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700388 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700389 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500390 public void setHour(int hour) {
391 if (mInitialHourOfDay != hour) {
392 mInitialHourOfDay = hour;
393 updateHeaderHour(hour, true);
394 updateHeaderAmPm();
395 mRadialTimePickerView.setCurrentHour(hour);
396 mRadialTimePickerView.setAmOrPm(mInitialHourOfDay < 12 ? AM : PM);
397 mDelegator.invalidate();
398 onTimeChanged();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700399 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700400 }
401
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700402 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500403 * @return the current hour in the range (0-23)
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700404 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700405 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500406 public int getHour() {
407 final int currentHour = mRadialTimePickerView.getCurrentHour();
408 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700409 return currentHour;
Alan Viverette4420ae82015-11-16 16:10:56 -0500410 }
411
412 if (mRadialTimePickerView.getAmOrPm() == PM) {
413 return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700414 } else {
Alan Viverette4420ae82015-11-16 16:10:56 -0500415 return currentHour % HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700416 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700417 }
418
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700419 /**
420 * Set the current minute (0-59).
421 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700422 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500423 public void setMinute(int minute) {
424 if (mInitialMinute != minute) {
425 mInitialMinute = minute;
426 updateHeaderMinute(minute, true);
427 mRadialTimePickerView.setCurrentMinute(minute);
428 mDelegator.invalidate();
429 onTimeChanged();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700430 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700431 }
432
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700433 /**
434 * @return The current minute.
435 */
436 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500437 public int getMinute() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700438 return mRadialTimePickerView.getCurrentMinute();
439 }
440
441 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500442 * Sets whether time is displayed in 24-hour mode or 12-hour mode with
443 * AM/PM indicators.
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700444 *
Alan Viverette4420ae82015-11-16 16:10:56 -0500445 * @param is24Hour {@code true} to display time in 24-hour mode or
446 * {@code false} for 12-hour mode with AM/PM
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700447 */
Alan Viverette4420ae82015-11-16 16:10:56 -0500448 public void setIs24Hour(boolean is24Hour) {
449 if (mIs24Hour != is24Hour) {
450 mIs24Hour = is24Hour;
451 mInitialHourOfDay = getHour();
452
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500453 updateHourFormat();
Alan Viverette4420ae82015-11-16 16:10:56 -0500454 updateUI(mRadialTimePickerView.getCurrentItemShowing());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700455 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700456 }
457
458 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500459 * @return {@code true} if time is displayed in 24-hour mode, or
460 * {@code false} if time is displayed in 12-hour mode with AM/PM
461 * indicators
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700462 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700463 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500464 public boolean is24Hour() {
465 return mIs24Hour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700466 }
467
468 @Override
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700469 public void setOnTimeChangedListener(TimePicker.OnTimeChangedListener callback) {
470 mOnTimeChangedListener = callback;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700471 }
472
473 @Override
474 public void setEnabled(boolean enabled) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700475 mHourView.setEnabled(enabled);
476 mMinuteView.setEnabled(enabled);
477 mAmLabel.setEnabled(enabled);
478 mPmLabel.setEnabled(enabled);
479 mRadialTimePickerView.setEnabled(enabled);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700480 mIsEnabled = enabled;
481 }
482
483 @Override
484 public boolean isEnabled() {
485 return mIsEnabled;
486 }
487
488 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700489 public int getBaseline() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700490 // does not support baseline alignment
491 return -1;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700492 }
493
494 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700495 public Parcelable onSaveInstanceState(Parcelable superState) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500496 return new SavedState(superState, getHour(), getMinute(),
497 is24Hour(), getCurrentItemShowing());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700498 }
499
500 @Override
501 public void onRestoreInstanceState(Parcelable state) {
Alan Viverette6b3f85f2016-03-01 16:48:04 -0500502 if (state instanceof SavedState) {
503 final SavedState ss = (SavedState) state;
504 initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing());
505 mRadialTimePickerView.invalidate();
506 }
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 Viveretteb0f54612016-04-12 14:58:09 -0400523
Alan Viverette4420ae82015-11-16 16:10:56 -0500524 mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour());
525 mTempCalendar.set(Calendar.MINUTE, getMinute());
Alan Viveretteb0f54612016-04-12 14:58:09 -0400526
527 final String selectedTime = DateUtils.formatDateTime(mContext,
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700528 mTempCalendar.getTimeInMillis(), flags);
Alan Viveretteb0f54612016-04-12 14:58:09 -0400529 final String selectionMode = mRadialTimePickerView.getCurrentItemShowing() == HOUR_INDEX ?
530 mSelectHours : mSelectMinutes;
531 event.getText().add(selectedTime + " " + selectionMode);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700532 }
533
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700534 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700535 * @return the index of the current item showing
536 */
537 private int getCurrentItemShowing() {
538 return mRadialTimePickerView.getCurrentItemShowing();
539 }
540
541 /**
542 * Propagate the time change
543 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700544 private void onTimeChanged() {
545 mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
546 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500547 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700548 }
549 }
550
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700551 private void tryVibrate() {
552 mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700553 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700554
555 private void updateAmPmLabelStates(int amOrPm) {
556 final boolean isAm = amOrPm == AM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700557 mAmLabel.setActivated(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700558 mAmLabel.setChecked(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700559
560 final boolean isPm = amOrPm == PM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700561 mPmLabel.setActivated(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700562 mPmLabel.setChecked(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700563 }
564
565 /**
566 * Called by the picker for updating the header display.
567 */
568 @Override
569 public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) {
Alan Viverette73c30682014-11-07 15:39:24 -0800570 switch (pickerIndex) {
571 case HOUR_INDEX:
572 if (mAllowAutoAdvance && autoAdvance) {
573 updateHeaderHour(newValue, false);
574 setCurrentItemShowing(MINUTE_INDEX, true, false);
575 mDelegator.announceForAccessibility(newValue + ". " + mSelectMinutes);
576 } else {
577 updateHeaderHour(newValue, true);
578 }
579 break;
580 case MINUTE_INDEX:
581 updateHeaderMinute(newValue, true);
582 break;
583 case AMPM_INDEX:
584 updateAmPmLabelStates(newValue);
585 break;
Alan Viverette73c30682014-11-07 15:39:24 -0800586 }
587
588 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500589 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700590 }
591 }
592
Alan Viveretteb3f24632015-10-22 16:01:48 -0400593 /**
594 * Converts hour-of-day (0-23) time into a localized hour number.
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500595 * <p>
596 * The localized value may be in the range (0-23), (1-24), (0-11), or
597 * (1-12) depending on the locale. This method does not handle leading
598 * zeroes.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400599 *
600 * @param hourOfDay the hour-of-day (0-23)
601 * @return a localized hour number
602 */
603 private int getLocalizedHour(int hourOfDay) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500604 if (!mIs24Hour) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400605 // Convert to hour-of-am-pm.
606 hourOfDay %= 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700607 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400608
609 if (!mHourFormatStartsAtZero && hourOfDay == 0) {
610 // Convert to clock-hour (either of-day or of-am-pm).
Alan Viverette4420ae82015-11-16 16:10:56 -0500611 hourOfDay = mIs24Hour ? 24 : 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700612 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400613
614 return hourOfDay;
615 }
616
617 private void updateHeaderHour(int hourOfDay, boolean announce) {
618 final int localizedHour = getLocalizedHour(hourOfDay);
619 mHourView.setValue(localizedHour);
620
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700621 if (announce) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400622 tryAnnounceForAccessibility(mHourView.getText(), true);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700623 }
624 }
625
Alan Viveretteb3f24632015-10-22 16:01:48 -0400626 private void updateHeaderMinute(int minuteOfHour, boolean announce) {
627 mMinuteView.setValue(minuteOfHour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700628
Alan Viveretteb3f24632015-10-22 16:01:48 -0400629 if (announce) {
630 tryAnnounceForAccessibility(mMinuteView.getText(), false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700631 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700632 }
633
634 /**
635 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
636 *
637 * See http://unicode.org/cldr/trac/browser/trunk/common/main
638 *
639 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
640 * separator as the character which is just after the hour marker in the returned pattern.
641 */
642 private void updateHeaderSeparator() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500643 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
644 (mIs24Hour) ? "Hm" : "hm");
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700645 final String separatorText;
646 // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats
647 final char[] hourFormats = {'H', 'h', 'K', 'k'};
648 int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats);
649 if (hIndex == -1) {
650 // Default case
651 separatorText = ":";
652 } else {
653 separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1));
654 }
655 mSeparatorView.setText(separatorText);
656 }
657
658 static private int lastIndexOfAny(String str, char[] any) {
659 final int lengthAny = any.length;
660 if (lengthAny > 0) {
661 for (int i = str.length() - 1; i >= 0; i--) {
662 char c = str.charAt(i);
663 for (int j = 0; j < lengthAny; j++) {
664 if (c == any[j]) {
665 return i;
666 }
667 }
668 }
669 }
670 return -1;
671 }
672
Alan Viveretteb3f24632015-10-22 16:01:48 -0400673 private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) {
674 if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) {
675 // TODO: Find a better solution, potentially live regions?
676 mDelegator.announceForAccessibility(text);
677 mLastAnnouncedText = text;
678 mLastAnnouncedIsHour = isHour;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700679 }
680 }
681
682 /**
683 * Show either Hours or Minutes.
684 */
685 private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) {
686 mRadialTimePickerView.setCurrentItemShowing(index, animateCircle);
687
688 if (index == HOUR_INDEX) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700689 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700690 mDelegator.announceForAccessibility(mSelectHours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700691 }
692 } else {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700693 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700694 mDelegator.announceForAccessibility(mSelectMinutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700695 }
696 }
697
Alan Viverettef2525f62015-03-24 18:03:38 -0700698 mHourView.setActivated(index == HOUR_INDEX);
699 mMinuteView.setActivated(index == MINUTE_INDEX);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700700 }
701
702 private void setAmOrPm(int amOrPm) {
703 updateAmPmLabelStates(amOrPm);
Alan Viverette30b57b62016-04-19 09:29:20 -0400704
705 if (mRadialTimePickerView.setAmOrPm(amOrPm) && mOnTimeChangedListener != null) {
706 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
707 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700708 }
709
Alan Viveretteb3f24632015-10-22 16:01:48 -0400710 private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() {
711 @Override
712 public void onValueChanged(NumericTextView view, int value,
713 boolean isValid, boolean isFinished) {
714 final Runnable commitCallback;
715 final View nextFocusTarget;
716 if (view == mHourView) {
717 commitCallback = mCommitHour;
718 nextFocusTarget = view.isFocused() ? mMinuteView : null;
719 } else if (view == mMinuteView) {
720 commitCallback = mCommitMinute;
721 nextFocusTarget = null;
722 } else {
723 return;
724 }
725
726 view.removeCallbacks(commitCallback);
727
728 if (isValid) {
729 if (isFinished) {
730 // Done with hours entry, make visual updates
731 // immediately and move to next focus if needed.
732 commitCallback.run();
733
734 if (nextFocusTarget != null) {
735 nextFocusTarget.requestFocus();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700736 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400737 } else {
738 // May still be making changes. Postpone visual
739 // updates to prevent distracting the user.
740 view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700741 }
742 }
743 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400744 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700745
Alan Viveretteb3f24632015-10-22 16:01:48 -0400746 private final Runnable mCommitHour = new Runnable() {
747 @Override
748 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500749 setHour(mHourView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400750 }
751 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700752
Alan Viveretteb3f24632015-10-22 16:01:48 -0400753 private final Runnable mCommitMinute = new Runnable() {
754 @Override
755 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500756 setMinute(mMinuteView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400757 }
758 };
759
760 private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() {
761 @Override
762 public void onFocusChange(View v, boolean focused) {
763 if (focused) {
764 switch (v.getId()) {
765 case R.id.am_label:
766 setAmOrPm(AM);
767 break;
768 case R.id.pm_label:
769 setAmOrPm(PM);
770 break;
771 case R.id.hours:
772 setCurrentItemShowing(HOUR_INDEX, true, true);
773 break;
774 case R.id.minutes:
775 setCurrentItemShowing(MINUTE_INDEX, true, true);
776 break;
777 default:
778 // Failed to handle this click, don't vibrate.
779 return;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700780 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400781
782 tryVibrate();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700783 }
784 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400785 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700786
787 private final View.OnClickListener mClickListener = new View.OnClickListener() {
788 @Override
789 public void onClick(View v) {
790
791 final int amOrPm;
792 switch (v.getId()) {
793 case R.id.am_label:
794 setAmOrPm(AM);
795 break;
796 case R.id.pm_label:
797 setAmOrPm(PM);
798 break;
799 case R.id.hours:
800 setCurrentItemShowing(HOUR_INDEX, true, true);
801 break;
802 case R.id.minutes:
803 setCurrentItemShowing(MINUTE_INDEX, true, true);
804 break;
805 default:
806 // Failed to handle this click, don't vibrate.
807 return;
808 }
809
810 tryVibrate();
811 }
812 };
813
Alan Viveretteb3f24632015-10-22 16:01:48 -0400814 /**
815 * Delegates unhandled touches in a view group to the nearest child view.
816 */
817 private static class NearestTouchDelegate implements View.OnTouchListener {
818 private View mInitialTouchTarget;
819
820 @Override
821 public boolean onTouch(View view, MotionEvent motionEvent) {
822 final int actionMasked = motionEvent.getActionMasked();
823 if (actionMasked == MotionEvent.ACTION_DOWN) {
Alan Viverette7add7e02015-11-20 14:19:39 -0500824 if (view instanceof ViewGroup) {
825 mInitialTouchTarget = findNearestChild((ViewGroup) view,
826 (int) motionEvent.getX(), (int) motionEvent.getY());
827 } else {
828 mInitialTouchTarget = null;
829 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400830 }
831
832 final View child = mInitialTouchTarget;
833 if (child == null) {
834 return false;
835 }
836
837 final float offsetX = view.getScrollX() - child.getLeft();
838 final float offsetY = view.getScrollY() - child.getTop();
839 motionEvent.offsetLocation(offsetX, offsetY);
840 final boolean handled = child.dispatchTouchEvent(motionEvent);
841 motionEvent.offsetLocation(-offsetX, -offsetY);
842
843 if (actionMasked == MotionEvent.ACTION_UP
844 || actionMasked == MotionEvent.ACTION_CANCEL) {
845 mInitialTouchTarget = null;
846 }
847
848 return handled;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700849 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700850
Alan Viveretteb3f24632015-10-22 16:01:48 -0400851 private View findNearestChild(ViewGroup v, int x, int y) {
852 View bestChild = null;
853 int bestDist = Integer.MAX_VALUE;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700854
Alan Viveretteb3f24632015-10-22 16:01:48 -0400855 for (int i = 0, count = v.getChildCount(); i < count; i++) {
856 final View child = v.getChildAt(i);
857 final int dX = x - (child.getLeft() + child.getWidth() / 2);
858 final int dY = y - (child.getTop() + child.getHeight() / 2);
859 final int dist = dX * dX + dY * dY;
860 if (bestDist > dist) {
861 bestChild = child;
862 bestDist = dist;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700863 }
864 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400865
866 return bestChild;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700867 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400868 }
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700869}