• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.camera;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.res.TypedArray;
22 import android.util.AttributeSet;
23 import android.util.Log;
24 import android.util.TypedValue;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 /**
30  * A type of <code>CameraPreference</code> whose number of possible values
31  * is limited.
32  */
33 public class ListPreference extends CameraPreference {
34     private final String TAG = "ListPreference";
35     private final String mKey;
36     private String mValue;
37     private final CharSequence[] mDefaultValues;
38 
39     private CharSequence[] mEntries;
40     private CharSequence[] mEntryValues;
41     private boolean mLoaded = false;
42 
ListPreference(Context context, AttributeSet attrs)43     public ListPreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45 
46         TypedArray a = context.obtainStyledAttributes(
47                 attrs, R.styleable.ListPreference, 0, 0);
48 
49         mKey = Util.checkNotNull(
50                 a.getString(R.styleable.ListPreference_key));
51 
52         // We allow the defaultValue attribute to be a string or an array of
53         // strings. The reason we need multiple default values is that some
54         // of them may be unsupported on a specific platform (for example,
55         // continuous auto-focus). In that case the first supported value
56         // in the array will be used.
57         int attrDefaultValue = R.styleable.ListPreference_defaultValue;
58         TypedValue tv = a.peekValue(attrDefaultValue);
59         if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
60             mDefaultValues = a.getTextArray(attrDefaultValue);
61         } else {
62             mDefaultValues = new CharSequence[1];
63             mDefaultValues[0] = a.getString(attrDefaultValue);
64         }
65 
66         setEntries(a.getTextArray(R.styleable.ListPreference_entries));
67         setEntryValues(a.getTextArray(
68                 R.styleable.ListPreference_entryValues));
69         a.recycle();
70     }
71 
getKey()72     public String getKey() {
73         return mKey;
74     }
75 
getEntries()76     public CharSequence[] getEntries() {
77         return mEntries;
78     }
79 
getEntryValues()80     public CharSequence[] getEntryValues() {
81         return mEntryValues;
82     }
83 
setEntries(CharSequence entries[])84     public void setEntries(CharSequence entries[]) {
85         mEntries = entries == null ? new CharSequence[0] : entries;
86     }
87 
setEntryValues(CharSequence values[])88     public void setEntryValues(CharSequence values[]) {
89         mEntryValues = values == null ? new CharSequence[0] : values;
90     }
91 
getValue()92     public String getValue() {
93         if (!mLoaded) {
94             mValue = getSharedPreferences().getString(mKey,
95                     findSupportedDefaultValue());
96             mLoaded = true;
97         }
98         return mValue;
99     }
100 
101     // Find the first value in mDefaultValues which is supported.
findSupportedDefaultValue()102     private String findSupportedDefaultValue() {
103         for (int i = 0; i < mDefaultValues.length; i++) {
104             for (int j = 0; j < mEntryValues.length; j++) {
105                 // Note that mDefaultValues[i] may be null (if unspecified
106                 // in the xml file).
107                 if (mEntryValues[j].equals(mDefaultValues[i])) {
108                     return mDefaultValues[i].toString();
109                 }
110             }
111         }
112         return null;
113     }
114 
setValue(String value)115     public void setValue(String value) {
116         if (findIndexOfValue(value) < 0) throw new IllegalArgumentException();
117         mValue = value;
118         persistStringValue(value);
119     }
120 
setValueIndex(int index)121     public void setValueIndex(int index) {
122         setValue(mEntryValues[index].toString());
123     }
124 
findIndexOfValue(String value)125     public int findIndexOfValue(String value) {
126         for (int i = 0, n = mEntryValues.length; i < n; ++i) {
127             if (Util.equals(mEntryValues[i], value)) return i;
128         }
129         return -1;
130     }
131 
getEntry()132     public String getEntry() {
133         return mEntries[findIndexOfValue(getValue())].toString();
134     }
135 
persistStringValue(String value)136     protected void persistStringValue(String value) {
137         SharedPreferences.Editor editor = getSharedPreferences().edit();
138         editor.putString(mKey, value);
139         editor.apply();
140     }
141 
142     @Override
reloadValue()143     public void reloadValue() {
144         this.mLoaded = false;
145     }
146 
filterUnsupported(List<String> supported)147     public void filterUnsupported(List<String> supported) {
148         ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
149         ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
150         for (int i = 0, len = mEntryValues.length; i < len; i++) {
151             if (supported.indexOf(mEntryValues[i].toString()) >= 0) {
152                 entries.add(mEntries[i]);
153                 entryValues.add(mEntryValues[i]);
154             }
155         }
156         int size = entries.size();
157         mEntries = entries.toArray(new CharSequence[size]);
158         mEntryValues = entryValues.toArray(new CharSequence[size]);
159     }
160 
print()161     public void print() {
162         Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue());
163         for (int i = 0; i < mEntryValues.length; i++) {
164             Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]);
165         }
166     }
167 }
168