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 | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 21 | import android.annotation.TestApi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.annotation.Widget; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Svetoslav Ganov | f592696 | 2011-07-12 12:26:20 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Alan Viverette | 68763be | 2016-05-25 11:42:42 -0400 | [diff] [blame] | 27 | import android.icu.util.Calendar; |
| 28 | import android.icu.util.TimeZone; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
Andrei Stingaceanu | dd80f00 | 2016-06-16 12:00:39 +0100 | [diff] [blame] | 31 | import android.text.format.DateUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 33 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.util.SparseArray; |
Svetoslav Ganov | d11e615 | 2012-03-20 12:13:02 -0700 | [diff] [blame] | 35 | import android.view.View; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 36 | import android.view.ViewStructure; |
Svetoslav Ganov | 8a2a895 | 2011-01-27 17:40:13 -0800 | [diff] [blame] | 37 | import android.view.accessibility.AccessibilityEvent; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 38 | import android.view.autofill.AutofillManager; |
| 39 | import android.view.autofill.AutofillValue; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 40 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 42 | import com.android.internal.R; |
| 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; |
Kenny Root | dddda8d | 2010-11-15 14:38:51 -0800 | [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 | d25eb9f | 2014-11-24 14:07:24 -0800 | [diff] [blame] | 49 | * Provides a widget for selecting a date. |
| 50 | * <p> |
| 51 | * When the {@link android.R.styleable#DatePicker_datePickerMode} attribute is |
| 52 | * set to {@code spinner}, the date can be selected using year, month, and day |
| 53 | * spinners or a {@link CalendarView}. The set of spinners and the calendar |
| 54 | * view are automatically synchronized. The client can customize whether only |
| 55 | * the spinners, or only the calendar view, or both to be displayed. |
| 56 | * </p> |
| 57 | * <p> |
| 58 | * When the {@link android.R.styleable#DatePicker_datePickerMode} attribute is |
| 59 | * set to {@code calendar}, the month and day can be selected using a |
| 60 | * calendar-style view while the year can be selected separately using a list. |
| 61 | * </p> |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 62 | * <p> |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 63 | * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> |
| 64 | * guide. |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 65 | * </p> |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 66 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | * For a dialog using this view, see {@link android.app.DatePickerDialog}. |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 68 | * </p> |
| 69 | * |
| 70 | * @attr ref android.R.styleable#DatePicker_startYear |
| 71 | * @attr ref android.R.styleable#DatePicker_endYear |
| 72 | * @attr ref android.R.styleable#DatePicker_maxDate |
| 73 | * @attr ref android.R.styleable#DatePicker_minDate |
| 74 | * @attr ref android.R.styleable#DatePicker_spinnersShown |
| 75 | * @attr ref android.R.styleable#DatePicker_calendarViewShown |
Alan Viverette | ba9bf41 | 2014-09-03 20:14:21 -0700 | [diff] [blame] | 76 | * @attr ref android.R.styleable#DatePicker_dayOfWeekBackground |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 77 | * @attr ref android.R.styleable#DatePicker_dayOfWeekTextAppearance |
Alan Viverette | ba9bf41 | 2014-09-03 20:14:21 -0700 | [diff] [blame] | 78 | * @attr ref android.R.styleable#DatePicker_headerBackground |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 79 | * @attr ref android.R.styleable#DatePicker_headerMonthTextAppearance |
| 80 | * @attr ref android.R.styleable#DatePicker_headerDayOfMonthTextAppearance |
| 81 | * @attr ref android.R.styleable#DatePicker_headerYearTextAppearance |
| 82 | * @attr ref android.R.styleable#DatePicker_yearListItemTextAppearance |
| 83 | * @attr ref android.R.styleable#DatePicker_yearListSelectorColor |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 84 | * @attr ref android.R.styleable#DatePicker_calendarTextColor |
Alan Viverette | d25eb9f | 2014-11-24 14:07:24 -0800 | [diff] [blame] | 85 | * @attr ref android.R.styleable#DatePicker_datePickerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | */ |
| 87 | @Widget |
| 88 | public class DatePicker extends FrameLayout { |
Philip P. Moltmann | 9668903 | 2017-03-09 13:19:55 -0800 | [diff] [blame] | 89 | private static final String LOG_TAG = DatePicker.class.getSimpleName(); |
| 90 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 91 | /** |
| 92 | * Presentation mode for the Holo-style date picker that uses a set of |
| 93 | * {@link android.widget.NumberPicker}s. |
| 94 | * |
| 95 | * @see #getMode() |
| 96 | * @hide Visible for testing only. |
| 97 | */ |
| 98 | @TestApi |
| 99 | public static final int MODE_SPINNER = 1; |
| 100 | |
| 101 | /** |
| 102 | * Presentation mode for the Material-style date picker that uses a |
| 103 | * calendar. |
| 104 | * |
| 105 | * @see #getMode() |
| 106 | * @hide Visible for testing only. |
| 107 | */ |
| 108 | @TestApi |
| 109 | public static final int MODE_CALENDAR = 2; |
| 110 | |
| 111 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 112 | @IntDef(prefix = { "MODE_" }, value = { |
| 113 | MODE_SPINNER, |
| 114 | MODE_CALENDAR |
| 115 | }) |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 116 | @Retention(RetentionPolicy.SOURCE) |
| 117 | public @interface DatePickerMode {} |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 118 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 119 | @UnsupportedAppUsage |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 120 | private final DatePickerDelegate mDelegate; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 121 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 122 | @DatePickerMode |
| 123 | private final int mMode; |
| 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | /** |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 126 | * The callback used to indicate the user changed the date. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | */ |
| 128 | public interface OnDateChangedListener { |
| 129 | |
| 130 | /** |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 131 | * Called upon a date change. |
| 132 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | * @param view The view associated with this listener. |
| 134 | * @param year The year that was set. |
| 135 | * @param monthOfYear The month that was set (0-11) for compatibility |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 136 | * with {@link java.util.Calendar}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | * @param dayOfMonth The day of the month that was set. |
| 138 | */ |
| 139 | void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth); |
| 140 | } |
| 141 | |
| 142 | public DatePicker(Context context) { |
| 143 | this(context, null); |
| 144 | } |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | public DatePicker(Context context, AttributeSet attrs) { |
Svetoslav Ganov | 4243dc3 | 2011-01-18 19:39:57 -0800 | [diff] [blame] | 147 | this(context, attrs, R.attr.datePickerStyle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 150 | public DatePicker(Context context, AttributeSet attrs, int defStyleAttr) { |
| 151 | this(context, attrs, defStyleAttr, 0); |
| 152 | } |
| 153 | |
| 154 | public DatePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 155 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 157 | // DatePicker is important by default, unless app developer overrode attribute. |
| 158 | if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) { |
| 159 | setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES); |
| 160 | } |
| 161 | |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 162 | final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DatePicker, |
| 163 | defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 164 | saveAttributeDataForStyleable(context, R.styleable.DatePicker, |
| 165 | attrs, a, defStyleAttr, defStyleRes); |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 166 | final boolean isDialogMode = a.getBoolean(R.styleable.DatePicker_dialogMode, false); |
| 167 | final int requestedMode = a.getInt(R.styleable.DatePicker_datePickerMode, MODE_SPINNER); |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 168 | final int firstDayOfWeek = a.getInt(R.styleable.DatePicker_firstDayOfWeek, 0); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 169 | a.recycle(); |
| 170 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 171 | if (requestedMode == MODE_CALENDAR && isDialogMode) { |
| 172 | // You want MODE_CALENDAR? YOU CAN'T HANDLE MODE_CALENDAR! Well, |
| 173 | // maybe you can depending on your screen size. Let's check... |
| 174 | mMode = context.getResources().getInteger(R.integer.date_picker_mode); |
| 175 | } else { |
| 176 | mMode = requestedMode; |
| 177 | } |
| 178 | |
| 179 | switch (mMode) { |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 180 | case MODE_CALENDAR: |
| 181 | mDelegate = createCalendarUIDelegate(context, attrs, defStyleAttr, defStyleRes); |
| 182 | break; |
| 183 | case MODE_SPINNER: |
| 184 | default: |
| 185 | mDelegate = createSpinnerUIDelegate(context, attrs, defStyleAttr, defStyleRes); |
| 186 | break; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 187 | } |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 188 | |
| 189 | if (firstDayOfWeek != 0) { |
| 190 | setFirstDayOfWeek(firstDayOfWeek); |
| 191 | } |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 192 | |
| 193 | mDelegate.setAutoFillChangeListener((v, y, m, d) -> { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 194 | final AutofillManager afm = context.getSystemService(AutofillManager.class); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 195 | if (afm != null) { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 196 | afm.notifyValueChanged(this); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 197 | } |
| 198 | }); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 201 | private DatePickerDelegate createSpinnerUIDelegate(Context context, AttributeSet attrs, |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 202 | int defStyleAttr, int defStyleRes) { |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 203 | return new DatePickerSpinnerDelegate(this, context, attrs, defStyleAttr, defStyleRes); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 206 | private DatePickerDelegate createCalendarUIDelegate(Context context, AttributeSet attrs, |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 207 | int defStyleAttr, int defStyleRes) { |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 208 | return new DatePickerCalendarDelegate(this, context, attrs, defStyleAttr, |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 209 | defStyleRes); |
| 210 | } |
| 211 | |
| 212 | /** |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 213 | * @return the picker's presentation mode, one of {@link #MODE_CALENDAR} or |
| 214 | * {@link #MODE_SPINNER} |
| 215 | * @attr ref android.R.styleable#DatePicker_datePickerMode |
| 216 | * @hide Visible for testing only. |
| 217 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 218 | @InspectableProperty(name = "datePickerMode", enumMapping = { |
Ashley Rose | 73abdd3c | 2019-03-15 17:32:56 -0400 | [diff] [blame] | 219 | @InspectableProperty.EnumEntry(value = MODE_SPINNER, name = "spinner"), |
| 220 | @InspectableProperty.EnumEntry(value = MODE_CALENDAR, name = "calendar") |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 221 | }) |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 222 | @DatePickerMode |
| 223 | @TestApi |
| 224 | public int getMode() { |
| 225 | return mMode; |
| 226 | } |
| 227 | |
| 228 | /** |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 229 | * Initialize the state. If the provided values designate an inconsistent |
Svetoslav Ganov | e9730bf | 2010-12-20 21:25:20 -0800 | [diff] [blame] | 230 | * date the values are normalized before updating the spinners. |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 231 | * |
| 232 | * @param year The initial year. |
| 233 | * @param monthOfYear The initial month <strong>starting from zero</strong>. |
| 234 | * @param dayOfMonth The initial day of the month. |
| 235 | * @param onDateChangedListener How user is notified date is changed by |
| 236 | * user, can be null. |
| 237 | */ |
| 238 | public void init(int year, int monthOfYear, int dayOfMonth, |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 239 | OnDateChangedListener onDateChangedListener) { |
| 240 | mDelegate.init(year, monthOfYear, dayOfMonth, onDateChangedListener); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Clara Bayarri | 68640b6 | 2016-06-02 14:56:18 +0100 | [diff] [blame] | 244 | * Set the callback that indicates the date has been adjusted by the user. |
| 245 | * |
| 246 | * @param onDateChangedListener How user is notified date is changed by |
| 247 | * user, can be null. |
| 248 | */ |
| 249 | public void setOnDateChangedListener(OnDateChangedListener onDateChangedListener) { |
| 250 | mDelegate.setOnDateChangedListener(onDateChangedListener); |
| 251 | } |
| 252 | |
| 253 | /** |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 254 | * Update the current date. |
Svetoslav Ganov | a911d4a | 2010-12-08 16:11:30 -0800 | [diff] [blame] | 255 | * |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 256 | * @param year The year. |
| 257 | * @param month The month which is <strong>starting from zero</strong>. |
| 258 | * @param dayOfMonth The day of the month. |
Svetoslav Ganov | a911d4a | 2010-12-08 16:11:30 -0800 | [diff] [blame] | 259 | */ |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 260 | public void updateDate(int year, int month, int dayOfMonth) { |
| 261 | mDelegate.updateDate(year, month, dayOfMonth); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @return The selected year. |
| 266 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 267 | @InspectableProperty(hasAttributeId = false) |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 268 | public int getYear() { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 269 | return mDelegate.getYear(); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @return The selected month. |
| 274 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 275 | @InspectableProperty(hasAttributeId = false) |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 276 | public int getMonth() { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 277 | return mDelegate.getMonth(); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @return The selected day of month. |
| 282 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 283 | @InspectableProperty(hasAttributeId = false) |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 284 | public int getDayOfMonth() { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 285 | return mDelegate.getDayOfMonth(); |
Svetoslav Ganov | 50f34d1 | 2010-12-03 16:05:40 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 289 | * Gets the minimal date supported by this {@link DatePicker} in |
| 290 | * milliseconds since January 1, 1970 00:00:00 in |
| 291 | * {@link TimeZone#getDefault()} time zone. |
| 292 | * <p> |
| 293 | * Note: The default minimal date is 01/01/1900. |
| 294 | * <p> |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 295 | * |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 296 | * @return The minimal supported date. |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 297 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 298 | @InspectableProperty |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 299 | public long getMinDate() { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 300 | return mDelegate.getMinDate().getTimeInMillis(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Sets the minimal date supported by this {@link NumberPicker} in |
| 305 | * milliseconds since January 1, 1970 00:00:00 in |
| 306 | * {@link TimeZone#getDefault()} time zone. |
| 307 | * |
| 308 | * @param minDate The minimal supported date. |
| 309 | */ |
| 310 | public void setMinDate(long minDate) { |
| 311 | mDelegate.setMinDate(minDate); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Gets the maximal date supported by this {@link DatePicker} in |
| 316 | * milliseconds since January 1, 1970 00:00:00 in |
| 317 | * {@link TimeZone#getDefault()} time zone. |
| 318 | * <p> |
| 319 | * Note: The default maximal date is 12/31/2100. |
| 320 | * <p> |
| 321 | * |
| 322 | * @return The maximal supported date. |
| 323 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 324 | @InspectableProperty |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 325 | public long getMaxDate() { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 326 | return mDelegate.getMaxDate().getTimeInMillis(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Sets the maximal date supported by this {@link DatePicker} in |
| 331 | * milliseconds since January 1, 1970 00:00:00 in |
| 332 | * {@link TimeZone#getDefault()} time zone. |
| 333 | * |
| 334 | * @param maxDate The maximal supported date. |
| 335 | */ |
| 336 | public void setMaxDate(long maxDate) { |
| 337 | mDelegate.setMaxDate(maxDate); |
| 338 | } |
| 339 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 340 | /** |
| 341 | * Sets the callback that indicates the current date is valid. |
| 342 | * |
| 343 | * @param callback the callback, may be null |
| 344 | * @hide |
| 345 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 346 | @UnsupportedAppUsage |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 347 | public void setValidationCallback(@Nullable ValidationCallback callback) { |
| 348 | mDelegate.setValidationCallback(callback); |
| 349 | } |
| 350 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 351 | @Override |
| 352 | public void setEnabled(boolean enabled) { |
| 353 | if (mDelegate.isEnabled() == enabled) { |
| 354 | return; |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 355 | } |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 356 | super.setEnabled(enabled); |
| 357 | mDelegate.setEnabled(enabled); |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 360 | @Override |
| 361 | public boolean isEnabled() { |
| 362 | return mDelegate.isEnabled(); |
Svetoslav Ganov | d11e615 | 2012-03-20 12:13:02 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 365 | /** @hide */ |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 366 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 367 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 368 | return mDelegate.dispatchPopulateAccessibilityEvent(event); |
| 369 | } |
| 370 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 371 | /** @hide */ |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 372 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 373 | public void onPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 374 | super.onPopulateAccessibilityEventInternal(event); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 375 | mDelegate.onPopulateAccessibilityEvent(event); |
| 376 | } |
| 377 | |
| 378 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 379 | public CharSequence getAccessibilityClassName() { |
| 380 | return DatePicker.class.getName(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | protected void onConfigurationChanged(Configuration newConfig) { |
| 385 | super.onConfigurationChanged(newConfig); |
| 386 | mDelegate.onConfigurationChanged(newConfig); |
| 387 | } |
| 388 | |
| 389 | /** |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 390 | * Sets the first day of week. |
| 391 | * |
| 392 | * @param firstDayOfWeek The first day of the week conforming to the |
| 393 | * {@link CalendarView} APIs. |
| 394 | * @see Calendar#SUNDAY |
| 395 | * @see Calendar#MONDAY |
| 396 | * @see Calendar#TUESDAY |
| 397 | * @see Calendar#WEDNESDAY |
| 398 | * @see Calendar#THURSDAY |
| 399 | * @see Calendar#FRIDAY |
| 400 | * @see Calendar#SATURDAY |
| 401 | * |
| 402 | * @attr ref android.R.styleable#DatePicker_firstDayOfWeek |
| 403 | */ |
| 404 | public void setFirstDayOfWeek(int firstDayOfWeek) { |
| 405 | if (firstDayOfWeek < Calendar.SUNDAY || firstDayOfWeek > Calendar.SATURDAY) { |
| 406 | throw new IllegalArgumentException("firstDayOfWeek must be between 1 and 7"); |
| 407 | } |
| 408 | mDelegate.setFirstDayOfWeek(firstDayOfWeek); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Gets the first day of week. |
| 413 | * |
| 414 | * @return The first day of the week conforming to the {@link CalendarView} |
| 415 | * APIs. |
| 416 | * @see Calendar#SUNDAY |
| 417 | * @see Calendar#MONDAY |
| 418 | * @see Calendar#TUESDAY |
| 419 | * @see Calendar#WEDNESDAY |
| 420 | * @see Calendar#THURSDAY |
| 421 | * @see Calendar#FRIDAY |
| 422 | * @see Calendar#SATURDAY |
| 423 | * |
| 424 | * @attr ref android.R.styleable#DatePicker_firstDayOfWeek |
| 425 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 426 | @InspectableProperty |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 427 | public int getFirstDayOfWeek() { |
| 428 | return mDelegate.getFirstDayOfWeek(); |
| 429 | } |
| 430 | |
| 431 | /** |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 432 | * Returns whether the {@link CalendarView} is shown. |
| 433 | * <p> |
| 434 | * <strong>Note:</strong> This method returns {@code false} when the |
| 435 | * {@link android.R.styleable#DatePicker_datePickerMode} attribute is set |
| 436 | * to {@code calendar}. |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 437 | * |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 438 | * @return {@code true} if the calendar view is shown |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 439 | * @see #getCalendarView() |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 440 | * @deprecated Not supported by Material-style {@code calendar} mode |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 441 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 442 | @InspectableProperty |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 443 | @Deprecated |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 444 | public boolean getCalendarViewShown() { |
| 445 | return mDelegate.getCalendarViewShown(); |
| 446 | } |
| 447 | |
| 448 | /** |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 449 | * Returns the {@link CalendarView} used by this picker. |
Alan Viverette | d25eb9f | 2014-11-24 14:07:24 -0800 | [diff] [blame] | 450 | * <p> |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 451 | * <strong>Note:</strong> This method throws an |
| 452 | * {@link UnsupportedOperationException} when the |
Alan Viverette | d25eb9f | 2014-11-24 14:07:24 -0800 | [diff] [blame] | 453 | * {@link android.R.styleable#DatePicker_datePickerMode} attribute is set |
| 454 | * to {@code calendar}. |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 455 | * |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 456 | * @return the calendar view |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 457 | * @see #getCalendarViewShown() |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 458 | * @deprecated Not supported by Material-style {@code calendar} mode |
| 459 | * @throws UnsupportedOperationException if called when the picker is |
| 460 | * displayed in {@code calendar} mode |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 461 | */ |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 462 | @Deprecated |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 463 | public CalendarView getCalendarView() { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 464 | return mDelegate.getCalendarView(); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Sets whether the {@link CalendarView} is shown. |
Alan Viverette | d25eb9f | 2014-11-24 14:07:24 -0800 | [diff] [blame] | 469 | * <p> |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 470 | * <strong>Note:</strong> Calling this method has no effect when the |
Alan Viverette | d25eb9f | 2014-11-24 14:07:24 -0800 | [diff] [blame] | 471 | * {@link android.R.styleable#DatePicker_datePickerMode} attribute is set |
| 472 | * to {@code calendar}. |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 473 | * |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 474 | * @param shown {@code true} to show the calendar view, {@code false} to |
| 475 | * hide it |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 476 | * @deprecated Not supported by Material-style {@code calendar} mode |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 477 | */ |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 478 | @Deprecated |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 479 | public void setCalendarViewShown(boolean shown) { |
| 480 | mDelegate.setCalendarViewShown(shown); |
| 481 | } |
| 482 | |
| 483 | /** |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 484 | * Returns whether the spinners are shown. |
| 485 | * <p> |
Alan Viverette | 835d798 | 2015-03-23 14:54:14 -0700 | [diff] [blame] | 486 | * <strong>Note:</strong> his method returns {@code false} when the |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 487 | * {@link android.R.styleable#DatePicker_datePickerMode} attribute is set |
| 488 | * to {@code calendar}. |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 489 | * |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 490 | * @return {@code true} if the spinners are shown |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 491 | * @deprecated Not supported by Material-style {@code calendar} mode |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 492 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 493 | @InspectableProperty |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 494 | @Deprecated |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 495 | public boolean getSpinnersShown() { |
| 496 | return mDelegate.getSpinnersShown(); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Sets whether the spinners are shown. |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 501 | * <p> |
| 502 | * Calling this method has no effect when the |
| 503 | * {@link android.R.styleable#DatePicker_datePickerMode} attribute is set |
| 504 | * to {@code calendar}. |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 505 | * |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 506 | * @param shown {@code true} to show the spinners, {@code false} to hide |
| 507 | * them |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 508 | * @deprecated Not supported by Material-style {@code calendar} mode |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 509 | */ |
Alan Viverette | 0e67214 | 2016-04-06 15:56:11 -0400 | [diff] [blame] | 510 | @Deprecated |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 511 | public void setSpinnersShown(boolean shown) { |
| 512 | mDelegate.setSpinnersShown(shown); |
| 513 | } |
| 514 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 515 | @Override |
| 516 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
Alan Viverette | d015e34 | 2014-09-15 19:27:55 -0700 | [diff] [blame] | 517 | dispatchThawSelfOnly(container); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Override |
| 521 | protected Parcelable onSaveInstanceState() { |
| 522 | Parcelable superState = super.onSaveInstanceState(); |
| 523 | return mDelegate.onSaveInstanceState(superState); |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | protected void onRestoreInstanceState(Parcelable state) { |
Craig Mautner | a67d909 | 2014-09-16 15:38:47 -0700 | [diff] [blame] | 528 | BaseSavedState ss = (BaseSavedState) state; |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 529 | super.onRestoreInstanceState(ss.getSuperState()); |
| 530 | mDelegate.onRestoreInstanceState(ss); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * A delegate interface that defined the public API of the DatePicker. Allows different |
| 535 | * DatePicker implementations. This would need to be implemented by the DatePicker delegates |
| 536 | * for the real behavior. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 537 | * |
| 538 | * @hide |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 539 | */ |
| 540 | interface DatePickerDelegate { |
| 541 | void init(int year, int monthOfYear, int dayOfMonth, |
| 542 | OnDateChangedListener onDateChangedListener); |
| 543 | |
Clara Bayarri | 68640b6 | 2016-06-02 14:56:18 +0100 | [diff] [blame] | 544 | void setOnDateChangedListener(OnDateChangedListener onDateChangedListener); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 545 | void setAutoFillChangeListener(OnDateChangedListener onDateChangedListener); |
Clara Bayarri | 68640b6 | 2016-06-02 14:56:18 +0100 | [diff] [blame] | 546 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 547 | void updateDate(int year, int month, int dayOfMonth); |
| 548 | |
| 549 | int getYear(); |
| 550 | int getMonth(); |
| 551 | int getDayOfMonth(); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 552 | |
| 553 | void autofill(AutofillValue value); |
| 554 | AutofillValue getAutofillValue(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 555 | |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 556 | void setFirstDayOfWeek(int firstDayOfWeek); |
| 557 | int getFirstDayOfWeek(); |
| 558 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 559 | void setMinDate(long minDate); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 560 | Calendar getMinDate(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 561 | |
| 562 | void setMaxDate(long maxDate); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 563 | Calendar getMaxDate(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 564 | |
| 565 | void setEnabled(boolean enabled); |
| 566 | boolean isEnabled(); |
| 567 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 568 | CalendarView getCalendarView(); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 569 | |
| 570 | void setCalendarViewShown(boolean shown); |
| 571 | boolean getCalendarViewShown(); |
| 572 | |
| 573 | void setSpinnersShown(boolean shown); |
| 574 | boolean getSpinnersShown(); |
| 575 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 576 | void setValidationCallback(ValidationCallback callback); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 577 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 578 | void onConfigurationChanged(Configuration newConfig); |
| 579 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 580 | Parcelable onSaveInstanceState(Parcelable superState); |
| 581 | void onRestoreInstanceState(Parcelable state); |
| 582 | |
| 583 | boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event); |
| 584 | void onPopulateAccessibilityEvent(AccessibilityEvent event); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
| 588 | * An abstract class which can be used as a start for DatePicker implementations |
| 589 | */ |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 590 | abstract static class AbstractDatePickerDelegate implements DatePickerDelegate { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 591 | // The delegator |
| 592 | protected DatePicker mDelegator; |
| 593 | |
| 594 | // The context |
| 595 | protected Context mContext; |
| 596 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 597 | // NOTE: when subclasses change this variable, they must call resetAutofilledValue(). |
Andrei Stingaceanu | dd80f00 | 2016-06-16 12:00:39 +0100 | [diff] [blame] | 598 | protected Calendar mCurrentDate; |
| 599 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 600 | // The current locale |
| 601 | protected Locale mCurrentLocale; |
| 602 | |
| 603 | // Callbacks |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 604 | protected OnDateChangedListener mOnDateChangedListener; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 605 | protected OnDateChangedListener mAutoFillChangeListener; |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 606 | protected ValidationCallback mValidationCallback; |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 607 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 608 | // The value that was passed to autofill() - it must be stored because it getAutofillValue() |
| 609 | // must return the exact same value that was autofilled, otherwise the widget will not be |
| 610 | // properly highlighted after autofill(). |
| 611 | private long mAutofilledValue; |
| 612 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 613 | public AbstractDatePickerDelegate(DatePicker delegator, Context context) { |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 614 | mDelegator = delegator; |
| 615 | mContext = context; |
| 616 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 617 | setCurrentLocale(Locale.getDefault()); |
| 618 | } |
| 619 | |
| 620 | protected void setCurrentLocale(Locale locale) { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 621 | if (!locale.equals(mCurrentLocale)) { |
| 622 | mCurrentLocale = locale; |
| 623 | onLocaleChanged(locale); |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 624 | } |
Svetoslav Ganov | d11e615 | 2012-03-20 12:13:02 -0700 | [diff] [blame] | 625 | } |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 626 | |
| 627 | @Override |
Clara Bayarri | 68640b6 | 2016-06-02 14:56:18 +0100 | [diff] [blame] | 628 | public void setOnDateChangedListener(OnDateChangedListener callback) { |
| 629 | mOnDateChangedListener = callback; |
| 630 | } |
| 631 | |
| 632 | @Override |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 633 | public void setAutoFillChangeListener(OnDateChangedListener callback) { |
| 634 | mAutoFillChangeListener = callback; |
| 635 | } |
| 636 | |
| 637 | @Override |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 638 | public void setValidationCallback(ValidationCallback callback) { |
| 639 | mValidationCallback = callback; |
| 640 | } |
| 641 | |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 642 | @Override |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 643 | public final void autofill(AutofillValue value) { |
| 644 | if (value == null || !value.isDate()) { |
| 645 | Log.w(LOG_TAG, value + " could not be autofilled into " + this); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | final long time = value.getDateValue(); |
| 650 | |
| 651 | final Calendar cal = Calendar.getInstance(mCurrentLocale); |
| 652 | cal.setTimeInMillis(time); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 653 | updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), |
| 654 | cal.get(Calendar.DAY_OF_MONTH)); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 655 | |
| 656 | // Must set mAutofilledValue *after* calling subclass method to make sure the value |
| 657 | // returned by getAutofillValue() matches it. |
| 658 | mAutofilledValue = time; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | @Override |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 662 | public final AutofillValue getAutofillValue() { |
| 663 | final long time = mAutofilledValue != 0 |
| 664 | ? mAutofilledValue |
| 665 | : mCurrentDate.getTimeInMillis(); |
| 666 | return AutofillValue.forDate(time); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * This method must be called every time the value of the year, month, and/or day is |
| 671 | * changed by a subclass method. |
| 672 | */ |
| 673 | protected void resetAutofilledValue() { |
| 674 | mAutofilledValue = 0; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 677 | protected void onValidationChanged(boolean valid) { |
| 678 | if (mValidationCallback != null) { |
| 679 | mValidationCallback.onValidationChanged(valid); |
| 680 | } |
| 681 | } |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 682 | |
| 683 | protected void onLocaleChanged(Locale locale) { |
| 684 | // Stub. |
| 685 | } |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 686 | |
Andrei Stingaceanu | dd80f00 | 2016-06-16 12:00:39 +0100 | [diff] [blame] | 687 | @Override |
| 688 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 689 | event.getText().add(getFormattedCurrentDate()); |
| 690 | } |
| 691 | |
| 692 | protected String getFormattedCurrentDate() { |
| 693 | return DateUtils.formatDateTime(mContext, mCurrentDate.getTimeInMillis(), |
| 694 | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR |
| 695 | | DateUtils.FORMAT_SHOW_WEEKDAY); |
| 696 | } |
| 697 | |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 698 | /** |
| 699 | * Class for managing state storing/restoring. |
| 700 | */ |
| 701 | static class SavedState extends View.BaseSavedState { |
| 702 | private final int mSelectedYear; |
| 703 | private final int mSelectedMonth; |
| 704 | private final int mSelectedDay; |
| 705 | private final long mMinDate; |
| 706 | private final long mMaxDate; |
| 707 | private final int mCurrentView; |
| 708 | private final int mListPosition; |
| 709 | private final int mListPositionOffset; |
| 710 | |
| 711 | public SavedState(Parcelable superState, int year, int month, int day, long minDate, |
| 712 | long maxDate) { |
| 713 | this(superState, year, month, day, minDate, maxDate, 0, 0, 0); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Constructor called from {@link DatePicker#onSaveInstanceState()} |
| 718 | */ |
| 719 | public SavedState(Parcelable superState, int year, int month, int day, long minDate, |
| 720 | long maxDate, int currentView, int listPosition, int listPositionOffset) { |
| 721 | super(superState); |
| 722 | mSelectedYear = year; |
| 723 | mSelectedMonth = month; |
| 724 | mSelectedDay = day; |
| 725 | mMinDate = minDate; |
| 726 | mMaxDate = maxDate; |
| 727 | mCurrentView = currentView; |
| 728 | mListPosition = listPosition; |
| 729 | mListPositionOffset = listPositionOffset; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Constructor called from {@link #CREATOR} |
| 734 | */ |
| 735 | private SavedState(Parcel in) { |
| 736 | super(in); |
| 737 | mSelectedYear = in.readInt(); |
| 738 | mSelectedMonth = in.readInt(); |
| 739 | mSelectedDay = in.readInt(); |
| 740 | mMinDate = in.readLong(); |
| 741 | mMaxDate = in.readLong(); |
| 742 | mCurrentView = in.readInt(); |
| 743 | mListPosition = in.readInt(); |
| 744 | mListPositionOffset = in.readInt(); |
| 745 | } |
| 746 | |
| 747 | @Override |
| 748 | public void writeToParcel(Parcel dest, int flags) { |
| 749 | super.writeToParcel(dest, flags); |
| 750 | dest.writeInt(mSelectedYear); |
| 751 | dest.writeInt(mSelectedMonth); |
| 752 | dest.writeInt(mSelectedDay); |
| 753 | dest.writeLong(mMinDate); |
| 754 | dest.writeLong(mMaxDate); |
| 755 | dest.writeInt(mCurrentView); |
| 756 | dest.writeInt(mListPosition); |
| 757 | dest.writeInt(mListPositionOffset); |
| 758 | } |
| 759 | |
| 760 | public int getSelectedDay() { |
| 761 | return mSelectedDay; |
| 762 | } |
| 763 | |
| 764 | public int getSelectedMonth() { |
| 765 | return mSelectedMonth; |
| 766 | } |
| 767 | |
| 768 | public int getSelectedYear() { |
| 769 | return mSelectedYear; |
| 770 | } |
| 771 | |
| 772 | public long getMinDate() { |
| 773 | return mMinDate; |
| 774 | } |
| 775 | |
| 776 | public long getMaxDate() { |
| 777 | return mMaxDate; |
| 778 | } |
| 779 | |
| 780 | public int getCurrentView() { |
| 781 | return mCurrentView; |
| 782 | } |
| 783 | |
| 784 | public int getListPosition() { |
| 785 | return mListPosition; |
| 786 | } |
| 787 | |
| 788 | public int getListPositionOffset() { |
| 789 | return mListPositionOffset; |
| 790 | } |
| 791 | |
| 792 | @SuppressWarnings("all") |
| 793 | // suppress unused and hiding |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 794 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 795 | |
| 796 | public SavedState createFromParcel(Parcel in) { |
| 797 | return new SavedState(in); |
| 798 | } |
| 799 | |
| 800 | public SavedState[] newArray(int size) { |
| 801 | return new SavedState[size]; |
| 802 | } |
| 803 | }; |
| 804 | } |
Svetoslav Ganov | a53efe9 | 2011-09-08 18:08:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Fabrice Di Meglio | 039a784 | 2013-08-28 17:41:26 -0700 | [diff] [blame] | 807 | /** |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 808 | * A callback interface for updating input validity when the date picker |
| 809 | * when included into a dialog. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 810 | * |
| 811 | * @hide |
| 812 | */ |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 813 | public interface ValidationCallback { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 814 | void onValidationChanged(boolean valid); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 815 | } |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 816 | |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 817 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 818 | public void dispatchProvideAutofillStructure(ViewStructure structure, int flags) { |
| 819 | // This view is self-sufficient for autofill, so it needs to call |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 820 | // onProvideAutoFillStructure() to fill itself, but it does not need to call |
| 821 | // dispatchProvideAutoFillStructure() to fill its children. |
Felipe Leme | e4f3065 | 2017-04-25 10:21:45 -0700 | [diff] [blame] | 822 | structure.setAutofillId(getAutofillId()); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 823 | onProvideAutofillStructure(structure, flags); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | @Override |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 827 | public void autofill(AutofillValue value) { |
| 828 | if (!isEnabled()) return; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 829 | |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 830 | mDelegate.autofill(value); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | @Override |
Felipe Leme | 8931e30 | 2017-03-06 13:44:35 -0800 | [diff] [blame] | 834 | public @AutofillType int getAutofillType() { |
| 835 | return isEnabled() ? AUTOFILL_TYPE_DATE : AUTOFILL_TYPE_NONE; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 839 | public AutofillValue getAutofillValue() { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 840 | return isEnabled() ? mDelegate.getAutofillValue() : null; |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 841 | } |
Svetoslav Ganov | 3fec3fe | 2011-09-01 14:48:37 -0700 | [diff] [blame] | 842 | } |