Home | History | Annotate | Download | only in camera
      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 
     26 import com.android.gallery3d.R;
     27 
     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;
     43     private CharSequence[] mLabels;
     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));
     72         setLabels(a.getTextArray(
     73                 R.styleable.ListPreference_labelList));
     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 
     89     public CharSequence[] getLabels() {
     90         return mLabels;
     91     }
     92 
     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 
    101     public void setLabels(CharSequence labels[]) {
    102         mLabels = labels == null ? new CharSequence[0] : labels;
    103     }
    104 
    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 int getCurrentIndex() {
    146         return findIndexOfValue(getValue());
    147     }
    148 
    149     public String getEntry() {
    150         return mEntries[findIndexOfValue(getValue())].toString();
    151     }
    152 
    153     public String getLabel() {
    154         return mLabels[findIndexOfValue(getValue())].toString();
    155     }
    156 
    157     protected void persistStringValue(String value) {
    158         SharedPreferences.Editor editor = getSharedPreferences().edit();
    159         editor.putString(mKey, value);
    160         editor.apply();
    161     }
    162 
    163     @Override
    164     public void reloadValue() {
    165         this.mLoaded = false;
    166     }
    167 
    168     public void filterUnsupported(List<String> supported) {
    169         ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
    170         ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
    171         for (int i = 0, len = mEntryValues.length; i < len; i++) {
    172             if (supported.indexOf(mEntryValues[i].toString()) >= 0) {
    173                 entries.add(mEntries[i]);
    174                 entryValues.add(mEntryValues[i]);
    175             }
    176         }
    177         int size = entries.size();
    178         mEntries = entries.toArray(new CharSequence[size]);
    179         mEntryValues = entryValues.toArray(new CharSequence[size]);
    180     }
    181 
    182     public void filterDuplicated() {
    183         ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
    184         ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
    185         for (int i = 0, len = mEntryValues.length; i < len; i++) {
    186             if (!entries.contains(mEntries[i])) {
    187                 entries.add(mEntries[i]);
    188                 entryValues.add(mEntryValues[i]);
    189             }
    190         }
    191         int size = entries.size();
    192         mEntries = entries.toArray(new CharSequence[size]);
    193         mEntryValues = entryValues.toArray(new CharSequence[size]);
    194     }
    195 
    196     public void print() {
    197         Log.v(TAG, "Preference key=" + getKey() + ". value=" + getValue());
    198         for (int i = 0; i < mEntryValues.length; i++) {
    199             Log.v(TAG, "entryValues[" + i + "]=" + mEntryValues[i]);
    200         }
    201     }
    202 }
    203