2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.ui;
19 import android.content.Context;
21 import com.android.camera.IconListPreference;
22 import com.android.camera.ListPreference;
23 import com.android.camera.Util;
25 import java.util.ArrayList;
27 class PreferenceAdapter
28 implements GLListView.Model, GLListView.OnItemSelectedListener {
30 private static final int ICON_NONE = 0;
32 private final ArrayList<GLView> mContent = new ArrayList<GLView>();
33 private final ListPreference mPreference;
34 private String mOverride;
36 public PreferenceAdapter(Context context, ListPreference preference) {
37 mPreference = preference;
38 generateContent(context, preference);
41 public void reload() {
42 updateContent(null, true);
45 public void overrideSettings(String settings) {
46 updateContent(settings, false);
49 private void updateContent(String settings, boolean reloadValues) {
50 if (!reloadValues && Util.equals(settings, mOverride)) return;
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);
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);
71 private void generateContent(Context context, ListPreference preference) {
72 GLOptionHeader header =
73 new GLOptionHeader(context, preference.getTitle());
75 CharSequence[] entries = preference.getEntries();
76 CharSequence[] values = preference.getEntryValues();
77 String value = preference.getValue();
79 if (preference instanceof IconListPreference) {
80 IconListPreference iPref = (IconListPreference) preference;
81 icons = iPref.getIconIds();
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));
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);
103 ((GLOptionItem) mContent.get(1 + oldIndex)).setChecked(false);
104 ((GLOptionItem) view).setChecked(true);
110 public GLView getView(int index) {
111 return mContent.get(index);
114 public boolean isSelectable(int index) {
115 return mContent.get(index) instanceof GLOptionItem;
119 return mContent.size();