blob: 4a24e26a282873d6e69fd83e8e6dafa3640ee0ae [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
61 private static final int HOUR_INDEX = 0;
62 private static final int MINUTE_INDEX = 1;
63
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 Viverettef2525f62015-03-24 18:03:38 -070085 private boolean mIsEnabled = true;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070086 private boolean mAllowAutoAdvance;
87 private int mInitialHourOfDay;
88 private int mInitialMinute;
Alan Viverette4420ae82015-11-16 16:10:56 -050089 private boolean mIs24Hour;
Alan Viveretteadbc95f2015-02-20 10:51:33 -080090 private boolean mIsAmPmAtStart;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070091
Alan Viverettedaf33ed2014-10-23 13:34:17 -070092 // Accessibility strings.
Alan Viverettedaf33ed2014-10-23 13:34:17 -070093 private String mSelectHours;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070094 private String mSelectMinutes;
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 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
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700547 private void tryVibrate() {
548 mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700549 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700550
551 private void updateAmPmLabelStates(int amOrPm) {
552 final boolean isAm = amOrPm == AM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700553 mAmLabel.setActivated(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700554 mAmLabel.setChecked(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700555
556 final boolean isPm = amOrPm == PM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700557 mPmLabel.setActivated(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700558 mPmLabel.setChecked(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700559 }
560
561 /**
562 * Called by the picker for updating the header display.
563 */
564 @Override
565 public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) {
Alan Viverette73c30682014-11-07 15:39:24 -0800566 switch (pickerIndex) {
567 case HOUR_INDEX:
568 if (mAllowAutoAdvance && autoAdvance) {
569 updateHeaderHour(newValue, false);
570 setCurrentItemShowing(MINUTE_INDEX, true, false);
571 mDelegator.announceForAccessibility(newValue + ". " + mSelectMinutes);
572 } else {
573 updateHeaderHour(newValue, true);
574 }
575 break;
576 case MINUTE_INDEX:
577 updateHeaderMinute(newValue, true);
578 break;
579 case AMPM_INDEX:
580 updateAmPmLabelStates(newValue);
581 break;
Alan Viverette73c30682014-11-07 15:39:24 -0800582 }
583
584 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500585 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700586 }
587 }
588
Alan Viveretteb3f24632015-10-22 16:01:48 -0400589 /**
590 * Converts hour-of-day (0-23) time into a localized hour number.
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500591 * <p>
592 * The localized value may be in the range (0-23), (1-24), (0-11), or
593 * (1-12) depending on the locale. This method does not handle leading
594 * zeroes.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400595 *
596 * @param hourOfDay the hour-of-day (0-23)
597 * @return a localized hour number
598 */
599 private int getLocalizedHour(int hourOfDay) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500600 if (!mIs24Hour) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400601 // Convert to hour-of-am-pm.
602 hourOfDay %= 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700603 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400604
605 if (!mHourFormatStartsAtZero && hourOfDay == 0) {
606 // Convert to clock-hour (either of-day or of-am-pm).
Alan Viverette4420ae82015-11-16 16:10:56 -0500607 hourOfDay = mIs24Hour ? 24 : 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700608 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400609
610 return hourOfDay;
611 }
612
613 private void updateHeaderHour(int hourOfDay, boolean announce) {
614 final int localizedHour = getLocalizedHour(hourOfDay);
615 mHourView.setValue(localizedHour);
616
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700617 if (announce) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400618 tryAnnounceForAccessibility(mHourView.getText(), true);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700619 }
620 }
621
Alan Viveretteb3f24632015-10-22 16:01:48 -0400622 private void updateHeaderMinute(int minuteOfHour, boolean announce) {
623 mMinuteView.setValue(minuteOfHour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700624
Alan Viveretteb3f24632015-10-22 16:01:48 -0400625 if (announce) {
626 tryAnnounceForAccessibility(mMinuteView.getText(), false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700627 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700628 }
629
630 /**
631 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
632 *
633 * See http://unicode.org/cldr/trac/browser/trunk/common/main
634 *
635 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
636 * separator as the character which is just after the hour marker in the returned pattern.
637 */
638 private void updateHeaderSeparator() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500639 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
640 (mIs24Hour) ? "Hm" : "hm");
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700641 final String separatorText;
642 // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats
643 final char[] hourFormats = {'H', 'h', 'K', 'k'};
644 int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats);
645 if (hIndex == -1) {
646 // Default case
647 separatorText = ":";
648 } else {
649 separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1));
650 }
651 mSeparatorView.setText(separatorText);
652 }
653
654 static private int lastIndexOfAny(String str, char[] any) {
655 final int lengthAny = any.length;
656 if (lengthAny > 0) {
657 for (int i = str.length() - 1; i >= 0; i--) {
658 char c = str.charAt(i);
659 for (int j = 0; j < lengthAny; j++) {
660 if (c == any[j]) {
661 return i;
662 }
663 }
664 }
665 }
666 return -1;
667 }
668
Alan Viveretteb3f24632015-10-22 16:01:48 -0400669 private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) {
670 if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) {
671 // TODO: Find a better solution, potentially live regions?
672 mDelegator.announceForAccessibility(text);
673 mLastAnnouncedText = text;
674 mLastAnnouncedIsHour = isHour;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700675 }
676 }
677
678 /**
679 * Show either Hours or Minutes.
680 */
681 private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) {
682 mRadialTimePickerView.setCurrentItemShowing(index, animateCircle);
683
684 if (index == HOUR_INDEX) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700685 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700686 mDelegator.announceForAccessibility(mSelectHours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700687 }
688 } else {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700689 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700690 mDelegator.announceForAccessibility(mSelectMinutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700691 }
692 }
693
Alan Viverettef2525f62015-03-24 18:03:38 -0700694 mHourView.setActivated(index == HOUR_INDEX);
695 mMinuteView.setActivated(index == MINUTE_INDEX);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700696 }
697
698 private void setAmOrPm(int amOrPm) {
699 updateAmPmLabelStates(amOrPm);
700 mRadialTimePickerView.setAmOrPm(amOrPm);
701 }
702
Alan Viveretteb3f24632015-10-22 16:01:48 -0400703 private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() {
704 @Override
705 public void onValueChanged(NumericTextView view, int value,
706 boolean isValid, boolean isFinished) {
707 final Runnable commitCallback;
708 final View nextFocusTarget;
709 if (view == mHourView) {
710 commitCallback = mCommitHour;
711 nextFocusTarget = view.isFocused() ? mMinuteView : null;
712 } else if (view == mMinuteView) {
713 commitCallback = mCommitMinute;
714 nextFocusTarget = null;
715 } else {
716 return;
717 }
718
719 view.removeCallbacks(commitCallback);
720
721 if (isValid) {
722 if (isFinished) {
723 // Done with hours entry, make visual updates
724 // immediately and move to next focus if needed.
725 commitCallback.run();
726
727 if (nextFocusTarget != null) {
728 nextFocusTarget.requestFocus();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700729 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400730 } else {
731 // May still be making changes. Postpone visual
732 // updates to prevent distracting the user.
733 view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700734 }
735 }
736 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400737 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700738
Alan Viveretteb3f24632015-10-22 16:01:48 -0400739 private final Runnable mCommitHour = new Runnable() {
740 @Override
741 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500742 setHour(mHourView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400743 }
744 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700745
Alan Viveretteb3f24632015-10-22 16:01:48 -0400746 private final Runnable mCommitMinute = new Runnable() {
747 @Override
748 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500749 setMinute(mMinuteView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400750 }
751 };
752
753 private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() {
754 @Override
755 public void onFocusChange(View v, boolean focused) {
756 if (focused) {
757 switch (v.getId()) {
758 case R.id.am_label:
759 setAmOrPm(AM);
760 break;
761 case R.id.pm_label:
762 setAmOrPm(PM);
763 break;
764 case R.id.hours:
765 setCurrentItemShowing(HOUR_INDEX, true, true);
766 break;
767 case R.id.minutes:
768 setCurrentItemShowing(MINUTE_INDEX, true, true);
769 break;
770 default:
771 // Failed to handle this click, don't vibrate.
772 return;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700773 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400774
775 tryVibrate();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700776 }
777 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400778 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700779
780 private final View.OnClickListener mClickListener = new View.OnClickListener() {
781 @Override
782 public void onClick(View v) {
783
784 final int amOrPm;
785 switch (v.getId()) {
786 case R.id.am_label:
787 setAmOrPm(AM);
788 break;
789 case R.id.pm_label:
790 setAmOrPm(PM);
791 break;
792 case R.id.hours:
793 setCurrentItemShowing(HOUR_INDEX, true, true);
794 break;
795 case R.id.minutes:
796 setCurrentItemShowing(MINUTE_INDEX, true, true);
797 break;
798 default:
799 // Failed to handle this click, don't vibrate.
800 return;
801 }
802
803 tryVibrate();
804 }
805 };
806
Alan Viveretteb3f24632015-10-22 16:01:48 -0400807 /**
808 * Delegates unhandled touches in a view group to the nearest child view.
809 */
810 private static class NearestTouchDelegate implements View.OnTouchListener {
811 private View mInitialTouchTarget;
812
813 @Override
814 public boolean onTouch(View view, MotionEvent motionEvent) {
815 final int actionMasked = motionEvent.getActionMasked();
816 if (actionMasked == MotionEvent.ACTION_DOWN) {
Alan Viverette7add7e02015-11-20 14:19:39 -0500817 if (view instanceof ViewGroup) {
818 mInitialTouchTarget = findNearestChild((ViewGroup) view,
819 (int) motionEvent.getX(), (int) motionEvent.getY());
820 } else {
821 mInitialTouchTarget = null;
822 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400823 }
824
825 final View child = mInitialTouchTarget;
826 if (child == null) {
827 return false;
828 }
829
830 final float offsetX = view.getScrollX() - child.getLeft();
831 final float offsetY = view.getScrollY() - child.getTop();
832 motionEvent.offsetLocation(offsetX, offsetY);
833 final boolean handled = child.dispatchTouchEvent(motionEvent);
834 motionEvent.offsetLocation(-offsetX, -offsetY);
835
836 if (actionMasked == MotionEvent.ACTION_UP
837 || actionMasked == MotionEvent.ACTION_CANCEL) {
838 mInitialTouchTarget = null;
839 }
840
841 return handled;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700842 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700843
Alan Viveretteb3f24632015-10-22 16:01:48 -0400844 private View findNearestChild(ViewGroup v, int x, int y) {
845 View bestChild = null;
846 int bestDist = Integer.MAX_VALUE;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700847
Alan Viveretteb3f24632015-10-22 16:01:48 -0400848 for (int i = 0, count = v.getChildCount(); i < count; i++) {
849 final View child = v.getChildAt(i);
850 final int dX = x - (child.getLeft() + child.getWidth() / 2);
851 final int dY = y - (child.getTop() + child.getHeight() / 2);
852 final int dist = dX * dX + dY * dY;
853 if (bestDist > dist) {
854 bestChild = child;
855 bestDist = dist;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700856 }
857 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400858
859 return bestChild;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700860 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400861 }
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700862}