blob: 34040d79b1dae93eca8148daf2b3b67dceff6ebd [file] [log] [blame]
Michael Kolb8872c232013-01-29 10:33:22 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera;
18
Sascha Haeberling8e963a52013-08-06 11:43:02 -070019import java.util.ArrayList;
20import java.util.List;
21
Michael Kolb8872c232013-01-29 10:33:22 -080022import android.content.Context;
23import android.content.SharedPreferences;
24import android.content.res.TypedArray;
25import android.util.AttributeSet;
26import android.util.Log;
27import android.util.TypedValue;
28
Angus Kongb50b5cb2013-08-09 14:55:20 -070029import com.android.camera.util.CameraUtil;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070030import com.android.camera2.R;
Michael Kolb8872c232013-01-29 10:33:22 -080031
32/**
33 * A type of <code>CameraPreference</code> whose number of possible values
34 * is limited.
35 */
36public class ListPreference extends CameraPreference {
37 private static final String TAG = "ListPreference";
38 private final String mKey;
39 private String mValue;
40 private final CharSequence[] mDefaultValues;
41
42 private CharSequence[] mEntries;
43 private CharSequence[] mEntryValues;
Michael Kolb10f4ba02013-04-10 08:50:51 -070044 private CharSequence[] mLabels;
Michael Kolb8872c232013-01-29 10:33:22 -080045 private boolean mLoaded = false;
46
47 public ListPreference(Context context, AttributeSet attrs) {
48 super(context, attrs);
49
50 TypedArray a = context.obtainStyledAttributes(
51 attrs, R.styleable.ListPreference, 0, 0);
52
Angus Kongb50b5cb2013-08-09 14:55:20 -070053 mKey = CameraUtil.checkNotNull(
Michael Kolb8872c232013-01-29 10:33:22 -080054 a.getString(R.styleable.ListPreference_key));
55
56 // We allow the defaultValue attribute to be a string or an array of
57 // strings. The reason we need multiple default values is that some
58 // of them may be unsupported on a specific platform (for example,
59 // continuous auto-focus). In that case the first supported value
60 // in the array will be used.
61 int attrDefaultValue = R.styleable.ListPreference_defaultValue;
62 TypedValue tv = a.peekValue(attrDefaultValue);
63 if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
64 mDefaultValues = a.getTextArray(attrDefaultValue);
65 } else {
66 mDefaultValues = new CharSequence[1];
67 mDefaultValues[0] = a.getString(attrDefaultValue);
68 }
69
70 setEntries(a.getTextArray(R.styleable.ListPreference_entries));
71 setEntryValues(a.getTextArray(
72 R.styleable.ListPreference_entryValues));
Michael Kolb10f4ba02013-04-10 08:50:51 -070073 setLabels(a.getTextArray(
George Mount1e259a82013-04-18 10:15:40 -070074 R.styleable.ListPreference_labelList));
Michael Kolb8872c232013-01-29 10:33:22 -080075 a.recycle();
76 }
77
78 public String getKey() {
79 return mKey;
80 }
81
82 public CharSequence[] getEntries() {
83 return mEntries;
84 }
85
86 public CharSequence[] getEntryValues() {
87 return mEntryValues;
88 }
89
Michael Kolb10f4ba02013-04-10 08:50:51 -070090 public CharSequence[] getLabels() {
91 return mLabels;
92 }
93
Michael Kolb8872c232013-01-29 10:33:22 -080094 public void setEntries(CharSequence entries[]) {
95 mEntries = entries == null ? new CharSequence[0] : entries;
96 }
97
98 public void setEntryValues(CharSequence values[]) {
99 mEntryValues = values == null ? new CharSequence[0] : values;
100 }
101
Michael Kolb10f4ba02013-04-10 08:50:51 -0700102 public void setLabels(CharSequence labels[]) {
103 mLabels = labels == null ? new CharSequence[0] : labels;
104 }
105
Michael Kolb8872c232013-01-29 10:33:22 -0800106 public String getValue() {
107 if (!mLoaded) {
108 mValue = getSharedPreferences().getString(mKey,
109 findSupportedDefaultValue());
110 mLoaded = true;
111 }
112 return mValue;
113 }
114
115 // Find the first value in mDefaultValues which is supported.
116 private String findSupportedDefaultValue() {
117 for (int i = 0; i < mDefaultValues.length; i++) {
118 for (int j = 0; j < mEntryValues.length; j++) {
119 // Note that mDefaultValues[i] may be null (if unspecified
120 // in the xml file).
121 if (mEntryValues[j].equals(mDefaultValues[i])) {
122 return mDefaultValues[i].toString();
123 }
124 }
125 }
126 return null;
127 }
128
129 public void setValue(String value) {
130 if (findIndexOfValue(value) < 0) throw new IllegalArgumentException();
131 mValue = value;
132 persistStringValue(value);
133 }
134
135 public void setValueIndex(int index) {
136 setValue(mEntryValues[index].toString());
137 }
138
139 public int findIndexOfValue(String value) {
140 for (int i = 0, n = mEntryValues.length; i < n; ++i) {
Angus Kongb50b5cb2013-08-09 14:55:20 -0700141 if (CameraUtil.equals(mEntryValues[i], value)) return i;
Michael Kolb8872c232013-01-29 10:33:22 -0800142 }
143 return -1;
144 }
145
Michael Kolbeb8adc12013-04-26 11:09:29 -0700146 public int getCurrentIndex() {
147 return findIndexOfValue(getValue());
148 }
149
Michael Kolb8872c232013-01-29 10:33:22 -0800150 public String getEntry() {
151 return mEntries[findIndexOfValue(getValue())].toString();
152 }
153
Michael Kolb10f4ba02013-04-10 08:50:51 -0700154 public String getLabel() {
155 return mLabels[findIndexOfValue(getValue())].toString();
156 }
157
Michael Kolb8872c232013-01-29 10:33:22 -0800158 protected void persistStringValue(String value) {
159 SharedPreferences.Editor editor = getSharedPreferences().edit();
160 editor.putString(mKey, value);
161 editor.apply();
162 }
163
164 @Override
165 public void reloadValue() {
166 this.mLoaded = false;
167 }
168
169 public void filterUnsupported(List<String> supported) {
170 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
171 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
172 for (int i = 0, len = mEntryValues.length; i < len; i++) {
173 if (supported.indexOf(mEntryValues[i].toString()) >= 0) {
174 entries.add(mEntries[i]);
175 entryValues.add(mEntryValues[i]);
176 }
177 }
178 int size = entries.size();
179 mEntries = entries.toArray(new CharSequence[size]);
180 mEntryValues = entryValues.toArray(new CharSequence[size]);
181 }
182
183 public void filterDuplicated() {
184 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
185 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
186 for (int i = 0, len = mEntryValues.length; i < len; i++) {
187 if (!entries.contains(mEntries[i])) {
188 entries.add(mEntries[i]);
189 entryValues.add(mEntryValues[i]);
190 }
191 }
192 int size = entries.size();
193 mEntries = entries.toArray(new CharSequence[size]);
194 mEntryValues = entryValues.toArray(new CharSequence[size]);
195 }
196
197 public void print() {
198 Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue());
199 for (int i = 0; i < mEntryValues.length; i++) {
200 Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]);
201 }
202 }
203}