Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 23 | import android.content.res.ColorStateList; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 25 | import android.content.res.TypedArray; |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 26 | import android.icu.text.DecimalFormatSymbols; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 27 | import android.os.Parcelable; |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 28 | import android.text.SpannableStringBuilder; |
Roozbeh Pournader | 01bcf1e | 2017-06-29 14:48:35 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 30 | import android.text.format.DateFormat; |
| 31 | import android.text.format.DateUtils; |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 32 | import android.text.style.TtsSpan; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 33 | import android.util.AttributeSet; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 34 | import android.util.StateSet; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 35 | import android.view.HapticFeedbackConstants; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 36 | import android.view.LayoutInflater; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 37 | import android.view.MotionEvent; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 38 | import android.view.View; |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 39 | import android.view.View.AccessibilityDelegate; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 40 | import android.view.View.MeasureSpec; |
| 41 | import android.view.ViewGroup; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityEvent; |
| 43 | import android.view.accessibility.AccessibilityNodeInfo; |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 44 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
Aurimas Liutikas | 7334441 | 2017-06-17 00:12:15 +0000 | [diff] [blame] | 45 | import android.view.inputmethod.InputMethodManager; |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 46 | import android.widget.RadialTimePickerView.OnValueSelectedListener; |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 47 | import android.widget.TextInputTimePickerView.OnValueTypedListener; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 48 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 49 | import com.android.internal.R; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 50 | import com.android.internal.widget.NumericTextView; |
| 51 | import com.android.internal.widget.NumericTextView.OnValueChangedListener; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 52 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 53 | import java.lang.annotation.Retention; |
| 54 | import java.lang.annotation.RetentionPolicy; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 55 | import java.util.Calendar; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 56 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 57 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 58 | * A delegate implementing the radial clock-based TimePicker. |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 59 | */ |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 60 | class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 61 | /** |
| 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 67 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 68 | @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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 75 | // Index used by RadialPickerLayout |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 76 | private static final int HOUR_INDEX = RadialTimePickerView.HOURS; |
| 77 | private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 78 | |
Alan Viverette | f86bbd0 | 2015-09-16 14:19:21 -0400 | [diff] [blame] | 79 | 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 Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 81 | |
Deepanshu Gupta | 491523d | 2015-10-06 17:56:37 -0700 | [diff] [blame] | 82 | private static final int AM = 0; |
| 83 | private static final int PM = 1; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 84 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 85 | private static final int HOURS_IN_HALF_DAY = 12; |
| 86 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 87 | private final NumericTextView mHourView; |
| 88 | private final NumericTextView mMinuteView; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 89 | private final View mAmPmLayout; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 90 | private final RadioButton mAmLabel; |
| 91 | private final RadioButton mPmLabel; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 92 | private final RadialTimePickerView mRadialTimePickerView; |
| 93 | private final TextView mSeparatorView; |
| 94 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 95 | 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 Viverette | 68016a6 | 2015-11-19 17:10:54 -0500 | [diff] [blame] | 104 | private final Calendar mTempCalendar; |
| 105 | |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 106 | // Accessibility strings. |
| 107 | private final String mSelectHours; |
| 108 | private final String mSelectMinutes; |
| 109 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 110 | private boolean mIsEnabled = true; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 111 | private boolean mAllowAutoAdvance; |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 112 | private int mCurrentHour; |
| 113 | private int mCurrentMinute; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 114 | private boolean mIs24Hour; |
Roozbeh Pournader | 6791c7b | 2017-07-05 18:09:41 -0700 | [diff] [blame] | 115 | |
| 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 Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 120 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 121 | // Localization data. |
| 122 | private boolean mHourFormatShowLeadingZero; |
| 123 | private boolean mHourFormatStartsAtZero; |
| 124 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 125 | // Most recent time announcement values for accessibility. |
| 126 | private CharSequence mLastAnnouncedText; |
| 127 | private boolean mLastAnnouncedIsHour; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 128 | |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 129 | public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs, |
| 130 | int defStyleAttr, int defStyleRes) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 131 | super(delegator, context); |
| 132 | |
| 133 | // process style attributes |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 134 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 140 | mSelectHours = res.getString(R.string.select_hours); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 141 | mSelectMinutes = res.getString(R.string.select_minutes); |
| 142 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 143 | final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout, |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 144 | R.layout.time_picker_material); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 145 | final View mainView = inflater.inflate(layoutResourceId, delegator); |
Adam Powell | 43da25c | 2017-05-23 15:56:59 -0700 | [diff] [blame] | 146 | mainView.setSaveFromParentEnabled(false); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 147 | mRadialTimePickerHeader = mainView.findViewById(R.id.time_header); |
| 148 | mRadialTimePickerHeader.setOnTouchListener(new NearestTouchDelegate()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 149 | |
| 150 | // Set up hour/minute labels. |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 151 | mHourView = (NumericTextView) mainView.findViewById(R.id.hours); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 152 | mHourView.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 153 | mHourView.setOnFocusChangeListener(mFocusListener); |
| 154 | mHourView.setOnDigitEnteredListener(mDigitEnteredListener); |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 155 | mHourView.setAccessibilityDelegate( |
| 156 | new ClickActionDelegate(context, R.string.select_hours)); |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 157 | mSeparatorView = (TextView) mainView.findViewById(R.id.separator); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 158 | mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 159 | mMinuteView.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 160 | mMinuteView.setOnFocusChangeListener(mFocusListener); |
| 161 | mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener); |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 162 | mMinuteView.setAccessibilityDelegate( |
| 163 | new ClickActionDelegate(context, R.string.select_minutes)); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 164 | mMinuteView.setRange(0, 59); |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 165 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 166 | // Set up AM/PM labels. |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 167 | mAmPmLayout = mainView.findViewById(R.id.ampm_layout); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 168 | mAmPmLayout.setOnTouchListener(new NearestTouchDelegate()); |
| 169 | |
| 170 | final String[] amPmStrings = TimePicker.getAmPmStrings(context); |
| 171 | mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label); |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 172 | mAmLabel.setText(obtainVerbatim(amPmStrings[0])); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 173 | mAmLabel.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 174 | ensureMinimumTextWidth(mAmLabel); |
| 175 | |
| 176 | mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label); |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 177 | mPmLabel.setText(obtainVerbatim(amPmStrings[1])); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 178 | mPmLabel.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 179 | ensureMinimumTextWidth(mPmLabel); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 180 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 181 | // 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 Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 201 | mTextInputPickerHeader = mainView.findViewById(R.id.input_header); |
| 202 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 203 | 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 Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 213 | mRadialTimePickerHeader.setBackground(a.getDrawable( |
| 214 | R.styleable.TimePicker_headerBackground)); |
| 215 | mTextInputPickerHeader.setBackground(a.getDrawable( |
| 216 | R.styleable.TimePicker_headerBackground)); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 219 | a.recycle(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 220 | |
Alan Viverette | 2b4dc11 | 2015-10-02 15:29:43 -0400 | [diff] [blame] | 221 | mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker); |
| 222 | mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 223 | mRadialTimePickerView.setOnValueSelectedListener(mOnValueSelectedListener); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 224 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 225 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 241 | mAllowAutoAdvance = true; |
| 242 | |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 243 | updateHourFormat(); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 244 | |
| 245 | // Initialize with current time. |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 246 | mTempCalendar = Calendar.getInstance(mLocale); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 247 | final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY); |
| 248 | final int currentMinute = mTempCalendar.get(Calendar.MINUTE); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 249 | initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 252 | 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 Liutikas | c0aa90d | 2017-05-15 15:24:16 -0700 | [diff] [blame] | 258 | mRadialTimePickerModeButton.setImageResource(R.drawable.btn_clock_material); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 259 | 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 Liutikas | 7334441 | 2017-06-17 00:12:15 +0000 | [diff] [blame] | 271 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 272 | if (imm != null) { |
| 273 | imm.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0); |
| 274 | } |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 275 | mRadialPickerModeEnabled = true; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean validateInput() { |
| 281 | return mTextInputPickerView.validateInput(); |
| 282 | } |
| 283 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 284 | /** |
| 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 Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 302 | * 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 Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 306 | */ |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 307 | private void updateHourFormat() { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 308 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern( |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 309 | mLocale, mIs24Hour ? "Hm" : "hm"); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 310 | 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 Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 327 | |
| 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 Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 333 | |
| 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 342 | static final CharSequence obtainVerbatim(String text) { |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 343 | return new SpannableStringBuilder().append(text, |
| 344 | new TtsSpan.VerbatimBuilder(text).build(), 0); |
| 345 | } |
| 346 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 347 | /** |
| 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 Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 395 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 411 | private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 412 | mCurrentHour = hourOfDay; |
| 413 | mCurrentMinute = minute; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 414 | mIs24Hour = is24HourView; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 415 | updateUI(index); |
| 416 | } |
| 417 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 418 | private void updateUI(int index) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 419 | updateHeaderAmPm(); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 420 | updateHeaderHour(mCurrentHour, false); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 421 | updateHeaderSeparator(); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 422 | updateHeaderMinute(mCurrentMinute, false); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 423 | updateRadialPicker(index); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 424 | updateTextInputPicker(); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 425 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 426 | mDelegator.invalidate(); |
| 427 | } |
| 428 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 429 | private void updateTextInputPicker() { |
| 430 | mTextInputPickerView.updateTextInputValues(getLocalizedHour(mCurrentHour), mCurrentMinute, |
| 431 | mCurrentHour < 12 ? AM : PM, mIs24Hour, mHourFormatStartsAtZero); |
| 432 | } |
| 433 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 434 | private void updateRadialPicker(int index) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 435 | mRadialTimePickerView.initialize(mCurrentHour, mCurrentMinute, mIs24Hour); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 436 | setCurrentItemShowing(index, false, true); |
| 437 | } |
| 438 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 439 | private void updateHeaderAmPm() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 440 | if (mIs24Hour) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 441 | mAmPmLayout.setVisibility(View.GONE); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 442 | } else { |
Roozbeh Pournader | 6791c7b | 2017-07-05 18:09:41 -0700 | [diff] [blame] | 443 | // 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 Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 447 | updateAmPmLabelStates(mCurrentHour < 12 ? AM : PM); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Roozbeh Pournader | 6791c7b | 2017-07-05 18:09:41 -0700 | [diff] [blame] | 451 | 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 Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Roozbeh Pournader | 6791c7b | 2017-07-05 18:09:41 -0700 | [diff] [blame] | 468 | 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 Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 504 | } |
Roozbeh Pournader | 6791c7b | 2017-07-05 18:09:41 -0700 | [diff] [blame] | 505 | |
| 506 | mAmPmLayout.setLayoutParams(params); |
Alan Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 509 | @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 Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 517 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 518 | * Set the current hour. |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 519 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 520 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 521 | public void setHour(int hour) { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 522 | setHourInternal(hour, FROM_EXTERNAL_API, true, true); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 523 | } |
| 524 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 525 | private void setHourInternal(int hour, @ChangeSource int source, boolean announce, |
| 526 | boolean notify) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 527 | if (mCurrentHour == hour) { |
| 528 | return; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 529 | } |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 530 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 531 | resetAutofilledValue(); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 532 | mCurrentHour = hour; |
| 533 | updateHeaderHour(hour, announce); |
| 534 | updateHeaderAmPm(); |
| 535 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 536 | if (source != FROM_RADIAL_PICKER) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 537 | mRadialTimePickerView.setCurrentHour(hour); |
| 538 | mRadialTimePickerView.setAmOrPm(hour < 12 ? AM : PM); |
| 539 | } |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 540 | if (source != FROM_INPUT_PICKER) { |
| 541 | updateTextInputPicker(); |
| 542 | } |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 543 | |
| 544 | mDelegator.invalidate(); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 545 | if (notify) { |
| 546 | onTimeChanged(); |
| 547 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 550 | /** |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 551 | * @return the current hour in the range (0-23) |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 552 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 553 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 554 | public int getHour() { |
| 555 | final int currentHour = mRadialTimePickerView.getCurrentHour(); |
| 556 | if (mIs24Hour) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 557 | return currentHour; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if (mRadialTimePickerView.getAmOrPm() == PM) { |
| 561 | return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 562 | } else { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 563 | return currentHour % HOURS_IN_HALF_DAY; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 564 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 567 | /** |
| 568 | * Set the current minute (0-59). |
| 569 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 570 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 571 | public void setMinute(int minute) { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 572 | setMinuteInternal(minute, FROM_EXTERNAL_API, true); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 575 | private void setMinuteInternal(int minute, @ChangeSource int source, boolean notify) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 576 | if (mCurrentMinute == minute) { |
| 577 | return; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 578 | } |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 579 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 580 | resetAutofilledValue(); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 581 | mCurrentMinute = minute; |
| 582 | updateHeaderMinute(minute, true); |
| 583 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 584 | if (source != FROM_RADIAL_PICKER) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 585 | mRadialTimePickerView.setCurrentMinute(minute); |
| 586 | } |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 587 | if (source != FROM_INPUT_PICKER) { |
| 588 | updateTextInputPicker(); |
| 589 | } |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 590 | |
| 591 | mDelegator.invalidate(); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 592 | if (notify) { |
| 593 | onTimeChanged(); |
| 594 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 597 | /** |
| 598 | * @return The current minute. |
| 599 | */ |
| 600 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 601 | public int getMinute() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 602 | return mRadialTimePickerView.getCurrentMinute(); |
| 603 | } |
| 604 | |
| 605 | /** |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 606 | * Sets whether time is displayed in 24-hour mode or 12-hour mode with |
| 607 | * AM/PM indicators. |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 608 | * |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 609 | * @param is24Hour {@code true} to display time in 24-hour mode or |
| 610 | * {@code false} for 12-hour mode with AM/PM |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 611 | */ |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 612 | public void setIs24Hour(boolean is24Hour) { |
| 613 | if (mIs24Hour != is24Hour) { |
| 614 | mIs24Hour = is24Hour; |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 615 | mCurrentHour = getHour(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 616 | |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 617 | updateHourFormat(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 618 | updateUI(mRadialTimePickerView.getCurrentItemShowing()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 619 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /** |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 623 | * @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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 626 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 627 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 628 | public boolean is24Hour() { |
| 629 | return mIs24Hour; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 633 | public void setEnabled(boolean enabled) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 634 | mHourView.setEnabled(enabled); |
| 635 | mMinuteView.setEnabled(enabled); |
| 636 | mAmLabel.setEnabled(enabled); |
| 637 | mPmLabel.setEnabled(enabled); |
| 638 | mRadialTimePickerView.setEnabled(enabled); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 639 | mIsEnabled = enabled; |
| 640 | } |
| 641 | |
| 642 | @Override |
| 643 | public boolean isEnabled() { |
| 644 | return mIsEnabled; |
| 645 | } |
| 646 | |
| 647 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 648 | public int getBaseline() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 649 | // does not support baseline alignment |
| 650 | return -1; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 654 | public Parcelable onSaveInstanceState(Parcelable superState) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 655 | return new SavedState(superState, getHour(), getMinute(), |
| 656 | is24Hour(), getCurrentItemShowing()); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public void onRestoreInstanceState(Parcelable state) { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 661 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 669 | 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 Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 677 | if (mIs24Hour) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 678 | flags |= DateUtils.FORMAT_24HOUR; |
| 679 | } else { |
| 680 | flags |= DateUtils.FORMAT_12HOUR; |
| 681 | } |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 682 | |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 683 | mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour()); |
| 684 | mTempCalendar.set(Calendar.MINUTE, getMinute()); |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 685 | |
| 686 | final String selectedTime = DateUtils.formatDateTime(mContext, |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 687 | mTempCalendar.getTimeInMillis(), flags); |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 688 | final String selectionMode = mRadialTimePickerView.getCurrentItemShowing() == HOUR_INDEX ? |
| 689 | mSelectHours : mSelectMinutes; |
| 690 | event.getText().add(selectedTime + " " + selectionMode); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 693 | /** @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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 721 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 722 | * @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 Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 731 | private void onTimeChanged() { |
| 732 | mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 733 | if (mOnTimeChangedListener != null) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 734 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 735 | } |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 736 | if (mAutoFillChangeListener != null) { |
| 737 | mAutoFillChangeListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
| 738 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 741 | private void tryVibrate() { |
| 742 | mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK); |
Elliott Hughes | 1cc51a6 | 2014-08-21 16:21:30 -0700 | [diff] [blame] | 743 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 744 | |
| 745 | private void updateAmPmLabelStates(int amOrPm) { |
| 746 | final boolean isAm = amOrPm == AM; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 747 | mAmLabel.setActivated(isAm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 748 | mAmLabel.setChecked(isAm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 749 | |
| 750 | final boolean isPm = amOrPm == PM; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 751 | mPmLabel.setActivated(isPm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 752 | mPmLabel.setChecked(isPm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 756 | * Converts hour-of-day (0-23) time into a localized hour number. |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 757 | * <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 Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 761 | * |
| 762 | * @param hourOfDay the hour-of-day (0-23) |
| 763 | * @return a localized hour number |
| 764 | */ |
| 765 | private int getLocalizedHour(int hourOfDay) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 766 | if (!mIs24Hour) { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 767 | // Convert to hour-of-am-pm. |
| 768 | hourOfDay %= 12; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 769 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 770 | |
| 771 | if (!mHourFormatStartsAtZero && hourOfDay == 0) { |
| 772 | // Convert to clock-hour (either of-day or of-am-pm). |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 773 | hourOfDay = mIs24Hour ? 24 : 12; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 774 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 775 | |
| 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 783 | if (announce) { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 784 | tryAnnounceForAccessibility(mHourView.getText(), true); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 788 | private void updateHeaderMinute(int minuteOfHour, boolean announce) { |
| 789 | mMinuteView.setValue(minuteOfHour); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 790 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 791 | if (announce) { |
| 792 | tryAnnounceForAccessibility(mMinuteView.getText(), false); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 793 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 794 | } |
| 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 Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 805 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale, |
| 806 | (mIs24Hour) ? "Hm" : "hm"); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 807 | 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 Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 818 | mTextInputPickerView.updateSeparator(separatorText); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 819 | } |
| 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 Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 836 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 842 | } |
| 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 852 | if (announce) { |
Alan Viverette | ffb46bf | 2014-10-24 12:06:11 -0700 | [diff] [blame] | 853 | mDelegator.announceForAccessibility(mSelectHours); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 854 | } |
| 855 | } else { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 856 | if (announce) { |
Alan Viverette | ffb46bf | 2014-10-24 12:06:11 -0700 | [diff] [blame] | 857 | mDelegator.announceForAccessibility(mSelectMinutes); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 861 | mHourView.setActivated(index == HOUR_INDEX); |
| 862 | mMinuteView.setActivated(index == MINUTE_INDEX); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | private void setAmOrPm(int amOrPm) { |
| 866 | updateAmPmLabelStates(amOrPm); |
Alan Viverette | 30b57b6 | 2016-04-19 09:29:20 -0400 | [diff] [blame] | 867 | |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 868 | if (mRadialTimePickerView.setAmOrPm(amOrPm)) { |
| 869 | mCurrentHour = getHour(); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 870 | updateTextInputPicker(); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 871 | if (mOnTimeChangedListener != null) { |
| 872 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
| 873 | } |
Alan Viverette | 30b57b6 | 2016-04-19 09:29:20 -0400 | [diff] [blame] | 874 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 877 | /** Listener for RadialTimePickerView interaction. */ |
| 878 | private final OnValueSelectedListener mOnValueSelectedListener = new OnValueSelectedListener() { |
| 879 | @Override |
Alan Viverette | 66a8562 | 2016-08-04 13:24:14 -0400 | [diff] [blame] | 880 | public void onValueSelected(int pickerType, int newValue, boolean autoAdvance) { |
Aurimas Liutikas | 2f16bc8 | 2017-02-28 12:16:57 -0800 | [diff] [blame] | 881 | boolean valueChanged = false; |
Alan Viverette | 66a8562 | 2016-08-04 13:24:14 -0400 | [diff] [blame] | 882 | switch (pickerType) { |
| 883 | case RadialTimePickerView.HOURS: |
Aurimas Liutikas | 2f16bc8 | 2017-02-28 12:16:57 -0800 | [diff] [blame] | 884 | if (getHour() != newValue) { |
| 885 | valueChanged = true; |
| 886 | } |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 887 | final boolean isTransition = mAllowAutoAdvance && autoAdvance; |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 888 | setHourInternal(newValue, FROM_RADIAL_PICKER, !isTransition, true); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 889 | if (isTransition) { |
| 890 | setCurrentItemShowing(MINUTE_INDEX, true, false); |
Alan Viverette | 66a8562 | 2016-08-04 13:24:14 -0400 | [diff] [blame] | 891 | |
| 892 | final int localizedHour = getLocalizedHour(newValue); |
| 893 | mDelegator.announceForAccessibility(localizedHour + ". " + mSelectMinutes); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 894 | } |
| 895 | break; |
Alan Viverette | 66a8562 | 2016-08-04 13:24:14 -0400 | [diff] [blame] | 896 | case RadialTimePickerView.MINUTES: |
Aurimas Liutikas | 2f16bc8 | 2017-02-28 12:16:57 -0800 | [diff] [blame] | 897 | if (getMinute() != newValue) { |
| 898 | valueChanged = true; |
| 899 | } |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 900 | setMinuteInternal(newValue, FROM_RADIAL_PICKER, true); |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 901 | break; |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 902 | } |
| 903 | |
Aurimas Liutikas | 2f16bc8 | 2017-02-28 12:16:57 -0800 | [diff] [blame] | 904 | if (mOnTimeChangedListener != null && valueChanged) { |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 905 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
| 906 | } |
| 907 | } |
| 908 | }; |
| 909 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 910 | private final OnValueTypedListener mOnValueTypedListener = new OnValueTypedListener() { |
| 911 | @Override |
| 912 | public void onValueChanged(int pickerType, int newValue) { |
| 913 | switch (pickerType) { |
| 914 | case TextInputTimePickerView.HOURS: |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 915 | setHourInternal(newValue, FROM_INPUT_PICKER, false, true); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 916 | break; |
| 917 | case TextInputTimePickerView.MINUTES: |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 918 | setMinuteInternal(newValue, FROM_INPUT_PICKER, true); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 919 | break; |
| 920 | case TextInputTimePickerView.AMPM: |
| 921 | setAmOrPm(newValue); |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | }; |
| 926 | |
Alan Viverette | 2a993b4 | 2016-04-28 12:56:09 -0400 | [diff] [blame] | 927 | /** Listener for keyboard interaction. */ |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 928 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 954 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 955 | } else { |
| 956 | // May still be making changes. Postpone visual |
| 957 | // updates to prevent distracting the user. |
| 958 | view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 962 | }; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 963 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 964 | private final Runnable mCommitHour = new Runnable() { |
| 965 | @Override |
| 966 | public void run() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 967 | setHour(mHourView.getValue()); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 968 | } |
| 969 | }; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 970 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 971 | private final Runnable mCommitMinute = new Runnable() { |
| 972 | @Override |
| 973 | public void run() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 974 | setMinute(mMinuteView.getValue()); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 975 | } |
| 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 998 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 999 | |
| 1000 | tryVibrate(); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1003 | }; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1004 | |
| 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 Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1032 | /** |
| 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 Viverette | 7add7e0 | 2015-11-20 14:19:39 -0500 | [diff] [blame] | 1042 | if (view instanceof ViewGroup) { |
| 1043 | mInitialTouchTarget = findNearestChild((ViewGroup) view, |
| 1044 | (int) motionEvent.getX(), (int) motionEvent.getY()); |
| 1045 | } else { |
| 1046 | mInitialTouchTarget = null; |
| 1047 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1048 | } |
| 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1067 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1068 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1069 | private View findNearestChild(ViewGroup v, int x, int y) { |
| 1070 | View bestChild = null; |
| 1071 | int bestDist = Integer.MAX_VALUE; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1072 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1073 | 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 Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1083 | |
| 1084 | return bestChild; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 1085 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 1086 | } |
Elliott Hughes | 1cc51a6 | 2014-08-21 16:21:30 -0700 | [diff] [blame] | 1087 | } |