PickerColumn.java revision 71d30e1cf5514761ba8ad4bd3c8c70540d60dbd3
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
15package android.support.v17.leanback.widget.picker;
16
17import android.os.Parcel;
18import android.os.Parcelable;
19
20/**
21 * Picker column class used by {@link Picker}, defines a contiguous value ranges and associated
22 * labels.  A PickerColumn has a minValue and maxValue to choose between.  The Picker column has
23 * a current value.
24 * The labels can be dynamically generated from value by {@link #setValueLabelFormat(String)} or
25 * a list of static labels set by {@link #setValueStaticLabels(String[])}.
26 */
27public class PickerColumn implements Parcelable {
28
29    private int mCurrentValue;
30    private int mMinValue;
31    private int mMaxValue;
32    private String[] mStaticLabels;
33    private String mValueFormat;
34
35    public PickerColumn() {
36    }
37
38    public PickerColumn(Parcel source) {
39        mValueFormat = source.readString();
40        int count = source.readInt();
41        if (count > 0) {
42            mStaticLabels = new String[count];
43            source.readStringArray(mStaticLabels);
44        }
45        mCurrentValue = source.readInt();
46        mMinValue = source.readInt();
47        mMaxValue = source.readInt();
48    }
49
50    /**
51     * Set string format to display label for value, e.g. "%02d".  The string format is only
52     * used when {@link #setValueStaticLabels(String[])} is not called.
53     * @param valueFormat String format to display label for value.
54     */
55    public void setValueLabelFormat(String valueFormat) {
56        mValueFormat = valueFormat;
57    }
58
59    /**
60     * Return string format to display label for value, e.g. "%02d".
61     * @return String format to display label for value.
62     */
63    public String getValueLabelFormat() {
64        return mValueFormat;
65    }
66
67    /**
68     * Set static labels for each value, minValue maps to labels[0], maxValue maps to
69     * labels[labels.length - 1].
70     * @param labels Static labels for each value between minValue and maxValue.
71     */
72    public void setValueStaticLabels(String[] labels) {
73        mStaticLabels = labels;
74    }
75
76    /**
77     * Get a label for value.  The label can be static ({@link #setValueStaticLabels(String[])} or
78     * dynamically generated (@link {@link #setValueLabelFormat(String)}.
79     * @param value Value between minValue and maxValue.
80     * @return Label for the value.
81     */
82    public String getValueLabelAt(int value) {
83        if (mStaticLabels == null) {
84            return String.format(mValueFormat, value);
85        }
86        return mStaticLabels[value];
87    }
88
89    /**
90     * Returns current value of the Column.
91     * @return Current value of the Column.
92     */
93    public int getCurrentValue() {
94        return mCurrentValue;
95    }
96
97    /**
98     * Sets current value of the Column.
99     * @return True if current value has changed.
100     */
101    public boolean setCurrentValue(int value) {
102        if (mCurrentValue != value) {
103            mCurrentValue = value;
104            return true;
105        }
106        return false;
107    }
108
109    /**
110     * Get total items count between minValue(inclusive) and maxValue (inclusive).
111     * @return Total items count between minValue(inclusive) and maxValue (inclusive).
112     */
113    public int getItemsCount() {
114        return mMaxValue - mMinValue + 1;
115    }
116
117    /**
118     * Returns minimal value (inclusive) of the Column.
119     * @return Minimal value (inclusive) of the Column.
120     */
121    public int getMinValue() {
122        return mMinValue;
123    }
124
125    /**
126     * Returns maximum value (inclusive) of the Column.
127     * @return Maximum value (inclusive) of the Column.
128     */
129    public int getMaxValue() {
130        return mMaxValue;
131    }
132
133    /**
134     * Sets minimal value (inclusive) of the Column.
135     * @param minValue New minimal value to set.
136     * @return True if minimal value changes.
137     */
138    public boolean setMinValue(int minValue) {
139        if (minValue != mMinValue) {
140            mMinValue = minValue;
141            return true;
142        }
143        return false;
144    }
145
146    /**
147     * Sets maximum value (inclusive) of the Column.
148     * @param maxValue New maximum value to set.
149     * @return True if maximum value changes.
150     */
151    public boolean setMaxValue(int maxValue) {
152        if (maxValue != mMaxValue) {
153            mMaxValue = maxValue;
154            return true;
155        }
156        return false;
157    }
158
159    public static Parcelable.Creator<PickerColumn>
160            CREATOR = new Parcelable.Creator<PickerColumn>() {
161
162                @Override
163                public PickerColumn createFromParcel(Parcel source) {
164                    return new PickerColumn(source);
165                }
166
167                @Override
168                public PickerColumn[] newArray(int size) {
169                    return new PickerColumn[size];
170                }
171            };
172
173    @Override
174    public int describeContents() {
175        return 0;
176    }
177
178    @Override
179    public void writeToParcel(Parcel dest, int flags) {
180        dest.writeString(mValueFormat);
181        if (mStaticLabels != null) {
182            dest.writeInt(mStaticLabels.length);
183            dest.writeStringArray(mStaticLabels);
184        } else {
185            dest.writeInt(0);
186        }
187        dest.writeInt(mCurrentValue);
188        dest.writeInt(mMinValue);
189        dest.writeInt(mMaxValue);
190    }
191
192}
193