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.R; 23 import com.android.camera.Util; 24 import com.android.camera.ui.GLListView.OnItemSelectedListener; 25 26 class BasicIndicator extends AbstractIndicator { 27 private static final int COLOR_OPTION_ITEM_HIGHLIGHT = 0xFF181818; 28 29 private final ResourceTexture mIcon[]; 30 private final IconListPreference mPreference; 31 protected int mIndex; 32 private GLListView mPopupContent; 33 private PreferenceAdapter mModel; 34 private String mOverride; 35 36 public BasicIndicator(Context context, IconListPreference preference) { 37 super(context); 38 mPreference = preference; 39 mIcon = new ResourceTexture[preference.getLargeIconIds().length]; 40 mIndex = preference.findIndexOfValue(preference.getValue()); 41 } 42 43 // Set the override and/or reload the value from preferences. 44 private void updateContent(String override, boolean reloadValue) { 45 if (!reloadValue && Util.equals(mOverride, override)) return; 46 IconListPreference pref = mPreference; 47 mOverride = override; 48 int index = pref.findIndexOfValue( 49 override == null ? pref.getValue() : override); 50 if (mIndex != index) { 51 mIndex = index; 52 invalidate(); 53 } 54 } 55 56 @Override 57 public void overrideSettings(String key, String settings) { 58 IconListPreference pref = mPreference; 59 if (!pref.getKey().equals(key)) return; 60 updateContent(settings, false); 61 } 62 63 @Override 64 public void reloadPreferences() { 65 if (mModel != null) mModel.reload(); 66 updateContent(null, true); 67 } 68 69 @Override 70 public GLView getPopupContent() { 71 if (mPopupContent == null) { 72 Context context = getGLRootView().getContext(); 73 mPopupContent = new GLListView(context); 74 mPopupContent.setHighLight( 75 new ColorTexture(COLOR_OPTION_ITEM_HIGHLIGHT)); 76 mPopupContent.setScroller(new NinePatchTexture( 77 context, R.drawable.scrollbar_handle_vertical)); 78 mModel = new PreferenceAdapter(context, mPreference); 79 mPopupContent.setOnItemSelectedListener(new MyListener(mModel)); 80 mPopupContent.setDataModel(mModel); 81 } 82 mModel.overrideSettings(mOverride); 83 return mPopupContent; 84 } 85 86 protected void onPreferenceChanged(int newIndex) { 87 if (newIndex == mIndex) return; 88 mIndex = newIndex; 89 invalidate(); 90 } 91 92 private class MyListener implements OnItemSelectedListener { 93 94 private final PreferenceAdapter mAdapter; 95 96 public MyListener(PreferenceAdapter adapter) { 97 mAdapter = adapter; 98 } 99 100 public void onItemSelected(GLView view, int position) { 101 mAdapter.onItemSelected(view, position); 102 onPreferenceChanged(position - 1); 103 } 104 } 105 106 @Override 107 protected ResourceTexture getIcon() { 108 int index = mIndex; 109 if (mIcon[index] == null) { 110 Context context = getGLRootView().getContext(); 111 mIcon[index] = new ResourceTexture( 112 context, mPreference.getLargeIconIds()[index]); 113 } 114 return mIcon[index]; 115 } 116 } 117