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.ListPreference; 22 import com.android.camera.R; 23 24 import java.util.HashMap; 25 26 public class OtherSettingsIndicator extends AbstractIndicator { 27 28 private final ListPreference mPreference[]; 29 private final GLListView.Model mAdapters[]; 30 private ResourceTexture mIcon; 31 private GLListView mPopupContent; 32 private Runnable mOnRestorePrefsClickedRunner; 33 private final HashMap<String, String> mOverrides = new HashMap<String, String>(); 34 35 public OtherSettingsIndicator( 36 Context context, ListPreference preference[]) { 37 super(context); 38 mPreference = preference; 39 // One extra for the restore settings 40 mAdapters = new GLListView.Model[preference.length + 1]; 41 } 42 43 @Override 44 protected ResourceTexture getIcon() { 45 if (mIcon == null) { 46 Context context = getGLRootView().getContext(); 47 mIcon = new ResourceTexture( 48 context, R.drawable.ic_viewfinder_settings); 49 } 50 return mIcon; 51 } 52 53 @Override 54 public void reloadPreferences() { 55 if (mPopupContent != null) { 56 ListPreference prefs[] = mPreference; 57 for (int i = 0, n = prefs.length; i < n; ++i) { 58 ((PreferenceAdapter) mAdapters[i]).reload(); 59 } 60 } 61 } 62 63 @Override 64 public void overrideSettings(String key, String value) { 65 if (value == null) { 66 mOverrides.remove(key); 67 } else { 68 mOverrides.put(key, value); 69 } 70 if (mPopupContent != null) { 71 ListPreference prefs[] = mPreference; 72 for (int i = 0, n = prefs.length; i < n; ++i) { 73 if (!prefs[i].getKey().equals(key)) continue; 74 ((PreferenceAdapter) mAdapters[i]).overrideSettings(value); 75 break; 76 } 77 } 78 } 79 80 private UberAdapter buildUberAdapter() { 81 ListPreference prefs[] = mPreference; 82 GLListView.Model adapters[] = mAdapters; 83 Context context = getGLRootView().getContext(); 84 for (int i = 0, n = prefs.length; i < n; ++i) { 85 adapters[i] = new PreferenceAdapter(context, prefs[i]); 86 String override = mOverrides.get(prefs[i].getKey()); 87 if (override != null) { 88 ((PreferenceAdapter) adapters[i]).overrideSettings(override); 89 } 90 } 91 adapters[prefs.length] = new RestoreSettingsModel(context); 92 return new UberAdapter(); 93 } 94 95 @Override 96 public GLView getPopupContent() { 97 if (mPopupContent == null) { 98 Context context = getGLRootView().getContext(); 99 mPopupContent = new GLListView(context); 100 mPopupContent.setHighLight(new NinePatchTexture( 101 context, R.drawable.optionitem_highlight)); 102 mPopupContent.setScroller(new NinePatchTexture( 103 context, R.drawable.scrollbar_handle_vertical)); 104 UberAdapter adapter = buildUberAdapter(); 105 mPopupContent.setOnItemSelectedListener(adapter); 106 mPopupContent.setDataModel(adapter); 107 } 108 return mPopupContent; 109 } 110 111 private class UberAdapter implements 112 GLListView.Model, GLListView.OnItemSelectedListener { 113 114 public GLView getView(int index) { 115 for (GLListView.Model adapter : mAdapters) { 116 if (index < adapter.size()) { 117 return adapter.getView(index); 118 } 119 index -= adapter.size(); 120 } 121 return null; 122 } 123 124 public boolean isSelectable(int index) { 125 for (GLListView.Model adapter : mAdapters) { 126 if (index < adapter.size()) { 127 return adapter.isSelectable(index); 128 } 129 index -= adapter.size(); 130 } 131 return true; 132 } 133 134 public int size() { 135 int size = 0; 136 for (GLListView.Model adapter : mAdapters) { 137 size += adapter.size(); 138 } 139 return size; 140 } 141 142 public void onItemSelected(GLView view, int position) { 143 for (GLListView.Model adapter : mAdapters) { 144 if (position < adapter.size()) { 145 ((GLListView.OnItemSelectedListener) 146 adapter).onItemSelected(view, position); 147 return; 148 } 149 position -= adapter.size(); 150 } 151 } 152 } 153 154 private class RestoreSettingsModel 155 implements GLListView.Model, GLListView.OnItemSelectedListener { 156 private final GLView mHeader; 157 private final GLView mItem; 158 159 public RestoreSettingsModel(Context context) { 160 mHeader = new GLOptionHeader(context, 161 context.getString(R.string.pref_restore_title)); 162 mItem = new RestoreSettingsItem( 163 context, context.getString(R.string.pref_restore_detail)); 164 } 165 166 public GLView getView(int index) { 167 return index == 0 ? mHeader : mItem; 168 } 169 170 public boolean isSelectable(int index) { 171 return index != 0; 172 } 173 174 public int size() { 175 return 2; 176 } 177 178 public void onItemSelected(GLView view, int position) { 179 if (mOnRestorePrefsClickedRunner != null) { 180 mOnRestorePrefsClickedRunner.run(); 181 } 182 } 183 } 184 185 public void setOnRestorePreferencesClickedRunner(Runnable l) { 186 mOnRestorePrefsClickedRunner = l; 187 } 188 } 189