PickerColumn.java revision 71d30e1cf5514761ba8ad4bd3c8c70540d60dbd3
171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu/* 271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Copyright (C) 2015 The Android Open Source Project 371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * 471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * in compliance with the License. You may obtain a copy of the License at 671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * 771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * http://www.apache.org/licenses/LICENSE-2.0 871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * 971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Unless required by applicable law or agreed to in writing, software distributed under the License 1071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 1171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * or implied. See the License for the specific language governing permissions and limitations under 1271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * the License. 1371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 1471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 1571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gupackage android.support.v17.leanback.widget.picker; 1671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 1771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Guimport android.os.Parcel; 1871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Guimport android.os.Parcelable; 1971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 2071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu/** 2171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Picker column class used by {@link Picker}, defines a contiguous value ranges and associated 2271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * labels. A PickerColumn has a minValue and maxValue to choose between. The Picker column has 2371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * a current value. 2471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * The labels can be dynamically generated from value by {@link #setValueLabelFormat(String)} or 2571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * a list of static labels set by {@link #setValueStaticLabels(String[])}. 2671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 2771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gupublic class PickerColumn implements Parcelable { 2871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 2971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu private int mCurrentValue; 3071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu private int mMinValue; 3171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu private int mMaxValue; 3271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu private String[] mStaticLabels; 3371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu private String mValueFormat; 3471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 3571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public PickerColumn() { 3671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 3771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 3871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public PickerColumn(Parcel source) { 3971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mValueFormat = source.readString(); 4071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu int count = source.readInt(); 4171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu if (count > 0) { 4271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mStaticLabels = new String[count]; 4371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu source.readStringArray(mStaticLabels); 4471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 4571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mCurrentValue = source.readInt(); 4671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mMinValue = source.readInt(); 4771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mMaxValue = source.readInt(); 4871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 4971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 5071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 5171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Set string format to display label for value, e.g. "%02d". The string format is only 5271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * used when {@link #setValueStaticLabels(String[])} is not called. 5371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @param valueFormat String format to display label for value. 5471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 5571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public void setValueLabelFormat(String valueFormat) { 5671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mValueFormat = valueFormat; 5771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 5871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 5971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 6071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Return string format to display label for value, e.g. "%02d". 6171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return String format to display label for value. 6271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 6371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public String getValueLabelFormat() { 6471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return mValueFormat; 6571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 6671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 6771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 6871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Set static labels for each value, minValue maps to labels[0], maxValue maps to 6971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * labels[labels.length - 1]. 7071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @param labels Static labels for each value between minValue and maxValue. 7171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 7271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public void setValueStaticLabels(String[] labels) { 7371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mStaticLabels = labels; 7471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 7571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 7671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 7771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Get a label for value. The label can be static ({@link #setValueStaticLabels(String[])} or 7871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * dynamically generated (@link {@link #setValueLabelFormat(String)}. 7971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @param value Value between minValue and maxValue. 8071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return Label for the value. 8171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 8271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public String getValueLabelAt(int value) { 8371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu if (mStaticLabels == null) { 8471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return String.format(mValueFormat, value); 8571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 8671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return mStaticLabels[value]; 8771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 8871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 8971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 9071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Returns current value of the Column. 9171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return Current value of the Column. 9271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 9371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public int getCurrentValue() { 9471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return mCurrentValue; 9571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 9671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 9771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 9871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Sets current value of the Column. 9971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return True if current value has changed. 10071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 10171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public boolean setCurrentValue(int value) { 10271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu if (mCurrentValue != value) { 10371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mCurrentValue = value; 10471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return true; 10571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 10671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return false; 10771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 10871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 10971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 11071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Get total items count between minValue(inclusive) and maxValue (inclusive). 11171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return Total items count between minValue(inclusive) and maxValue (inclusive). 11271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 11371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public int getItemsCount() { 11471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return mMaxValue - mMinValue + 1; 11571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 11671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 11771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 11871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Returns minimal value (inclusive) of the Column. 11971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return Minimal value (inclusive) of the Column. 12071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 12171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public int getMinValue() { 12271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return mMinValue; 12371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 12471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 12571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 12671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Returns maximum value (inclusive) of the Column. 12771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return Maximum value (inclusive) of the Column. 12871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 12971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public int getMaxValue() { 13071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return mMaxValue; 13171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 13271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 13371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 13471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Sets minimal value (inclusive) of the Column. 13571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @param minValue New minimal value to set. 13671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return True if minimal value changes. 13771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 13871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public boolean setMinValue(int minValue) { 13971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu if (minValue != mMinValue) { 14071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mMinValue = minValue; 14171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return true; 14271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 14371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return false; 14471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 14571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 14671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu /** 14771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Sets maximum value (inclusive) of the Column. 14871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @param maxValue New maximum value to set. 14971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * @return True if maximum value changes. 15071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */ 15171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public boolean setMaxValue(int maxValue) { 15271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu if (maxValue != mMaxValue) { 15371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu mMaxValue = maxValue; 15471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return true; 15571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 15671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return false; 15771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 15871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 15971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public static Parcelable.Creator<PickerColumn> 16071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu CREATOR = new Parcelable.Creator<PickerColumn>() { 16171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 16271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu @Override 16371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public PickerColumn createFromParcel(Parcel source) { 16471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return new PickerColumn(source); 16571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 16671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 16771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu @Override 16871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public PickerColumn[] newArray(int size) { 16971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return new PickerColumn[size]; 17071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 17171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu }; 17271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 17371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu @Override 17471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public int describeContents() { 17571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu return 0; 17671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 17771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 17871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu @Override 17971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu public void writeToParcel(Parcel dest, int flags) { 18071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeString(mValueFormat); 18171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu if (mStaticLabels != null) { 18271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeInt(mStaticLabels.length); 18371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeStringArray(mStaticLabels); 18471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } else { 18571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeInt(0); 18671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 18771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeInt(mCurrentValue); 18871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeInt(mMinValue); 18971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu dest.writeInt(mMaxValue); 19071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu } 19171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu 19271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu} 193