blob: c192d3403660d8ea8344b9c6583b5d07c691a532 [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
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
Michael Kolb8872c232013-01-29 10:33:22 -080023import android.util.TypedValue;
24
Angus Kong2bca2102014-03-11 16:27:30 -070025import com.android.camera.debug.Log;
Angus Kongb50b5cb2013-08-09 14:55:20 -070026import com.android.camera.util.CameraUtil;
Seth Raphael9e039852013-11-12 11:21:17 -080027import com.android.camera.util.UsageStatistics;
Sascha Haeberling8e963a52013-08-06 11:43:02 -070028import com.android.camera2.R;
Michael Kolb8872c232013-01-29 10:33:22 -080029
Angus Kong2bca2102014-03-11 16:27:30 -070030import java.util.ArrayList;
31import java.util.List;
32
Michael Kolb8872c232013-01-29 10:33:22 -080033/**
34 * A type of <code>CameraPreference</code> whose number of possible values
35 * is limited.
36 */
37public class ListPreference extends CameraPreference {
Angus Kong2bca2102014-03-11 16:27:30 -070038 private static final Log.Tag TAG = new Log.Tag("ListPreference");
Michael Kolb8872c232013-01-29 10:33:22 -080039 private final String mKey;
40 private String mValue;
41 private final CharSequence[] mDefaultValues;
42
43 private CharSequence[] mEntries;
44 private CharSequence[] mEntryValues;
Michael Kolb10f4ba02013-04-10 08:50:51 -070045 private CharSequence[] mLabels;
Michael Kolb8872c232013-01-29 10:33:22 -080046 private boolean mLoaded = false;
47
48 public ListPreference(Context context, AttributeSet attrs) {
49 super(context, attrs);
50
51 TypedArray a = context.obtainStyledAttributes(
52 attrs, R.styleable.ListPreference, 0, 0);
53
Angus Kongb50b5cb2013-08-09 14:55:20 -070054 mKey = CameraUtil.checkNotNull(
Michael Kolb8872c232013-01-29 10:33:22 -080055 a.getString(R.styleable.ListPreference_key));
56
57 // We allow the defaultValue attribute to be a string or an array of
58 // strings. The reason we need multiple default values is that some
59 // of them may be unsupported on a specific platform (for example,
60 // continuous auto-focus). In that case the first supported value
61 // in the array will be used.
62 int attrDefaultValue = R.styleable.ListPreference_defaultValue;
63 TypedValue tv = a.peekValue(attrDefaultValue);
64 if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
65 mDefaultValues = a.getTextArray(attrDefaultValue);
66 } else {
67 mDefaultValues = new CharSequence[1];
68 mDefaultValues[0] = a.getString(attrDefaultValue);
69 }
70
71 setEntries(a.getTextArray(R.styleable.ListPreference_entries));
72 setEntryValues(a.getTextArray(
73 R.styleable.ListPreference_entryValues));
Michael Kolb10f4ba02013-04-10 08:50:51 -070074 setLabels(a.getTextArray(
George Mount1e259a82013-04-18 10:15:40 -070075 R.styleable.ListPreference_labelList));
Michael Kolb8872c232013-01-29 10:33:22 -080076 a.recycle();
77 }
78
79 public String getKey() {
80 return mKey;
81 }
82
Erin Dahlgren357b7672013-11-20 17:38:14 -080083 public String getDefault() {
84 return findSupportedDefaultValue();
85 }
86
Michael Kolb8872c232013-01-29 10:33:22 -080087 public CharSequence[] getEntries() {
88 return mEntries;
89 }
90
91 public CharSequence[] getEntryValues() {
92 return mEntryValues;
93 }
94
Michael Kolb10f4ba02013-04-10 08:50:51 -070095 public CharSequence[] getLabels() {
96 return mLabels;
97 }
98
Michael Kolb8872c232013-01-29 10:33:22 -080099 public void setEntries(CharSequence entries[]) {
100 mEntries = entries == null ? new CharSequence[0] : entries;
101 }
102
103 public void setEntryValues(CharSequence values[]) {
104 mEntryValues = values == null ? new CharSequence[0] : values;
105 }
106
Michael Kolb10f4ba02013-04-10 08:50:51 -0700107 public void setLabels(CharSequence labels[]) {
108 mLabels = labels == null ? new CharSequence[0] : labels;
109 }
110
Michael Kolb8872c232013-01-29 10:33:22 -0800111 public String getValue() {
112 if (!mLoaded) {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800113 SharedPreferences preferences = getSharedPreferences();
114 if (preferences == null) {
115 // TEMP
116 // This module doesn't use ComboPreferences.
117 // Check this value and use the global settings
118 // manager instead.
119 return mValue;
120 } else {
121 mValue = preferences.getString(mKey,
Michael Kolb8872c232013-01-29 10:33:22 -0800122 findSupportedDefaultValue());
Erin Dahlgren357b7672013-11-20 17:38:14 -0800123 }
Michael Kolb8872c232013-01-29 10:33:22 -0800124 mLoaded = true;
125 }
126 return mValue;
127 }
128
129 // Find the first value in mDefaultValues which is supported.
130 private String findSupportedDefaultValue() {
131 for (int i = 0; i < mDefaultValues.length; i++) {
132 for (int j = 0; j < mEntryValues.length; j++) {
133 // Note that mDefaultValues[i] may be null (if unspecified
134 // in the xml file).
135 if (mEntryValues[j].equals(mDefaultValues[i])) {
136 return mDefaultValues[i].toString();
137 }
138 }
139 }
140 return null;
141 }
142
Erin Dahlgren357b7672013-11-20 17:38:14 -0800143 public boolean setValue(String value) {
Michael Kolb8872c232013-01-29 10:33:22 -0800144 if (findIndexOfValue(value) < 0) throw new IllegalArgumentException();
145 mValue = value;
Erin Dahlgren357b7672013-11-20 17:38:14 -0800146 return persistStringValue(value);
Michael Kolb8872c232013-01-29 10:33:22 -0800147 }
148
Erin Dahlgren357b7672013-11-20 17:38:14 -0800149 public boolean setValueIndex(int index) {
150 return setValue(mEntryValues[index].toString());
151 }
152
153 public String getValueAtIndex(int index) {
154 return mEntryValues[index].toString();
Michael Kolb8872c232013-01-29 10:33:22 -0800155 }
156
157 public int findIndexOfValue(String value) {
158 for (int i = 0, n = mEntryValues.length; i < n; ++i) {
Angus Kongb50b5cb2013-08-09 14:55:20 -0700159 if (CameraUtil.equals(mEntryValues[i], value)) return i;
Michael Kolb8872c232013-01-29 10:33:22 -0800160 }
161 return -1;
162 }
163
Michael Kolbeb8adc12013-04-26 11:09:29 -0700164 public int getCurrentIndex() {
165 return findIndexOfValue(getValue());
166 }
167
Michael Kolb8872c232013-01-29 10:33:22 -0800168 public String getEntry() {
169 return mEntries[findIndexOfValue(getValue())].toString();
170 }
171
Michael Kolb10f4ba02013-04-10 08:50:51 -0700172 public String getLabel() {
173 return mLabels[findIndexOfValue(getValue())].toString();
174 }
175
Erin Dahlgren357b7672013-11-20 17:38:14 -0800176 protected boolean persistStringValue(String value) {
177 SharedPreferences preferences = getSharedPreferences();
178 if (preferences == null) {
179 return false;
180 }
Seth Raphael5e09d012013-12-18 13:45:03 -0800181 String oldValue = preferences.getString(mKey, null);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800182 SharedPreferences.Editor editor = preferences.edit();
Michael Kolb8872c232013-01-29 10:33:22 -0800183 editor.putString(mKey, value);
184 editor.apply();
Seth Raphael5e09d012013-12-18 13:45:03 -0800185 UsageStatistics.changePreference(mKey, value, oldValue);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800186 return true;
Michael Kolb8872c232013-01-29 10:33:22 -0800187 }
188
189 @Override
190 public void reloadValue() {
191 this.mLoaded = false;
192 }
193
194 public void filterUnsupported(List<String> supported) {
195 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
196 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
197 for (int i = 0, len = mEntryValues.length; i < len; i++) {
198 if (supported.indexOf(mEntryValues[i].toString()) >= 0) {
199 entries.add(mEntries[i]);
200 entryValues.add(mEntryValues[i]);
201 }
202 }
203 int size = entries.size();
204 mEntries = entries.toArray(new CharSequence[size]);
205 mEntryValues = entryValues.toArray(new CharSequence[size]);
206 }
207
208 public void filterDuplicated() {
209 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
210 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
211 for (int i = 0, len = mEntryValues.length; i < len; i++) {
212 if (!entries.contains(mEntries[i])) {
213 entries.add(mEntries[i]);
214 entryValues.add(mEntryValues[i]);
215 }
216 }
217 int size = entries.size();
218 mEntries = entries.toArray(new CharSequence[size]);
219 mEntryValues = entryValues.toArray(new CharSequence[size]);
220 }
221
222 public void print() {
223 Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue());
224 for (int i = 0; i < mEntryValues.length; i++) {
225 Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]);
226 }
227 }
228}