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 com.android.camera.CameraSettings;
20 import com.android.camera.ListPreference;
21 import com.android.camera.PreferenceGroup;
22 import com.android.camera.R;
23 import com.android.camera.RecordLocationPreference;
24 
25 import android.content.Context;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.view.LayoutInflater;
29 import android.view.ViewGroup;
30 import android.widget.AdapterView;
31 import android.widget.ListView;
32 import android.widget.ArrayAdapter;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /* A popup window that contains several camera settings. */
38 public class OtherSettingsPopup extends AbstractSettingPopup
39         implements InLineSettingItem.Listener,
40         AdapterView.OnItemClickListener {
41     private static final String TAG = "OtherSettingsPopup";
42 
43     private Listener mListener;
44     private ArrayList<ListPreference> mListItem = new ArrayList<ListPreference>();
45 
46     static public interface Listener {
onSettingChanged()47         public void onSettingChanged();
onRestorePreferencesClicked()48         public void onRestorePreferencesClicked();
49     }
50 
51     private class OtherSettingsAdapter extends ArrayAdapter {
52         LayoutInflater mInflater;
53 
OtherSettingsAdapter()54         OtherSettingsAdapter() {
55             super(OtherSettingsPopup.this.getContext(), 0, mListItem);
56             mInflater = LayoutInflater.from(getContext());
57         }
58 
getSettingLayoutId(ListPreference pref)59         private int getSettingLayoutId(ListPreference pref) {
60             // If the preference is null, it will be the only item , i.e.
61             // 'Restore setting' in the popup window.
62             if (pref == null) return R.layout.in_line_setting_restore;
63 
64             // Currently, the RecordLocationPreference is the only setting
65             // which applies the on/off switch.
66             if (CameraSettings.KEY_RECORD_LOCATION.equals(pref.getKey())) {
67                 return R.layout.in_line_setting_switch;
68             }
69             return R.layout.in_line_setting_knob;
70         }
71 
72         @Override
getView(int position, View convertView, ViewGroup parent)73         public View getView(int position, View convertView, ViewGroup parent) {
74             if (convertView != null) return convertView;
75 
76             ListPreference pref = mListItem.get(position);
77 
78             int viewLayoutId = getSettingLayoutId(pref);
79             InLineSettingItem view = (InLineSettingItem)
80                     mInflater.inflate(viewLayoutId, parent, false);
81 
82             view.initialize(pref); // no init for restore one
83             view.setSettingChangedListener(OtherSettingsPopup.this);
84             return view;
85         }
86     }
87 
setSettingChangedListener(Listener listener)88     public void setSettingChangedListener(Listener listener) {
89         mListener = listener;
90     }
91 
OtherSettingsPopup(Context context, AttributeSet attrs)92     public OtherSettingsPopup(Context context, AttributeSet attrs) {
93         super(context, attrs);
94     }
95 
initialize(PreferenceGroup group, String[] keys)96     public void initialize(PreferenceGroup group, String[] keys) {
97         // Prepare the setting items.
98         for (int i = 0; i < keys.length; ++i) {
99             ListPreference pref = group.findPreference(keys[i]);
100             if (pref != null) mListItem.add(pref);
101         }
102 
103         // Prepare the restore setting line.
104         mListItem.add(null);
105 
106         ArrayAdapter mListItemAdapter = new OtherSettingsAdapter();
107         ((ListView) mSettingList).setAdapter(mListItemAdapter);
108         ((ListView) mSettingList).setOnItemClickListener(this);
109         ((ListView) mSettingList).setSelector(android.R.color.transparent);
110     }
111 
112     @Override
onSettingChanged()113     public void onSettingChanged() {
114         if (mListener != null) {
115             mListener.onSettingChanged();
116         }
117     }
118 
119     // Scene mode can override other camera settings (ex: flash mode).
overrideSettings(final String ... keyvalues)120     public void overrideSettings(final String ... keyvalues) {
121         int count = mSettingList.getChildCount();
122         for (int i = 0; i < keyvalues.length; i += 2) {
123             String key = keyvalues[i];
124             String value = keyvalues[i + 1];
125             for (int j = 0; j < count; j++) {
126                 ListPreference pref = (ListPreference) mListItem.get(j);
127                 if (pref != null && key.equals(pref.getKey())) {
128                     InLineSettingItem settingItem =
129                             (InLineSettingItem) mSettingList.getChildAt(j);
130                     settingItem.overrideSettings(value);
131                 }
132             }
133         }
134     }
135 
136     @Override
onItemClick(AdapterView<?> parent, View view, int position, long id)137     public void onItemClick(AdapterView<?> parent, View view, int position,
138             long id) {
139         if ((position == mListItem.size() - 1) && (mListener != null)) {
140             mListener.onRestorePreferencesClicked();
141         }
142     }
143 
144     @Override
reloadPreference()145     public void reloadPreference() {
146         int count = mSettingList.getChildCount();
147         for (int i = 0; i < count; i++) {
148             ListPreference pref = (ListPreference) mListItem.get(i);
149             if (pref != null) {
150                 InLineSettingItem settingItem =
151                         (InLineSettingItem) mSettingList.getChildAt(i);
152                 settingItem.reloadPreference();
153             }
154         }
155     }
156 }
157