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