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 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 21 | import android.content.res.ColorStateList; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 24 | import android.os.Parcelable; |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 25 | import android.text.SpannableStringBuilder; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 26 | import android.text.format.DateFormat; |
| 27 | import android.text.format.DateUtils; |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 28 | import android.text.style.TtsSpan; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 30 | import android.util.StateSet; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 31 | import android.view.HapticFeedbackConstants; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 33 | import android.view.MotionEvent; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 34 | import android.view.View; |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 35 | import android.view.View.AccessibilityDelegate; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 36 | import android.view.View.MeasureSpec; |
| 37 | import android.view.ViewGroup; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 38 | import android.view.accessibility.AccessibilityEvent; |
| 39 | import android.view.accessibility.AccessibilityNodeInfo; |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 40 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 41 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 42 | import com.android.internal.R; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 43 | import com.android.internal.widget.NumericTextView; |
| 44 | import com.android.internal.widget.NumericTextView.OnValueChangedListener; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 45 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 46 | import java.util.Calendar; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 47 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 48 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 49 | * A delegate implementing the radial clock-based TimePicker. |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 50 | */ |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 51 | class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate implements |
| 52 | RadialTimePickerView.OnValueSelectedListener { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 53 | /** |
| 54 | * Delay in milliseconds before valid but potentially incomplete, for |
| 55 | * example "1" but not "12", keyboard edits are propagated from the |
| 56 | * hour / minute fields to the radial picker. |
| 57 | */ |
| 58 | private static final long DELAY_COMMIT_MILLIS = 2000; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 59 | |
| 60 | // Index used by RadialPickerLayout |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 61 | private static final int HOUR_INDEX = RadialTimePickerView.HOURS; |
| 62 | private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 63 | |
| 64 | // NOT a real index for the purpose of what's showing. |
| 65 | private static final int AMPM_INDEX = 2; |
| 66 | |
Alan Viverette | f86bbd0 | 2015-09-16 14:19:21 -0400 | [diff] [blame] | 67 | private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor}; |
| 68 | private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha}; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 69 | |
Deepanshu Gupta | 491523d | 2015-10-06 17:56:37 -0700 | [diff] [blame] | 70 | private static final int AM = 0; |
| 71 | private static final int PM = 1; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 72 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 73 | private static final int HOURS_IN_HALF_DAY = 12; |
| 74 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 75 | private final NumericTextView mHourView; |
| 76 | private final NumericTextView mMinuteView; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 77 | private final View mAmPmLayout; |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 78 | private final RadioButton mAmLabel; |
| 79 | private final RadioButton mPmLabel; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 80 | private final RadialTimePickerView mRadialTimePickerView; |
| 81 | private final TextView mSeparatorView; |
| 82 | |
Alan Viverette | 68016a6 | 2015-11-19 17:10:54 -0500 | [diff] [blame] | 83 | private final Calendar mTempCalendar; |
| 84 | |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 85 | // Accessibility strings. |
| 86 | private final String mSelectHours; |
| 87 | private final String mSelectMinutes; |
| 88 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 89 | private boolean mIsEnabled = true; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 90 | private boolean mAllowAutoAdvance; |
| 91 | private int mInitialHourOfDay; |
| 92 | private int mInitialMinute; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 93 | private boolean mIs24Hour; |
Alan Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 94 | private boolean mIsAmPmAtStart; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 95 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 96 | // Localization data. |
| 97 | private boolean mHourFormatShowLeadingZero; |
| 98 | private boolean mHourFormatStartsAtZero; |
| 99 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 100 | // Most recent time announcement values for accessibility. |
| 101 | private CharSequence mLastAnnouncedText; |
| 102 | private boolean mLastAnnouncedIsHour; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 103 | |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 104 | public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs, |
| 105 | int defStyleAttr, int defStyleRes) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 106 | super(delegator, context); |
| 107 | |
| 108 | // process style attributes |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 109 | final TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 110 | R.styleable.TimePicker, defStyleAttr, defStyleRes); |
| 111 | final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 112 | Context.LAYOUT_INFLATER_SERVICE); |
| 113 | final Resources res = mContext.getResources(); |
| 114 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 115 | mSelectHours = res.getString(R.string.select_hours); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 116 | mSelectMinutes = res.getString(R.string.select_minutes); |
| 117 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 118 | final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout, |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 119 | R.layout.time_picker_material); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 120 | final View mainView = inflater.inflate(layoutResourceId, delegator); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 121 | final View headerView = mainView.findViewById(R.id.time_header); |
| 122 | headerView.setOnTouchListener(new NearestTouchDelegate()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 123 | |
| 124 | // Set up hour/minute labels. |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 125 | mHourView = (NumericTextView) mainView.findViewById(R.id.hours); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 126 | mHourView.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 127 | mHourView.setOnFocusChangeListener(mFocusListener); |
| 128 | mHourView.setOnDigitEnteredListener(mDigitEnteredListener); |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 129 | mHourView.setAccessibilityDelegate( |
| 130 | new ClickActionDelegate(context, R.string.select_hours)); |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 131 | mSeparatorView = (TextView) mainView.findViewById(R.id.separator); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 132 | mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 133 | mMinuteView.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 134 | mMinuteView.setOnFocusChangeListener(mFocusListener); |
| 135 | mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener); |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 136 | mMinuteView.setAccessibilityDelegate( |
| 137 | new ClickActionDelegate(context, R.string.select_minutes)); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 138 | mMinuteView.setRange(0, 59); |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 139 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 140 | // Set up AM/PM labels. |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 141 | mAmPmLayout = mainView.findViewById(R.id.ampm_layout); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 142 | mAmPmLayout.setOnTouchListener(new NearestTouchDelegate()); |
| 143 | |
| 144 | final String[] amPmStrings = TimePicker.getAmPmStrings(context); |
| 145 | mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label); |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 146 | mAmLabel.setText(obtainVerbatim(amPmStrings[0])); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 147 | mAmLabel.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 148 | ensureMinimumTextWidth(mAmLabel); |
| 149 | |
| 150 | mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label); |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 151 | mPmLabel.setText(obtainVerbatim(amPmStrings[1])); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 152 | mPmLabel.setOnClickListener(mClickListener); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 153 | ensureMinimumTextWidth(mPmLabel); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 154 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 155 | // For the sake of backwards compatibility, attempt to extract the text |
| 156 | // color from the header time text appearance. If it's set, we'll let |
| 157 | // that override the "real" header text color. |
| 158 | ColorStateList headerTextColor = null; |
| 159 | |
| 160 | @SuppressWarnings("deprecation") |
| 161 | final int timeHeaderTextAppearance = a.getResourceId( |
| 162 | R.styleable.TimePicker_headerTimeTextAppearance, 0); |
| 163 | if (timeHeaderTextAppearance != 0) { |
| 164 | final TypedArray textAppearance = mContext.obtainStyledAttributes(null, |
| 165 | ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance); |
| 166 | final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0); |
| 167 | headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor); |
| 168 | textAppearance.recycle(); |
| 169 | } |
| 170 | |
| 171 | if (headerTextColor == null) { |
| 172 | headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor); |
| 173 | } |
| 174 | |
| 175 | if (headerTextColor != null) { |
| 176 | mHourView.setTextColor(headerTextColor); |
| 177 | mSeparatorView.setTextColor(headerTextColor); |
| 178 | mMinuteView.setTextColor(headerTextColor); |
| 179 | mAmLabel.setTextColor(headerTextColor); |
| 180 | mPmLabel.setTextColor(headerTextColor); |
| 181 | } |
| 182 | |
| 183 | // Set up header background, if available. |
| 184 | if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 185 | headerView.setBackground(a.getDrawable(R.styleable.TimePicker_headerBackground)); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 188 | a.recycle(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 189 | |
Alan Viverette | 2b4dc11 | 2015-10-02 15:29:43 -0400 | [diff] [blame] | 190 | mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker); |
| 191 | mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 192 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 193 | setupListeners(); |
| 194 | |
| 195 | mAllowAutoAdvance = true; |
| 196 | |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 197 | updateHourFormat(); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 198 | |
| 199 | // Initialize with current time. |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 200 | mTempCalendar = Calendar.getInstance(mLocale); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 201 | final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY); |
| 202 | final int currentMinute = mTempCalendar.get(Calendar.MINUTE); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 203 | initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Ensures that a TextView is wide enough to contain its text without |
| 208 | * wrapping or clipping. Measures the specified view and sets the minimum |
| 209 | * width to the view's desired width. |
| 210 | * |
| 211 | * @param v the text view to measure |
| 212 | */ |
| 213 | private static void ensureMinimumTextWidth(TextView v) { |
| 214 | v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| 215 | |
| 216 | // Set both the TextView and the View version of minimum |
| 217 | // width because they are subtly different. |
| 218 | final int minWidth = v.getMeasuredWidth(); |
| 219 | v.setMinWidth(minWidth); |
| 220 | v.setMinimumWidth(minWidth); |
| 221 | } |
| 222 | |
| 223 | /** |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 224 | * Updates hour formatting based on the current locale and 24-hour mode. |
| 225 | * <p> |
| 226 | * Determines how the hour should be formatted, sets member variables for |
| 227 | * leading zero and starting hour, and sets the hour view's presentation. |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 228 | */ |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 229 | private void updateHourFormat() { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 230 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern( |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 231 | mLocale, mIs24Hour ? "Hm" : "hm"); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 232 | final int lengthPattern = bestDateTimePattern.length(); |
| 233 | boolean showLeadingZero = false; |
| 234 | char hourFormat = '\0'; |
| 235 | |
| 236 | for (int i = 0; i < lengthPattern; i++) { |
| 237 | final char c = bestDateTimePattern.charAt(i); |
| 238 | if (c == 'H' || c == 'h' || c == 'K' || c == 'k') { |
| 239 | hourFormat = c; |
| 240 | if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) { |
| 241 | showLeadingZero = true; |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | mHourFormatShowLeadingZero = showLeadingZero; |
| 248 | mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H'; |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 249 | |
| 250 | // Update hour text field. |
| 251 | final int minHour = mHourFormatStartsAtZero ? 0 : 1; |
| 252 | final int maxHour = (mIs24Hour ? 23 : 11) + minHour; |
| 253 | mHourView.setRange(minHour, maxHour); |
| 254 | mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Alan Viverette | f63757b | 2015-04-01 17:14:45 -0700 | [diff] [blame] | 257 | private static final CharSequence obtainVerbatim(String text) { |
| 258 | return new SpannableStringBuilder().append(text, |
| 259 | new TtsSpan.VerbatimBuilder(text).build(), 0); |
| 260 | } |
| 261 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 262 | /** |
| 263 | * The legacy text color might have been poorly defined. Ensures that it |
| 264 | * has an appropriate activated state, using the selected state if one |
| 265 | * exists or modifying the default text color otherwise. |
| 266 | * |
| 267 | * @param color a legacy text color, or {@code null} |
| 268 | * @return a color state list with an appropriate activated state, or |
| 269 | * {@code null} if a valid activated state could not be generated |
| 270 | */ |
| 271 | @Nullable |
| 272 | private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) { |
| 273 | if (color == null || color.hasState(R.attr.state_activated)) { |
| 274 | return color; |
| 275 | } |
| 276 | |
| 277 | final int activatedColor; |
| 278 | final int defaultColor; |
| 279 | if (color.hasState(R.attr.state_selected)) { |
| 280 | activatedColor = color.getColorForState(StateSet.get( |
| 281 | StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0); |
| 282 | defaultColor = color.getColorForState(StateSet.get( |
| 283 | StateSet.VIEW_STATE_ENABLED), 0); |
| 284 | } else { |
| 285 | activatedColor = color.getDefaultColor(); |
| 286 | |
| 287 | // Generate a non-activated color using the disabled alpha. |
| 288 | final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA); |
| 289 | final float disabledAlpha = ta.getFloat(0, 0.30f); |
| 290 | defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha); |
| 291 | } |
| 292 | |
| 293 | if (activatedColor == 0 || defaultColor == 0) { |
| 294 | // We somehow failed to obtain the colors. |
| 295 | return null; |
| 296 | } |
| 297 | |
| 298 | final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}}; |
| 299 | final int[] colors = new int[] { activatedColor, defaultColor }; |
| 300 | return new ColorStateList(stateSet, colors); |
| 301 | } |
| 302 | |
| 303 | private int multiplyAlphaComponent(int color, float alphaMod) { |
| 304 | final int srcRgb = color & 0xFFFFFF; |
| 305 | final int srcAlpha = (color >> 24) & 0xFF; |
| 306 | final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f); |
| 307 | return srcRgb | (dstAlpha << 24); |
| 308 | } |
| 309 | |
Alan Viverette | 3fc00e31 | 2014-12-10 09:46:49 -0800 | [diff] [blame] | 310 | private static class ClickActionDelegate extends AccessibilityDelegate { |
| 311 | private final AccessibilityAction mClickAction; |
| 312 | |
| 313 | public ClickActionDelegate(Context context, int resId) { |
| 314 | mClickAction = new AccessibilityAction( |
| 315 | AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId)); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 320 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 321 | |
| 322 | info.addAction(mClickAction); |
| 323 | } |
| 324 | } |
| 325 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 326 | private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) { |
| 327 | mInitialHourOfDay = hourOfDay; |
| 328 | mInitialMinute = minute; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 329 | mIs24Hour = is24HourView; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 330 | updateUI(index); |
| 331 | } |
| 332 | |
| 333 | private void setupListeners() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 334 | mRadialTimePickerView.setOnValueSelectedListener(this); |
| 335 | } |
| 336 | |
| 337 | private void updateUI(int index) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 338 | updateHeaderAmPm(); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 339 | updateHeaderHour(mInitialHourOfDay, false); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 340 | updateHeaderSeparator(); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 341 | updateHeaderMinute(mInitialMinute, false); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 342 | updateRadialPicker(index); |
| 343 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 344 | mDelegator.invalidate(); |
| 345 | } |
| 346 | |
| 347 | private void updateRadialPicker(int index) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 348 | mRadialTimePickerView.initialize(mInitialHourOfDay, mInitialMinute, mIs24Hour); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 349 | setCurrentItemShowing(index, false, true); |
| 350 | } |
| 351 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 352 | private void updateHeaderAmPm() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 353 | if (mIs24Hour) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 354 | mAmPmLayout.setVisibility(View.GONE); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 355 | } else { |
Alan Viverette | d9f3fdf | 2014-11-12 09:31:22 -0800 | [diff] [blame] | 356 | // Ensure that AM/PM layout is in the correct position. |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 357 | final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm"); |
Alan Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 358 | final boolean isAmPmAtStart = dateTimePattern.startsWith("a"); |
| 359 | setAmPmAtStart(isAmPmAtStart); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 360 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 361 | updateAmPmLabelStates(mInitialHourOfDay < 12 ? AM : PM); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Alan Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 365 | private void setAmPmAtStart(boolean isAmPmAtStart) { |
| 366 | if (mIsAmPmAtStart != isAmPmAtStart) { |
| 367 | mIsAmPmAtStart = isAmPmAtStart; |
| 368 | |
| 369 | final RelativeLayout.LayoutParams params = |
| 370 | (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams(); |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 371 | if (params.getRule(RelativeLayout.RIGHT_OF) != 0 || |
| 372 | params.getRule(RelativeLayout.LEFT_OF) != 0) { |
| 373 | if (isAmPmAtStart) { |
| 374 | params.removeRule(RelativeLayout.RIGHT_OF); |
| 375 | params.addRule(RelativeLayout.LEFT_OF, mHourView.getId()); |
| 376 | } else { |
| 377 | params.removeRule(RelativeLayout.LEFT_OF); |
| 378 | params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId()); |
| 379 | } |
Alan Viverette | adbc95f | 2015-02-20 10:51:33 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | mAmPmLayout.setLayoutParams(params); |
| 383 | } |
| 384 | } |
| 385 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 386 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 387 | * Set the current hour. |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 388 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 389 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 390 | public void setHour(int hour) { |
| 391 | if (mInitialHourOfDay != hour) { |
| 392 | mInitialHourOfDay = hour; |
| 393 | updateHeaderHour(hour, true); |
| 394 | updateHeaderAmPm(); |
| 395 | mRadialTimePickerView.setCurrentHour(hour); |
| 396 | mRadialTimePickerView.setAmOrPm(mInitialHourOfDay < 12 ? AM : PM); |
| 397 | mDelegator.invalidate(); |
| 398 | onTimeChanged(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 399 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 402 | /** |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 403 | * @return the current hour in the range (0-23) |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 404 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 405 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 406 | public int getHour() { |
| 407 | final int currentHour = mRadialTimePickerView.getCurrentHour(); |
| 408 | if (mIs24Hour) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 409 | return currentHour; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (mRadialTimePickerView.getAmOrPm() == PM) { |
| 413 | return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 414 | } else { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 415 | return currentHour % HOURS_IN_HALF_DAY; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 416 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Set the current minute (0-59). |
| 421 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 422 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 423 | public void setMinute(int minute) { |
| 424 | if (mInitialMinute != minute) { |
| 425 | mInitialMinute = minute; |
| 426 | updateHeaderMinute(minute, true); |
| 427 | mRadialTimePickerView.setCurrentMinute(minute); |
| 428 | mDelegator.invalidate(); |
| 429 | onTimeChanged(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 430 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 433 | /** |
| 434 | * @return The current minute. |
| 435 | */ |
| 436 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 437 | public int getMinute() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 438 | return mRadialTimePickerView.getCurrentMinute(); |
| 439 | } |
| 440 | |
| 441 | /** |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 442 | * Sets whether time is displayed in 24-hour mode or 12-hour mode with |
| 443 | * AM/PM indicators. |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 444 | * |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 445 | * @param is24Hour {@code true} to display time in 24-hour mode or |
| 446 | * {@code false} for 12-hour mode with AM/PM |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 447 | */ |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 448 | public void setIs24Hour(boolean is24Hour) { |
| 449 | if (mIs24Hour != is24Hour) { |
| 450 | mIs24Hour = is24Hour; |
| 451 | mInitialHourOfDay = getHour(); |
| 452 | |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 453 | updateHourFormat(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 454 | updateUI(mRadialTimePickerView.getCurrentItemShowing()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 455 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 459 | * @return {@code true} if time is displayed in 24-hour mode, or |
| 460 | * {@code false} if time is displayed in 12-hour mode with AM/PM |
| 461 | * indicators |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 462 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 463 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 464 | public boolean is24Hour() { |
| 465 | return mIs24Hour; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | @Override |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 469 | public void setOnTimeChangedListener(TimePicker.OnTimeChangedListener callback) { |
| 470 | mOnTimeChangedListener = callback; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void setEnabled(boolean enabled) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 475 | mHourView.setEnabled(enabled); |
| 476 | mMinuteView.setEnabled(enabled); |
| 477 | mAmLabel.setEnabled(enabled); |
| 478 | mPmLabel.setEnabled(enabled); |
| 479 | mRadialTimePickerView.setEnabled(enabled); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 480 | mIsEnabled = enabled; |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public boolean isEnabled() { |
| 485 | return mIsEnabled; |
| 486 | } |
| 487 | |
| 488 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 489 | public int getBaseline() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 490 | // does not support baseline alignment |
| 491 | return -1; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 495 | public Parcelable onSaveInstanceState(Parcelable superState) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 496 | return new SavedState(superState, getHour(), getMinute(), |
| 497 | is24Hour(), getCurrentItemShowing()); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public void onRestoreInstanceState(Parcelable state) { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 502 | if (state instanceof SavedState) { |
| 503 | final SavedState ss = (SavedState) state; |
| 504 | initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing()); |
| 505 | mRadialTimePickerView.invalidate(); |
| 506 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 510 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 511 | onPopulateAccessibilityEvent(event); |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 517 | int flags = DateUtils.FORMAT_SHOW_TIME; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 518 | if (mIs24Hour) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 519 | flags |= DateUtils.FORMAT_24HOUR; |
| 520 | } else { |
| 521 | flags |= DateUtils.FORMAT_12HOUR; |
| 522 | } |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 523 | |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 524 | mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour()); |
| 525 | mTempCalendar.set(Calendar.MINUTE, getMinute()); |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 526 | |
| 527 | final String selectedTime = DateUtils.formatDateTime(mContext, |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 528 | mTempCalendar.getTimeInMillis(), flags); |
Alan Viverette | b0f5461 | 2016-04-12 14:58:09 -0400 | [diff] [blame] | 529 | final String selectionMode = mRadialTimePickerView.getCurrentItemShowing() == HOUR_INDEX ? |
| 530 | mSelectHours : mSelectMinutes; |
| 531 | event.getText().add(selectedTime + " " + selectionMode); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 534 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 535 | * @return the index of the current item showing |
| 536 | */ |
| 537 | private int getCurrentItemShowing() { |
| 538 | return mRadialTimePickerView.getCurrentItemShowing(); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Propagate the time change |
| 543 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 544 | private void onTimeChanged() { |
| 545 | mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 546 | if (mOnTimeChangedListener != null) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 547 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 551 | private void tryVibrate() { |
| 552 | mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK); |
Elliott Hughes | 1cc51a6 | 2014-08-21 16:21:30 -0700 | [diff] [blame] | 553 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 554 | |
| 555 | private void updateAmPmLabelStates(int amOrPm) { |
| 556 | final boolean isAm = amOrPm == AM; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 557 | mAmLabel.setActivated(isAm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 558 | mAmLabel.setChecked(isAm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 559 | |
| 560 | final boolean isPm = amOrPm == PM; |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 561 | mPmLabel.setActivated(isPm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 562 | mPmLabel.setChecked(isPm); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Called by the picker for updating the header display. |
| 567 | */ |
| 568 | @Override |
| 569 | public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) { |
Alan Viverette | 73c3068 | 2014-11-07 15:39:24 -0800 | [diff] [blame] | 570 | switch (pickerIndex) { |
| 571 | case HOUR_INDEX: |
| 572 | if (mAllowAutoAdvance && autoAdvance) { |
| 573 | updateHeaderHour(newValue, false); |
| 574 | setCurrentItemShowing(MINUTE_INDEX, true, false); |
| 575 | mDelegator.announceForAccessibility(newValue + ". " + mSelectMinutes); |
| 576 | } else { |
| 577 | updateHeaderHour(newValue, true); |
| 578 | } |
| 579 | break; |
| 580 | case MINUTE_INDEX: |
| 581 | updateHeaderMinute(newValue, true); |
| 582 | break; |
| 583 | case AMPM_INDEX: |
| 584 | updateAmPmLabelStates(newValue); |
| 585 | break; |
Alan Viverette | 73c3068 | 2014-11-07 15:39:24 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | if (mOnTimeChangedListener != null) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 589 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 593 | /** |
| 594 | * Converts hour-of-day (0-23) time into a localized hour number. |
Alan Viverette | 3b7e2b9 | 2015-11-16 16:38:38 -0500 | [diff] [blame] | 595 | * <p> |
| 596 | * The localized value may be in the range (0-23), (1-24), (0-11), or |
| 597 | * (1-12) depending on the locale. This method does not handle leading |
| 598 | * zeroes. |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 599 | * |
| 600 | * @param hourOfDay the hour-of-day (0-23) |
| 601 | * @return a localized hour number |
| 602 | */ |
| 603 | private int getLocalizedHour(int hourOfDay) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 604 | if (!mIs24Hour) { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 605 | // Convert to hour-of-am-pm. |
| 606 | hourOfDay %= 12; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 607 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 608 | |
| 609 | if (!mHourFormatStartsAtZero && hourOfDay == 0) { |
| 610 | // Convert to clock-hour (either of-day or of-am-pm). |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 611 | hourOfDay = mIs24Hour ? 24 : 12; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 612 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 613 | |
| 614 | return hourOfDay; |
| 615 | } |
| 616 | |
| 617 | private void updateHeaderHour(int hourOfDay, boolean announce) { |
| 618 | final int localizedHour = getLocalizedHour(hourOfDay); |
| 619 | mHourView.setValue(localizedHour); |
| 620 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 621 | if (announce) { |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 622 | tryAnnounceForAccessibility(mHourView.getText(), true); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 626 | private void updateHeaderMinute(int minuteOfHour, boolean announce) { |
| 627 | mMinuteView.setValue(minuteOfHour); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 628 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 629 | if (announce) { |
| 630 | tryAnnounceForAccessibility(mMinuteView.getText(), false); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 631 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
| 635 | * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":". |
| 636 | * |
| 637 | * See http://unicode.org/cldr/trac/browser/trunk/common/main |
| 638 | * |
| 639 | * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the |
| 640 | * separator as the character which is just after the hour marker in the returned pattern. |
| 641 | */ |
| 642 | private void updateHeaderSeparator() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 643 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale, |
| 644 | (mIs24Hour) ? "Hm" : "hm"); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 645 | final String separatorText; |
| 646 | // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats |
| 647 | final char[] hourFormats = {'H', 'h', 'K', 'k'}; |
| 648 | int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats); |
| 649 | if (hIndex == -1) { |
| 650 | // Default case |
| 651 | separatorText = ":"; |
| 652 | } else { |
| 653 | separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1)); |
| 654 | } |
| 655 | mSeparatorView.setText(separatorText); |
| 656 | } |
| 657 | |
| 658 | static private int lastIndexOfAny(String str, char[] any) { |
| 659 | final int lengthAny = any.length; |
| 660 | if (lengthAny > 0) { |
| 661 | for (int i = str.length() - 1; i >= 0; i--) { |
| 662 | char c = str.charAt(i); |
| 663 | for (int j = 0; j < lengthAny; j++) { |
| 664 | if (c == any[j]) { |
| 665 | return i; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | return -1; |
| 671 | } |
| 672 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 673 | private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) { |
| 674 | if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) { |
| 675 | // TODO: Find a better solution, potentially live regions? |
| 676 | mDelegator.announceForAccessibility(text); |
| 677 | mLastAnnouncedText = text; |
| 678 | mLastAnnouncedIsHour = isHour; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Show either Hours or Minutes. |
| 684 | */ |
| 685 | private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) { |
| 686 | mRadialTimePickerView.setCurrentItemShowing(index, animateCircle); |
| 687 | |
| 688 | if (index == HOUR_INDEX) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 689 | if (announce) { |
Alan Viverette | ffb46bf | 2014-10-24 12:06:11 -0700 | [diff] [blame] | 690 | mDelegator.announceForAccessibility(mSelectHours); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 691 | } |
| 692 | } else { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 693 | if (announce) { |
Alan Viverette | ffb46bf | 2014-10-24 12:06:11 -0700 | [diff] [blame] | 694 | mDelegator.announceForAccessibility(mSelectMinutes); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Alan Viverette | f2525f6 | 2015-03-24 18:03:38 -0700 | [diff] [blame] | 698 | mHourView.setActivated(index == HOUR_INDEX); |
| 699 | mMinuteView.setActivated(index == MINUTE_INDEX); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | private void setAmOrPm(int amOrPm) { |
| 703 | updateAmPmLabelStates(amOrPm); |
Alan Viverette | 30b57b6 | 2016-04-19 09:29:20 -0400 | [diff] [blame] | 704 | |
| 705 | if (mRadialTimePickerView.setAmOrPm(amOrPm) && mOnTimeChangedListener != null) { |
| 706 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
| 707 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 710 | private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() { |
| 711 | @Override |
| 712 | public void onValueChanged(NumericTextView view, int value, |
| 713 | boolean isValid, boolean isFinished) { |
| 714 | final Runnable commitCallback; |
| 715 | final View nextFocusTarget; |
| 716 | if (view == mHourView) { |
| 717 | commitCallback = mCommitHour; |
| 718 | nextFocusTarget = view.isFocused() ? mMinuteView : null; |
| 719 | } else if (view == mMinuteView) { |
| 720 | commitCallback = mCommitMinute; |
| 721 | nextFocusTarget = null; |
| 722 | } else { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | view.removeCallbacks(commitCallback); |
| 727 | |
| 728 | if (isValid) { |
| 729 | if (isFinished) { |
| 730 | // Done with hours entry, make visual updates |
| 731 | // immediately and move to next focus if needed. |
| 732 | commitCallback.run(); |
| 733 | |
| 734 | if (nextFocusTarget != null) { |
| 735 | nextFocusTarget.requestFocus(); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 736 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 737 | } else { |
| 738 | // May still be making changes. Postpone visual |
| 739 | // updates to prevent distracting the user. |
| 740 | view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 744 | }; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 745 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 746 | private final Runnable mCommitHour = new Runnable() { |
| 747 | @Override |
| 748 | public void run() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 749 | setHour(mHourView.getValue()); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 750 | } |
| 751 | }; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 752 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 753 | private final Runnable mCommitMinute = new Runnable() { |
| 754 | @Override |
| 755 | public void run() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 756 | setMinute(mMinuteView.getValue()); |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 757 | } |
| 758 | }; |
| 759 | |
| 760 | private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() { |
| 761 | @Override |
| 762 | public void onFocusChange(View v, boolean focused) { |
| 763 | if (focused) { |
| 764 | switch (v.getId()) { |
| 765 | case R.id.am_label: |
| 766 | setAmOrPm(AM); |
| 767 | break; |
| 768 | case R.id.pm_label: |
| 769 | setAmOrPm(PM); |
| 770 | break; |
| 771 | case R.id.hours: |
| 772 | setCurrentItemShowing(HOUR_INDEX, true, true); |
| 773 | break; |
| 774 | case R.id.minutes: |
| 775 | setCurrentItemShowing(MINUTE_INDEX, true, true); |
| 776 | break; |
| 777 | default: |
| 778 | // Failed to handle this click, don't vibrate. |
| 779 | return; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 780 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 781 | |
| 782 | tryVibrate(); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 785 | }; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 786 | |
| 787 | private final View.OnClickListener mClickListener = new View.OnClickListener() { |
| 788 | @Override |
| 789 | public void onClick(View v) { |
| 790 | |
| 791 | final int amOrPm; |
| 792 | switch (v.getId()) { |
| 793 | case R.id.am_label: |
| 794 | setAmOrPm(AM); |
| 795 | break; |
| 796 | case R.id.pm_label: |
| 797 | setAmOrPm(PM); |
| 798 | break; |
| 799 | case R.id.hours: |
| 800 | setCurrentItemShowing(HOUR_INDEX, true, true); |
| 801 | break; |
| 802 | case R.id.minutes: |
| 803 | setCurrentItemShowing(MINUTE_INDEX, true, true); |
| 804 | break; |
| 805 | default: |
| 806 | // Failed to handle this click, don't vibrate. |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | tryVibrate(); |
| 811 | } |
| 812 | }; |
| 813 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 814 | /** |
| 815 | * Delegates unhandled touches in a view group to the nearest child view. |
| 816 | */ |
| 817 | private static class NearestTouchDelegate implements View.OnTouchListener { |
| 818 | private View mInitialTouchTarget; |
| 819 | |
| 820 | @Override |
| 821 | public boolean onTouch(View view, MotionEvent motionEvent) { |
| 822 | final int actionMasked = motionEvent.getActionMasked(); |
| 823 | if (actionMasked == MotionEvent.ACTION_DOWN) { |
Alan Viverette | 7add7e0 | 2015-11-20 14:19:39 -0500 | [diff] [blame] | 824 | if (view instanceof ViewGroup) { |
| 825 | mInitialTouchTarget = findNearestChild((ViewGroup) view, |
| 826 | (int) motionEvent.getX(), (int) motionEvent.getY()); |
| 827 | } else { |
| 828 | mInitialTouchTarget = null; |
| 829 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | final View child = mInitialTouchTarget; |
| 833 | if (child == null) { |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | final float offsetX = view.getScrollX() - child.getLeft(); |
| 838 | final float offsetY = view.getScrollY() - child.getTop(); |
| 839 | motionEvent.offsetLocation(offsetX, offsetY); |
| 840 | final boolean handled = child.dispatchTouchEvent(motionEvent); |
| 841 | motionEvent.offsetLocation(-offsetX, -offsetY); |
| 842 | |
| 843 | if (actionMasked == MotionEvent.ACTION_UP |
| 844 | || actionMasked == MotionEvent.ACTION_CANCEL) { |
| 845 | mInitialTouchTarget = null; |
| 846 | } |
| 847 | |
| 848 | return handled; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 849 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 850 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 851 | private View findNearestChild(ViewGroup v, int x, int y) { |
| 852 | View bestChild = null; |
| 853 | int bestDist = Integer.MAX_VALUE; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 854 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 855 | for (int i = 0, count = v.getChildCount(); i < count; i++) { |
| 856 | final View child = v.getChildAt(i); |
| 857 | final int dX = x - (child.getLeft() + child.getWidth() / 2); |
| 858 | final int dY = y - (child.getTop() + child.getHeight() / 2); |
| 859 | final int dist = dX * dX + dY * dY; |
| 860 | if (bestDist > dist) { |
| 861 | bestChild = child; |
| 862 | bestDist = dist; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 865 | |
| 866 | return bestChild; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 867 | } |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 868 | } |
Elliott Hughes | 1cc51a6 | 2014-08-21 16:21:30 -0700 | [diff] [blame] | 869 | } |