• 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 
24 import com.android.camera.R;
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 
35     private final String mKey;
36     private String mValue;
37     private final String mDefaultValue;
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         mDefaultValue = a.getString(R.styleable.ListPreference_defaultValue);
52 
53         setEntries(a.getTextArray(R.styleable.ListPreference_entries));
54         setEntryValues(a.getTextArray(
55                 R.styleable.ListPreference_entryValues));
56         a.recycle();
57     }
58 
getKey()59     public String getKey() {
60         return mKey;
61     }
62 
getEntries()63     public CharSequence[] getEntries() {
64         return mEntries;
65     }
66 
getEntryValues()67     public CharSequence[] getEntryValues() {
68         return mEntryValues;
69     }
70 
setEntries(CharSequence entries[])71     public void setEntries(CharSequence entries[]) {
72         mEntries = entries == null ? new CharSequence[0] : entries;
73     }
74 
setEntryValues(CharSequence values[])75     public void setEntryValues(CharSequence values[]) {
76         mEntryValues = values == null ? new CharSequence[0] : values;
77     }
78 
getValue()79     public String getValue() {
80         if (!mLoaded) {
81             mValue = getSharedPreferences().getString(mKey, mDefaultValue);
82             mLoaded = true;
83         }
84         return mValue;
85     }
86 
setValue(String value)87     public void setValue(String value) {
88         if (findIndexOfValue(value) < 0) throw new IllegalArgumentException();
89         mValue = value;
90         persistStringValue(value);
91     }
92 
setValueIndex(int index)93     public void setValueIndex(int index) {
94         setValue(mEntryValues[index].toString());
95     }
96 
findIndexOfValue(String value)97     public int findIndexOfValue(String value) {
98         for (int i = 0, n = mEntryValues.length; i < n; ++i) {
99             if (Util.equals(mEntryValues[i], value)) return i;
100         }
101         return -1;
102     }
103 
getEntry()104     public String getEntry() {
105         return mEntries[findIndexOfValue(getValue())].toString();
106     }
107 
persistStringValue(String value)108     protected void persistStringValue(String value) {
109         SharedPreferences.Editor editor = getSharedPreferences().edit();
110         editor.putString(mKey, value);
111         editor.apply();
112     }
113 
114     @Override
reloadValue()115     public void reloadValue() {
116         this.mLoaded = false;
117     }
118 
filterUnsupported(List<String> supported)119     public void filterUnsupported(List<String> supported) {
120         ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
121         ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
122         for (int i = 0, len = mEntryValues.length; i < len; i++) {
123             if (supported.indexOf(mEntryValues[i].toString()) >= 0) {
124                 entries.add(mEntries[i]);
125                 entryValues.add(mEntryValues[i]);
126             }
127         }
128         int size = entries.size();
129         mEntries = entries.toArray(new CharSequence[size]);
130         mEntryValues = entryValues.toArray(new CharSequence[size]);
131     }
132 }
133