GuidedDatePickerAction.java revision b88b36aa081a500eb0e9d4be0bac85b33cd57dde
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.support.v17.leanback.widget.picker.DatePicker;
18
19import java.util.Calendar;
20
21/**
22 * Subclass of GuidedAction that can choose a date.  The Action is editable by default; to make it
23 * read only, call hasEditableActivatorView(false) on the Builder.
24 */
25public class GuidedDatePickerAction extends GuidedAction {
26
27    /**
28     * Base Builder class to build GuidedDatePickerAction.  Subclass this BuilderBase when app needs
29     * to subclass GuidedDatePickerAction, implement your build() which should call
30     * {@link #applyDatePickerValues(GuidedDatePickerAction)}.  When using GuidedDatePickerAction
31     * directly, use {@link Builder}.
32     */
33    public abstract static class BuilderBase<B extends BuilderBase>
34            extends GuidedAction.BuilderBase<B> {
35
36        private String mDatePickerFormat;
37        private long mDate;
38
39        public BuilderBase(Context context) {
40            super(context);
41            Calendar c = Calendar.getInstance();
42            mDate = c.getTimeInMillis();
43            hasEditableActivatorView(true);
44        }
45
46        /**
47         * Sets format of date Picker.  When the format is not specified,
48         * a default format of current locale will be used.
49         * @param format Format of showing Date, e.g. "YMD"
50         * @return This Builder object.
51         */
52        public B datePickerFormat(String format) {
53            mDatePickerFormat = format;
54            return (B) this;
55        }
56
57        /**
58         * Sets a Date for date picker, see {@link Calendar#getTimeInMillis()}.
59         * @param date See {@link Calendar#getTimeInMillis()}.
60         * @return This Builder Object.
61         */
62        public B date(long date) {
63            mDate = date;
64            return (B) this;
65        }
66
67        /**
68         * Apply values to GuidedDatePickerAction.
69         * @param action GuidedDatePickerAction to apply values.
70         */
71        protected final void applyDatePickerValues(GuidedDatePickerAction action) {
72            super.applyValues(action);
73            action.mDatePickerFormat = mDatePickerFormat;
74            action.mDate = mDate;
75        }
76
77    }
78
79    /**
80     * Builder class to build a GuidedDatePickerAction.
81     */
82    public final static class Builder extends BuilderBase<Builder> {
83        public Builder(Context context) {
84            super(context);
85        }
86
87        /**
88         * Builds the GuidedDatePickerAction corresponding to this Builder.
89         * @return The GuidedDatePickerAction as configured through this Builder.
90         */
91        public GuidedDatePickerAction build() {
92            GuidedDatePickerAction action = new GuidedDatePickerAction();
93            applyDatePickerValues(action);
94            return action;
95        }
96    }
97
98    private String mDatePickerFormat;
99    private long mDate;
100
101    /**
102     * Returns format of date Picker or null if not specified.  When the
103     * format is not specified, a default format of current locale will be used.
104     * @return Format of showing Date, e.g. "YMD".  Returns null if using current locale's default.
105     */
106    public String getDatePickerFormat() {
107        return mDatePickerFormat;
108    }
109
110    /**
111     * Get current value of DatePicker;
112     * @return Current value of DatePicker;
113     */
114    public long getDate() {
115        return mDate;
116    }
117
118    /**
119     * Sets current value of DatePicker;
120     * @param date New value to update current value of DatePicker;
121     */
122    public void setDate(long date) {
123        mDate = date;
124    }
125}
126