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; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 23 | import android.util.TypedValue; |
| 24 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 25 | import com.android.camera.debug.Log; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 26 | import com.android.camera.util.CameraUtil; |
Seth Raphael | 9e03985 | 2013-11-12 11:21:17 -0800 | [diff] [blame] | 27 | import com.android.camera.util.UsageStatistics; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 28 | import com.android.camera2.R; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 29 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 33 | /** |
| 34 | * A type of <code>CameraPreference</code> whose number of possible values |
| 35 | * is limited. |
| 36 | */ |
| 37 | public class ListPreference extends CameraPreference { |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 38 | private static final Log.Tag TAG = new Log.Tag("ListPreference"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 39 | private final String mKey; |
| 40 | private String mValue; |
| 41 | private final CharSequence[] mDefaultValues; |
| 42 | |
| 43 | private CharSequence[] mEntries; |
| 44 | private CharSequence[] mEntryValues; |
Michael Kolb | 10f4ba0 | 2013-04-10 08:50:51 -0700 | [diff] [blame] | 45 | private CharSequence[] mLabels; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 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 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 54 | mKey = CameraUtil.checkNotNull( |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 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)); |
Michael Kolb | 10f4ba0 | 2013-04-10 08:50:51 -0700 | [diff] [blame] | 74 | setLabels(a.getTextArray( |
George Mount | 1e259a8 | 2013-04-18 10:15:40 -0700 | [diff] [blame] | 75 | R.styleable.ListPreference_labelList)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 76 | a.recycle(); |
| 77 | } |
| 78 | |
| 79 | public String getKey() { |
| 80 | return mKey; |
| 81 | } |
| 82 | |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 83 | public String getDefault() { |
| 84 | return findSupportedDefaultValue(); |
| 85 | } |
| 86 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 87 | public CharSequence[] getEntries() { |
| 88 | return mEntries; |
| 89 | } |
| 90 | |
| 91 | public CharSequence[] getEntryValues() { |
| 92 | return mEntryValues; |
| 93 | } |
| 94 | |
Michael Kolb | 10f4ba0 | 2013-04-10 08:50:51 -0700 | [diff] [blame] | 95 | public CharSequence[] getLabels() { |
| 96 | return mLabels; |
| 97 | } |
| 98 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 99 | public void setEntries(CharSequence entries[]) { |
| 100 | mEntries = entries == null ? new CharSequence[0] : entries; |
| 101 | } |
| 102 | |
| 103 | public void setEntryValues(CharSequence values[]) { |
| 104 | mEntryValues = values == null ? new CharSequence[0] : values; |
| 105 | } |
| 106 | |
Michael Kolb | 10f4ba0 | 2013-04-10 08:50:51 -0700 | [diff] [blame] | 107 | public void setLabels(CharSequence labels[]) { |
| 108 | mLabels = labels == null ? new CharSequence[0] : labels; |
| 109 | } |
| 110 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 111 | public String getValue() { |
| 112 | if (!mLoaded) { |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 113 | SharedPreferences preferences = getSharedPreferences(); |
| 114 | if (preferences == null) { |
| 115 | // TEMP |
| 116 | // This module doesn't use ComboPreferences. |
| 117 | // Check this value and use the global settings |
| 118 | // manager instead. |
| 119 | return mValue; |
| 120 | } else { |
| 121 | mValue = preferences.getString(mKey, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 122 | findSupportedDefaultValue()); |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 123 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 124 | mLoaded = true; |
| 125 | } |
| 126 | return mValue; |
| 127 | } |
| 128 | |
| 129 | // Find the first value in mDefaultValues which is supported. |
| 130 | private String findSupportedDefaultValue() { |
| 131 | for (int i = 0; i < mDefaultValues.length; i++) { |
| 132 | for (int j = 0; j < mEntryValues.length; j++) { |
| 133 | // Note that mDefaultValues[i] may be null (if unspecified |
| 134 | // in the xml file). |
| 135 | if (mEntryValues[j].equals(mDefaultValues[i])) { |
| 136 | return mDefaultValues[i].toString(); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 143 | public boolean setValue(String value) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 144 | if (findIndexOfValue(value) < 0) throw new IllegalArgumentException(); |
| 145 | mValue = value; |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 146 | return persistStringValue(value); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 149 | public boolean setValueIndex(int index) { |
| 150 | return setValue(mEntryValues[index].toString()); |
| 151 | } |
| 152 | |
| 153 | public String getValueAtIndex(int index) { |
| 154 | return mEntryValues[index].toString(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public int findIndexOfValue(String value) { |
| 158 | for (int i = 0, n = mEntryValues.length; i < n; ++i) { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 159 | if (CameraUtil.equals(mEntryValues[i], value)) return i; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 160 | } |
| 161 | return -1; |
| 162 | } |
| 163 | |
Michael Kolb | eb8adc1 | 2013-04-26 11:09:29 -0700 | [diff] [blame] | 164 | public int getCurrentIndex() { |
| 165 | return findIndexOfValue(getValue()); |
| 166 | } |
| 167 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 168 | public String getEntry() { |
| 169 | return mEntries[findIndexOfValue(getValue())].toString(); |
| 170 | } |
| 171 | |
Michael Kolb | 10f4ba0 | 2013-04-10 08:50:51 -0700 | [diff] [blame] | 172 | public String getLabel() { |
| 173 | return mLabels[findIndexOfValue(getValue())].toString(); |
| 174 | } |
| 175 | |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 176 | protected boolean persistStringValue(String value) { |
| 177 | SharedPreferences preferences = getSharedPreferences(); |
| 178 | if (preferences == null) { |
| 179 | return false; |
| 180 | } |
Seth Raphael | 5e09d01 | 2013-12-18 13:45:03 -0800 | [diff] [blame] | 181 | String oldValue = preferences.getString(mKey, null); |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 182 | SharedPreferences.Editor editor = preferences.edit(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 183 | editor.putString(mKey, value); |
| 184 | editor.apply(); |
Seth Raphael | 5e09d01 | 2013-12-18 13:45:03 -0800 | [diff] [blame] | 185 | UsageStatistics.changePreference(mKey, value, oldValue); |
Erin Dahlgren | 357b767 | 2013-11-20 17:38:14 -0800 | [diff] [blame] | 186 | return true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void reloadValue() { |
| 191 | this.mLoaded = false; |
| 192 | } |
| 193 | |
| 194 | public void filterUnsupported(List<String> supported) { |
| 195 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 196 | ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); |
| 197 | for (int i = 0, len = mEntryValues.length; i < len; i++) { |
| 198 | if (supported.indexOf(mEntryValues[i].toString()) >= 0) { |
| 199 | entries.add(mEntries[i]); |
| 200 | entryValues.add(mEntryValues[i]); |
| 201 | } |
| 202 | } |
| 203 | int size = entries.size(); |
| 204 | mEntries = entries.toArray(new CharSequence[size]); |
| 205 | mEntryValues = entryValues.toArray(new CharSequence[size]); |
| 206 | } |
| 207 | |
| 208 | public void filterDuplicated() { |
| 209 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 210 | ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); |
| 211 | for (int i = 0, len = mEntryValues.length; i < len; i++) { |
| 212 | if (!entries.contains(mEntries[i])) { |
| 213 | entries.add(mEntries[i]); |
| 214 | entryValues.add(mEntryValues[i]); |
| 215 | } |
| 216 | } |
| 217 | int size = entries.size(); |
| 218 | mEntries = entries.toArray(new CharSequence[size]); |
| 219 | mEntryValues = entryValues.toArray(new CharSequence[size]); |
| 220 | } |
| 221 | |
| 222 | public void print() { |
| 223 | Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue()); |
| 224 | for (int i = 0; i < mEntryValues.length; i++) { |
| 225 | Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]); |
| 226 | } |
| 227 | } |
| 228 | } |