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