Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.ColorStateList; |
| 22 | import android.content.res.Configuration; |
| 23 | import android.content.res.Resources; |
| 24 | import android.content.res.TypedArray; |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 25 | import android.icu.text.DateFormat; |
Alan Viverette | c3e5a82 | 2016-05-16 13:35:56 -0400 | [diff] [blame] | 26 | import android.icu.text.DisplayContext; |
Alan Viverette | 68763be | 2016-05-25 11:42:42 -0400 | [diff] [blame] | 27 | import android.icu.util.Calendar; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 28 | import android.os.Parcelable; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 30 | import android.util.StateSet; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 31 | import android.view.HapticFeedbackConstants; |
| 32 | import android.view.LayoutInflater; |
| 33 | import android.view.View; |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 34 | import android.view.View.OnClickListener; |
| 35 | import android.view.ViewGroup; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityEvent; |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 37 | import android.widget.DayPickerView.OnDaySelectedListener; |
| 38 | import android.widget.YearPickerView.OnYearSelectedListener; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 39 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 40 | import com.android.internal.R; |
| 41 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 42 | import java.util.Locale; |
| 43 | |
| 44 | /** |
| 45 | * A delegate for picking up a date (day / month / year). |
| 46 | */ |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 47 | class DatePickerCalendarDelegate extends DatePicker.AbstractDatePickerDelegate { |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 48 | private static final int USE_LOCALE = 0; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 49 | |
| 50 | private static final int UNINITIALIZED = -1; |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 51 | private static final int VIEW_MONTH_DAY = 0; |
| 52 | private static final int VIEW_YEAR = 1; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 53 | |
| 54 | private static final int DEFAULT_START_YEAR = 1900; |
| 55 | private static final int DEFAULT_END_YEAR = 2100; |
| 56 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 57 | private static final int ANIMATION_DURATION = 300; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 58 | |
Alan Viverette | 60b674e | 2015-03-25 13:00:42 -0700 | [diff] [blame] | 59 | private static final int[] ATTRS_TEXT_COLOR = new int[] { |
| 60 | com.android.internal.R.attr.textColor}; |
| 61 | private static final int[] ATTRS_DISABLED_ALPHA = new int[] { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 62 | com.android.internal.R.attr.disabledAlpha}; |
Alan Viverette | 7119d0d | 2014-08-25 17:27:02 -0700 | [diff] [blame] | 63 | |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 64 | private DateFormat mYearFormat; |
| 65 | private DateFormat mMonthDayFormat; |
Alan Viverette | 7119d0d | 2014-08-25 17:27:02 -0700 | [diff] [blame] | 66 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 67 | // Top-level container. |
| 68 | private ViewGroup mContainer; |
Alan Viverette | 7119d0d | 2014-08-25 17:27:02 -0700 | [diff] [blame] | 69 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 70 | // Header views. |
| 71 | private TextView mHeaderYear; |
| 72 | private TextView mHeaderMonthDay; |
| 73 | |
| 74 | // Picker views. |
| 75 | private ViewAnimator mAnimator; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 76 | private DayPickerView mDayPickerView; |
| 77 | private YearPickerView mYearPickerView; |
| 78 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 79 | // Accessibility strings. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 80 | private String mSelectDay; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 81 | private String mSelectYear; |
| 82 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 83 | private int mCurrentView = UNINITIALIZED; |
| 84 | |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 85 | private final Calendar mTempDate; |
| 86 | private final Calendar mMinDate; |
| 87 | private final Calendar mMaxDate; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 88 | |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 89 | private int mFirstDayOfWeek = USE_LOCALE; |
| 90 | |
Chet Haase | 3053b2f | 2014-08-06 07:51:50 -0700 | [diff] [blame] | 91 | public DatePickerCalendarDelegate(DatePicker delegator, Context context, AttributeSet attrs, |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 92 | int defStyleAttr, int defStyleRes) { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 93 | super(delegator, context); |
| 94 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 95 | final Locale locale = mCurrentLocale; |
Alan Viverette | 452fe34 | 2015-03-23 14:26:09 -0700 | [diff] [blame] | 96 | mCurrentDate = Calendar.getInstance(locale); |
| 97 | mTempDate = Calendar.getInstance(locale); |
| 98 | mMinDate = Calendar.getInstance(locale); |
| 99 | mMaxDate = Calendar.getInstance(locale); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 100 | |
You Kim | 49e6c4a | 2015-02-26 19:06:16 +0900 | [diff] [blame] | 101 | mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1); |
| 102 | mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 103 | |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 104 | final Resources res = mDelegator.getResources(); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 105 | final TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 106 | R.styleable.DatePicker, defStyleAttr, defStyleRes); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 107 | final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 108 | Context.LAYOUT_INFLATER_SERVICE); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 109 | final int layoutResourceId = a.getResourceId( |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 110 | R.styleable.DatePicker_internalLayout, R.layout.date_picker_material); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 111 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 112 | // Set up and attach container. |
Alan Viverette | f7aa925 | 2015-06-03 14:07:26 -0700 | [diff] [blame] | 113 | mContainer = (ViewGroup) inflater.inflate(layoutResourceId, mDelegator, false); |
Adam Powell | 43da25c | 2017-05-23 15:56:59 -0700 | [diff] [blame] | 114 | mContainer.setSaveFromParentEnabled(false); |
Alan Viverette | f7aa925 | 2015-06-03 14:07:26 -0700 | [diff] [blame] | 115 | mDelegator.addView(mContainer); |
Alan Viverette | 7119d0d | 2014-08-25 17:27:02 -0700 | [diff] [blame] | 116 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 117 | // Set up header views. |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 118 | final ViewGroup header = mContainer.findViewById(R.id.date_picker_header); |
| 119 | mHeaderYear = header.findViewById(R.id.date_picker_header_year); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 120 | mHeaderYear.setOnClickListener(mOnHeaderClickListener); |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 121 | mHeaderMonthDay = header.findViewById(R.id.date_picker_header_date); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 122 | mHeaderMonthDay.setOnClickListener(mOnHeaderClickListener); |
Alan Viverette | 60727e0 | 2014-07-28 16:56:32 -0700 | [diff] [blame] | 123 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 124 | // For the sake of backwards compatibility, attempt to extract the text |
| 125 | // color from the header month text appearance. If it's set, we'll let |
| 126 | // that override the "real" header text color. |
| 127 | ColorStateList headerTextColor = null; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 128 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 129 | @SuppressWarnings("deprecation") |
| 130 | final int monthHeaderTextAppearance = a.getResourceId( |
Alan Viverette | 4e5168f | 2015-01-08 15:24:45 -0800 | [diff] [blame] | 131 | R.styleable.DatePicker_headerMonthTextAppearance, 0); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 132 | if (monthHeaderTextAppearance != 0) { |
| 133 | final TypedArray textAppearance = mContext.obtainStyledAttributes(null, |
| 134 | ATTRS_TEXT_COLOR, 0, monthHeaderTextAppearance); |
| 135 | final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0); |
| 136 | headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor); |
| 137 | textAppearance.recycle(); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 140 | if (headerTextColor == null) { |
| 141 | headerTextColor = a.getColorStateList(R.styleable.DatePicker_headerTextColor); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 144 | if (headerTextColor != null) { |
| 145 | mHeaderYear.setTextColor(headerTextColor); |
| 146 | mHeaderMonthDay.setTextColor(headerTextColor); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 147 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 148 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 149 | // Set up header background, if available. |
| 150 | if (a.hasValueOrEmpty(R.styleable.DatePicker_headerBackground)) { |
| 151 | header.setBackground(a.getDrawable(R.styleable.DatePicker_headerBackground)); |
| 152 | } |
| 153 | |
Alan Viverette | 60b674e | 2015-03-25 13:00:42 -0700 | [diff] [blame] | 154 | a.recycle(); |
| 155 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 156 | // Set up picker container. |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 157 | mAnimator = mContainer.findViewById(R.id.animator); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 158 | |
| 159 | // Set up day picker view. |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 160 | mDayPickerView = mAnimator.findViewById(R.id.date_picker_day_picker); |
Alan Viverette | e763c9b | 2014-11-06 15:22:31 -0800 | [diff] [blame] | 161 | mDayPickerView.setFirstDayOfWeek(mFirstDayOfWeek); |
Alan Viverette | 4612740 | 2014-11-13 10:50:37 -0800 | [diff] [blame] | 162 | mDayPickerView.setMinDate(mMinDate.getTimeInMillis()); |
| 163 | mDayPickerView.setMaxDate(mMaxDate.getTimeInMillis()); |
| 164 | mDayPickerView.setDate(mCurrentDate.getTimeInMillis()); |
Alan Viverette | e763c9b | 2014-11-06 15:22:31 -0800 | [diff] [blame] | 165 | mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener); |
Alan Viverette | 50eb025 | 2014-10-24 14:34:26 -0700 | [diff] [blame] | 166 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 167 | // Set up year picker view. |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 168 | mYearPickerView = mAnimator.findViewById(R.id.date_picker_year_picker); |
Alan Viverette | 2a90fa6 | 2015-02-17 13:09:04 -0800 | [diff] [blame] | 169 | mYearPickerView.setRange(mMinDate, mMaxDate); |
George Mount | e998c3f | 2015-10-27 08:46:44 -0700 | [diff] [blame] | 170 | mYearPickerView.setYear(mCurrentDate.get(Calendar.YEAR)); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 171 | mYearPickerView.setOnYearSelectedListener(mOnYearSelectedListener); |
Alan Viverette | 4e5168f | 2015-01-08 15:24:45 -0800 | [diff] [blame] | 172 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 173 | // Set up content descriptions. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 174 | mSelectDay = res.getString(R.string.select_day); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 175 | mSelectYear = res.getString(R.string.select_year); |
| 176 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 177 | // Initialize for current locale. This also initializes the date, so no |
| 178 | // need to call onDateChanged. |
| 179 | onLocaleChanged(mCurrentLocale); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 180 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 181 | setCurrentView(VIEW_MONTH_DAY); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 185 | * The legacy text color might have been poorly defined. Ensures that it |
| 186 | * has an appropriate activated state, using the selected state if one |
| 187 | * exists or modifying the default text color otherwise. |
| 188 | * |
| 189 | * @param color a legacy text color, or {@code null} |
| 190 | * @return a color state list with an appropriate activated state, or |
| 191 | * {@code null} if a valid activated state could not be generated |
| 192 | */ |
| 193 | @Nullable |
| 194 | private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) { |
| 195 | if (color == null || color.hasState(R.attr.state_activated)) { |
| 196 | return color; |
| 197 | } |
| 198 | |
| 199 | final int activatedColor; |
| 200 | final int defaultColor; |
| 201 | if (color.hasState(R.attr.state_selected)) { |
| 202 | activatedColor = color.getColorForState(StateSet.get( |
| 203 | StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0); |
| 204 | defaultColor = color.getColorForState(StateSet.get( |
| 205 | StateSet.VIEW_STATE_ENABLED), 0); |
| 206 | } else { |
| 207 | activatedColor = color.getDefaultColor(); |
| 208 | |
| 209 | // Generate a non-activated color using the disabled alpha. |
| 210 | final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA); |
| 211 | final float disabledAlpha = ta.getFloat(0, 0.30f); |
| 212 | defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha); |
| 213 | } |
| 214 | |
| 215 | if (activatedColor == 0 || defaultColor == 0) { |
| 216 | // We somehow failed to obtain the colors. |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}}; |
| 221 | final int[] colors = new int[] { activatedColor, defaultColor }; |
| 222 | return new ColorStateList(stateSet, colors); |
| 223 | } |
| 224 | |
| 225 | private int multiplyAlphaComponent(int color, float alphaMod) { |
| 226 | final int srcRgb = color & 0xFFFFFF; |
| 227 | final int srcAlpha = (color >> 24) & 0xFF; |
| 228 | final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f); |
| 229 | return srcRgb | (dstAlpha << 24); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Listener called when the user selects a day in the day picker view. |
| 234 | */ |
| 235 | private final OnDaySelectedListener mOnDaySelectedListener = new OnDaySelectedListener() { |
| 236 | @Override |
| 237 | public void onDaySelected(DayPickerView view, Calendar day) { |
| 238 | mCurrentDate.setTimeInMillis(day.getTimeInMillis()); |
| 239 | onDateChanged(true, true); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | /** |
| 244 | * Listener called when the user selects a year in the year picker view. |
| 245 | */ |
| 246 | private final OnYearSelectedListener mOnYearSelectedListener = new OnYearSelectedListener() { |
| 247 | @Override |
| 248 | public void onYearChanged(YearPickerView view, int year) { |
| 249 | // If the newly selected month / year does not contain the |
| 250 | // currently selected day number, change the selected day number |
| 251 | // to the last day of the selected month or year. |
| 252 | // e.g. Switching from Mar to Apr when Mar 31 is selected -> Apr 30 |
| 253 | // e.g. Switching from 2012 to 2013 when Feb 29, 2012 is selected -> Feb 28, 2013 |
| 254 | final int day = mCurrentDate.get(Calendar.DAY_OF_MONTH); |
| 255 | final int month = mCurrentDate.get(Calendar.MONTH); |
| 256 | final int daysInMonth = getDaysInMonth(month, year); |
| 257 | if (day > daysInMonth) { |
| 258 | mCurrentDate.set(Calendar.DAY_OF_MONTH, daysInMonth); |
| 259 | } |
| 260 | |
| 261 | mCurrentDate.set(Calendar.YEAR, year); |
| 262 | onDateChanged(true, true); |
| 263 | |
| 264 | // Automatically switch to day picker. |
| 265 | setCurrentView(VIEW_MONTH_DAY); |
George Mount | e998c3f | 2015-10-27 08:46:44 -0700 | [diff] [blame] | 266 | |
| 267 | // Switch focus back to the year text. |
| 268 | mHeaderYear.requestFocus(); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 269 | } |
| 270 | }; |
| 271 | |
| 272 | /** |
| 273 | * Listener called when the user clicks on a header item. |
| 274 | */ |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 275 | private final OnClickListener mOnHeaderClickListener = v -> { |
| 276 | tryVibrate(); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 277 | |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 278 | switch (v.getId()) { |
| 279 | case R.id.date_picker_header_year: |
| 280 | setCurrentView(VIEW_YEAR); |
| 281 | break; |
| 282 | case R.id.date_picker_header_date: |
| 283 | setCurrentView(VIEW_MONTH_DAY); |
| 284 | break; |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 285 | } |
| 286 | }; |
| 287 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 288 | @Override |
| 289 | protected void onLocaleChanged(Locale locale) { |
| 290 | final TextView headerYear = mHeaderYear; |
| 291 | if (headerYear == null) { |
| 292 | // Abort, we haven't initialized yet. This method will get called |
| 293 | // again later after everything has been set up. |
| 294 | return; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 295 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 296 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 297 | // Update the date formatter. |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 298 | mMonthDayFormat = DateFormat.getInstanceForSkeleton("EMMMd", locale); |
Alan Viverette | c3e5a82 | 2016-05-16 13:35:56 -0400 | [diff] [blame] | 299 | mMonthDayFormat.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE); |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 300 | mYearFormat = DateFormat.getInstanceForSkeleton("y", locale); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 301 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 302 | // Update the header text. |
| 303 | onCurrentDateChanged(false); |
| 304 | } |
| 305 | |
| 306 | private void onCurrentDateChanged(boolean announce) { |
| 307 | if (mHeaderYear == null) { |
| 308 | // Abort, we haven't initialized yet. This method will get called |
| 309 | // again later after everything has been set up. |
| 310 | return; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 313 | final String year = mYearFormat.format(mCurrentDate.getTime()); |
| 314 | mHeaderYear.setText(year); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 315 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 316 | final String monthDay = mMonthDayFormat.format(mCurrentDate.getTime()); |
| 317 | mHeaderMonthDay.setText(monthDay); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 318 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 319 | // TODO: This should use live regions. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 320 | if (announce) { |
Andrei Stingaceanu | dd80f00 | 2016-06-16 12:00:39 +0100 | [diff] [blame] | 321 | mAnimator.announceForAccessibility(getFormattedCurrentDate()); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 322 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | private void setCurrentView(final int viewIndex) { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 326 | switch (viewIndex) { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 327 | case VIEW_MONTH_DAY: |
| 328 | mDayPickerView.setDate(mCurrentDate.getTimeInMillis()); |
| 329 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 330 | if (mCurrentView != viewIndex) { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 331 | mHeaderMonthDay.setActivated(true); |
| 332 | mHeaderYear.setActivated(false); |
| 333 | mAnimator.setDisplayedChild(VIEW_MONTH_DAY); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 334 | mCurrentView = viewIndex; |
| 335 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 336 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 337 | mAnimator.announceForAccessibility(mSelectDay); |
| 338 | break; |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 339 | case VIEW_YEAR: |
George Mount | e998c3f | 2015-10-27 08:46:44 -0700 | [diff] [blame] | 340 | final int year = mCurrentDate.get(Calendar.YEAR); |
| 341 | mYearPickerView.setYear(year); |
Alan Viverette | ca38c43 | 2017-08-18 10:44:08 -0400 | [diff] [blame] | 342 | mYearPickerView.post(() -> { |
| 343 | mYearPickerView.requestFocus(); |
| 344 | final View selected = mYearPickerView.getSelectedView(); |
| 345 | if (selected != null) { |
| 346 | selected.requestFocus(); |
George Mount | e998c3f | 2015-10-27 08:46:44 -0700 | [diff] [blame] | 347 | } |
| 348 | }); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 349 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 350 | if (mCurrentView != viewIndex) { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 351 | mHeaderMonthDay.setActivated(false); |
| 352 | mHeaderYear.setActivated(true); |
| 353 | mAnimator.setDisplayedChild(VIEW_YEAR); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 354 | mCurrentView = viewIndex; |
| 355 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 356 | |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 357 | mAnimator.announceForAccessibility(mSelectYear); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | @Override |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 363 | public void init(int year, int month, int dayOfMonth, |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 364 | DatePicker.OnDateChangedListener callBack) { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 365 | setDate(year, month, dayOfMonth); |
Alan Viverette | 0cecbc9 | 2014-12-12 14:32:43 -0800 | [diff] [blame] | 366 | onDateChanged(false, false); |
Alan Viverette | a9a75f5 | 2016-04-01 13:22:10 -0400 | [diff] [blame] | 367 | |
Clara Bayarri | 68640b6 | 2016-06-02 14:56:18 +0100 | [diff] [blame] | 368 | mOnDateChangedListener = callBack; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void updateDate(int year, int month, int dayOfMonth) { |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 373 | setDate(year, month, dayOfMonth); |
| 374 | onDateChanged(false, true); |
| 375 | } |
| 376 | |
| 377 | private void setDate(int year, int month, int dayOfMonth) { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 378 | mCurrentDate.set(Calendar.YEAR, year); |
| 379 | mCurrentDate.set(Calendar.MONTH, month); |
| 380 | mCurrentDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); |
Felipe Leme | f480e8c | 2017-08-10 18:38:44 -0700 | [diff] [blame] | 381 | resetAutofilledValue(); |
Alan Viverette | e6ec07f | 2014-12-10 11:24:27 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Alan Viverette | 0cecbc9 | 2014-12-12 14:32:43 -0800 | [diff] [blame] | 384 | private void onDateChanged(boolean fromUser, boolean callbackToClient) { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 385 | final int year = mCurrentDate.get(Calendar.YEAR); |
| 386 | |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 387 | if (callbackToClient |
| 388 | && (mOnDateChangedListener != null || mAutoFillChangeListener != null)) { |
Alan Viverette | e6ec07f | 2014-12-10 11:24:27 -0800 | [diff] [blame] | 389 | final int monthOfYear = mCurrentDate.get(Calendar.MONTH); |
| 390 | final int dayOfMonth = mCurrentDate.get(Calendar.DAY_OF_MONTH); |
Felipe Leme | 305b72c | 2017-02-27 12:46:04 -0800 | [diff] [blame] | 391 | if (mOnDateChangedListener != null) { |
| 392 | mOnDateChangedListener.onDateChanged(mDelegator, year, monthOfYear, dayOfMonth); |
| 393 | } |
| 394 | if (mAutoFillChangeListener != null) { |
| 395 | mAutoFillChangeListener.onDateChanged(mDelegator, year, monthOfYear, dayOfMonth); |
| 396 | } |
Alan Viverette | 9468c6a | 2014-08-21 13:56:54 -0700 | [diff] [blame] | 397 | } |
Alan Viverette | e6ec07f | 2014-12-10 11:24:27 -0800 | [diff] [blame] | 398 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 399 | mDayPickerView.setDate(mCurrentDate.getTimeInMillis()); |
| 400 | mYearPickerView.setYear(year); |
Alan Viverette | e6ec07f | 2014-12-10 11:24:27 -0800 | [diff] [blame] | 401 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 402 | onCurrentDateChanged(fromUser); |
Alan Viverette | e6ec07f | 2014-12-10 11:24:27 -0800 | [diff] [blame] | 403 | |
| 404 | if (fromUser) { |
| 405 | tryVibrate(); |
| 406 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public int getYear() { |
| 411 | return mCurrentDate.get(Calendar.YEAR); |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public int getMonth() { |
| 416 | return mCurrentDate.get(Calendar.MONTH); |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public int getDayOfMonth() { |
| 421 | return mCurrentDate.get(Calendar.DAY_OF_MONTH); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void setMinDate(long minDate) { |
| 426 | mTempDate.setTimeInMillis(minDate); |
| 427 | if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR) |
Alan Viverette | 3fb5c7b | 2016-05-25 12:34:55 -0400 | [diff] [blame] | 428 | && mTempDate.get(Calendar.DAY_OF_YEAR) == mMinDate.get(Calendar.DAY_OF_YEAR)) { |
| 429 | // Same day, no-op. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | if (mCurrentDate.before(mTempDate)) { |
| 433 | mCurrentDate.setTimeInMillis(minDate); |
Alan Viverette | 0cecbc9 | 2014-12-12 14:32:43 -0800 | [diff] [blame] | 434 | onDateChanged(false, true); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 435 | } |
| 436 | mMinDate.setTimeInMillis(minDate); |
Alan Viverette | 4612740 | 2014-11-13 10:50:37 -0800 | [diff] [blame] | 437 | mDayPickerView.setMinDate(minDate); |
Alan Viverette | 50eb025 | 2014-10-24 14:34:26 -0700 | [diff] [blame] | 438 | mYearPickerView.setRange(mMinDate, mMaxDate); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public Calendar getMinDate() { |
| 443 | return mMinDate; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public void setMaxDate(long maxDate) { |
| 448 | mTempDate.setTimeInMillis(maxDate); |
| 449 | if (mTempDate.get(Calendar.YEAR) == mMaxDate.get(Calendar.YEAR) |
Alan Viverette | 3fb5c7b | 2016-05-25 12:34:55 -0400 | [diff] [blame] | 450 | && mTempDate.get(Calendar.DAY_OF_YEAR) == mMaxDate.get(Calendar.DAY_OF_YEAR)) { |
| 451 | // Same day, no-op. |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 452 | return; |
| 453 | } |
| 454 | if (mCurrentDate.after(mTempDate)) { |
| 455 | mCurrentDate.setTimeInMillis(maxDate); |
Alan Viverette | 0cecbc9 | 2014-12-12 14:32:43 -0800 | [diff] [blame] | 456 | onDateChanged(false, true); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 457 | } |
| 458 | mMaxDate.setTimeInMillis(maxDate); |
Alan Viverette | 4612740 | 2014-11-13 10:50:37 -0800 | [diff] [blame] | 459 | mDayPickerView.setMaxDate(maxDate); |
Alan Viverette | 50eb025 | 2014-10-24 14:34:26 -0700 | [diff] [blame] | 460 | mYearPickerView.setRange(mMinDate, mMaxDate); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public Calendar getMaxDate() { |
| 465 | return mMaxDate; |
| 466 | } |
| 467 | |
| 468 | @Override |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 469 | public void setFirstDayOfWeek(int firstDayOfWeek) { |
| 470 | mFirstDayOfWeek = firstDayOfWeek; |
Alan Viverette | e763c9b | 2014-11-06 15:22:31 -0800 | [diff] [blame] | 471 | |
| 472 | mDayPickerView.setFirstDayOfWeek(firstDayOfWeek); |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | @Override |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 476 | public int getFirstDayOfWeek() { |
Alan Viverette | 0a04bb0 | 2014-09-03 20:48:02 -0700 | [diff] [blame] | 477 | if (mFirstDayOfWeek != USE_LOCALE) { |
| 478 | return mFirstDayOfWeek; |
| 479 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 480 | return mCurrentDate.getFirstDayOfWeek(); |
| 481 | } |
| 482 | |
| 483 | @Override |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 484 | public void setEnabled(boolean enabled) { |
Alan Viverette | f7aa925 | 2015-06-03 14:07:26 -0700 | [diff] [blame] | 485 | mContainer.setEnabled(enabled); |
| 486 | mDayPickerView.setEnabled(enabled); |
| 487 | mYearPickerView.setEnabled(enabled); |
| 488 | mHeaderYear.setEnabled(enabled); |
| 489 | mHeaderMonthDay.setEnabled(enabled); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public boolean isEnabled() { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 494 | return mContainer.isEnabled(); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | @Override |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 498 | public CalendarView getCalendarView() { |
Alan Viverette | f7aa925 | 2015-06-03 14:07:26 -0700 | [diff] [blame] | 499 | throw new UnsupportedOperationException("Not supported by calendar-mode DatePicker"); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void setCalendarViewShown(boolean shown) { |
| 504 | // No-op for compatibility with the old DatePicker. |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public boolean getCalendarViewShown() { |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void setSpinnersShown(boolean shown) { |
| 514 | // No-op for compatibility with the old DatePicker. |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public boolean getSpinnersShown() { |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | @Override |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 523 | public void onConfigurationChanged(Configuration newConfig) { |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 524 | setCurrentLocale(newConfig.locale); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | @Override |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 528 | public Parcelable onSaveInstanceState(Parcelable superState) { |
| 529 | final int year = mCurrentDate.get(Calendar.YEAR); |
| 530 | final int month = mCurrentDate.get(Calendar.MONTH); |
| 531 | final int day = mCurrentDate.get(Calendar.DAY_OF_MONTH); |
| 532 | |
| 533 | int listPosition = -1; |
| 534 | int listPositionOffset = -1; |
| 535 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 536 | if (mCurrentView == VIEW_MONTH_DAY) { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 537 | listPosition = mDayPickerView.getMostVisiblePosition(); |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 538 | } else if (mCurrentView == VIEW_YEAR) { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 539 | listPosition = mYearPickerView.getFirstVisiblePosition(); |
| 540 | listPositionOffset = mYearPickerView.getFirstPositionOffset(); |
| 541 | } |
| 542 | |
| 543 | return new SavedState(superState, year, month, day, mMinDate.getTimeInMillis(), |
| 544 | mMaxDate.getTimeInMillis(), mCurrentView, listPosition, listPositionOffset); |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void onRestoreInstanceState(Parcelable state) { |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 549 | if (state instanceof SavedState) { |
| 550 | final SavedState ss = (SavedState) state; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 551 | |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 552 | // TODO: Move instance state into DayPickerView, YearPickerView. |
| 553 | mCurrentDate.set(ss.getSelectedYear(), ss.getSelectedMonth(), ss.getSelectedDay()); |
| 554 | mMinDate.setTimeInMillis(ss.getMinDate()); |
| 555 | mMaxDate.setTimeInMillis(ss.getMaxDate()); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 556 | |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 557 | onCurrentDateChanged(false); |
Alan Viverette | 816aa14 | 2015-04-10 15:41:10 -0700 | [diff] [blame] | 558 | |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 559 | final int currentView = ss.getCurrentView(); |
| 560 | setCurrentView(currentView); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 561 | |
Alan Viverette | 6b3f85f | 2016-03-01 16:48:04 -0500 | [diff] [blame] | 562 | final int listPosition = ss.getListPosition(); |
| 563 | if (listPosition != -1) { |
| 564 | if (currentView == VIEW_MONTH_DAY) { |
| 565 | mDayPickerView.setPosition(listPosition); |
| 566 | } else if (currentView == VIEW_YEAR) { |
| 567 | final int listPositionOffset = ss.getListPositionOffset(); |
| 568 | mYearPickerView.setSelectionFromTop(listPosition, listPositionOffset); |
| 569 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 576 | onPopulateAccessibilityEvent(event); |
| 577 | return true; |
| 578 | } |
| 579 | |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 580 | public CharSequence getAccessibilityClassName() { |
| 581 | return DatePicker.class.getName(); |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Neil Fuller | 9900c52 | 2019-04-17 08:48:23 +0100 | [diff] [blame] | 584 | private static int getDaysInMonth(int month, int year) { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 585 | switch (month) { |
| 586 | case Calendar.JANUARY: |
| 587 | case Calendar.MARCH: |
| 588 | case Calendar.MAY: |
| 589 | case Calendar.JULY: |
| 590 | case Calendar.AUGUST: |
| 591 | case Calendar.OCTOBER: |
| 592 | case Calendar.DECEMBER: |
| 593 | return 31; |
| 594 | case Calendar.APRIL: |
| 595 | case Calendar.JUNE: |
| 596 | case Calendar.SEPTEMBER: |
| 597 | case Calendar.NOVEMBER: |
| 598 | return 30; |
| 599 | case Calendar.FEBRUARY: |
Neil Fuller | 6c0984a | 2019-04-17 15:54:22 +0100 | [diff] [blame] | 600 | return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28; |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 601 | default: |
| 602 | throw new IllegalArgumentException("Invalid Month"); |
| 603 | } |
| 604 | } |
| 605 | |
Alan Viverette | 0ef59ac | 2015-03-23 13:13:25 -0700 | [diff] [blame] | 606 | private void tryVibrate() { |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 607 | mDelegator.performHapticFeedback(HapticFeedbackConstants.CALENDAR_DATE); |
| 608 | } |
Fabrice Di Meglio | bd9152f | 2013-10-01 11:21:31 -0700 | [diff] [blame] | 609 | } |