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