ListPreference.java revision 52064aa4bacfae605823b99f675649b1786e8c8a
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;
23
24/**
25 * A type of <code>CameraPreference</code> whose number of possible values
26 * is limited.
27 */
28public class ListPreference extends CameraPreference {
29
30    private String mKey;
31    private String mValue;
32    private String mDefaultValue;
33
34    private CharSequence[] mEntries;
35    private CharSequence[] mEntryValues;
36    private boolean mLoaded = false;
37
38    public ListPreference(Context context, AttributeSet attrs) {
39        super(context, attrs);
40
41        TypedArray a = context.obtainStyledAttributes(
42                attrs, R.styleable.ListPreference, 0, 0);
43
44        mKey = Util.checkNotNull(
45                a.getString(R.styleable.ListPreference_key));
46        mDefaultValue = a.getString(R.styleable.ListPreference_defaultValue);
47
48        setEntries(a.getTextArray(R.styleable.ListPreference_entries));
49        setEntryValues(a.getTextArray(
50                R.styleable.ListPreference_entryValues));
51        a.recycle();
52    }
53
54    public String getKey() {
55        return mKey;
56    }
57
58    public CharSequence[] getEntries() {
59        return mEntries;
60    }
61
62    public CharSequence[] getEntryValues() {
63        return mEntryValues;
64    }
65
66    public void setEntries(CharSequence entries[]) {
67        mEntries = Util.checkNotNull(entries);
68    }
69
70    public void setEntryValues(CharSequence values[]) {
71        mEntryValues = Util.checkNotNull(values);
72    }
73
74    public String getValue() {
75        if (!mLoaded) {
76            mValue = getSharedPreferences().getString(mKey, mDefaultValue);
77            mLoaded = true;
78        }
79        return mValue;
80    }
81
82    public void setValue(String value) {
83        if (findIndexOfValue(value) < 0) throw new IllegalArgumentException();
84        mValue = value;
85        persistStringValue(value);
86    }
87
88    public void setValueIndex(int index) {
89        setValue(mEntryValues[index].toString());
90    }
91
92    public int findIndexOfValue(String value) {
93        for (int i = 0, n = mEntryValues.length; i < n; ++i) {
94            if (Util.equals(mEntryValues[i], value)) return i;
95        }
96        return -1;
97    }
98
99    public String getEntry() {
100        return mEntries[findIndexOfValue(getValue())].toString();
101    }
102
103    protected void persistStringValue(String value) {
104        SharedPreferences.Editor editor = getSharedPreferences().edit();
105        editor.putString(mKey, value);
106        editor.commit();
107    }
108
109    @Override
110    public void reloadValue() {
111        this.mLoaded = false;
112    }
113}
114