1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package androidx.leanback.widget; 15 16 import android.content.Context; 17 import android.os.Bundle; 18 19 import java.util.Calendar; 20 import java.util.TimeZone; 21 22 /** 23 * Subclass of GuidedAction that can choose a date. The Action is editable by default; to make it 24 * read only, call hasEditableActivatorView(false) on the Builder. 25 */ 26 public class GuidedDatePickerAction extends GuidedAction { 27 28 /** 29 * Base Builder class to build GuidedDatePickerAction. Subclass this BuilderBase when app needs 30 * to subclass GuidedDatePickerAction, implement your build() which should call 31 * {@link #applyDatePickerValues(GuidedDatePickerAction)}. When using GuidedDatePickerAction 32 * directly, use {@link Builder}. 33 */ 34 public abstract static class BuilderBase<B extends BuilderBase> 35 extends GuidedAction.BuilderBase<B> { 36 37 private String mDatePickerFormat; 38 private long mDate; 39 private long mMinDate = Long.MIN_VALUE; 40 private long mMaxDate = Long.MAX_VALUE; 41 BuilderBase(Context context)42 public BuilderBase(Context context) { 43 super(context); 44 Calendar c = Calendar.getInstance(); 45 mDate = c.getTimeInMillis(); 46 hasEditableActivatorView(true); 47 } 48 49 /** 50 * Sets format of date Picker or null for default. The format is a case insensitive String 51 * containing the day ('d'), month ('m'), and year ('y'). When the format is not specified, 52 * a default format of current locale will be used. 53 * @param format Format of showing Date, e.g. "YMD". 54 * @return This Builder object. 55 */ datePickerFormat(String format)56 public B datePickerFormat(String format) { 57 mDatePickerFormat = format; 58 return (B) this; 59 } 60 61 /** 62 * Sets a Date for date picker in milliseconds since January 1, 1970 00:00:00 in 63 * {@link TimeZone#getDefault()} time zone. 64 * @return This Builder Object. 65 */ date(long date)66 public B date(long date) { 67 mDate = date; 68 return (B) this; 69 } 70 71 /** 72 * Sets minimal Date for date picker in milliseconds since January 1, 1970 00:00:00 in 73 * {@link TimeZone#getDefault()} time zone. 74 * @return This Builder Object. 75 */ minDate(long minDate)76 public B minDate(long minDate) { 77 mMinDate = minDate; 78 return (B) this; 79 } 80 81 /** 82 * Sets maximum Date for date picker in milliseconds since January 1, 1970 00:00:00 in 83 * {@link TimeZone#getDefault()} time zone. 84 * @return This Builder Object. 85 */ maxDate(long maxDate)86 public B maxDate(long maxDate) { 87 mMaxDate = maxDate; 88 return (B) this; 89 } 90 91 /** 92 * Apply values to GuidedDatePickerAction. 93 * @param action GuidedDatePickerAction to apply values. 94 */ applyDatePickerValues(GuidedDatePickerAction action)95 protected final void applyDatePickerValues(GuidedDatePickerAction action) { 96 super.applyValues(action); 97 action.mDatePickerFormat = mDatePickerFormat; 98 action.mDate = mDate; 99 if (mMinDate > mMaxDate) { 100 throw new IllegalArgumentException("MinDate cannot be larger than MaxDate"); 101 } 102 action.mMinDate = mMinDate; 103 action.mMaxDate = mMaxDate; 104 } 105 106 } 107 108 /** 109 * Builder class to build a GuidedDatePickerAction. 110 */ 111 public final static class Builder extends BuilderBase<Builder> { Builder(Context context)112 public Builder(Context context) { 113 super(context); 114 } 115 116 /** 117 * Builds the GuidedDatePickerAction corresponding to this Builder. 118 * @return The GuidedDatePickerAction as configured through this Builder. 119 */ build()120 public GuidedDatePickerAction build() { 121 GuidedDatePickerAction action = new GuidedDatePickerAction(); 122 applyDatePickerValues(action); 123 return action; 124 } 125 } 126 127 String mDatePickerFormat; 128 long mDate; 129 long mMinDate = Long.MIN_VALUE; 130 long mMaxDate = Long.MAX_VALUE; 131 132 /** 133 * Returns format of date Picker or null if not specified. The format is a case insensitive 134 * String containing the * day ('d'), month ('m'), and year ('y'). When the format is not 135 * specified, a default format of current locale will 136 * be used. 137 * @return Format of showing Date, e.g. "YMD". Returns null if using current locale's default. 138 */ getDatePickerFormat()139 public String getDatePickerFormat() { 140 return mDatePickerFormat; 141 } 142 143 /** 144 * Get current value of DatePicker in milliseconds since January 1, 1970 00:00:00 in 145 * {@link TimeZone#getDefault()} time zone. 146 * @return Current value of DatePicker Action. 147 */ getDate()148 public long getDate() { 149 return mDate; 150 } 151 152 /** 153 * Sets current value of DatePicker in milliseconds since January 1, 1970 00:00:00 in 154 * {@link TimeZone#getDefault()} time zone. 155 * @param date New value to update current value of DatePicker Action. 156 */ setDate(long date)157 public void setDate(long date) { 158 mDate = date; 159 } 160 161 /** 162 * Get minimal value of DatePicker in milliseconds since January 1, 1970 00:00:00 in 163 * {@link TimeZone#getDefault()} time zone. -1 if not set. 164 * @return Minimal value of DatePicker Action or Long.MIN_VALUE if not set. 165 */ getMinDate()166 public long getMinDate() { 167 return mMinDate; 168 } 169 170 /** 171 * Get maximum value of DatePicker in milliseconds since January 1, 1970 00:00:00 in 172 * {@link TimeZone#getDefault()} time zone. 173 * @return Maximum value of DatePicker Action or Long.MAX_VALUE if not set. 174 */ getMaxDate()175 public long getMaxDate() { 176 return mMaxDate; 177 } 178 179 @Override onSaveInstanceState(Bundle bundle, String key)180 public void onSaveInstanceState(Bundle bundle, String key) { 181 bundle.putLong(key, getDate()); 182 } 183 184 @Override onRestoreInstanceState(Bundle bundle, String key)185 public void onRestoreInstanceState(Bundle bundle, String key) { 186 setDate(bundle.getLong(key, getDate())); 187 } 188 } 189