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 import android.util.AttributeSet;
     21 import android.util.Log;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.AdapterView;
     26 import android.widget.ArrayAdapter;
     27 import android.widget.ListView;
     28 
     29 import com.android.camera.ListPreference;
     30 import com.android.camera.PreferenceGroup;
     31 import com.android.camera.R;
     32 
     33 import java.util.ArrayList;
     34 
     35 /* A popup window that contains several camera settings. */
     36 public class MoreSettingPopup extends AbstractSettingPopup
     37         implements InLineSettingItem.Listener,
     38         AdapterView.OnItemClickListener {
     39     @SuppressWarnings("unused")
     40     private static final String TAG = "MoreSettingPopup";
     41 
     42     private Listener mListener;
     43     private ArrayList<ListPreference> mListItem = new ArrayList<ListPreference>();
     44 
     45     // Keep track of which setting items are disabled
     46     // e.g. White balance will be disabled when scene mode is set to non-auto
     47     private boolean[] mEnabled;
     48 
     49     static public interface Listener {
     50         public void onSettingChanged(ListPreference pref);
     51         public void onPreferenceClicked(ListPreference pref);
     52     }
     53 
     54     private class MoreSettingAdapter extends ArrayAdapter<ListPreference> {
     55         LayoutInflater mInflater;
     56         String mOnString;
     57         String mOffString;
     58         MoreSettingAdapter() {
     59             super(MoreSettingPopup.this.getContext(), 0, mListItem);
     60             Context context = getContext();
     61             mInflater = LayoutInflater.from(context);
     62             mOnString = context.getString(R.string.setting_on);
     63             mOffString = context.getString(R.string.setting_off);
     64         }
     65 
     66         private int getSettingLayoutId(ListPreference pref) {
     67 
     68             if (isOnOffPreference(pref)) {
     69                 return R.layout.in_line_setting_check_box;
     70             }
     71             return R.layout.in_line_setting_menu;
     72         }
     73 
     74         private boolean isOnOffPreference(ListPreference pref) {
     75             CharSequence[] entries = pref.getEntries();
     76             if (entries.length != 2) return false;
     77             String str1 = entries[0].toString();
     78             String str2 = entries[1].toString();
     79             return ((str1.equals(mOnString) && str2.equals(mOffString)) ||
     80                     (str1.equals(mOffString) && str2.equals(mOnString)));
     81         }
     82 
     83         @Override
     84         public View getView(int position, View convertView, ViewGroup parent) {
     85             if (convertView != null) return convertView;
     86 
     87             ListPreference pref = mListItem.get(position);
     88 
     89             int viewLayoutId = getSettingLayoutId(pref);
     90             InLineSettingItem view = (InLineSettingItem)
     91                     mInflater.inflate(viewLayoutId, parent, false);
     92 
     93             view.initialize(pref); // no init for restore one
     94             view.setSettingChangedListener(MoreSettingPopup.this);
     95             if (position >= 0 && position < mEnabled.length) {
     96                 view.setEnabled(mEnabled[position]);
     97             } else {
     98                 Log.w(TAG, "Invalid input: enabled list length, " + mEnabled.length
     99                         + " position " + position);
    100             }
    101             return view;
    102         }
    103 
    104         @Override
    105         public boolean isEnabled(int position) {
    106             if (position >= 0 && position < mEnabled.length) {
    107                 return mEnabled[position];
    108             }
    109             return true;
    110         }
    111     }
    112 
    113     public void setSettingChangedListener(Listener listener) {
    114         mListener = listener;
    115     }
    116 
    117     public MoreSettingPopup(Context context, AttributeSet attrs) {
    118         super(context, attrs);
    119     }
    120 
    121     public void initialize(PreferenceGroup group, String[] keys) {
    122         // Prepare the setting items.
    123         for (int i = 0; i < keys.length; ++i) {
    124             ListPreference pref = group.findPreference(keys[i]);
    125             if (pref != null) mListItem.add(pref);
    126         }
    127 
    128         ArrayAdapter<ListPreference> mListItemAdapter = new MoreSettingAdapter();
    129         ((ListView) mSettingList).setAdapter(mListItemAdapter);
    130         ((ListView) mSettingList).setOnItemClickListener(this);
    131         ((ListView) mSettingList).setSelector(android.R.color.transparent);
    132         // Initialize mEnabled
    133         mEnabled = new boolean[mListItem.size()];
    134         for (int i = 0; i < mEnabled.length; i++) {
    135             mEnabled[i] = true;
    136         }
    137     }
    138 
    139     // When preferences are disabled, we will display them grayed out. Users
    140     // will not be able to change the disabled preferences, but they can still see
    141     // the current value of the preferences
    142     public void setPreferenceEnabled(String key, boolean enable) {
    143         int count = mEnabled == null ? 0 : mEnabled.length;
    144         for (int j = 0; j < count; j++) {
    145             ListPreference pref = mListItem.get(j);
    146             if (pref != null && key.equals(pref.getKey())) {
    147                 mEnabled[j] = enable;
    148                 break;
    149             }
    150         }
    151     }
    152 
    153     public void onSettingChanged(ListPreference pref) {
    154         if (mListener != null) {
    155             mListener.onSettingChanged(pref);
    156         }
    157     }
    158 
    159     // Scene mode can override other camera settings (ex: flash mode).
    160     public void overrideSettings(final String ... keyvalues) {
    161         int count = mEnabled == null ? 0 : mEnabled.length;
    162         for (int i = 0; i < keyvalues.length; i += 2) {
    163             String key = keyvalues[i];
    164             String value = keyvalues[i + 1];
    165             for (int j = 0; j < count; j++) {
    166                 ListPreference pref = mListItem.get(j);
    167                 if (pref != null && key.equals(pref.getKey())) {
    168                     // Change preference
    169                     if (value != null) pref.setValue(value);
    170                     // If the preference is overridden, disable the preference
    171                     boolean enable = value == null;
    172                     mEnabled[j] = enable;
    173                     if (mSettingList.getChildCount() > j) {
    174                         mSettingList.getChildAt(j).setEnabled(enable);
    175                     }
    176                 }
    177             }
    178         }
    179         reloadPreference();
    180     }
    181 
    182     @Override
    183     public void onItemClick(AdapterView<?> parent, View view, int position,
    184             long id) {
    185         if (mListener != null) {
    186             ListPreference pref = mListItem.get(position);
    187             mListener.onPreferenceClicked(pref);
    188         }
    189     }
    190 
    191     @Override
    192     public void reloadPreference() {
    193         int count = mSettingList.getChildCount();
    194         for (int i = 0; i < count; i++) {
    195             ListPreference pref = mListItem.get(i);
    196             if (pref != null) {
    197                 InLineSettingItem settingItem =
    198                         (InLineSettingItem) mSettingList.getChildAt(i);
    199                 settingItem.reloadPreference();
    200             }
    201         }
    202     }
    203 }
    204