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 | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 19 | import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_AUTO; |
| 20 | import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES; |
| 21 | |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 25 | import android.os.Parcelable; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 26 | import android.text.format.DateFormat; |
Fabrice Di Meglio | 014b8cf | 2013-12-18 12:51:22 -0800 | [diff] [blame] | 27 | import android.text.format.DateUtils; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 29 | import android.view.LayoutInflater; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.view.accessibility.AccessibilityEvent; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 33 | import android.view.inputmethod.EditorInfo; |
| 34 | import android.view.inputmethod.InputMethodManager; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 35 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 36 | import com.android.internal.R; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 37 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 38 | import libcore.icu.LocaleData; |
| 39 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 40 | import java.util.Calendar; |
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 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 43 | * A delegate implementing the basic spinner-based TimePicker. |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 44 | */ |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 45 | class TimePickerSpinnerDelegate extends TimePicker.AbstractTimePickerDelegate { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 46 | private static final boolean DEFAULT_ENABLED_STATE = true; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 47 | private static final int HOURS_IN_HALF_DAY = 12; |
| 48 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 49 | private final NumberPicker mHourSpinner; |
| 50 | private final NumberPicker mMinuteSpinner; |
| 51 | private final NumberPicker mAmPmSpinner; |
| 52 | private final EditText mHourSpinnerInput; |
| 53 | private final EditText mMinuteSpinnerInput; |
| 54 | private final EditText mAmPmSpinnerInput; |
| 55 | private final TextView mDivider; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 56 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 57 | // Note that the legacy implementation of the TimePicker is |
| 58 | // using a button for toggling between AM/PM while the new |
| 59 | // version uses a NumberPicker spinner. Therefore the code |
| 60 | // accommodates these two cases to be backwards compatible. |
| 61 | private final Button mAmPmButton; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 62 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 63 | private final String[] mAmPmStrings; |
Alan Viverette | 26c563b | 2014-10-17 12:49:16 -0700 | [diff] [blame] | 64 | |
Alan Viverette | 68016a6 | 2015-11-19 17:10:54 -0500 | [diff] [blame] | 65 | private final Calendar mTempCalendar; |
| 66 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 67 | private boolean mIsEnabled = DEFAULT_ENABLED_STATE; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 68 | private boolean mHourWithTwoDigit; |
| 69 | private char mHourFormat; |
Fabrice Di Meglio | 014b8cf | 2013-12-18 12:51:22 -0800 | [diff] [blame] | 70 | |
Alan Viverette | 68016a6 | 2015-11-19 17:10:54 -0500 | [diff] [blame] | 71 | private boolean mIs24HourView; |
| 72 | private boolean mIsAm; |
| 73 | |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 74 | public TimePickerSpinnerDelegate(TimePicker delegator, Context context, AttributeSet attrs, |
| 75 | int defStyleAttr, int defStyleRes) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 76 | super(delegator, context); |
| 77 | |
| 78 | // process style attributes |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 79 | final TypedArray a = mContext.obtainStyledAttributes( |
| 80 | attrs, R.styleable.TimePicker, defStyleAttr, defStyleRes); |
| 81 | final int layoutResourceId = a.getResourceId( |
| 82 | R.styleable.TimePicker_legacyLayout, R.layout.time_picker_legacy); |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 83 | a.recycle(); |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 84 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 85 | final LayoutInflater inflater = LayoutInflater.from(mContext); |
Adam Powell | 43da25c | 2017-05-23 15:56:59 -0700 | [diff] [blame] | 86 | final View view = inflater.inflate(layoutResourceId, mDelegator, true); |
| 87 | view.setSaveFromParentEnabled(false); |
Alan Viverette | 67945c1 | 2014-10-15 13:20:47 -0700 | [diff] [blame] | 88 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 89 | // hour |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 90 | mHourSpinner = delegator.findViewById(R.id.hour); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 91 | mHourSpinner.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { |
| 92 | public void onValueChange(NumberPicker spinner, int oldVal, int newVal) { |
| 93 | updateInputState(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 94 | if (!is24Hour()) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 95 | if ((oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) || |
| 96 | (oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1)) { |
| 97 | mIsAm = !mIsAm; |
| 98 | updateAmPmControl(); |
| 99 | } |
| 100 | } |
| 101 | onTimeChanged(); |
| 102 | } |
| 103 | }); |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 104 | mHourSpinnerInput = mHourSpinner.findViewById(R.id.numberpicker_input); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 105 | mHourSpinnerInput.setImeOptions(EditorInfo.IME_ACTION_NEXT); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 106 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 107 | // divider (only for the new widget style) |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 108 | mDivider = mDelegator.findViewById(R.id.divider); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 109 | if (mDivider != null) { |
| 110 | setDividerText(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 111 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 112 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 113 | // minute |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 114 | mMinuteSpinner = mDelegator.findViewById(R.id.minute); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 115 | mMinuteSpinner.setMinValue(0); |
| 116 | mMinuteSpinner.setMaxValue(59); |
| 117 | mMinuteSpinner.setOnLongPressUpdateInterval(100); |
| 118 | mMinuteSpinner.setFormatter(NumberPicker.getTwoDigitFormatter()); |
| 119 | mMinuteSpinner.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { |
| 120 | public void onValueChange(NumberPicker spinner, int oldVal, int newVal) { |
| 121 | updateInputState(); |
| 122 | int minValue = mMinuteSpinner.getMinValue(); |
| 123 | int maxValue = mMinuteSpinner.getMaxValue(); |
| 124 | if (oldVal == maxValue && newVal == minValue) { |
| 125 | int newHour = mHourSpinner.getValue() + 1; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 126 | if (!is24Hour() && newHour == HOURS_IN_HALF_DAY) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 127 | mIsAm = !mIsAm; |
| 128 | updateAmPmControl(); |
| 129 | } |
| 130 | mHourSpinner.setValue(newHour); |
| 131 | } else if (oldVal == minValue && newVal == maxValue) { |
| 132 | int newHour = mHourSpinner.getValue() - 1; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 133 | if (!is24Hour() && newHour == HOURS_IN_HALF_DAY - 1) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 134 | mIsAm = !mIsAm; |
| 135 | updateAmPmControl(); |
| 136 | } |
| 137 | mHourSpinner.setValue(newHour); |
| 138 | } |
| 139 | onTimeChanged(); |
| 140 | } |
| 141 | }); |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 142 | mMinuteSpinnerInput = mMinuteSpinner.findViewById(R.id.numberpicker_input); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 143 | mMinuteSpinnerInput.setImeOptions(EditorInfo.IME_ACTION_NEXT); |
| 144 | |
| 145 | // Get the localized am/pm strings and use them in the spinner. |
| 146 | mAmPmStrings = getAmPmStrings(context); |
| 147 | |
| 148 | // am/pm |
| 149 | final View amPmView = mDelegator.findViewById(R.id.amPm); |
| 150 | if (amPmView instanceof Button) { |
| 151 | mAmPmSpinner = null; |
| 152 | mAmPmSpinnerInput = null; |
| 153 | mAmPmButton = (Button) amPmView; |
| 154 | mAmPmButton.setOnClickListener(new View.OnClickListener() { |
| 155 | public void onClick(View button) { |
| 156 | button.requestFocus(); |
| 157 | mIsAm = !mIsAm; |
| 158 | updateAmPmControl(); |
| 159 | onTimeChanged(); |
| 160 | } |
| 161 | }); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 162 | } else { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 163 | mAmPmButton = null; |
| 164 | mAmPmSpinner = (NumberPicker) amPmView; |
| 165 | mAmPmSpinner.setMinValue(0); |
| 166 | mAmPmSpinner.setMaxValue(1); |
| 167 | mAmPmSpinner.setDisplayedValues(mAmPmStrings); |
| 168 | mAmPmSpinner.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { |
| 169 | public void onValueChange(NumberPicker picker, int oldVal, int newVal) { |
| 170 | updateInputState(); |
| 171 | picker.requestFocus(); |
| 172 | mIsAm = !mIsAm; |
| 173 | updateAmPmControl(); |
| 174 | onTimeChanged(); |
| 175 | } |
| 176 | }); |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 177 | mAmPmSpinnerInput = mAmPmSpinner.findViewById(R.id.numberpicker_input); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 178 | mAmPmSpinnerInput.setImeOptions(EditorInfo.IME_ACTION_DONE); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 179 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 180 | |
| 181 | if (isAmPmAtStart()) { |
| 182 | // Move the am/pm view to the beginning |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 183 | ViewGroup amPmParent = delegator.findViewById(R.id.timePickerLayout); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 184 | amPmParent.removeView(amPmView); |
| 185 | amPmParent.addView(amPmView, 0); |
| 186 | // Swap layout margins if needed. They may be not symmetrical (Old Standard Theme |
| 187 | // for example and not for Holo Theme) |
| 188 | ViewGroup.MarginLayoutParams lp = |
| 189 | (ViewGroup.MarginLayoutParams) amPmView.getLayoutParams(); |
| 190 | final int startMargin = lp.getMarginStart(); |
| 191 | final int endMargin = lp.getMarginEnd(); |
| 192 | if (startMargin != endMargin) { |
| 193 | lp.setMarginStart(endMargin); |
| 194 | lp.setMarginEnd(startMargin); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | getHourFormatData(); |
| 199 | |
| 200 | // update controls to initial state |
| 201 | updateHourControl(); |
| 202 | updateMinuteControl(); |
| 203 | updateAmPmControl(); |
| 204 | |
| 205 | // set to current time |
Alan Viverette | 68016a6 | 2015-11-19 17:10:54 -0500 | [diff] [blame] | 206 | mTempCalendar = Calendar.getInstance(mLocale); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 207 | setHour(mTempCalendar.get(Calendar.HOUR_OF_DAY)); |
| 208 | setMinute(mTempCalendar.get(Calendar.MINUTE)); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 209 | |
| 210 | if (!isEnabled()) { |
| 211 | setEnabled(false); |
| 212 | } |
| 213 | |
| 214 | // set the content descriptions |
| 215 | setContentDescriptions(); |
| 216 | |
| 217 | // If not explicitly specified this view is important for accessibility. |
| 218 | if (mDelegator.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 219 | mDelegator.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 220 | } |
| 221 | } |
| 222 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 223 | @Override |
| 224 | public boolean validateInput() { |
| 225 | return true; |
| 226 | } |
| 227 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 228 | private void getHourFormatData() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 229 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale, |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 230 | (mIs24HourView) ? "Hm" : "hm"); |
| 231 | final int lengthPattern = bestDateTimePattern.length(); |
| 232 | mHourWithTwoDigit = false; |
| 233 | char hourFormat = '\0'; |
| 234 | // Check if the returned pattern is single or double 'H', 'h', 'K', 'k'. We also save |
| 235 | // the hour format that we found. |
| 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 | mHourFormat = c; |
| 240 | if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) { |
| 241 | mHourWithTwoDigit = true; |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | private boolean isAmPmAtStart() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 249 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale, |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 250 | "hm" /* skeleton */); |
| 251 | |
| 252 | return bestDateTimePattern.startsWith("a"); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 255 | /** |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 256 | * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":". |
| 257 | * |
| 258 | * See http://unicode.org/cldr/trac/browser/trunk/common/main |
| 259 | * |
| 260 | * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the |
| 261 | * separator as the character which is just after the hour marker in the returned pattern. |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 262 | */ |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 263 | private void setDividerText() { |
| 264 | final String skeleton = (mIs24HourView) ? "Hm" : "hm"; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 265 | final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale, |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 266 | skeleton); |
| 267 | final String separatorText; |
| 268 | int hourIndex = bestDateTimePattern.lastIndexOf('H'); |
| 269 | if (hourIndex == -1) { |
| 270 | hourIndex = bestDateTimePattern.lastIndexOf('h'); |
| 271 | } |
| 272 | if (hourIndex == -1) { |
| 273 | // Default case |
| 274 | separatorText = ":"; |
| 275 | } else { |
| 276 | int minuteIndex = bestDateTimePattern.indexOf('m', hourIndex + 1); |
| 277 | if (minuteIndex == -1) { |
| 278 | separatorText = Character.toString(bestDateTimePattern.charAt(hourIndex + 1)); |
| 279 | } else { |
| 280 | separatorText = bestDateTimePattern.substring(hourIndex + 1, minuteIndex); |
| 281 | } |
| 282 | } |
| 283 | mDivider.setText(separatorText); |
| 284 | } |
| 285 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 286 | @Override |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 287 | public void setDate(int hour, int minute) { |
| 288 | setCurrentHour(hour, false); |
| 289 | setCurrentMinute(minute, false); |
| 290 | |
| 291 | onTimeChanged(); |
| 292 | } |
| 293 | |
| 294 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 295 | public void setHour(int hour) { |
| 296 | setCurrentHour(hour, true); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 299 | private void setCurrentHour(int currentHour, boolean notifyTimeChanged) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 300 | // why was Integer used in the first place? |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 301 | if (currentHour == getHour()) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 302 | return; |
| 303 | } |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 304 | resetAutofilledValue(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 305 | if (!is24Hour()) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 306 | // convert [0,23] ordinal to wall clock display |
| 307 | if (currentHour >= HOURS_IN_HALF_DAY) { |
| 308 | mIsAm = false; |
| 309 | if (currentHour > HOURS_IN_HALF_DAY) { |
| 310 | currentHour = currentHour - HOURS_IN_HALF_DAY; |
| 311 | } |
| 312 | } else { |
| 313 | mIsAm = true; |
| 314 | if (currentHour == 0) { |
| 315 | currentHour = HOURS_IN_HALF_DAY; |
| 316 | } |
| 317 | } |
| 318 | updateAmPmControl(); |
| 319 | } |
| 320 | mHourSpinner.setValue(currentHour); |
| 321 | if (notifyTimeChanged) { |
| 322 | onTimeChanged(); |
| 323 | } |
| 324 | } |
| 325 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 326 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 327 | public int getHour() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 328 | int currentHour = mHourSpinner.getValue(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 329 | if (is24Hour()) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 330 | return currentHour; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 331 | } else if (mIsAm) { |
| 332 | return currentHour % HOURS_IN_HALF_DAY; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 333 | } else { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 334 | return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 338 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 339 | public void setMinute(int minute) { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 340 | setCurrentMinute(minute, true); |
| 341 | } |
| 342 | |
| 343 | private void setCurrentMinute(int minute, boolean notifyTimeChanged) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 344 | if (minute == getMinute()) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 345 | return; |
| 346 | } |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 347 | resetAutofilledValue(); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 348 | mMinuteSpinner.setValue(minute); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 349 | if (notifyTimeChanged) { |
| 350 | onTimeChanged(); |
| 351 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 354 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 355 | public int getMinute() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 356 | return mMinuteSpinner.getValue(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 359 | public void setIs24Hour(boolean is24Hour) { |
| 360 | if (mIs24HourView == is24Hour) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 361 | return; |
| 362 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 363 | // cache the current hour since spinner range changes and BEFORE changing mIs24HourView!! |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 364 | int currentHour = getHour(); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 365 | // Order is important here. |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 366 | mIs24HourView = is24Hour; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 367 | getHourFormatData(); |
| 368 | updateHourControl(); |
| 369 | // set value after spinner range is updated |
| 370 | setCurrentHour(currentHour, false); |
| 371 | updateMinuteControl(); |
| 372 | updateAmPmControl(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 375 | @Override |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 376 | public boolean is24Hour() { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 377 | return mIs24HourView; |
| 378 | } |
| 379 | |
| 380 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 381 | public void setEnabled(boolean enabled) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 382 | mMinuteSpinner.setEnabled(enabled); |
| 383 | if (mDivider != null) { |
| 384 | mDivider.setEnabled(enabled); |
| 385 | } |
| 386 | mHourSpinner.setEnabled(enabled); |
| 387 | if (mAmPmSpinner != null) { |
| 388 | mAmPmSpinner.setEnabled(enabled); |
| 389 | } else { |
| 390 | mAmPmButton.setEnabled(enabled); |
| 391 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 392 | mIsEnabled = enabled; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public boolean isEnabled() { |
| 397 | return mIsEnabled; |
| 398 | } |
| 399 | |
| 400 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 401 | public int getBaseline() { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 402 | return mHourSpinner.getBaseline(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 406 | public Parcelable onSaveInstanceState(Parcelable superState) { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 407 | return new SavedState(superState, getHour(), getMinute(), is24Hour()); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public void onRestoreInstanceState(Parcelable state) { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 412 | if (state instanceof SavedState) { |
| 413 | final SavedState ss = (SavedState) state; |
| 414 | setHour(ss.getHour()); |
| 415 | setMinute(ss.getMinute()); |
| 416 | } |
Fabrice Di Meglio | 014b8cf | 2013-12-18 12:51:22 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Override |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 420 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
Fabrice Di Meglio | 014b8cf | 2013-12-18 12:51:22 -0800 | [diff] [blame] | 421 | onPopulateAccessibilityEvent(event); |
| 422 | return true; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
Fabrice Di Meglio | 014b8cf | 2013-12-18 12:51:22 -0800 | [diff] [blame] | 427 | int flags = DateUtils.FORMAT_SHOW_TIME; |
| 428 | if (mIs24HourView) { |
| 429 | flags |= DateUtils.FORMAT_24HOUR; |
| 430 | } else { |
| 431 | flags |= DateUtils.FORMAT_12HOUR; |
| 432 | } |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 433 | mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour()); |
| 434 | mTempCalendar.set(Calendar.MINUTE, getMinute()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 435 | String selectedDateUtterance = DateUtils.formatDateTime(mContext, |
Fabrice Di Meglio | 014b8cf | 2013-12-18 12:51:22 -0800 | [diff] [blame] | 436 | mTempCalendar.getTimeInMillis(), flags); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 437 | event.getText().add(selectedDateUtterance); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 440 | /** @hide */ |
| 441 | @Override |
| 442 | @TestApi |
| 443 | public View getHourView() { |
| 444 | return mHourSpinnerInput; |
| 445 | } |
| 446 | |
| 447 | /** @hide */ |
| 448 | @Override |
| 449 | @TestApi |
| 450 | public View getMinuteView() { |
| 451 | return mMinuteSpinnerInput; |
| 452 | } |
| 453 | |
| 454 | /** @hide */ |
| 455 | @Override |
| 456 | @TestApi |
| 457 | public View getAmView() { |
| 458 | return mAmPmSpinnerInput; |
| 459 | } |
| 460 | |
| 461 | /** @hide */ |
| 462 | @Override |
| 463 | @TestApi |
| 464 | public View getPmView() { |
| 465 | return mAmPmSpinnerInput; |
| 466 | } |
| 467 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 468 | private void updateInputState() { |
| 469 | // Make sure that if the user changes the value and the IME is active |
| 470 | // for one of the inputs if this widget, the IME is closed. If the user |
| 471 | // changed the value via the IME and there is a next input the IME will |
| 472 | // be shown, otherwise the user chose another means of changing the |
| 473 | // value and having the IME up makes no sense. |
| 474 | InputMethodManager inputMethodManager = InputMethodManager.peekInstance(); |
| 475 | if (inputMethodManager != null) { |
| 476 | if (inputMethodManager.isActive(mHourSpinnerInput)) { |
| 477 | mHourSpinnerInput.clearFocus(); |
| 478 | inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0); |
| 479 | } else if (inputMethodManager.isActive(mMinuteSpinnerInput)) { |
| 480 | mMinuteSpinnerInput.clearFocus(); |
| 481 | inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0); |
| 482 | } else if (inputMethodManager.isActive(mAmPmSpinnerInput)) { |
| 483 | mAmPmSpinnerInput.clearFocus(); |
| 484 | inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | private void updateAmPmControl() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 490 | if (is24Hour()) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 491 | if (mAmPmSpinner != null) { |
| 492 | mAmPmSpinner.setVisibility(View.GONE); |
| 493 | } else { |
| 494 | mAmPmButton.setVisibility(View.GONE); |
| 495 | } |
| 496 | } else { |
| 497 | int index = mIsAm ? Calendar.AM : Calendar.PM; |
| 498 | if (mAmPmSpinner != null) { |
| 499 | mAmPmSpinner.setValue(index); |
| 500 | mAmPmSpinner.setVisibility(View.VISIBLE); |
| 501 | } else { |
| 502 | mAmPmButton.setText(mAmPmStrings[index]); |
| 503 | mAmPmButton.setVisibility(View.VISIBLE); |
| 504 | } |
| 505 | } |
| 506 | mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 507 | } |
| 508 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 509 | private void onTimeChanged() { |
| 510 | mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 511 | if (mOnTimeChangedListener != null) { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 512 | mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), |
| 513 | getMinute()); |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 514 | } |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 515 | if (mAutoFillChangeListener != null) { |
| 516 | mAutoFillChangeListener.onTimeChanged(mDelegator, getHour(), getMinute()); |
| 517 | } |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | private void updateHourControl() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 521 | if (is24Hour()) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 522 | // 'k' means 1-24 hour |
| 523 | if (mHourFormat == 'k') { |
| 524 | mHourSpinner.setMinValue(1); |
| 525 | mHourSpinner.setMaxValue(24); |
| 526 | } else { |
| 527 | mHourSpinner.setMinValue(0); |
| 528 | mHourSpinner.setMaxValue(23); |
| 529 | } |
| 530 | } else { |
| 531 | // 'K' means 0-11 hour |
| 532 | if (mHourFormat == 'K') { |
| 533 | mHourSpinner.setMinValue(0); |
| 534 | mHourSpinner.setMaxValue(11); |
| 535 | } else { |
| 536 | mHourSpinner.setMinValue(1); |
| 537 | mHourSpinner.setMaxValue(12); |
| 538 | } |
| 539 | } |
| 540 | mHourSpinner.setFormatter(mHourWithTwoDigit ? NumberPicker.getTwoDigitFormatter() : null); |
| 541 | } |
| 542 | |
| 543 | private void updateMinuteControl() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 544 | if (is24Hour()) { |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 545 | mMinuteSpinnerInput.setImeOptions(EditorInfo.IME_ACTION_DONE); |
| 546 | } else { |
| 547 | mMinuteSpinnerInput.setImeOptions(EditorInfo.IME_ACTION_NEXT); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | private void setContentDescriptions() { |
| 552 | // Minute |
| 553 | trySetContentDescription(mMinuteSpinner, R.id.increment, |
| 554 | R.string.time_picker_increment_minute_button); |
| 555 | trySetContentDescription(mMinuteSpinner, R.id.decrement, |
| 556 | R.string.time_picker_decrement_minute_button); |
| 557 | // Hour |
| 558 | trySetContentDescription(mHourSpinner, R.id.increment, |
| 559 | R.string.time_picker_increment_hour_button); |
| 560 | trySetContentDescription(mHourSpinner, R.id.decrement, |
| 561 | R.string.time_picker_decrement_hour_button); |
| 562 | // AM/PM |
| 563 | if (mAmPmSpinner != null) { |
| 564 | trySetContentDescription(mAmPmSpinner, R.id.increment, |
| 565 | R.string.time_picker_increment_set_pm_button); |
| 566 | trySetContentDescription(mAmPmSpinner, R.id.decrement, |
| 567 | R.string.time_picker_decrement_set_am_button); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | private void trySetContentDescription(View root, int viewId, int contDescResId) { |
| 572 | View target = root.findViewById(viewId); |
| 573 | if (target != null) { |
| 574 | target.setContentDescription(mContext.getString(contDescResId)); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 578 | public static String[] getAmPmStrings(Context context) { |
| 579 | String[] result = new String[2]; |
| 580 | LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); |
Narayan Kamath | 14b5039 | 2015-07-08 18:45:39 +0100 | [diff] [blame] | 581 | result[0] = d.amPm[0].length() > 4 ? d.narrowAm : d.amPm[0]; |
| 582 | result[1] = d.amPm[1].length() > 4 ? d.narrowPm : d.amPm[1]; |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 583 | return result; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 584 | } |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 585 | } |