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 java.util.ArrayList; 20 import java.util.List; 21 22 import android.content.Context; 23 import android.content.SharedPreferences; 24 import android.content.res.TypedArray; 25 import android.util.AttributeSet; 26 import android.util.Log; 27 import android.util.TypedValue; 28 29 import com.android.camera.util.CameraUtil; 30 import com.android.camera.util.UsageStatistics; 31 import com.android.camera2.R; 32 33 /** 34 * A type of <code>CameraPreference</code> whose number of possible values 35 * is limited. 36 */ 37 public class ListPreference extends CameraPreference { 38 private static final String TAG = "ListPreference"; 39 private final String mKey; 40 private String mValue; 41 private final CharSequence[] mDefaultValues; 42 43 private CharSequence[] mEntries; 44 private CharSequence[] mEntryValues; 45 private CharSequence[] mLabels; 46 private boolean mLoaded = false; 47 48 public ListPreference(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 51 TypedArray a = context.obtainStyledAttributes( 52 attrs, R.styleable.ListPreference, 0, 0); 53 54 mKey = CameraUtil.checkNotNull( 55 a.getString(R.styleable.ListPreference_key)); 56 57 // We allow the defaultValue attribute to be a string or an array of 58 // strings. The reason we need multiple default values is that some 59 // of them may be unsupported on a specific platform (for example, 60 // continuous auto-focus). In that case the first supported value 61 // in the array will be used. 62 int attrDefaultValue = R.styleable.ListPreference_defaultValue; 63 TypedValue tv = a.peekValue(attrDefaultValue); 64 if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) { 65 mDefaultValues = a.getTextArray(attrDefaultValue); 66 } else { 67 mDefaultValues = new CharSequence[1]; 68 mDefaultValues[0] = a.getString(attrDefaultValue); 69 } 70 71 setEntries(a.getTextArray(R.styleable.ListPreference_entries)); 72 setEntryValues(a.getTextArray( 73 R.styleable.ListPreference_entryValues)); 74 setLabels(a.getTextArray( 75 R.styleable.ListPreference_labelList)); 76 a.recycle(); 77 } 78 79 public String getKey() { 80 return mKey; 81 } 82 83 public CharSequence[] getEntries() { 84 return mEntries; 85 } 86 87 public CharSequence[] getEntryValues() { 88 return mEntryValues; 89 } 90 91 public CharSequence[] getLabels() { 92 return mLabels; 93 } 94 95 public void setEntries(CharSequence entries[]) { 96 mEntries = entries == null ? new CharSequence[0] : entries; 97 } 98 99 public void setEntryValues(CharSequence values[]) { 100 mEntryValues = values == null ? new CharSequence[0] : values; 101 } 102 103 public void setLabels(CharSequence labels[]) { 104 mLabels = labels == null ? new CharSequence[0] : labels; 105 } 106 107 public String getValue() { 108 if (!mLoaded) { 109 mValue = getSharedPreferences().getString(mKey, 110 findSupportedDefaultValue()); 111 mLoaded = true; 112 } 113 return mValue; 114 } 115 116 // Find the first value in mDefaultValues which is supported. 117 private String findSupportedDefaultValue() { 118 for (int i = 0; i < mDefaultValues.length; i++) { 119 for (int j = 0; j < mEntryValues.length; j++) { 120 // Note that mDefaultValues[i] may be null (if unspecified 121 // in the xml file). 122 if (mEntryValues[j].equals(mDefaultValues[i])) { 123 return mDefaultValues[i].toString(); 124 } 125 } 126 } 127 return null; 128 } 129 130 public void setValue(String value) { 131 if (findIndexOfValue(value) < 0) throw new IllegalArgumentException(); 132 mValue = value; 133 persistStringValue(value); 134 } 135 136 public void setValueIndex(int index) { 137 setValue(mEntryValues[index].toString()); 138 } 139 140 public int findIndexOfValue(String value) { 141 for (int i = 0, n = mEntryValues.length; i < n; ++i) { 142 if (CameraUtil.equals(mEntryValues[i], value)) return i; 143 } 144 return -1; 145 } 146 147 public int getCurrentIndex() { 148 return findIndexOfValue(getValue()); 149 } 150 151 public String getEntry() { 152 return mEntries[findIndexOfValue(getValue())].toString(); 153 } 154 155 public String getLabel() { 156 return mLabels[findIndexOfValue(getValue())].toString(); 157 } 158 159 protected void persistStringValue(String value) { 160 SharedPreferences.Editor editor = getSharedPreferences().edit(); 161 editor.putString(mKey, value); 162 editor.apply(); 163 UsageStatistics.onEvent("CameraSettingsChange", value, mKey); 164 } 165 166 @Override 167 public void reloadValue() { 168 this.mLoaded = false; 169 } 170 171 public void filterUnsupported(List<String> supported) { 172 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 173 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 174 for (int i = 0, len = mEntryValues.length; i < len; i++) { 175 if (supported.indexOf(mEntryValues[i].toString()) >= 0) { 176 entries.add(mEntries[i]); 177 entryValues.add(mEntryValues[i]); 178 } 179 } 180 int size = entries.size(); 181 mEntries = entries.toArray(new CharSequence[size]); 182 mEntryValues = entryValues.toArray(new CharSequence[size]); 183 } 184 185 public void filterDuplicated() { 186 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 187 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 188 for (int i = 0, len = mEntryValues.length; i < len; i++) { 189 if (!entries.contains(mEntries[i])) { 190 entries.add(mEntries[i]); 191 entryValues.add(mEntryValues[i]); 192 } 193 } 194 int size = entries.size(); 195 mEntries = entries.toArray(new CharSequence[size]); 196 mEntryValues = entryValues.toArray(new CharSequence[size]); 197 } 198 199 public void print() { 200 Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue()); 201 for (int i = 0; i < mEntryValues.length; i++) { 202 Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]); 203 } 204 } 205 } 206