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_switch;
     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     public void onSettingChanged(ListPreference pref) {
    140         if (mListener != null) {
    141             mListener.onSettingChanged(pref);
    142         }
    143     }
    144 
    145     // Scene mode can override other camera settings (ex: flash mode).
    146     public void overrideSettings(final String ... keyvalues) {
    147         int count = mEnabled == null ? 0 : mEnabled.length;
    148         for (int i = 0; i < keyvalues.length; i += 2) {
    149             String key = keyvalues[i];
    150             String value = keyvalues[i + 1];
    151             for (int j = 0; j < count; j++) {
    152                 ListPreference pref = mListItem.get(j);
    153                 if (pref != null && key.equals(pref.getKey())) {
    154                     // Change preference
    155                     if (value != null) pref.setValue(value);
    156                     // If the preference is overridden, disable the preference
    157                     boolean enable = value == null;
    158                     mEnabled[j] = enable;
    159                     if (mSettingList.getChildCount() > j) {
    160                         mSettingList.getChildAt(j).setEnabled(enable);
    161                     }
    162                 }
    163             }
    164         }
    165         reloadPreference();
    166     }
    167 
    168     @Override
    169     public void onItemClick(AdapterView<?> parent, View view, int position,
    170             long id) {
    171         if (mListener != null) {
    172             ListPreference pref = mListItem.get(position);
    173             mListener.onPreferenceClicked(pref);
    174         }
    175     }
    176 
    177     @Override
    178     public void reloadPreference() {
    179         int count = mSettingList.getChildCount();
    180         for (int i = 0; i < count; i++) {
    181             ListPreference pref = mListItem.get(i);
    182             if (pref != null) {
    183                 InLineSettingItem settingItem =
    184                         (InLineSettingItem) mSettingList.getChildAt(i);
    185                 settingItem.reloadPreference();
    186             }
    187         }
    188     }
    189 }
    190