blob: dc9a585b56e2306fc479003295681a040064176b [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
Aurimas Liutikasab14d822017-01-24 17:46:10 -080019import android.annotation.IntDef;
Alan Viverettef2525f62015-03-24 18:03:38 -070020import android.annotation.Nullable;
Andrei Stingaceanuf87b0e12016-07-04 17:40:14 +010021import android.annotation.TestApi;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070022import android.content.Context;
Alan Viverettef2525f62015-03-24 18:03:38 -070023import android.content.res.ColorStateList;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070024import android.content.res.Resources;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070025import android.content.res.TypedArray;
Aurimas Liutikasab14d822017-01-24 17:46:10 -080026import android.icu.text.DecimalFormatSymbols;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070027import android.os.Parcelable;
Alan Viverettef63757b2015-04-01 17:14:45 -070028import android.text.SpannableStringBuilder;
Roozbeh Pournader01bcf1e2017-06-29 14:48:35 -070029import android.text.TextUtils;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070030import android.text.format.DateFormat;
31import android.text.format.DateUtils;
Alan Viverettef63757b2015-04-01 17:14:45 -070032import android.text.style.TtsSpan;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070033import android.util.AttributeSet;
Alan Viverettef2525f62015-03-24 18:03:38 -070034import android.util.StateSet;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070035import android.view.HapticFeedbackConstants;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070036import android.view.LayoutInflater;
Alan Viveretteb3f24632015-10-22 16:01:48 -040037import android.view.MotionEvent;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070038import android.view.View;
Alan Viverette3fc00e312014-12-10 09:46:49 -080039import android.view.View.AccessibilityDelegate;
Alan Viveretteb3f24632015-10-22 16:01:48 -040040import android.view.View.MeasureSpec;
41import android.view.ViewGroup;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityNodeInfo;
Alan Viverette3fc00e312014-12-10 09:46:49 -080044import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Aurimas Liutikas73344412017-06-17 00:12:15 +000045import android.view.inputmethod.InputMethodManager;
Alan Viverette2a993b42016-04-28 12:56:09 -040046import android.widget.RadialTimePickerView.OnValueSelectedListener;
Aurimas Liutikasab14d822017-01-24 17:46:10 -080047import android.widget.TextInputTimePickerView.OnValueTypedListener;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070048
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070049import com.android.internal.R;
Alan Viveretteb3f24632015-10-22 16:01:48 -040050import com.android.internal.widget.NumericTextView;
51import com.android.internal.widget.NumericTextView.OnValueChangedListener;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070052
David Ogutu628bb612018-01-29 12:56:40 -050053
Aurimas Liutikasab14d822017-01-24 17:46:10 -080054import java.lang.annotation.Retention;
55import java.lang.annotation.RetentionPolicy;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070056import java.util.Calendar;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070057
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070058/**
Alan Viverettedaf33ed2014-10-23 13:34:17 -070059 * A delegate implementing the radial clock-based TimePicker.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070060 */
Alan Viverette2a993b42016-04-28 12:56:09 -040061class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate {
Alan Viveretteb3f24632015-10-22 16:01:48 -040062 /**
63 * Delay in milliseconds before valid but potentially incomplete, for
64 * example "1" but not "12", keyboard edits are propagated from the
65 * hour / minute fields to the radial picker.
66 */
67 private static final long DELAY_COMMIT_MILLIS = 2000;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070068
Aurimas Liutikasab14d822017-01-24 17:46:10 -080069 @IntDef({FROM_EXTERNAL_API, FROM_RADIAL_PICKER, FROM_INPUT_PICKER})
70 @Retention(RetentionPolicy.SOURCE)
71 private @interface ChangeSource {}
72 private static final int FROM_EXTERNAL_API = 0;
73 private static final int FROM_RADIAL_PICKER = 1;
74 private static final int FROM_INPUT_PICKER = 2;
75
Alan Viverettedaf33ed2014-10-23 13:34:17 -070076 // Index used by RadialPickerLayout
Alan Viveretteb0f54612016-04-12 14:58:09 -040077 private static final int HOUR_INDEX = RadialTimePickerView.HOURS;
78 private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070079
Alan Viverettef86bbd02015-09-16 14:19:21 -040080 private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor};
81 private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha};
Alan Viverettef2525f62015-03-24 18:03:38 -070082
Deepanshu Gupta491523d2015-10-06 17:56:37 -070083 private static final int AM = 0;
84 private static final int PM = 1;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070085
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070086 private static final int HOURS_IN_HALF_DAY = 12;
87
Alan Viveretteb3f24632015-10-22 16:01:48 -040088 private final NumericTextView mHourView;
89 private final NumericTextView mMinuteView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070090 private final View mAmPmLayout;
Alan Viveretteb3f24632015-10-22 16:01:48 -040091 private final RadioButton mAmLabel;
92 private final RadioButton mPmLabel;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070093 private final RadialTimePickerView mRadialTimePickerView;
94 private final TextView mSeparatorView;
95
Aurimas Liutikasab14d822017-01-24 17:46:10 -080096 private boolean mRadialPickerModeEnabled = true;
97 private final ImageButton mRadialTimePickerModeButton;
98 private final String mRadialTimePickerModeEnabledDescription;
99 private final String mTextInputPickerModeEnabledDescription;
100 private final View mRadialTimePickerHeader;
101 private final View mTextInputPickerHeader;
102
103 private final TextInputTimePickerView mTextInputPickerView;
104
Alan Viverette68016a62015-11-19 17:10:54 -0500105 private final Calendar mTempCalendar;
106
Alan Viveretteb0f54612016-04-12 14:58:09 -0400107 // Accessibility strings.
108 private final String mSelectHours;
109 private final String mSelectMinutes;
110
Alan Viverettef2525f62015-03-24 18:03:38 -0700111 private boolean mIsEnabled = true;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700112 private boolean mAllowAutoAdvance;
Alan Viverette2a993b42016-04-28 12:56:09 -0400113 private int mCurrentHour;
114 private int mCurrentMinute;
Alan Viverette4420ae82015-11-16 16:10:56 -0500115 private boolean mIs24Hour;
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700116
117 // The portrait layout puts AM/PM at the right by default.
118 private boolean mIsAmPmAtLeft = false;
119 // The landscape layouts put AM/PM at the bottom by default.
120 private boolean mIsAmPmAtTop = false;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700121
Alan Viveretteb3f24632015-10-22 16:01:48 -0400122 // Localization data.
123 private boolean mHourFormatShowLeadingZero;
124 private boolean mHourFormatStartsAtZero;
125
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700126 // Most recent time announcement values for accessibility.
127 private CharSequence mLastAnnouncedText;
128 private boolean mLastAnnouncedIsHour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700129
Chet Haase3053b2f2014-08-06 07:51:50 -0700130 public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs,
131 int defStyleAttr, int defStyleRes) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700132 super(delegator, context);
133
134 // process style attributes
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700135 final TypedArray a = mContext.obtainStyledAttributes(attrs,
136 R.styleable.TimePicker, defStyleAttr, defStyleRes);
137 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
138 Context.LAYOUT_INFLATER_SERVICE);
139 final Resources res = mContext.getResources();
140
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700141 mSelectHours = res.getString(R.string.select_hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700142 mSelectMinutes = res.getString(R.string.select_minutes);
143
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700144 final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout,
Alan Viverette62c79e92015-02-26 09:47:10 -0800145 R.layout.time_picker_material);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700146 final View mainView = inflater.inflate(layoutResourceId, delegator);
Adam Powell43da25c2017-05-23 15:56:59 -0700147 mainView.setSaveFromParentEnabled(false);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800148 mRadialTimePickerHeader = mainView.findViewById(R.id.time_header);
149 mRadialTimePickerHeader.setOnTouchListener(new NearestTouchDelegate());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700150
151 // Set up hour/minute labels.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400152 mHourView = (NumericTextView) mainView.findViewById(R.id.hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700153 mHourView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400154 mHourView.setOnFocusChangeListener(mFocusListener);
155 mHourView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800156 mHourView.setAccessibilityDelegate(
157 new ClickActionDelegate(context, R.string.select_hours));
Alan Viverette62c79e92015-02-26 09:47:10 -0800158 mSeparatorView = (TextView) mainView.findViewById(R.id.separator);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400159 mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700160 mMinuteView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400161 mMinuteView.setOnFocusChangeListener(mFocusListener);
162 mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800163 mMinuteView.setAccessibilityDelegate(
164 new ClickActionDelegate(context, R.string.select_minutes));
Alan Viveretteb3f24632015-10-22 16:01:48 -0400165 mMinuteView.setRange(0, 59);
Alan Viverettef63757b2015-04-01 17:14:45 -0700166
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700167 // Set up AM/PM labels.
Alan Viverette62c79e92015-02-26 09:47:10 -0800168 mAmPmLayout = mainView.findViewById(R.id.ampm_layout);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400169 mAmPmLayout.setOnTouchListener(new NearestTouchDelegate());
170
171 final String[] amPmStrings = TimePicker.getAmPmStrings(context);
172 mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700173 mAmLabel.setText(obtainVerbatim(amPmStrings[0]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700174 mAmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400175 ensureMinimumTextWidth(mAmLabel);
176
177 mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700178 mPmLabel.setText(obtainVerbatim(amPmStrings[1]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700179 mPmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400180 ensureMinimumTextWidth(mPmLabel);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700181
Alan Viverettef2525f62015-03-24 18:03:38 -0700182 // For the sake of backwards compatibility, attempt to extract the text
183 // color from the header time text appearance. If it's set, we'll let
184 // that override the "real" header text color.
185 ColorStateList headerTextColor = null;
186
187 @SuppressWarnings("deprecation")
188 final int timeHeaderTextAppearance = a.getResourceId(
189 R.styleable.TimePicker_headerTimeTextAppearance, 0);
190 if (timeHeaderTextAppearance != 0) {
191 final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
192 ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance);
193 final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
194 headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
195 textAppearance.recycle();
196 }
197
198 if (headerTextColor == null) {
199 headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor);
200 }
201
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800202 mTextInputPickerHeader = mainView.findViewById(R.id.input_header);
203
Alan Viverettef2525f62015-03-24 18:03:38 -0700204 if (headerTextColor != null) {
205 mHourView.setTextColor(headerTextColor);
206 mSeparatorView.setTextColor(headerTextColor);
207 mMinuteView.setTextColor(headerTextColor);
208 mAmLabel.setTextColor(headerTextColor);
209 mPmLabel.setTextColor(headerTextColor);
210 }
211
212 // Set up header background, if available.
213 if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) {
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800214 mRadialTimePickerHeader.setBackground(a.getDrawable(
215 R.styleable.TimePicker_headerBackground));
216 mTextInputPickerHeader.setBackground(a.getDrawable(
217 R.styleable.TimePicker_headerBackground));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700218 }
219
Alan Viverette51344782014-07-16 17:39:27 -0700220 a.recycle();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700221
Alan Viverette2b4dc112015-10-02 15:29:43 -0400222 mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker);
223 mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes);
Alan Viverette2a993b42016-04-28 12:56:09 -0400224 mRadialTimePickerView.setOnValueSelectedListener(mOnValueSelectedListener);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700225
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800226 mTextInputPickerView = (TextInputTimePickerView) mainView.findViewById(R.id.input_mode);
227 mTextInputPickerView.setListener(mOnValueTypedListener);
228
229 mRadialTimePickerModeButton =
230 (ImageButton) mainView.findViewById(R.id.toggle_mode);
231 mRadialTimePickerModeButton.setOnClickListener(new View.OnClickListener() {
232 @Override
233 public void onClick(View v) {
234 toggleRadialPickerMode();
235 }
236 });
237 mRadialTimePickerModeEnabledDescription = context.getResources().getString(
238 R.string.time_picker_radial_mode_description);
239 mTextInputPickerModeEnabledDescription = context.getResources().getString(
240 R.string.time_picker_text_input_mode_description);
241
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700242 mAllowAutoAdvance = true;
243
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500244 updateHourFormat();
Alan Viveretteb3f24632015-10-22 16:01:48 -0400245
246 // Initialize with current time.
Alan Viverette4420ae82015-11-16 16:10:56 -0500247 mTempCalendar = Calendar.getInstance(mLocale);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400248 final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY);
249 final int currentMinute = mTempCalendar.get(Calendar.MINUTE);
Alan Viverette4420ae82015-11-16 16:10:56 -0500250 initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400251 }
252
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800253 private void toggleRadialPickerMode() {
254 if (mRadialPickerModeEnabled) {
255 mRadialTimePickerView.setVisibility(View.GONE);
256 mRadialTimePickerHeader.setVisibility(View.GONE);
257 mTextInputPickerHeader.setVisibility(View.VISIBLE);
258 mTextInputPickerView.setVisibility(View.VISIBLE);
Aurimas Liutikasc0aa90d2017-05-15 15:24:16 -0700259 mRadialTimePickerModeButton.setImageResource(R.drawable.btn_clock_material);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800260 mRadialTimePickerModeButton.setContentDescription(
261 mRadialTimePickerModeEnabledDescription);
262 mRadialPickerModeEnabled = false;
263 } else {
264 mRadialTimePickerView.setVisibility(View.VISIBLE);
265 mRadialTimePickerHeader.setVisibility(View.VISIBLE);
266 mTextInputPickerHeader.setVisibility(View.GONE);
267 mTextInputPickerView.setVisibility(View.GONE);
268 mRadialTimePickerModeButton.setImageResource(R.drawable.btn_keyboard_key_material);
269 mRadialTimePickerModeButton.setContentDescription(
270 mTextInputPickerModeEnabledDescription);
271 updateTextInputPicker();
Yohei Yukawa484d4af2018-09-17 16:47:08 -0700272 InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
Aurimas Liutikas73344412017-06-17 00:12:15 +0000273 if (imm != null) {
274 imm.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
275 }
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800276 mRadialPickerModeEnabled = true;
277 }
278 }
279
280 @Override
281 public boolean validateInput() {
282 return mTextInputPickerView.validateInput();
283 }
284
Alan Viveretteb3f24632015-10-22 16:01:48 -0400285 /**
286 * Ensures that a TextView is wide enough to contain its text without
287 * wrapping or clipping. Measures the specified view and sets the minimum
288 * width to the view's desired width.
289 *
290 * @param v the text view to measure
291 */
292 private static void ensureMinimumTextWidth(TextView v) {
293 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
294
295 // Set both the TextView and the View version of minimum
296 // width because they are subtly different.
297 final int minWidth = v.getMeasuredWidth();
298 v.setMinWidth(minWidth);
299 v.setMinimumWidth(minWidth);
300 }
301
302 /**
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500303 * Updates hour formatting based on the current locale and 24-hour mode.
304 * <p>
305 * Determines how the hour should be formatted, sets member variables for
306 * leading zero and starting hour, and sets the hour view's presentation.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400307 */
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500308 private void updateHourFormat() {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400309 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500310 mLocale, mIs24Hour ? "Hm" : "hm");
Alan Viveretteb3f24632015-10-22 16:01:48 -0400311 final int lengthPattern = bestDateTimePattern.length();
312 boolean showLeadingZero = false;
313 char hourFormat = '\0';
314
315 for (int i = 0; i < lengthPattern; i++) {
316 final char c = bestDateTimePattern.charAt(i);
317 if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
318 hourFormat = c;
319 if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
320 showLeadingZero = true;
321 }
322 break;
323 }
324 }
325
326 mHourFormatShowLeadingZero = showLeadingZero;
327 mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H';
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500328
329 // Update hour text field.
330 final int minHour = mHourFormatStartsAtZero ? 0 : 1;
331 final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
332 mHourView.setRange(minHour, maxHour);
333 mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800334
335 final String[] digits = DecimalFormatSymbols.getInstance(mLocale).getDigitStrings();
336 int maxCharLength = 0;
337 for (int i = 0; i < 10; i++) {
338 maxCharLength = Math.max(maxCharLength, digits[i].length());
339 }
340 mTextInputPickerView.setHourFormat(maxCharLength * 2);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700341 }
342
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800343 static final CharSequence obtainVerbatim(String text) {
Alan Viverettef63757b2015-04-01 17:14:45 -0700344 return new SpannableStringBuilder().append(text,
345 new TtsSpan.VerbatimBuilder(text).build(), 0);
346 }
347
Alan Viverettef2525f62015-03-24 18:03:38 -0700348 /**
349 * The legacy text color might have been poorly defined. Ensures that it
350 * has an appropriate activated state, using the selected state if one
351 * exists or modifying the default text color otherwise.
352 *
353 * @param color a legacy text color, or {@code null}
354 * @return a color state list with an appropriate activated state, or
355 * {@code null} if a valid activated state could not be generated
356 */
357 @Nullable
358 private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
359 if (color == null || color.hasState(R.attr.state_activated)) {
360 return color;
361 }
362
363 final int activatedColor;
364 final int defaultColor;
365 if (color.hasState(R.attr.state_selected)) {
366 activatedColor = color.getColorForState(StateSet.get(
367 StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
368 defaultColor = color.getColorForState(StateSet.get(
369 StateSet.VIEW_STATE_ENABLED), 0);
370 } else {
371 activatedColor = color.getDefaultColor();
372
373 // Generate a non-activated color using the disabled alpha.
374 final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
375 final float disabledAlpha = ta.getFloat(0, 0.30f);
376 defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
377 }
378
379 if (activatedColor == 0 || defaultColor == 0) {
380 // We somehow failed to obtain the colors.
381 return null;
382 }
383
384 final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
385 final int[] colors = new int[] { activatedColor, defaultColor };
386 return new ColorStateList(stateSet, colors);
387 }
388
389 private int multiplyAlphaComponent(int color, float alphaMod) {
390 final int srcRgb = color & 0xFFFFFF;
391 final int srcAlpha = (color >> 24) & 0xFF;
392 final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f);
393 return srcRgb | (dstAlpha << 24);
394 }
395
Alan Viverette3fc00e312014-12-10 09:46:49 -0800396 private static class ClickActionDelegate extends AccessibilityDelegate {
397 private final AccessibilityAction mClickAction;
398
399 public ClickActionDelegate(Context context, int resId) {
400 mClickAction = new AccessibilityAction(
401 AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId));
402 }
403
404 @Override
405 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
406 super.onInitializeAccessibilityNodeInfo(host, info);
407
408 info.addAction(mClickAction);
409 }
410 }
411
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700412 private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400413 mCurrentHour = hourOfDay;
414 mCurrentMinute = minute;
Alan Viverette4420ae82015-11-16 16:10:56 -0500415 mIs24Hour = is24HourView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700416 updateUI(index);
417 }
418
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700419 private void updateUI(int index) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700420 updateHeaderAmPm();
Alan Viverette2a993b42016-04-28 12:56:09 -0400421 updateHeaderHour(mCurrentHour, false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700422 updateHeaderSeparator();
Alan Viverette2a993b42016-04-28 12:56:09 -0400423 updateHeaderMinute(mCurrentMinute, false);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400424 updateRadialPicker(index);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800425 updateTextInputPicker();
Alan Viveretteb3f24632015-10-22 16:01:48 -0400426
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700427 mDelegator.invalidate();
428 }
429
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800430 private void updateTextInputPicker() {
431 mTextInputPickerView.updateTextInputValues(getLocalizedHour(mCurrentHour), mCurrentMinute,
432 mCurrentHour < 12 ? AM : PM, mIs24Hour, mHourFormatStartsAtZero);
433 }
434
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700435 private void updateRadialPicker(int index) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400436 mRadialTimePickerView.initialize(mCurrentHour, mCurrentMinute, mIs24Hour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700437 setCurrentItemShowing(index, false, true);
438 }
439
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700440 private void updateHeaderAmPm() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500441 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700442 mAmPmLayout.setVisibility(View.GONE);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700443 } else {
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700444 // Find the location of AM/PM based on locale information.
445 final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm");
446 final boolean isAmPmAtStart = dateTimePattern.startsWith("a");
447 setAmPmStart(isAmPmAtStart);
Alan Viverette2a993b42016-04-28 12:56:09 -0400448 updateAmPmLabelStates(mCurrentHour < 12 ? AM : PM);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700449 }
450 }
451
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700452 private void setAmPmStart(boolean isAmPmAtStart) {
453 final RelativeLayout.LayoutParams params =
454 (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
455 if (params.getRule(RelativeLayout.RIGHT_OF) != 0
456 || params.getRule(RelativeLayout.LEFT_OF) != 0) {
Tetsutoki Shiozawa7cbd1942017-08-08 13:25:00 +0900457 final int margin = (int) (mContext.getResources().getDisplayMetrics().density * 8);
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700458 // Horizontal mode, with AM/PM appearing to left/right of hours and minutes.
459 final boolean isAmPmAtLeft;
460 if (TextUtils.getLayoutDirectionFromLocale(mLocale) == View.LAYOUT_DIRECTION_LTR) {
461 isAmPmAtLeft = isAmPmAtStart;
462 } else {
463 isAmPmAtLeft = !isAmPmAtStart;
464 }
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800465
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700466 if (isAmPmAtLeft) {
467 params.removeRule(RelativeLayout.RIGHT_OF);
468 params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
469 } else {
470 params.removeRule(RelativeLayout.LEFT_OF);
471 params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
472 }
Tetsutoki Shiozawa7cbd1942017-08-08 13:25:00 +0900473
474 if (isAmPmAtStart) {
475 params.setMarginStart(0);
476 params.setMarginEnd(margin);
477 } else {
478 params.setMarginStart(margin);
479 params.setMarginEnd(0);
480 }
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700481 mIsAmPmAtLeft = isAmPmAtLeft;
482 } else if (params.getRule(RelativeLayout.BELOW) != 0
483 || params.getRule(RelativeLayout.ABOVE) != 0) {
484 // Vertical mode, with AM/PM appearing to top/bottom of hours and minutes.
485 if (mIsAmPmAtTop == isAmPmAtStart) {
486 // AM/PM is already at the correct location. No change needed.
487 return;
488 }
489
490 final int otherViewId;
491 if (isAmPmAtStart) {
492 otherViewId = params.getRule(RelativeLayout.BELOW);
493 params.removeRule(RelativeLayout.BELOW);
494 params.addRule(RelativeLayout.ABOVE, otherViewId);
495 } else {
496 otherViewId = params.getRule(RelativeLayout.ABOVE);
497 params.removeRule(RelativeLayout.ABOVE);
498 params.addRule(RelativeLayout.BELOW, otherViewId);
499 }
500
501 // Switch the top and bottom paddings on the other view.
502 final View otherView = mRadialTimePickerHeader.findViewById(otherViewId);
503 final int top = otherView.getPaddingTop();
504 final int bottom = otherView.getPaddingBottom();
505 final int left = otherView.getPaddingLeft();
506 final int right = otherView.getPaddingRight();
507 otherView.setPadding(left, bottom, right, top);
508
509 mIsAmPmAtTop = isAmPmAtStart;
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800510 }
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700511
512 mAmPmLayout.setLayoutParams(params);
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800513 }
514
Felipe Lemef480e8c2017-08-10 18:38:44 -0700515 @Override
516 public void setDate(int hour, int minute) {
517 setHourInternal(hour, FROM_EXTERNAL_API, true, false);
518 setMinuteInternal(minute, FROM_EXTERNAL_API, false);
519
520 onTimeChanged();
521 }
522
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700523 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700524 * Set the current hour.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700525 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700526 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500527 public void setHour(int hour) {
Felipe Lemef480e8c2017-08-10 18:38:44 -0700528 setHourInternal(hour, FROM_EXTERNAL_API, true, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400529 }
530
Felipe Lemef480e8c2017-08-10 18:38:44 -0700531 private void setHourInternal(int hour, @ChangeSource int source, boolean announce,
532 boolean notify) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400533 if (mCurrentHour == hour) {
534 return;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700535 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400536
Felipe Lemef480e8c2017-08-10 18:38:44 -0700537 resetAutofilledValue();
Alan Viverette2a993b42016-04-28 12:56:09 -0400538 mCurrentHour = hour;
539 updateHeaderHour(hour, announce);
540 updateHeaderAmPm();
541
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800542 if (source != FROM_RADIAL_PICKER) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400543 mRadialTimePickerView.setCurrentHour(hour);
544 mRadialTimePickerView.setAmOrPm(hour < 12 ? AM : PM);
545 }
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800546 if (source != FROM_INPUT_PICKER) {
547 updateTextInputPicker();
548 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400549
550 mDelegator.invalidate();
Felipe Lemef480e8c2017-08-10 18:38:44 -0700551 if (notify) {
552 onTimeChanged();
553 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700554 }
555
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700556 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500557 * @return the current hour in the range (0-23)
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700558 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700559 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500560 public int getHour() {
561 final int currentHour = mRadialTimePickerView.getCurrentHour();
562 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700563 return currentHour;
Alan Viverette4420ae82015-11-16 16:10:56 -0500564 }
565
566 if (mRadialTimePickerView.getAmOrPm() == PM) {
567 return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700568 } else {
Alan Viverette4420ae82015-11-16 16:10:56 -0500569 return currentHour % HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700570 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700571 }
572
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700573 /**
574 * Set the current minute (0-59).
575 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700576 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500577 public void setMinute(int minute) {
Felipe Lemef480e8c2017-08-10 18:38:44 -0700578 setMinuteInternal(minute, FROM_EXTERNAL_API, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400579 }
580
Felipe Lemef480e8c2017-08-10 18:38:44 -0700581 private void setMinuteInternal(int minute, @ChangeSource int source, boolean notify) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400582 if (mCurrentMinute == minute) {
583 return;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700584 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400585
Felipe Lemef480e8c2017-08-10 18:38:44 -0700586 resetAutofilledValue();
Alan Viverette2a993b42016-04-28 12:56:09 -0400587 mCurrentMinute = minute;
588 updateHeaderMinute(minute, true);
589
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800590 if (source != FROM_RADIAL_PICKER) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400591 mRadialTimePickerView.setCurrentMinute(minute);
592 }
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800593 if (source != FROM_INPUT_PICKER) {
594 updateTextInputPicker();
595 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400596
597 mDelegator.invalidate();
Felipe Lemef480e8c2017-08-10 18:38:44 -0700598 if (notify) {
599 onTimeChanged();
600 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700601 }
602
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700603 /**
604 * @return The current minute.
605 */
606 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500607 public int getMinute() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700608 return mRadialTimePickerView.getCurrentMinute();
609 }
610
611 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500612 * Sets whether time is displayed in 24-hour mode or 12-hour mode with
613 * AM/PM indicators.
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700614 *
Alan Viverette4420ae82015-11-16 16:10:56 -0500615 * @param is24Hour {@code true} to display time in 24-hour mode or
616 * {@code false} for 12-hour mode with AM/PM
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700617 */
Alan Viverette4420ae82015-11-16 16:10:56 -0500618 public void setIs24Hour(boolean is24Hour) {
619 if (mIs24Hour != is24Hour) {
620 mIs24Hour = is24Hour;
Alan Viverette2a993b42016-04-28 12:56:09 -0400621 mCurrentHour = getHour();
Alan Viverette4420ae82015-11-16 16:10:56 -0500622
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500623 updateHourFormat();
Alan Viverette4420ae82015-11-16 16:10:56 -0500624 updateUI(mRadialTimePickerView.getCurrentItemShowing());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700625 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700626 }
627
628 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500629 * @return {@code true} if time is displayed in 24-hour mode, or
630 * {@code false} if time is displayed in 12-hour mode with AM/PM
631 * indicators
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700632 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700633 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500634 public boolean is24Hour() {
635 return mIs24Hour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700636 }
637
638 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700639 public void setEnabled(boolean enabled) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700640 mHourView.setEnabled(enabled);
641 mMinuteView.setEnabled(enabled);
642 mAmLabel.setEnabled(enabled);
643 mPmLabel.setEnabled(enabled);
644 mRadialTimePickerView.setEnabled(enabled);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700645 mIsEnabled = enabled;
646 }
647
648 @Override
649 public boolean isEnabled() {
650 return mIsEnabled;
651 }
652
653 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700654 public int getBaseline() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700655 // does not support baseline alignment
656 return -1;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700657 }
658
659 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700660 public Parcelable onSaveInstanceState(Parcelable superState) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500661 return new SavedState(superState, getHour(), getMinute(),
662 is24Hour(), getCurrentItemShowing());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700663 }
664
665 @Override
666 public void onRestoreInstanceState(Parcelable state) {
Alan Viverette6b3f85f2016-03-01 16:48:04 -0500667 if (state instanceof SavedState) {
668 final SavedState ss = (SavedState) state;
669 initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing());
670 mRadialTimePickerView.invalidate();
671 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700672 }
673
674 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700675 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
676 onPopulateAccessibilityEvent(event);
677 return true;
678 }
679
680 @Override
681 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
682 int flags = DateUtils.FORMAT_SHOW_TIME;
Alan Viverette4420ae82015-11-16 16:10:56 -0500683 if (mIs24Hour) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700684 flags |= DateUtils.FORMAT_24HOUR;
685 } else {
686 flags |= DateUtils.FORMAT_12HOUR;
687 }
Alan Viveretteb0f54612016-04-12 14:58:09 -0400688
Alan Viverette4420ae82015-11-16 16:10:56 -0500689 mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour());
690 mTempCalendar.set(Calendar.MINUTE, getMinute());
Alan Viveretteb0f54612016-04-12 14:58:09 -0400691
692 final String selectedTime = DateUtils.formatDateTime(mContext,
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700693 mTempCalendar.getTimeInMillis(), flags);
Alan Viveretteb0f54612016-04-12 14:58:09 -0400694 final String selectionMode = mRadialTimePickerView.getCurrentItemShowing() == HOUR_INDEX ?
695 mSelectHours : mSelectMinutes;
696 event.getText().add(selectedTime + " " + selectionMode);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700697 }
698
Andrei Stingaceanuf87b0e12016-07-04 17:40:14 +0100699 /** @hide */
700 @Override
701 @TestApi
702 public View getHourView() {
703 return mHourView;
704 }
705
706 /** @hide */
707 @Override
708 @TestApi
709 public View getMinuteView() {
710 return mMinuteView;
711 }
712
713 /** @hide */
714 @Override
715 @TestApi
716 public View getAmView() {
717 return mAmLabel;
718 }
719
720 /** @hide */
721 @Override
722 @TestApi
723 public View getPmView() {
724 return mPmLabel;
725 }
726
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700727 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700728 * @return the index of the current item showing
729 */
730 private int getCurrentItemShowing() {
731 return mRadialTimePickerView.getCurrentItemShowing();
732 }
733
734 /**
735 * Propagate the time change
736 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700737 private void onTimeChanged() {
738 mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
739 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500740 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700741 }
Felipe Leme305b72c2017-02-27 12:46:04 -0800742 if (mAutoFillChangeListener != null) {
743 mAutoFillChangeListener.onTimeChanged(mDelegator, getHour(), getMinute());
744 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700745 }
746
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700747 private void tryVibrate() {
748 mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700749 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700750
751 private void updateAmPmLabelStates(int amOrPm) {
752 final boolean isAm = amOrPm == AM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700753 mAmLabel.setActivated(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700754 mAmLabel.setChecked(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700755
756 final boolean isPm = amOrPm == PM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700757 mPmLabel.setActivated(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700758 mPmLabel.setChecked(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700759 }
760
761 /**
Alan Viveretteb3f24632015-10-22 16:01:48 -0400762 * Converts hour-of-day (0-23) time into a localized hour number.
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500763 * <p>
764 * The localized value may be in the range (0-23), (1-24), (0-11), or
765 * (1-12) depending on the locale. This method does not handle leading
766 * zeroes.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400767 *
768 * @param hourOfDay the hour-of-day (0-23)
769 * @return a localized hour number
770 */
771 private int getLocalizedHour(int hourOfDay) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500772 if (!mIs24Hour) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400773 // Convert to hour-of-am-pm.
774 hourOfDay %= 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700775 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400776
777 if (!mHourFormatStartsAtZero && hourOfDay == 0) {
778 // Convert to clock-hour (either of-day or of-am-pm).
Alan Viverette4420ae82015-11-16 16:10:56 -0500779 hourOfDay = mIs24Hour ? 24 : 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700780 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400781
782 return hourOfDay;
783 }
784
785 private void updateHeaderHour(int hourOfDay, boolean announce) {
786 final int localizedHour = getLocalizedHour(hourOfDay);
787 mHourView.setValue(localizedHour);
788
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700789 if (announce) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400790 tryAnnounceForAccessibility(mHourView.getText(), true);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700791 }
792 }
793
Alan Viveretteb3f24632015-10-22 16:01:48 -0400794 private void updateHeaderMinute(int minuteOfHour, boolean announce) {
795 mMinuteView.setValue(minuteOfHour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700796
Alan Viveretteb3f24632015-10-22 16:01:48 -0400797 if (announce) {
798 tryAnnounceForAccessibility(mMinuteView.getText(), false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700799 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700800 }
801
802 /**
803 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
804 *
805 * See http://unicode.org/cldr/trac/browser/trunk/common/main
806 *
807 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
808 * separator as the character which is just after the hour marker in the returned pattern.
809 */
810 private void updateHeaderSeparator() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500811 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
812 (mIs24Hour) ? "Hm" : "hm");
David Ogutu628bb612018-01-29 12:56:40 -0500813 final String separatorText = getHourMinSeparatorFromPattern(bestDateTimePattern);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700814 mSeparatorView.setText(separatorText);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800815 mTextInputPickerView.updateSeparator(separatorText);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700816 }
817
David Ogutu628bb612018-01-29 12:56:40 -0500818 /**
819 * This helper method extracts the time separator from the {@code datetimePattern}.
820 *
821 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
822 *
823 * See http://unicode.org/cldr/trac/browser/trunk/common/main
824 *
825 * @return Separator string. This is the character or set of quoted characters just after the
826 * hour marker in {@code dateTimePattern}. Returns a colon (:) if it can't locate the
827 * separator.
828 *
829 * @hide
830 */
831 private static String getHourMinSeparatorFromPattern(String dateTimePattern) {
832 final String defaultSeparator = ":";
833 boolean foundHourPattern = false;
834 for (int i = 0; i < dateTimePattern.length(); i++) {
835 switch (dateTimePattern.charAt(i)) {
836 // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats.
837 case 'H':
838 case 'h':
839 case 'K':
840 case 'k':
841 foundHourPattern = true;
842 continue;
843 case ' ': // skip spaces
844 continue;
845 case '\'':
846 if (!foundHourPattern) {
847 continue;
848 }
849 SpannableStringBuilder quotedSubstring = new SpannableStringBuilder(
850 dateTimePattern.substring(i));
851 int quotedTextLength = DateFormat.appendQuotedText(quotedSubstring, 0);
852 return quotedSubstring.subSequence(0, quotedTextLength).toString();
853 default:
854 if (!foundHourPattern) {
855 continue;
856 }
857 return Character.toString(dateTimePattern.charAt(i));
858 }
859 }
860 return defaultSeparator;
861 }
862
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700863 static private int lastIndexOfAny(String str, char[] any) {
864 final int lengthAny = any.length;
865 if (lengthAny > 0) {
866 for (int i = str.length() - 1; i >= 0; i--) {
867 char c = str.charAt(i);
868 for (int j = 0; j < lengthAny; j++) {
869 if (c == any[j]) {
870 return i;
871 }
872 }
873 }
874 }
875 return -1;
876 }
877
Alan Viveretteb3f24632015-10-22 16:01:48 -0400878 private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) {
879 if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) {
880 // TODO: Find a better solution, potentially live regions?
881 mDelegator.announceForAccessibility(text);
882 mLastAnnouncedText = text;
883 mLastAnnouncedIsHour = isHour;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700884 }
885 }
886
887 /**
888 * Show either Hours or Minutes.
889 */
890 private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) {
891 mRadialTimePickerView.setCurrentItemShowing(index, animateCircle);
892
893 if (index == HOUR_INDEX) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700894 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700895 mDelegator.announceForAccessibility(mSelectHours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700896 }
897 } else {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700898 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700899 mDelegator.announceForAccessibility(mSelectMinutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700900 }
901 }
902
Alan Viverettef2525f62015-03-24 18:03:38 -0700903 mHourView.setActivated(index == HOUR_INDEX);
904 mMinuteView.setActivated(index == MINUTE_INDEX);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700905 }
906
907 private void setAmOrPm(int amOrPm) {
908 updateAmPmLabelStates(amOrPm);
Alan Viverette30b57b62016-04-19 09:29:20 -0400909
Alan Viverette2a993b42016-04-28 12:56:09 -0400910 if (mRadialTimePickerView.setAmOrPm(amOrPm)) {
911 mCurrentHour = getHour();
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800912 updateTextInputPicker();
Alan Viverette2a993b42016-04-28 12:56:09 -0400913 if (mOnTimeChangedListener != null) {
914 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
915 }
Alan Viverette30b57b62016-04-19 09:29:20 -0400916 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700917 }
918
Alan Viverette2a993b42016-04-28 12:56:09 -0400919 /** Listener for RadialTimePickerView interaction. */
920 private final OnValueSelectedListener mOnValueSelectedListener = new OnValueSelectedListener() {
921 @Override
Alan Viverette66a85622016-08-04 13:24:14 -0400922 public void onValueSelected(int pickerType, int newValue, boolean autoAdvance) {
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800923 boolean valueChanged = false;
Alan Viverette66a85622016-08-04 13:24:14 -0400924 switch (pickerType) {
925 case RadialTimePickerView.HOURS:
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800926 if (getHour() != newValue) {
927 valueChanged = true;
928 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400929 final boolean isTransition = mAllowAutoAdvance && autoAdvance;
Felipe Lemef480e8c2017-08-10 18:38:44 -0700930 setHourInternal(newValue, FROM_RADIAL_PICKER, !isTransition, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400931 if (isTransition) {
932 setCurrentItemShowing(MINUTE_INDEX, true, false);
Alan Viverette66a85622016-08-04 13:24:14 -0400933
934 final int localizedHour = getLocalizedHour(newValue);
935 mDelegator.announceForAccessibility(localizedHour + ". " + mSelectMinutes);
Alan Viverette2a993b42016-04-28 12:56:09 -0400936 }
937 break;
Alan Viverette66a85622016-08-04 13:24:14 -0400938 case RadialTimePickerView.MINUTES:
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800939 if (getMinute() != newValue) {
940 valueChanged = true;
941 }
Felipe Lemef480e8c2017-08-10 18:38:44 -0700942 setMinuteInternal(newValue, FROM_RADIAL_PICKER, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400943 break;
Alan Viverette2a993b42016-04-28 12:56:09 -0400944 }
945
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800946 if (mOnTimeChangedListener != null && valueChanged) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400947 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
948 }
949 }
950 };
951
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800952 private final OnValueTypedListener mOnValueTypedListener = new OnValueTypedListener() {
953 @Override
954 public void onValueChanged(int pickerType, int newValue) {
955 switch (pickerType) {
956 case TextInputTimePickerView.HOURS:
Felipe Lemef480e8c2017-08-10 18:38:44 -0700957 setHourInternal(newValue, FROM_INPUT_PICKER, false, true);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800958 break;
959 case TextInputTimePickerView.MINUTES:
Felipe Lemef480e8c2017-08-10 18:38:44 -0700960 setMinuteInternal(newValue, FROM_INPUT_PICKER, true);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800961 break;
962 case TextInputTimePickerView.AMPM:
963 setAmOrPm(newValue);
964 break;
965 }
966 }
967 };
968
Alan Viverette2a993b42016-04-28 12:56:09 -0400969 /** Listener for keyboard interaction. */
Alan Viveretteb3f24632015-10-22 16:01:48 -0400970 private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() {
971 @Override
972 public void onValueChanged(NumericTextView view, int value,
973 boolean isValid, boolean isFinished) {
974 final Runnable commitCallback;
975 final View nextFocusTarget;
976 if (view == mHourView) {
977 commitCallback = mCommitHour;
978 nextFocusTarget = view.isFocused() ? mMinuteView : null;
979 } else if (view == mMinuteView) {
980 commitCallback = mCommitMinute;
981 nextFocusTarget = null;
982 } else {
983 return;
984 }
985
986 view.removeCallbacks(commitCallback);
987
988 if (isValid) {
989 if (isFinished) {
990 // Done with hours entry, make visual updates
991 // immediately and move to next focus if needed.
992 commitCallback.run();
993
994 if (nextFocusTarget != null) {
995 nextFocusTarget.requestFocus();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700996 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400997 } else {
998 // May still be making changes. Postpone visual
999 // updates to prevent distracting the user.
1000 view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS);
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001001 }
1002 }
1003 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001004 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001005
Alan Viveretteb3f24632015-10-22 16:01:48 -04001006 private final Runnable mCommitHour = new Runnable() {
1007 @Override
1008 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -05001009 setHour(mHourView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -04001010 }
1011 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001012
Alan Viveretteb3f24632015-10-22 16:01:48 -04001013 private final Runnable mCommitMinute = new Runnable() {
1014 @Override
1015 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -05001016 setMinute(mMinuteView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -04001017 }
1018 };
1019
1020 private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() {
1021 @Override
1022 public void onFocusChange(View v, boolean focused) {
1023 if (focused) {
1024 switch (v.getId()) {
1025 case R.id.am_label:
1026 setAmOrPm(AM);
1027 break;
1028 case R.id.pm_label:
1029 setAmOrPm(PM);
1030 break;
1031 case R.id.hours:
1032 setCurrentItemShowing(HOUR_INDEX, true, true);
1033 break;
1034 case R.id.minutes:
1035 setCurrentItemShowing(MINUTE_INDEX, true, true);
1036 break;
1037 default:
1038 // Failed to handle this click, don't vibrate.
1039 return;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001040 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001041
1042 tryVibrate();
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001043 }
1044 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001045 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001046
1047 private final View.OnClickListener mClickListener = new View.OnClickListener() {
1048 @Override
1049 public void onClick(View v) {
1050
1051 final int amOrPm;
1052 switch (v.getId()) {
1053 case R.id.am_label:
1054 setAmOrPm(AM);
1055 break;
1056 case R.id.pm_label:
1057 setAmOrPm(PM);
1058 break;
1059 case R.id.hours:
1060 setCurrentItemShowing(HOUR_INDEX, true, true);
1061 break;
1062 case R.id.minutes:
1063 setCurrentItemShowing(MINUTE_INDEX, true, true);
1064 break;
1065 default:
1066 // Failed to handle this click, don't vibrate.
1067 return;
1068 }
1069
1070 tryVibrate();
1071 }
1072 };
1073
Alan Viveretteb3f24632015-10-22 16:01:48 -04001074 /**
1075 * Delegates unhandled touches in a view group to the nearest child view.
1076 */
1077 private static class NearestTouchDelegate implements View.OnTouchListener {
1078 private View mInitialTouchTarget;
1079
1080 @Override
1081 public boolean onTouch(View view, MotionEvent motionEvent) {
1082 final int actionMasked = motionEvent.getActionMasked();
1083 if (actionMasked == MotionEvent.ACTION_DOWN) {
Alan Viverette7add7e02015-11-20 14:19:39 -05001084 if (view instanceof ViewGroup) {
1085 mInitialTouchTarget = findNearestChild((ViewGroup) view,
1086 (int) motionEvent.getX(), (int) motionEvent.getY());
1087 } else {
1088 mInitialTouchTarget = null;
1089 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001090 }
1091
1092 final View child = mInitialTouchTarget;
1093 if (child == null) {
1094 return false;
1095 }
1096
1097 final float offsetX = view.getScrollX() - child.getLeft();
1098 final float offsetY = view.getScrollY() - child.getTop();
1099 motionEvent.offsetLocation(offsetX, offsetY);
1100 final boolean handled = child.dispatchTouchEvent(motionEvent);
1101 motionEvent.offsetLocation(-offsetX, -offsetY);
1102
1103 if (actionMasked == MotionEvent.ACTION_UP
1104 || actionMasked == MotionEvent.ACTION_CANCEL) {
1105 mInitialTouchTarget = null;
1106 }
1107
1108 return handled;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001109 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001110
Alan Viveretteb3f24632015-10-22 16:01:48 -04001111 private View findNearestChild(ViewGroup v, int x, int y) {
1112 View bestChild = null;
1113 int bestDist = Integer.MAX_VALUE;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001114
Alan Viveretteb3f24632015-10-22 16:01:48 -04001115 for (int i = 0, count = v.getChildCount(); i < count; i++) {
1116 final View child = v.getChildAt(i);
1117 final int dX = x - (child.getLeft() + child.getWidth() / 2);
1118 final int dY = y - (child.getTop() + child.getHeight() / 2);
1119 final int dist = dX * dX + dY * dY;
1120 if (bestDist > dist) {
1121 bestChild = child;
1122 bestDist = dist;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001123 }
1124 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001125
1126 return bestChild;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001127 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001128 }
Elliott Hughes1cc51a62014-08-21 16:21:30 -07001129}