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