blob: 706b0ce225dc43f6108b037510ab0e9ee759eb95 [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
Aurimas Liutikasab14d822017-01-24 17:46:10 -080053import java.lang.annotation.Retention;
54import java.lang.annotation.RetentionPolicy;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070055import java.util.Calendar;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070056
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070057/**
Alan Viverettedaf33ed2014-10-23 13:34:17 -070058 * A delegate implementing the radial clock-based TimePicker.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070059 */
Alan Viverette2a993b42016-04-28 12:56:09 -040060class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate {
Alan Viveretteb3f24632015-10-22 16:01:48 -040061 /**
62 * Delay in milliseconds before valid but potentially incomplete, for
63 * example "1" but not "12", keyboard edits are propagated from the
64 * hour / minute fields to the radial picker.
65 */
66 private static final long DELAY_COMMIT_MILLIS = 2000;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070067
Aurimas Liutikasab14d822017-01-24 17:46:10 -080068 @IntDef({FROM_EXTERNAL_API, FROM_RADIAL_PICKER, FROM_INPUT_PICKER})
69 @Retention(RetentionPolicy.SOURCE)
70 private @interface ChangeSource {}
71 private static final int FROM_EXTERNAL_API = 0;
72 private static final int FROM_RADIAL_PICKER = 1;
73 private static final int FROM_INPUT_PICKER = 2;
74
Alan Viverettedaf33ed2014-10-23 13:34:17 -070075 // Index used by RadialPickerLayout
Alan Viveretteb0f54612016-04-12 14:58:09 -040076 private static final int HOUR_INDEX = RadialTimePickerView.HOURS;
77 private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070078
Alan Viverettef86bbd02015-09-16 14:19:21 -040079 private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor};
80 private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha};
Alan Viverettef2525f62015-03-24 18:03:38 -070081
Deepanshu Gupta491523d2015-10-06 17:56:37 -070082 private static final int AM = 0;
83 private static final int PM = 1;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070084
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070085 private static final int HOURS_IN_HALF_DAY = 12;
86
Alan Viveretteb3f24632015-10-22 16:01:48 -040087 private final NumericTextView mHourView;
88 private final NumericTextView mMinuteView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070089 private final View mAmPmLayout;
Alan Viveretteb3f24632015-10-22 16:01:48 -040090 private final RadioButton mAmLabel;
91 private final RadioButton mPmLabel;
Alan Viverettedaf33ed2014-10-23 13:34:17 -070092 private final RadialTimePickerView mRadialTimePickerView;
93 private final TextView mSeparatorView;
94
Aurimas Liutikasab14d822017-01-24 17:46:10 -080095 private boolean mRadialPickerModeEnabled = true;
96 private final ImageButton mRadialTimePickerModeButton;
97 private final String mRadialTimePickerModeEnabledDescription;
98 private final String mTextInputPickerModeEnabledDescription;
99 private final View mRadialTimePickerHeader;
100 private final View mTextInputPickerHeader;
101
102 private final TextInputTimePickerView mTextInputPickerView;
103
Alan Viverette68016a62015-11-19 17:10:54 -0500104 private final Calendar mTempCalendar;
105
Alan Viveretteb0f54612016-04-12 14:58:09 -0400106 // Accessibility strings.
107 private final String mSelectHours;
108 private final String mSelectMinutes;
109
Alan Viverettef2525f62015-03-24 18:03:38 -0700110 private boolean mIsEnabled = true;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700111 private boolean mAllowAutoAdvance;
Alan Viverette2a993b42016-04-28 12:56:09 -0400112 private int mCurrentHour;
113 private int mCurrentMinute;
Alan Viverette4420ae82015-11-16 16:10:56 -0500114 private boolean mIs24Hour;
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700115
116 // The portrait layout puts AM/PM at the right by default.
117 private boolean mIsAmPmAtLeft = false;
118 // The landscape layouts put AM/PM at the bottom by default.
119 private boolean mIsAmPmAtTop = false;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700120
Alan Viveretteb3f24632015-10-22 16:01:48 -0400121 // Localization data.
122 private boolean mHourFormatShowLeadingZero;
123 private boolean mHourFormatStartsAtZero;
124
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700125 // Most recent time announcement values for accessibility.
126 private CharSequence mLastAnnouncedText;
127 private boolean mLastAnnouncedIsHour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700128
Chet Haase3053b2f2014-08-06 07:51:50 -0700129 public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs,
130 int defStyleAttr, int defStyleRes) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700131 super(delegator, context);
132
133 // process style attributes
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700134 final TypedArray a = mContext.obtainStyledAttributes(attrs,
135 R.styleable.TimePicker, defStyleAttr, defStyleRes);
136 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
137 Context.LAYOUT_INFLATER_SERVICE);
138 final Resources res = mContext.getResources();
139
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700140 mSelectHours = res.getString(R.string.select_hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700141 mSelectMinutes = res.getString(R.string.select_minutes);
142
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700143 final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout,
Alan Viverette62c79e92015-02-26 09:47:10 -0800144 R.layout.time_picker_material);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700145 final View mainView = inflater.inflate(layoutResourceId, delegator);
Adam Powell43da25c2017-05-23 15:56:59 -0700146 mainView.setSaveFromParentEnabled(false);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800147 mRadialTimePickerHeader = mainView.findViewById(R.id.time_header);
148 mRadialTimePickerHeader.setOnTouchListener(new NearestTouchDelegate());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700149
150 // Set up hour/minute labels.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400151 mHourView = (NumericTextView) mainView.findViewById(R.id.hours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700152 mHourView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400153 mHourView.setOnFocusChangeListener(mFocusListener);
154 mHourView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800155 mHourView.setAccessibilityDelegate(
156 new ClickActionDelegate(context, R.string.select_hours));
Alan Viverette62c79e92015-02-26 09:47:10 -0800157 mSeparatorView = (TextView) mainView.findViewById(R.id.separator);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400158 mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700159 mMinuteView.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400160 mMinuteView.setOnFocusChangeListener(mFocusListener);
161 mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener);
Alan Viverette3fc00e312014-12-10 09:46:49 -0800162 mMinuteView.setAccessibilityDelegate(
163 new ClickActionDelegate(context, R.string.select_minutes));
Alan Viveretteb3f24632015-10-22 16:01:48 -0400164 mMinuteView.setRange(0, 59);
Alan Viverettef63757b2015-04-01 17:14:45 -0700165
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700166 // Set up AM/PM labels.
Alan Viverette62c79e92015-02-26 09:47:10 -0800167 mAmPmLayout = mainView.findViewById(R.id.ampm_layout);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400168 mAmPmLayout.setOnTouchListener(new NearestTouchDelegate());
169
170 final String[] amPmStrings = TimePicker.getAmPmStrings(context);
171 mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700172 mAmLabel.setText(obtainVerbatim(amPmStrings[0]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700173 mAmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400174 ensureMinimumTextWidth(mAmLabel);
175
176 mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label);
Alan Viverettef63757b2015-04-01 17:14:45 -0700177 mPmLabel.setText(obtainVerbatim(amPmStrings[1]));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700178 mPmLabel.setOnClickListener(mClickListener);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400179 ensureMinimumTextWidth(mPmLabel);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700180
Alan Viverettef2525f62015-03-24 18:03:38 -0700181 // For the sake of backwards compatibility, attempt to extract the text
182 // color from the header time text appearance. If it's set, we'll let
183 // that override the "real" header text color.
184 ColorStateList headerTextColor = null;
185
186 @SuppressWarnings("deprecation")
187 final int timeHeaderTextAppearance = a.getResourceId(
188 R.styleable.TimePicker_headerTimeTextAppearance, 0);
189 if (timeHeaderTextAppearance != 0) {
190 final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
191 ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance);
192 final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
193 headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
194 textAppearance.recycle();
195 }
196
197 if (headerTextColor == null) {
198 headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor);
199 }
200
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800201 mTextInputPickerHeader = mainView.findViewById(R.id.input_header);
202
Alan Viverettef2525f62015-03-24 18:03:38 -0700203 if (headerTextColor != null) {
204 mHourView.setTextColor(headerTextColor);
205 mSeparatorView.setTextColor(headerTextColor);
206 mMinuteView.setTextColor(headerTextColor);
207 mAmLabel.setTextColor(headerTextColor);
208 mPmLabel.setTextColor(headerTextColor);
209 }
210
211 // Set up header background, if available.
212 if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) {
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800213 mRadialTimePickerHeader.setBackground(a.getDrawable(
214 R.styleable.TimePicker_headerBackground));
215 mTextInputPickerHeader.setBackground(a.getDrawable(
216 R.styleable.TimePicker_headerBackground));
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700217 }
218
Alan Viverette51344782014-07-16 17:39:27 -0700219 a.recycle();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700220
Alan Viverette2b4dc112015-10-02 15:29:43 -0400221 mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker);
222 mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes);
Alan Viverette2a993b42016-04-28 12:56:09 -0400223 mRadialTimePickerView.setOnValueSelectedListener(mOnValueSelectedListener);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700224
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800225 mTextInputPickerView = (TextInputTimePickerView) mainView.findViewById(R.id.input_mode);
226 mTextInputPickerView.setListener(mOnValueTypedListener);
227
228 mRadialTimePickerModeButton =
229 (ImageButton) mainView.findViewById(R.id.toggle_mode);
230 mRadialTimePickerModeButton.setOnClickListener(new View.OnClickListener() {
231 @Override
232 public void onClick(View v) {
233 toggleRadialPickerMode();
234 }
235 });
236 mRadialTimePickerModeEnabledDescription = context.getResources().getString(
237 R.string.time_picker_radial_mode_description);
238 mTextInputPickerModeEnabledDescription = context.getResources().getString(
239 R.string.time_picker_text_input_mode_description);
240
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700241 mAllowAutoAdvance = true;
242
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500243 updateHourFormat();
Alan Viveretteb3f24632015-10-22 16:01:48 -0400244
245 // Initialize with current time.
Alan Viverette4420ae82015-11-16 16:10:56 -0500246 mTempCalendar = Calendar.getInstance(mLocale);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400247 final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY);
248 final int currentMinute = mTempCalendar.get(Calendar.MINUTE);
Alan Viverette4420ae82015-11-16 16:10:56 -0500249 initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400250 }
251
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800252 private void toggleRadialPickerMode() {
253 if (mRadialPickerModeEnabled) {
254 mRadialTimePickerView.setVisibility(View.GONE);
255 mRadialTimePickerHeader.setVisibility(View.GONE);
256 mTextInputPickerHeader.setVisibility(View.VISIBLE);
257 mTextInputPickerView.setVisibility(View.VISIBLE);
Aurimas Liutikasc0aa90d2017-05-15 15:24:16 -0700258 mRadialTimePickerModeButton.setImageResource(R.drawable.btn_clock_material);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800259 mRadialTimePickerModeButton.setContentDescription(
260 mRadialTimePickerModeEnabledDescription);
261 mRadialPickerModeEnabled = false;
262 } else {
263 mRadialTimePickerView.setVisibility(View.VISIBLE);
264 mRadialTimePickerHeader.setVisibility(View.VISIBLE);
265 mTextInputPickerHeader.setVisibility(View.GONE);
266 mTextInputPickerView.setVisibility(View.GONE);
267 mRadialTimePickerModeButton.setImageResource(R.drawable.btn_keyboard_key_material);
268 mRadialTimePickerModeButton.setContentDescription(
269 mTextInputPickerModeEnabledDescription);
270 updateTextInputPicker();
Aurimas Liutikas73344412017-06-17 00:12:15 +0000271 InputMethodManager imm = InputMethodManager.peekInstance();
272 if (imm != null) {
273 imm.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
274 }
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800275 mRadialPickerModeEnabled = true;
276 }
277 }
278
279 @Override
280 public boolean validateInput() {
281 return mTextInputPickerView.validateInput();
282 }
283
Alan Viveretteb3f24632015-10-22 16:01:48 -0400284 /**
285 * Ensures that a TextView is wide enough to contain its text without
286 * wrapping or clipping. Measures the specified view and sets the minimum
287 * width to the view's desired width.
288 *
289 * @param v the text view to measure
290 */
291 private static void ensureMinimumTextWidth(TextView v) {
292 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
293
294 // Set both the TextView and the View version of minimum
295 // width because they are subtly different.
296 final int minWidth = v.getMeasuredWidth();
297 v.setMinWidth(minWidth);
298 v.setMinimumWidth(minWidth);
299 }
300
301 /**
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500302 * Updates hour formatting based on the current locale and 24-hour mode.
303 * <p>
304 * Determines how the hour should be formatted, sets member variables for
305 * leading zero and starting hour, and sets the hour view's presentation.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400306 */
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500307 private void updateHourFormat() {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400308 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500309 mLocale, mIs24Hour ? "Hm" : "hm");
Alan Viveretteb3f24632015-10-22 16:01:48 -0400310 final int lengthPattern = bestDateTimePattern.length();
311 boolean showLeadingZero = false;
312 char hourFormat = '\0';
313
314 for (int i = 0; i < lengthPattern; i++) {
315 final char c = bestDateTimePattern.charAt(i);
316 if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
317 hourFormat = c;
318 if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
319 showLeadingZero = true;
320 }
321 break;
322 }
323 }
324
325 mHourFormatShowLeadingZero = showLeadingZero;
326 mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H';
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500327
328 // Update hour text field.
329 final int minHour = mHourFormatStartsAtZero ? 0 : 1;
330 final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
331 mHourView.setRange(minHour, maxHour);
332 mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800333
334 final String[] digits = DecimalFormatSymbols.getInstance(mLocale).getDigitStrings();
335 int maxCharLength = 0;
336 for (int i = 0; i < 10; i++) {
337 maxCharLength = Math.max(maxCharLength, digits[i].length());
338 }
339 mTextInputPickerView.setHourFormat(maxCharLength * 2);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700340 }
341
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800342 static final CharSequence obtainVerbatim(String text) {
Alan Viverettef63757b2015-04-01 17:14:45 -0700343 return new SpannableStringBuilder().append(text,
344 new TtsSpan.VerbatimBuilder(text).build(), 0);
345 }
346
Alan Viverettef2525f62015-03-24 18:03:38 -0700347 /**
348 * The legacy text color might have been poorly defined. Ensures that it
349 * has an appropriate activated state, using the selected state if one
350 * exists or modifying the default text color otherwise.
351 *
352 * @param color a legacy text color, or {@code null}
353 * @return a color state list with an appropriate activated state, or
354 * {@code null} if a valid activated state could not be generated
355 */
356 @Nullable
357 private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
358 if (color == null || color.hasState(R.attr.state_activated)) {
359 return color;
360 }
361
362 final int activatedColor;
363 final int defaultColor;
364 if (color.hasState(R.attr.state_selected)) {
365 activatedColor = color.getColorForState(StateSet.get(
366 StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
367 defaultColor = color.getColorForState(StateSet.get(
368 StateSet.VIEW_STATE_ENABLED), 0);
369 } else {
370 activatedColor = color.getDefaultColor();
371
372 // Generate a non-activated color using the disabled alpha.
373 final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
374 final float disabledAlpha = ta.getFloat(0, 0.30f);
375 defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
376 }
377
378 if (activatedColor == 0 || defaultColor == 0) {
379 // We somehow failed to obtain the colors.
380 return null;
381 }
382
383 final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
384 final int[] colors = new int[] { activatedColor, defaultColor };
385 return new ColorStateList(stateSet, colors);
386 }
387
388 private int multiplyAlphaComponent(int color, float alphaMod) {
389 final int srcRgb = color & 0xFFFFFF;
390 final int srcAlpha = (color >> 24) & 0xFF;
391 final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f);
392 return srcRgb | (dstAlpha << 24);
393 }
394
Alan Viverette3fc00e312014-12-10 09:46:49 -0800395 private static class ClickActionDelegate extends AccessibilityDelegate {
396 private final AccessibilityAction mClickAction;
397
398 public ClickActionDelegate(Context context, int resId) {
399 mClickAction = new AccessibilityAction(
400 AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId));
401 }
402
403 @Override
404 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
405 super.onInitializeAccessibilityNodeInfo(host, info);
406
407 info.addAction(mClickAction);
408 }
409 }
410
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700411 private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400412 mCurrentHour = hourOfDay;
413 mCurrentMinute = minute;
Alan Viverette4420ae82015-11-16 16:10:56 -0500414 mIs24Hour = is24HourView;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700415 updateUI(index);
416 }
417
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700418 private void updateUI(int index) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700419 updateHeaderAmPm();
Alan Viverette2a993b42016-04-28 12:56:09 -0400420 updateHeaderHour(mCurrentHour, false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700421 updateHeaderSeparator();
Alan Viverette2a993b42016-04-28 12:56:09 -0400422 updateHeaderMinute(mCurrentMinute, false);
Alan Viveretteb3f24632015-10-22 16:01:48 -0400423 updateRadialPicker(index);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800424 updateTextInputPicker();
Alan Viveretteb3f24632015-10-22 16:01:48 -0400425
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700426 mDelegator.invalidate();
427 }
428
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800429 private void updateTextInputPicker() {
430 mTextInputPickerView.updateTextInputValues(getLocalizedHour(mCurrentHour), mCurrentMinute,
431 mCurrentHour < 12 ? AM : PM, mIs24Hour, mHourFormatStartsAtZero);
432 }
433
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700434 private void updateRadialPicker(int index) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400435 mRadialTimePickerView.initialize(mCurrentHour, mCurrentMinute, mIs24Hour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700436 setCurrentItemShowing(index, false, true);
437 }
438
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700439 private void updateHeaderAmPm() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500440 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700441 mAmPmLayout.setVisibility(View.GONE);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700442 } else {
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700443 // Find the location of AM/PM based on locale information.
444 final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm");
445 final boolean isAmPmAtStart = dateTimePattern.startsWith("a");
446 setAmPmStart(isAmPmAtStart);
Alan Viverette2a993b42016-04-28 12:56:09 -0400447 updateAmPmLabelStates(mCurrentHour < 12 ? AM : PM);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700448 }
449 }
450
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700451 private void setAmPmStart(boolean isAmPmAtStart) {
452 final RelativeLayout.LayoutParams params =
453 (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams();
454 if (params.getRule(RelativeLayout.RIGHT_OF) != 0
455 || params.getRule(RelativeLayout.LEFT_OF) != 0) {
456 // Horizontal mode, with AM/PM appearing to left/right of hours and minutes.
457 final boolean isAmPmAtLeft;
458 if (TextUtils.getLayoutDirectionFromLocale(mLocale) == View.LAYOUT_DIRECTION_LTR) {
459 isAmPmAtLeft = isAmPmAtStart;
460 } else {
461 isAmPmAtLeft = !isAmPmAtStart;
462 }
463 if (mIsAmPmAtLeft == isAmPmAtLeft) {
464 // AM/PM is already at the correct location. No change needed.
465 return;
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800466 }
467
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700468 if (isAmPmAtLeft) {
469 params.removeRule(RelativeLayout.RIGHT_OF);
470 params.addRule(RelativeLayout.LEFT_OF, mHourView.getId());
471 } else {
472 params.removeRule(RelativeLayout.LEFT_OF);
473 params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId());
474 }
475 mIsAmPmAtLeft = isAmPmAtLeft;
476 } else if (params.getRule(RelativeLayout.BELOW) != 0
477 || params.getRule(RelativeLayout.ABOVE) != 0) {
478 // Vertical mode, with AM/PM appearing to top/bottom of hours and minutes.
479 if (mIsAmPmAtTop == isAmPmAtStart) {
480 // AM/PM is already at the correct location. No change needed.
481 return;
482 }
483
484 final int otherViewId;
485 if (isAmPmAtStart) {
486 otherViewId = params.getRule(RelativeLayout.BELOW);
487 params.removeRule(RelativeLayout.BELOW);
488 params.addRule(RelativeLayout.ABOVE, otherViewId);
489 } else {
490 otherViewId = params.getRule(RelativeLayout.ABOVE);
491 params.removeRule(RelativeLayout.ABOVE);
492 params.addRule(RelativeLayout.BELOW, otherViewId);
493 }
494
495 // Switch the top and bottom paddings on the other view.
496 final View otherView = mRadialTimePickerHeader.findViewById(otherViewId);
497 final int top = otherView.getPaddingTop();
498 final int bottom = otherView.getPaddingBottom();
499 final int left = otherView.getPaddingLeft();
500 final int right = otherView.getPaddingRight();
501 otherView.setPadding(left, bottom, right, top);
502
503 mIsAmPmAtTop = isAmPmAtStart;
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800504 }
Roozbeh Pournader6791c7b2017-07-05 18:09:41 -0700505
506 mAmPmLayout.setLayoutParams(params);
Alan Viveretteadbc95f2015-02-20 10:51:33 -0800507 }
508
Felipe Lemef480e8c2017-08-10 18:38:44 -0700509 @Override
510 public void setDate(int hour, int minute) {
511 setHourInternal(hour, FROM_EXTERNAL_API, true, false);
512 setMinuteInternal(minute, FROM_EXTERNAL_API, false);
513
514 onTimeChanged();
515 }
516
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700517 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700518 * Set the current hour.
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700519 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700520 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500521 public void setHour(int hour) {
Felipe Lemef480e8c2017-08-10 18:38:44 -0700522 setHourInternal(hour, FROM_EXTERNAL_API, true, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400523 }
524
Felipe Lemef480e8c2017-08-10 18:38:44 -0700525 private void setHourInternal(int hour, @ChangeSource int source, boolean announce,
526 boolean notify) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400527 if (mCurrentHour == hour) {
528 return;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700529 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400530
Felipe Lemef480e8c2017-08-10 18:38:44 -0700531 resetAutofilledValue();
Alan Viverette2a993b42016-04-28 12:56:09 -0400532 mCurrentHour = hour;
533 updateHeaderHour(hour, announce);
534 updateHeaderAmPm();
535
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800536 if (source != FROM_RADIAL_PICKER) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400537 mRadialTimePickerView.setCurrentHour(hour);
538 mRadialTimePickerView.setAmOrPm(hour < 12 ? AM : PM);
539 }
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800540 if (source != FROM_INPUT_PICKER) {
541 updateTextInputPicker();
542 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400543
544 mDelegator.invalidate();
Felipe Lemef480e8c2017-08-10 18:38:44 -0700545 if (notify) {
546 onTimeChanged();
547 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700548 }
549
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700550 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500551 * @return the current hour in the range (0-23)
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700552 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700553 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500554 public int getHour() {
555 final int currentHour = mRadialTimePickerView.getCurrentHour();
556 if (mIs24Hour) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700557 return currentHour;
Alan Viverette4420ae82015-11-16 16:10:56 -0500558 }
559
560 if (mRadialTimePickerView.getAmOrPm() == PM) {
561 return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700562 } else {
Alan Viverette4420ae82015-11-16 16:10:56 -0500563 return currentHour % HOURS_IN_HALF_DAY;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700564 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700565 }
566
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700567 /**
568 * Set the current minute (0-59).
569 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700570 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500571 public void setMinute(int minute) {
Felipe Lemef480e8c2017-08-10 18:38:44 -0700572 setMinuteInternal(minute, FROM_EXTERNAL_API, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400573 }
574
Felipe Lemef480e8c2017-08-10 18:38:44 -0700575 private void setMinuteInternal(int minute, @ChangeSource int source, boolean notify) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400576 if (mCurrentMinute == minute) {
577 return;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700578 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400579
Felipe Lemef480e8c2017-08-10 18:38:44 -0700580 resetAutofilledValue();
Alan Viverette2a993b42016-04-28 12:56:09 -0400581 mCurrentMinute = minute;
582 updateHeaderMinute(minute, true);
583
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800584 if (source != FROM_RADIAL_PICKER) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400585 mRadialTimePickerView.setCurrentMinute(minute);
586 }
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800587 if (source != FROM_INPUT_PICKER) {
588 updateTextInputPicker();
589 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400590
591 mDelegator.invalidate();
Felipe Lemef480e8c2017-08-10 18:38:44 -0700592 if (notify) {
593 onTimeChanged();
594 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700595 }
596
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700597 /**
598 * @return The current minute.
599 */
600 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500601 public int getMinute() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700602 return mRadialTimePickerView.getCurrentMinute();
603 }
604
605 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500606 * Sets whether time is displayed in 24-hour mode or 12-hour mode with
607 * AM/PM indicators.
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700608 *
Alan Viverette4420ae82015-11-16 16:10:56 -0500609 * @param is24Hour {@code true} to display time in 24-hour mode or
610 * {@code false} for 12-hour mode with AM/PM
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700611 */
Alan Viverette4420ae82015-11-16 16:10:56 -0500612 public void setIs24Hour(boolean is24Hour) {
613 if (mIs24Hour != is24Hour) {
614 mIs24Hour = is24Hour;
Alan Viverette2a993b42016-04-28 12:56:09 -0400615 mCurrentHour = getHour();
Alan Viverette4420ae82015-11-16 16:10:56 -0500616
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500617 updateHourFormat();
Alan Viverette4420ae82015-11-16 16:10:56 -0500618 updateUI(mRadialTimePickerView.getCurrentItemShowing());
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700619 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700620 }
621
622 /**
Alan Viverette4420ae82015-11-16 16:10:56 -0500623 * @return {@code true} if time is displayed in 24-hour mode, or
624 * {@code false} if time is displayed in 12-hour mode with AM/PM
625 * indicators
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700626 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700627 @Override
Alan Viverette4420ae82015-11-16 16:10:56 -0500628 public boolean is24Hour() {
629 return mIs24Hour;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700630 }
631
632 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700633 public void setEnabled(boolean enabled) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700634 mHourView.setEnabled(enabled);
635 mMinuteView.setEnabled(enabled);
636 mAmLabel.setEnabled(enabled);
637 mPmLabel.setEnabled(enabled);
638 mRadialTimePickerView.setEnabled(enabled);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700639 mIsEnabled = enabled;
640 }
641
642 @Override
643 public boolean isEnabled() {
644 return mIsEnabled;
645 }
646
647 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700648 public int getBaseline() {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700649 // does not support baseline alignment
650 return -1;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700651 }
652
653 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700654 public Parcelable onSaveInstanceState(Parcelable superState) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500655 return new SavedState(superState, getHour(), getMinute(),
656 is24Hour(), getCurrentItemShowing());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700657 }
658
659 @Override
660 public void onRestoreInstanceState(Parcelable state) {
Alan Viverette6b3f85f2016-03-01 16:48:04 -0500661 if (state instanceof SavedState) {
662 final SavedState ss = (SavedState) state;
663 initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing());
664 mRadialTimePickerView.invalidate();
665 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700666 }
667
668 @Override
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700669 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
670 onPopulateAccessibilityEvent(event);
671 return true;
672 }
673
674 @Override
675 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
676 int flags = DateUtils.FORMAT_SHOW_TIME;
Alan Viverette4420ae82015-11-16 16:10:56 -0500677 if (mIs24Hour) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700678 flags |= DateUtils.FORMAT_24HOUR;
679 } else {
680 flags |= DateUtils.FORMAT_12HOUR;
681 }
Alan Viveretteb0f54612016-04-12 14:58:09 -0400682
Alan Viverette4420ae82015-11-16 16:10:56 -0500683 mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour());
684 mTempCalendar.set(Calendar.MINUTE, getMinute());
Alan Viveretteb0f54612016-04-12 14:58:09 -0400685
686 final String selectedTime = DateUtils.formatDateTime(mContext,
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700687 mTempCalendar.getTimeInMillis(), flags);
Alan Viveretteb0f54612016-04-12 14:58:09 -0400688 final String selectionMode = mRadialTimePickerView.getCurrentItemShowing() == HOUR_INDEX ?
689 mSelectHours : mSelectMinutes;
690 event.getText().add(selectedTime + " " + selectionMode);
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700691 }
692
Andrei Stingaceanuf87b0e12016-07-04 17:40:14 +0100693 /** @hide */
694 @Override
695 @TestApi
696 public View getHourView() {
697 return mHourView;
698 }
699
700 /** @hide */
701 @Override
702 @TestApi
703 public View getMinuteView() {
704 return mMinuteView;
705 }
706
707 /** @hide */
708 @Override
709 @TestApi
710 public View getAmView() {
711 return mAmLabel;
712 }
713
714 /** @hide */
715 @Override
716 @TestApi
717 public View getPmView() {
718 return mPmLabel;
719 }
720
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700721 /**
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700722 * @return the index of the current item showing
723 */
724 private int getCurrentItemShowing() {
725 return mRadialTimePickerView.getCurrentItemShowing();
726 }
727
728 /**
729 * Propagate the time change
730 */
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700731 private void onTimeChanged() {
732 mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
733 if (mOnTimeChangedListener != null) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500734 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700735 }
Felipe Leme305b72c2017-02-27 12:46:04 -0800736 if (mAutoFillChangeListener != null) {
737 mAutoFillChangeListener.onTimeChanged(mDelegator, getHour(), getMinute());
738 }
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700739 }
740
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700741 private void tryVibrate() {
742 mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
Elliott Hughes1cc51a62014-08-21 16:21:30 -0700743 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700744
745 private void updateAmPmLabelStates(int amOrPm) {
746 final boolean isAm = amOrPm == AM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700747 mAmLabel.setActivated(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700748 mAmLabel.setChecked(isAm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700749
750 final boolean isPm = amOrPm == PM;
Alan Viverettef2525f62015-03-24 18:03:38 -0700751 mPmLabel.setActivated(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700752 mPmLabel.setChecked(isPm);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700753 }
754
755 /**
Alan Viveretteb3f24632015-10-22 16:01:48 -0400756 * Converts hour-of-day (0-23) time into a localized hour number.
Alan Viverette3b7e2b92015-11-16 16:38:38 -0500757 * <p>
758 * The localized value may be in the range (0-23), (1-24), (0-11), or
759 * (1-12) depending on the locale. This method does not handle leading
760 * zeroes.
Alan Viveretteb3f24632015-10-22 16:01:48 -0400761 *
762 * @param hourOfDay the hour-of-day (0-23)
763 * @return a localized hour number
764 */
765 private int getLocalizedHour(int hourOfDay) {
Alan Viverette4420ae82015-11-16 16:10:56 -0500766 if (!mIs24Hour) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400767 // Convert to hour-of-am-pm.
768 hourOfDay %= 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700769 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400770
771 if (!mHourFormatStartsAtZero && hourOfDay == 0) {
772 // Convert to clock-hour (either of-day or of-am-pm).
Alan Viverette4420ae82015-11-16 16:10:56 -0500773 hourOfDay = mIs24Hour ? 24 : 12;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700774 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400775
776 return hourOfDay;
777 }
778
779 private void updateHeaderHour(int hourOfDay, boolean announce) {
780 final int localizedHour = getLocalizedHour(hourOfDay);
781 mHourView.setValue(localizedHour);
782
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700783 if (announce) {
Alan Viveretteb3f24632015-10-22 16:01:48 -0400784 tryAnnounceForAccessibility(mHourView.getText(), true);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700785 }
786 }
787
Alan Viveretteb3f24632015-10-22 16:01:48 -0400788 private void updateHeaderMinute(int minuteOfHour, boolean announce) {
789 mMinuteView.setValue(minuteOfHour);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700790
Alan Viveretteb3f24632015-10-22 16:01:48 -0400791 if (announce) {
792 tryAnnounceForAccessibility(mMinuteView.getText(), false);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700793 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700794 }
795
796 /**
797 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
798 *
799 * See http://unicode.org/cldr/trac/browser/trunk/common/main
800 *
801 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
802 * separator as the character which is just after the hour marker in the returned pattern.
803 */
804 private void updateHeaderSeparator() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500805 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
806 (mIs24Hour) ? "Hm" : "hm");
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700807 final String separatorText;
808 // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats
809 final char[] hourFormats = {'H', 'h', 'K', 'k'};
810 int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats);
811 if (hIndex == -1) {
812 // Default case
813 separatorText = ":";
814 } else {
815 separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1));
816 }
817 mSeparatorView.setText(separatorText);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800818 mTextInputPickerView.updateSeparator(separatorText);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700819 }
820
821 static private int lastIndexOfAny(String str, char[] any) {
822 final int lengthAny = any.length;
823 if (lengthAny > 0) {
824 for (int i = str.length() - 1; i >= 0; i--) {
825 char c = str.charAt(i);
826 for (int j = 0; j < lengthAny; j++) {
827 if (c == any[j]) {
828 return i;
829 }
830 }
831 }
832 }
833 return -1;
834 }
835
Alan Viveretteb3f24632015-10-22 16:01:48 -0400836 private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) {
837 if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) {
838 // TODO: Find a better solution, potentially live regions?
839 mDelegator.announceForAccessibility(text);
840 mLastAnnouncedText = text;
841 mLastAnnouncedIsHour = isHour;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700842 }
843 }
844
845 /**
846 * Show either Hours or Minutes.
847 */
848 private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) {
849 mRadialTimePickerView.setCurrentItemShowing(index, animateCircle);
850
851 if (index == HOUR_INDEX) {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700852 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700853 mDelegator.announceForAccessibility(mSelectHours);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700854 }
855 } else {
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700856 if (announce) {
Alan Viveretteffb46bf2014-10-24 12:06:11 -0700857 mDelegator.announceForAccessibility(mSelectMinutes);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700858 }
859 }
860
Alan Viverettef2525f62015-03-24 18:03:38 -0700861 mHourView.setActivated(index == HOUR_INDEX);
862 mMinuteView.setActivated(index == MINUTE_INDEX);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700863 }
864
865 private void setAmOrPm(int amOrPm) {
866 updateAmPmLabelStates(amOrPm);
Alan Viverette30b57b62016-04-19 09:29:20 -0400867
Alan Viverette2a993b42016-04-28 12:56:09 -0400868 if (mRadialTimePickerView.setAmOrPm(amOrPm)) {
869 mCurrentHour = getHour();
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800870 updateTextInputPicker();
Alan Viverette2a993b42016-04-28 12:56:09 -0400871 if (mOnTimeChangedListener != null) {
872 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
873 }
Alan Viverette30b57b62016-04-19 09:29:20 -0400874 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700875 }
876
Alan Viverette2a993b42016-04-28 12:56:09 -0400877 /** Listener for RadialTimePickerView interaction. */
878 private final OnValueSelectedListener mOnValueSelectedListener = new OnValueSelectedListener() {
879 @Override
Alan Viverette66a85622016-08-04 13:24:14 -0400880 public void onValueSelected(int pickerType, int newValue, boolean autoAdvance) {
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800881 boolean valueChanged = false;
Alan Viverette66a85622016-08-04 13:24:14 -0400882 switch (pickerType) {
883 case RadialTimePickerView.HOURS:
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800884 if (getHour() != newValue) {
885 valueChanged = true;
886 }
Alan Viverette2a993b42016-04-28 12:56:09 -0400887 final boolean isTransition = mAllowAutoAdvance && autoAdvance;
Felipe Lemef480e8c2017-08-10 18:38:44 -0700888 setHourInternal(newValue, FROM_RADIAL_PICKER, !isTransition, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400889 if (isTransition) {
890 setCurrentItemShowing(MINUTE_INDEX, true, false);
Alan Viverette66a85622016-08-04 13:24:14 -0400891
892 final int localizedHour = getLocalizedHour(newValue);
893 mDelegator.announceForAccessibility(localizedHour + ". " + mSelectMinutes);
Alan Viverette2a993b42016-04-28 12:56:09 -0400894 }
895 break;
Alan Viverette66a85622016-08-04 13:24:14 -0400896 case RadialTimePickerView.MINUTES:
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800897 if (getMinute() != newValue) {
898 valueChanged = true;
899 }
Felipe Lemef480e8c2017-08-10 18:38:44 -0700900 setMinuteInternal(newValue, FROM_RADIAL_PICKER, true);
Alan Viverette2a993b42016-04-28 12:56:09 -0400901 break;
Alan Viverette2a993b42016-04-28 12:56:09 -0400902 }
903
Aurimas Liutikas2f16bc82017-02-28 12:16:57 -0800904 if (mOnTimeChangedListener != null && valueChanged) {
Alan Viverette2a993b42016-04-28 12:56:09 -0400905 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute());
906 }
907 }
908 };
909
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800910 private final OnValueTypedListener mOnValueTypedListener = new OnValueTypedListener() {
911 @Override
912 public void onValueChanged(int pickerType, int newValue) {
913 switch (pickerType) {
914 case TextInputTimePickerView.HOURS:
Felipe Lemef480e8c2017-08-10 18:38:44 -0700915 setHourInternal(newValue, FROM_INPUT_PICKER, false, true);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800916 break;
917 case TextInputTimePickerView.MINUTES:
Felipe Lemef480e8c2017-08-10 18:38:44 -0700918 setMinuteInternal(newValue, FROM_INPUT_PICKER, true);
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800919 break;
920 case TextInputTimePickerView.AMPM:
921 setAmOrPm(newValue);
922 break;
923 }
924 }
925 };
926
Alan Viverette2a993b42016-04-28 12:56:09 -0400927 /** Listener for keyboard interaction. */
Alan Viveretteb3f24632015-10-22 16:01:48 -0400928 private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() {
929 @Override
930 public void onValueChanged(NumericTextView view, int value,
931 boolean isValid, boolean isFinished) {
932 final Runnable commitCallback;
933 final View nextFocusTarget;
934 if (view == mHourView) {
935 commitCallback = mCommitHour;
936 nextFocusTarget = view.isFocused() ? mMinuteView : null;
937 } else if (view == mMinuteView) {
938 commitCallback = mCommitMinute;
939 nextFocusTarget = null;
940 } else {
941 return;
942 }
943
944 view.removeCallbacks(commitCallback);
945
946 if (isValid) {
947 if (isFinished) {
948 // Done with hours entry, make visual updates
949 // immediately and move to next focus if needed.
950 commitCallback.run();
951
952 if (nextFocusTarget != null) {
953 nextFocusTarget.requestFocus();
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700954 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400955 } else {
956 // May still be making changes. Postpone visual
957 // updates to prevent distracting the user.
958 view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS);
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700959 }
960 }
961 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400962 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700963
Alan Viveretteb3f24632015-10-22 16:01:48 -0400964 private final Runnable mCommitHour = new Runnable() {
965 @Override
966 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500967 setHour(mHourView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400968 }
969 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700970
Alan Viveretteb3f24632015-10-22 16:01:48 -0400971 private final Runnable mCommitMinute = new Runnable() {
972 @Override
973 public void run() {
Alan Viverette4420ae82015-11-16 16:10:56 -0500974 setMinute(mMinuteView.getValue());
Alan Viveretteb3f24632015-10-22 16:01:48 -0400975 }
976 };
977
978 private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() {
979 @Override
980 public void onFocusChange(View v, boolean focused) {
981 if (focused) {
982 switch (v.getId()) {
983 case R.id.am_label:
984 setAmOrPm(AM);
985 break;
986 case R.id.pm_label:
987 setAmOrPm(PM);
988 break;
989 case R.id.hours:
990 setCurrentItemShowing(HOUR_INDEX, true, true);
991 break;
992 case R.id.minutes:
993 setCurrentItemShowing(MINUTE_INDEX, true, true);
994 break;
995 default:
996 // Failed to handle this click, don't vibrate.
997 return;
Alan Viverettedaf33ed2014-10-23 13:34:17 -0700998 }
Alan Viveretteb3f24632015-10-22 16:01:48 -0400999
1000 tryVibrate();
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001001 }
1002 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001003 };
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001004
1005 private final View.OnClickListener mClickListener = new View.OnClickListener() {
1006 @Override
1007 public void onClick(View v) {
1008
1009 final int amOrPm;
1010 switch (v.getId()) {
1011 case R.id.am_label:
1012 setAmOrPm(AM);
1013 break;
1014 case R.id.pm_label:
1015 setAmOrPm(PM);
1016 break;
1017 case R.id.hours:
1018 setCurrentItemShowing(HOUR_INDEX, true, true);
1019 break;
1020 case R.id.minutes:
1021 setCurrentItemShowing(MINUTE_INDEX, true, true);
1022 break;
1023 default:
1024 // Failed to handle this click, don't vibrate.
1025 return;
1026 }
1027
1028 tryVibrate();
1029 }
1030 };
1031
Alan Viveretteb3f24632015-10-22 16:01:48 -04001032 /**
1033 * Delegates unhandled touches in a view group to the nearest child view.
1034 */
1035 private static class NearestTouchDelegate implements View.OnTouchListener {
1036 private View mInitialTouchTarget;
1037
1038 @Override
1039 public boolean onTouch(View view, MotionEvent motionEvent) {
1040 final int actionMasked = motionEvent.getActionMasked();
1041 if (actionMasked == MotionEvent.ACTION_DOWN) {
Alan Viverette7add7e02015-11-20 14:19:39 -05001042 if (view instanceof ViewGroup) {
1043 mInitialTouchTarget = findNearestChild((ViewGroup) view,
1044 (int) motionEvent.getX(), (int) motionEvent.getY());
1045 } else {
1046 mInitialTouchTarget = null;
1047 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001048 }
1049
1050 final View child = mInitialTouchTarget;
1051 if (child == null) {
1052 return false;
1053 }
1054
1055 final float offsetX = view.getScrollX() - child.getLeft();
1056 final float offsetY = view.getScrollY() - child.getTop();
1057 motionEvent.offsetLocation(offsetX, offsetY);
1058 final boolean handled = child.dispatchTouchEvent(motionEvent);
1059 motionEvent.offsetLocation(-offsetX, -offsetY);
1060
1061 if (actionMasked == MotionEvent.ACTION_UP
1062 || actionMasked == MotionEvent.ACTION_CANCEL) {
1063 mInitialTouchTarget = null;
1064 }
1065
1066 return handled;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001067 }
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001068
Alan Viveretteb3f24632015-10-22 16:01:48 -04001069 private View findNearestChild(ViewGroup v, int x, int y) {
1070 View bestChild = null;
1071 int bestDist = Integer.MAX_VALUE;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001072
Alan Viveretteb3f24632015-10-22 16:01:48 -04001073 for (int i = 0, count = v.getChildCount(); i < count; i++) {
1074 final View child = v.getChildAt(i);
1075 final int dX = x - (child.getLeft() + child.getWidth() / 2);
1076 final int dY = y - (child.getTop() + child.getHeight() / 2);
1077 final int dist = dX * dX + dY * dY;
1078 if (bestDist > dist) {
1079 bestChild = child;
1080 bestDist = dist;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001081 }
1082 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001083
1084 return bestChild;
Alan Viverettedaf33ed2014-10-23 13:34:17 -07001085 }
Alan Viveretteb3f24632015-10-22 16:01:48 -04001086 }
Elliott Hughes1cc51a62014-08-21 16:21:30 -07001087}