ListPreference.java revision d456809fe6646db9ed759ecc6457f9280d18f2a6
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
17package com.android.camera;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import android.util.Log;
24
25import com.android.camera.R;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * A type of <code>CameraPreference</code> whose number of possible values
32 * is limited.
33 */
34public class ListPreference extends CameraPreference {
35    private final String TAG = "ListPreference";
36    private final String mKey;
37    private String mValue;
38    private final String mDefaultValue;
39
40    private CharSequence[] mEntries;
41    private CharSequence[] mEntryValues;
42    private boolean mLoaded = false;
43
44    public ListPreference(Context context, AttributeSet attrs) {
45        super(context, attrs);
46
47        TypedArray a = context.obtainStyledAttributes(
48                attrs, R.styleable.ListPreference, 0, 0);
49
50        mKey = Util.checkNotNull(
51                a.getString(R.styleable.ListPreference_key));
52        mDefaultValue = a.getString(R.styleable.ListPreference_defaultValue);
53
54        setEntries(a.getTextArray(R.styleable.ListPreference_entries));
55        setEntryValues(a.getTextArray(
56                R.styleable.ListPreference_entryValues));
57        a.recycle();
58    }
59
60    public String getKey() {
61        return mKey;
62    }
63
64    public CharSequence[] getEntries() {
65        return mEntries;
66    }
67
68    public CharSequence[] getEntryValues() {
69        return mEntryValues;
70    }
71
72    public void setEntries(CharSequence entries[]) {
73        mEntries = entries == null ? new CharSequence[0] : entries;
74    }
75
76    public void setEntryValues(CharSequence values[]) {
77        mEntryValues = values == null ? new CharSequence[0] : values;
78    }
79
80    public String getValue() {
81        if (!mLoaded) {
82            mValue = getSharedPreferences().getString(mKey, mDefaultValue);
83            mLoaded = true;
84        }
85        return mValue;
86    }
87
88    public void setValue(String value) {
89        if (findIndexOfValue(value) < 0) throw new IllegalArgumentException();
90        mValue = value;
91        persistStringValue(value);
92    }
93
94    public void setValueIndex(int index) {
95        setValue(mEntryValues[index].toString());
96    }
97
98    public int findIndexOfValue(String value) {
99        for (int i = 0, n = mEntryValues.length; i < n; ++i) {
100            if (Util.equals(mEntryValues[i], value)) return i;
101        }
102        return -1;
103    }
104
105    public String getEntry() {
106        return mEntries[findIndexOfValue(getValue())].toString();
107    }
108
109    protected void persistStringValue(String value) {
110        SharedPreferences.Editor editor = getSharedPreferences().edit();
111        editor.putString(mKey, value);
112        editor.apply();
113    }
114
115    @Override
116    public void reloadValue() {
117        this.mLoaded = false;
118    }
119
120    public void filterUnsupported(List<String> supported) {
121        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
122        ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
123        for (int i = 0, len = mEntryValues.length; i < len; i++) {
124            if (supported.indexOf(mEntryValues[i].toString()) >= 0) {
125                entries.add(mEntries[i]);
126                entryValues.add(mEntryValues[i]);
127            }
128        }
129        int size = entries.size();
130        mEntries = entries.toArray(new CharSequence[size]);
131        mEntryValues = entryValues.toArray(new CharSequence[size]);
132    }
133
134    public void print() {
135        Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue());
136        for (int i = 0; i < mEntryValues.length; i++) {
137            Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]);
138        }
139    }
140}
141