Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 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.ui;
     18 
     19 import android.content.Context;
     20 
     21 import com.android.camera.IconListPreference;
     22 import com.android.camera.ListPreference;
     23 import com.android.camera.Util;
     24 
     25 import java.util.ArrayList;
     26 
     27 class PreferenceAdapter
     28         implements GLListView.Model, GLListView.OnItemSelectedListener {
     29 
     30     private static final int ICON_NONE = 0;
     31 
     32     private final ArrayList<GLView> mContent = new ArrayList<GLView>();
     33     private final ListPreference mPreference;
     34     private String mOverride;
     35 
     36     public PreferenceAdapter(Context context, ListPreference preference) {
     37         mPreference = preference;
     38         generateContent(context, preference);
     39     }
     40 
     41     public void reload() {
     42         updateContent(null, true);
     43     }
     44 
     45     public void overrideSettings(String settings) {
     46         updateContent(settings, false);
     47     }
     48 
     49     private void updateContent(String settings, boolean reloadValues) {
     50         if (!reloadValues && Util.equals(settings, mOverride)) return;
     51         mOverride = settings;
     52 
     53         CharSequence[] values = mPreference.getEntryValues();
     54         String value = mPreference.getValue();
     55         if (settings == null) {
     56             for (int i = 1, n = mContent.size(); i < n; ++i) {
     57                 GLOptionItem item = (GLOptionItem) mContent.get(i);
     58                 item.setChecked(values[i - 1].equals(value));
     59                 item.setEnabled(true);
     60             }
     61         } else {
     62             for (int i = 1, n = mContent.size(); i < n; ++i) {
     63                 GLOptionItem item = (GLOptionItem) mContent.get(i);
     64                 boolean checked = values[i - 1].equals(settings);
     65                 item.setChecked(checked);
     66                 item.setEnabled(checked);
     67             }
     68         }
     69     }
     70 
     71     private void generateContent(Context context, ListPreference preference) {
     72         GLOptionHeader header =
     73                 new GLOptionHeader(context, preference.getTitle());
     74         mContent.add(header);
     75         CharSequence[] entries = preference.getEntries();
     76         CharSequence[] values = preference.getEntryValues();
     77         String value = preference.getValue();
     78         int [] icons = null;
     79         if (preference instanceof IconListPreference) {
     80             IconListPreference iPref = (IconListPreference) preference;
     81             icons = iPref.getIconIds();
     82         }
     83         for (int i = 0, n = entries.length; i < n; ++i) {
     84             GLOptionItem item = new GLOptionItem(
     85                     context, icons == null ? ICON_NONE : icons[i],
     86                     entries[i].toString());
     87             item.setChecked(values[i].equals(value));
     88             mContent.add(item);
     89         }
     90     }
     91 
     92     public void onItemSelected(GLView view, int position) {
     93         if (mOverride != null) return;
     94         ListPreference pref = mPreference;
     95         CharSequence[] values = pref.getEntryValues();
     96         if (position < values.length + 1) {
     97             int index = position - 1;
     98             int oldIndex = pref.findIndexOfValue(pref.getValue());
     99             if (oldIndex != index) {
    100                 synchronized (pref.getSharedPreferences()) {
    101                     pref.setValueIndex(index);
    102                 }
    103                 ((GLOptionItem) mContent.get(1 + oldIndex)).setChecked(false);
    104                 ((GLOptionItem) view).setChecked(true);
    105             }
    106             return;
    107         }
    108     }
    109 
    110     public GLView getView(int index) {
    111         return mContent.get(index);
    112     }
    113 
    114     public boolean isSelectable(int index) {
    115         return mContent.get(index) instanceof GLOptionItem;
    116     }
    117 
    118     public int size() {
    119         return mContent.size();
    120     }
    121 }
    122