The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 20 | import android.annotation.IntRange; |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 23 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.annotation.Widget; |
| 25 | import android.content.Context; |
Svetoslav Ganov | 4243dc3 | 2011-01-18 19:39:57 -0800 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 27 | import android.icu.util.Calendar; |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 28 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Parcelable; |
| 30 | import android.util.AttributeSet; |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 31 | import android.util.Log; |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 32 | import android.util.MathUtils; |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 33 | import android.view.View; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 34 | import android.view.ViewStructure; |
Svetoslav Ganov | 8a2a895 | 2011-01-27 17:40:13 -0800 | [diff] [blame] | 35 | import android.view.accessibility.AccessibilityEvent; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 36 | import android.view.autofill.AutofillManager; |
| 37 | import android.view.autofill.AutofillValue; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 38 | import android.view.inspector.InspectableProperty; |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 39 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 40 | import com.android.internal.R; |
| 41 | |
| 42 | import libcore.icu.LocaleData; |
| 43 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 44 | import java.lang.annotation.Retention; |
| 45 | import java.lang.annotation.RetentionPolicy; |
Svetoslav Ganov | f592696 | 2011-07-12 12:26:20 -0700 | [diff] [blame] | 46 | import java.util.Locale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 49 | * A widget for selecting the time of day, in either 24-hour or AM/PM mode. |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 50 | * <p> |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 51 | * For a dialog using this view, see {@link android.app.TimePickerDialog}. See |
| 52 | * the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> |
| 53 | * guide for more information. |
| 54 | * |
| 55 | * @attr ref android.R.styleable#TimePicker_timePickerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | */ |
| 57 | @Widget |
| 58 | public class TimePicker extends FrameLayout { |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 59 | private static final String LOG_TAG = TimePicker.class.getSimpleName(); |
| 60 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 61 | /** |
| 62 | * Presentation mode for the Holo-style time picker that uses a set of |
| 63 | * {@link android.widget.NumberPicker}s. |
| 64 | * |
| 65 | * @see #getMode() |
| 66 | * @hide Visible for testing only. |
| 67 | */ |
| 68 | @TestApi |
| 69 | public static final int MODE_SPINNER = 1; |
| 70 | |
| 71 | /** |
| 72 | * Presentation mode for the Material-style time picker that uses a clock |
| 73 | * face. |
| 74 | * |
| 75 | * @see #getMode() |
| 76 | * @hide Visible for testing only. |
| 77 | */ |
| 78 | @TestApi |
| 79 | public static final int MODE_CLOCK = 2; |
| 80 | |
| 81 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 82 | @IntDef(prefix = { "MODE_" }, value = { |
| 83 | MODE_SPINNER, |
| 84 | MODE_CLOCK |
| 85 | }) |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 86 | @Retention(RetentionPolicy.SOURCE) |
| 87 | public @interface TimePickerMode {} |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 88 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 89 | @UnsupportedAppUsage |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 90 | private final TimePickerDelegate mDelegate; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 91 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 92 | @TimePickerMode |
| 93 | private final int mMode; |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | /** |
| 96 | * The callback interface used to indicate the time has been adjusted. |
| 97 | */ |
| 98 | public interface OnTimeChangedListener { |
| 99 | |
| 100 | /** |
| 101 | * @param view The view associated with this listener. |
| 102 | * @param hourOfDay The current hour. |
| 103 | * @param minute The current minute. |
| 104 | */ |
| 105 | void onTimeChanged(TimePicker view, int hourOfDay, int minute); |
| 106 | } |
| 107 | |
| 108 | public TimePicker(Context context) { |
| 109 | this(context, null); |
| 110 | } |
Svetoslav Ganov | 4243dc3 | 2011-01-18 19:39:57 -0800 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | public TimePicker(Context context, AttributeSet attrs) { |
Svetoslav Ganov | 4243dc3 | 2011-01-18 19:39:57 -0800 | [diff] [blame] | 113 | this(context, attrs, R.attr.timePickerStyle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 116 | public TimePicker(Context context, AttributeSet attrs, int defStyleAttr) { |
| 117 | this(context, attrs, defStyleAttr, 0); |
| 118 | } |
| 119 | |
| 120 | public TimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 121 | super(context, attrs, defStyleAttr, defStyleRes); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 122 | |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 123 | // DatePicker is important by default, unless app developer overrode attribute. |
| 124 | if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) { |
| 125 | setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES); |
| 126 | } |
| 127 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 128 | final TypedArray a = context.obtainStyledAttributes( |
| 129 | attrs, R.styleable.TimePicker, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 130 | saveAttributeDataForStyleable(context, R.styleable.TimePicker, |
| 131 | attrs, a, defStyleAttr, defStyleRes); |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 132 | final boolean isDialogMode = a.getBoolean(R.styleable.TimePicker_dialogMode, false); |
| 133 | final int requestedMode = a.getInt(R.styleable.TimePicker_timePickerMode, MODE_SPINNER); |
Fabrice Di Meglio | dfaa6c7 | 2014-07-10 19:33:33 -0700 | [diff] [blame] | 134 | a.recycle(); |
| 135 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 136 | if (requestedMode == MODE_CLOCK && isDialogMode) { |
| 137 | // You want MODE_CLOCK? YOU CAN'T HANDLE MODE_CLOCK! Well, maybe |
| 138 | // you can depending on your screen size. Let's check... |
| 139 | mMode = context.getResources().getInteger(R.integer.time_picker_mode); |
| 140 | } else { |
| 141 | mMode = requestedMode; |
| 142 | } |
| 143 | |
| 144 | switch (mMode) { |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 145 | case MODE_CLOCK: |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 146 | mDelegate = new TimePickerClockDelegate( |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 147 | this, context, attrs, defStyleAttr, defStyleRes); |
| 148 | break; |
| 149 | case MODE_SPINNER: |
| 150 | default: |
Alan Viverette | daf33ed | 2014-10-23 13:34:17 -0700 | [diff] [blame] | 151 | mDelegate = new TimePickerSpinnerDelegate( |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 152 | this, context, attrs, defStyleAttr, defStyleRes); |
| 153 | break; |
Fabrice Di Meglio | dfaa6c7 | 2014-07-10 19:33:33 -0700 | [diff] [blame] | 154 | } |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 155 | mDelegate.setAutoFillChangeListener((v, h, m) -> { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 156 | final AutofillManager afm = context.getSystemService(AutofillManager.class); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 157 | if (afm != null) { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 158 | afm.notifyValueChanged(this); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 159 | } |
| 160 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | } |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 162 | |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 163 | /** |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 164 | * @return the picker's presentation mode, one of {@link #MODE_CLOCK} or |
| 165 | * {@link #MODE_SPINNER} |
| 166 | * @attr ref android.R.styleable#TimePicker_timePickerMode |
| 167 | * @hide Visible for testing only. |
| 168 | */ |
| 169 | @TimePickerMode |
| 170 | @TestApi |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 171 | @InspectableProperty(name = "timePickerMode", enumMapping = { |
| 172 | @InspectableProperty.EnumMap(name = "clock", value = MODE_CLOCK), |
| 173 | @InspectableProperty.EnumMap(name = "spinner", value = MODE_SPINNER) |
| 174 | }) |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 175 | public int getMode() { |
| 176 | return mMode; |
| 177 | } |
| 178 | |
| 179 | /** |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 180 | * Sets the currently selected hour using 24-hour time. |
| 181 | * |
| 182 | * @param hour the hour to set, in the range (0-23) |
| 183 | * @see #getHour() |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 184 | */ |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 185 | public void setHour(@IntRange(from = 0, to = 23) int hour) { |
| 186 | mDelegate.setHour(MathUtils.constrain(hour, 0, 23)); |
Fabrice Di Meglio | 64902bd | 2013-08-15 17:49:49 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 189 | /** |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 190 | * Returns the currently selected hour using 24-hour time. |
| 191 | * |
| 192 | * @return the currently selected hour, in the range (0-23) |
| 193 | * @see #setHour(int) |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 194 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 195 | @InspectableProperty(hasAttributeId = false) |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 196 | public int getHour() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 197 | return mDelegate.getHour(); |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 201 | * Sets the currently selected minute. |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 202 | * |
| 203 | * @param minute the minute to set, in the range (0-59) |
| 204 | * @see #getMinute() |
| 205 | */ |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 206 | public void setMinute(@IntRange(from = 0, to = 59) int minute) { |
| 207 | mDelegate.setMinute(MathUtils.constrain(minute, 0, 59)); |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns the currently selected minute. |
| 212 | * |
| 213 | * @return the currently selected minute, in the range (0-59) |
| 214 | * @see #setMinute(int) |
| 215 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 216 | @InspectableProperty(hasAttributeId = false) |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 217 | public int getMinute() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 218 | return mDelegate.getMinute(); |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 222 | * Sets the currently selected hour using 24-hour time. |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 223 | * |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 224 | * @param currentHour the hour to set, in the range (0-23) |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 225 | * @deprecated Use {@link #setHour(int)} |
| 226 | */ |
| 227 | @Deprecated |
| 228 | public void setCurrentHour(@NonNull Integer currentHour) { |
| 229 | setHour(currentHour); |
| 230 | } |
| 231 | |
| 232 | /** |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 233 | * @return the currently selected hour, in the range (0-23) |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 234 | * @deprecated Use {@link #getHour()} |
| 235 | */ |
| 236 | @NonNull |
| 237 | @Deprecated |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 238 | public Integer getCurrentHour() { |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 239 | return getHour(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 240 | } |
Fabrice Di Meglio | 64902bd | 2013-08-15 17:49:49 -0700 | [diff] [blame] | 241 | |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 242 | /** |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 243 | * Sets the currently selected minute. |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 244 | * |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 245 | * @param currentMinute the minute to set, in the range (0-59) |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 246 | * @deprecated Use {@link #setMinute(int)} |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 247 | */ |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 248 | @Deprecated |
| 249 | public void setCurrentMinute(@NonNull Integer currentMinute) { |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 250 | setMinute(currentMinute); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 254 | * @return the currently selected minute, in the range (0-59) |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 255 | * @deprecated Use {@link #getMinute()} |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 256 | */ |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 257 | @NonNull |
| 258 | @Deprecated |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 259 | public Integer getCurrentMinute() { |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 260 | return getMinute(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 264 | * Sets whether this widget displays time in 24-hour mode or 12-hour mode |
| 265 | * with an AM/PM picker. |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 266 | * |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 267 | * @param is24HourView {@code true} to display in 24-hour mode, |
| 268 | * {@code false} for 12-hour mode with AM/PM |
| 269 | * @see #is24HourView() |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 270 | */ |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 271 | public void setIs24HourView(@NonNull Boolean is24HourView) { |
| 272 | if (is24HourView == null) { |
| 273 | return; |
| 274 | } |
| 275 | |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 276 | mDelegate.setIs24Hour(is24HourView); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Alan Viverette | 646a0f8 | 2015-03-18 13:24:07 -0700 | [diff] [blame] | 280 | * @return {@code true} if this widget displays time in 24-hour mode, |
| 281 | * {@code false} otherwise} |
| 282 | * @see #setIs24HourView(Boolean) |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 283 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 284 | @InspectableProperty(hasAttributeId = false, name = "24Hour") |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 285 | public boolean is24HourView() { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 286 | return mDelegate.is24Hour(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Set the callback that indicates the time has been adjusted by the user. |
| 291 | * |
| 292 | * @param onTimeChangedListener the callback, should not be null. |
| 293 | */ |
| 294 | public void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener) { |
| 295 | mDelegate.setOnTimeChangedListener(onTimeChangedListener); |
Fabrice Di Meglio | 64902bd | 2013-08-15 17:49:49 -0700 | [diff] [blame] | 296 | } |
| 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | @Override |
| 299 | public void setEnabled(boolean enabled) { |
| 300 | super.setEnabled(enabled); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 301 | mDelegate.setEnabled(enabled); |
Svetoslav Ganov | 51c52ed | 2010-12-28 13:45:03 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public boolean isEnabled() { |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 306 | return mDelegate.isEnabled(); |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public int getBaseline() { |
| 311 | return mDelegate.getBaseline(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 314 | /** |
| 315 | * Validates whether current input by the user is a valid time based on the locale. TimePicker |
| 316 | * will show an error message to the user if the time is not valid. |
| 317 | * |
| 318 | * @return {@code true} if the input is valid, {@code false} otherwise |
| 319 | */ |
| 320 | public boolean validateInput() { |
| 321 | return mDelegate.validateInput(); |
| 322 | } |
| 323 | |
Svetoslav Ganov | f592696 | 2011-07-12 12:26:20 -0700 | [diff] [blame] | 324 | @Override |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 325 | protected Parcelable onSaveInstanceState() { |
| 326 | Parcelable superState = super.onSaveInstanceState(); |
| 327 | return mDelegate.onSaveInstanceState(superState); |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | protected void onRestoreInstanceState(Parcelable state) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 332 | BaseSavedState ss = (BaseSavedState) state; |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 333 | super.onRestoreInstanceState(ss.getSuperState()); |
| 334 | mDelegate.onRestoreInstanceState(ss); |
| 335 | } |
| 336 | |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 337 | @Override |
| 338 | public CharSequence getAccessibilityClassName() { |
| 339 | return TimePicker.class.getName(); |
| 340 | } |
| 341 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 342 | /** @hide */ |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 343 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 344 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 345 | return mDelegate.dispatchPopulateAccessibilityEvent(event); |
| 346 | } |
| 347 | |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 348 | /** @hide */ |
| 349 | @TestApi |
| 350 | public View getHourView() { |
| 351 | return mDelegate.getHourView(); |
| 352 | } |
| 353 | |
| 354 | /** @hide */ |
| 355 | @TestApi |
| 356 | public View getMinuteView() { |
| 357 | return mDelegate.getMinuteView(); |
| 358 | } |
| 359 | |
| 360 | /** @hide */ |
| 361 | @TestApi |
| 362 | public View getAmView() { |
| 363 | return mDelegate.getAmView(); |
| 364 | } |
| 365 | |
| 366 | /** @hide */ |
| 367 | @TestApi |
| 368 | public View getPmView() { |
| 369 | return mDelegate.getPmView(); |
| 370 | } |
| 371 | |
Svetoslav Ganov | f592696 | 2011-07-12 12:26:20 -0700 | [diff] [blame] | 372 | /** |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 373 | * A delegate interface that defined the public API of the TimePicker. Allows different |
| 374 | * TimePicker implementations. This would need to be implemented by the TimePicker delegates |
| 375 | * for the real behavior. |
Svetoslav Ganov | f592696 | 2011-07-12 12:26:20 -0700 | [diff] [blame] | 376 | */ |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 377 | interface TimePickerDelegate { |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 378 | void setHour(@IntRange(from = 0, to = 23) int hour); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 379 | int getHour(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 380 | |
Alan Viverette | f0ac2ba | 2016-08-04 13:23:33 -0400 | [diff] [blame] | 381 | void setMinute(@IntRange(from = 0, to = 59) int minute); |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 382 | int getMinute(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 383 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 384 | void setDate(@IntRange(from = 0, to = 23) int hour, |
| 385 | @IntRange(from = 0, to = 59) int minute); |
| 386 | |
| 387 | void autofill(AutofillValue value); |
| 388 | AutofillValue getAutofillValue(); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 389 | |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 390 | void setIs24Hour(boolean is24Hour); |
| 391 | boolean is24Hour(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 392 | |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 393 | boolean validateInput(); |
| 394 | |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 395 | void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 396 | void setAutoFillChangeListener(OnTimeChangedListener autoFillChangeListener); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 397 | |
| 398 | void setEnabled(boolean enabled); |
| 399 | boolean isEnabled(); |
| 400 | |
| 401 | int getBaseline(); |
| 402 | |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 403 | Parcelable onSaveInstanceState(Parcelable superState); |
| 404 | void onRestoreInstanceState(Parcelable state); |
| 405 | |
| 406 | boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event); |
| 407 | void onPopulateAccessibilityEvent(AccessibilityEvent event); |
Andrei Stingaceanu | f87b0e1 | 2016-07-04 17:40:14 +0100 | [diff] [blame] | 408 | |
| 409 | /** @hide */ |
| 410 | @TestApi View getHourView(); |
| 411 | |
| 412 | /** @hide */ |
| 413 | @TestApi View getMinuteView(); |
| 414 | |
| 415 | /** @hide */ |
| 416 | @TestApi View getAmView(); |
| 417 | |
| 418 | /** @hide */ |
| 419 | @TestApi View getPmView(); |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Alan Viverette | b3f2463 | 2015-10-22 16:01:48 -0400 | [diff] [blame] | 422 | static String[] getAmPmStrings(Context context) { |
| 423 | final Locale locale = context.getResources().getConfiguration().locale; |
| 424 | final LocaleData d = LocaleData.get(locale); |
| 425 | |
| 426 | final String[] result = new String[2]; |
| 427 | result[0] = d.amPm[0].length() > 4 ? d.narrowAm : d.amPm[0]; |
| 428 | result[1] = d.amPm[1].length() > 4 ? d.narrowPm : d.amPm[1]; |
| 429 | return result; |
| 430 | } |
| 431 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 432 | /** |
| 433 | * An abstract class which can be used as a start for TimePicker implementations |
| 434 | */ |
| 435 | abstract static class AbstractTimePickerDelegate implements TimePickerDelegate { |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 436 | protected final TimePicker mDelegator; |
| 437 | protected final Context mContext; |
| 438 | protected final Locale mLocale; |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 439 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 440 | protected OnTimeChangedListener mOnTimeChangedListener; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 441 | protected OnTimeChangedListener mAutoFillChangeListener; |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 442 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 443 | // The value that was passed to autofill() - it must be stored because it getAutofillValue() |
| 444 | // must return the exact same value that was autofilled, otherwise the widget will not be |
| 445 | // properly highlighted after autofill(). |
| 446 | private long mAutofilledValue; |
| 447 | |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 448 | public AbstractTimePickerDelegate(@NonNull TimePicker delegator, @NonNull Context context) { |
Fabrice Di Meglio | 9e4009e | 2013-08-19 13:16:46 -0700 | [diff] [blame] | 449 | mDelegator = delegator; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 450 | mContext = context; |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 451 | mLocale = context.getResources().getConfiguration().locale; |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 452 | } |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 453 | |
Clara Bayarri | 68640b6 | 2016-06-02 14:56:18 +0100 | [diff] [blame] | 454 | @Override |
| 455 | public void setOnTimeChangedListener(OnTimeChangedListener callback) { |
| 456 | mOnTimeChangedListener = callback; |
| 457 | } |
| 458 | |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 459 | @Override |
| 460 | public void setAutoFillChangeListener(OnTimeChangedListener callback) { |
| 461 | mAutoFillChangeListener = callback; |
| 462 | } |
| 463 | |
| 464 | @Override |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 465 | public final void autofill(AutofillValue value) { |
| 466 | if (value == null || !value.isDate()) { |
| 467 | Log.w(LOG_TAG, value + " could not be autofilled into " + this); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | final long time = value.getDateValue(); |
| 472 | |
| 473 | final Calendar cal = Calendar.getInstance(mLocale); |
| 474 | cal.setTimeInMillis(time); |
| 475 | setDate(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); |
| 476 | |
| 477 | // Must set mAutofilledValue *after* calling subclass method to make sure the value |
| 478 | // returned by getAutofillValue() matches it. |
| 479 | mAutofilledValue = time; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | @Override |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 483 | public final AutofillValue getAutofillValue() { |
| 484 | if (mAutofilledValue != 0) { |
| 485 | return AutofillValue.forDate(mAutofilledValue); |
| 486 | } |
| 487 | |
| 488 | final Calendar cal = Calendar.getInstance(mLocale); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 489 | cal.set(Calendar.HOUR_OF_DAY, getHour()); |
| 490 | cal.set(Calendar.MINUTE, getMinute()); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 491 | return AutofillValue.forDate(cal.getTimeInMillis()); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * This method must be called every time the value of the hour and/or minute is changed by |
| 496 | * a subclass method. |
| 497 | */ |
| 498 | protected void resetAutofilledValue() { |
| 499 | mAutofilledValue = 0; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 502 | protected static class SavedState extends View.BaseSavedState { |
| 503 | private final int mHour; |
| 504 | private final int mMinute; |
| 505 | private final boolean mIs24HourMode; |
| 506 | private final int mCurrentItemShowing; |
| 507 | |
| 508 | public SavedState(Parcelable superState, int hour, int minute, boolean is24HourMode) { |
| 509 | this(superState, hour, minute, is24HourMode, 0); |
| 510 | } |
| 511 | |
| 512 | public SavedState(Parcelable superState, int hour, int minute, boolean is24HourMode, |
| 513 | int currentItemShowing) { |
| 514 | super(superState); |
| 515 | mHour = hour; |
| 516 | mMinute = minute; |
| 517 | mIs24HourMode = is24HourMode; |
| 518 | mCurrentItemShowing = currentItemShowing; |
| 519 | } |
| 520 | |
| 521 | private SavedState(Parcel in) { |
| 522 | super(in); |
| 523 | mHour = in.readInt(); |
| 524 | mMinute = in.readInt(); |
| 525 | mIs24HourMode = (in.readInt() == 1); |
| 526 | mCurrentItemShowing = in.readInt(); |
| 527 | } |
| 528 | |
| 529 | public int getHour() { |
| 530 | return mHour; |
| 531 | } |
| 532 | |
| 533 | public int getMinute() { |
| 534 | return mMinute; |
| 535 | } |
| 536 | |
| 537 | public boolean is24HourMode() { |
| 538 | return mIs24HourMode; |
| 539 | } |
| 540 | |
| 541 | public int getCurrentItemShowing() { |
| 542 | return mCurrentItemShowing; |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void writeToParcel(Parcel dest, int flags) { |
| 547 | super.writeToParcel(dest, flags); |
| 548 | dest.writeInt(mHour); |
| 549 | dest.writeInt(mMinute); |
| 550 | dest.writeInt(mIs24HourMode ? 1 : 0); |
| 551 | dest.writeInt(mCurrentItemShowing); |
| 552 | } |
| 553 | |
| 554 | @SuppressWarnings({"unused", "hiding"}) |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 555 | public static final @android.annotation.NonNull Creator<SavedState> CREATOR = new Creator<SavedState>() { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 556 | public SavedState createFromParcel(Parcel in) { |
| 557 | return new SavedState(in); |
| 558 | } |
| 559 | |
| 560 | public SavedState[] newArray(int size) { |
| 561 | return new SavedState[size]; |
| 562 | } |
| 563 | }; |
| 564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 566 | |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 567 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 568 | public void dispatchProvideAutofillStructure(ViewStructure structure, int flags) { |
| 569 | // This view is self-sufficient for autofill, so it needs to call |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 570 | // onProvideAutoFillStructure() to fill itself, but it does not need to call |
| 571 | // dispatchProvideAutoFillStructure() to fill its children. |
Felipe Leme | e4f3065 | 2017-04-25 10:21:45 -0700 | [diff] [blame] | 572 | structure.setAutofillId(getAutofillId()); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 573 | onProvideAutofillStructure(structure, flags); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | @Override |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 577 | public void autofill(AutofillValue value) { |
| 578 | if (!isEnabled()) return; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 579 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 580 | mDelegate.autofill(value); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | @Override |
Felipe Leme | 8931e30 | 2017-03-06 13:44:35 -0800 | [diff] [blame] | 584 | public @AutofillType int getAutofillType() { |
| 585 | return isEnabled() ? AUTOFILL_TYPE_DATE : AUTOFILL_TYPE_NONE; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 589 | public AutofillValue getAutofillValue() { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 590 | return isEnabled() ? mDelegate.getAutofillValue() : null; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 591 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | } |