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