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