OtherSettingsPopup.java revision 92e9f2c3078c4625f9531443c75e51c3dfef8b48
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
17package com.android.camera.ui;
18
19import com.android.camera.CameraSettings;
20import com.android.camera.ListPreference;
21import com.android.camera.PreferenceGroup;
22import com.android.camera.R;
23
24import android.content.Context;
25import android.util.AttributeSet;
26import android.util.Log;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.AdapterView;
30import android.widget.ListView;
31import android.widget.SimpleAdapter;
32import android.widget.TextView;
33
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.List;
37import java.util.Map;
38
39/* A popup window that contains several camera settings. */
40public class OtherSettingsPopup extends AbstractSettingPopup
41        implements InLineSettingPicker.Listener,
42        AdapterView.OnItemClickListener {
43    private static final String TAG = "OtherSettingsPopup";
44    private static final String[] OTHER_SETTING_KEYS = {
45            CameraSettings.KEY_RECORD_LOCATION,
46            CameraSettings.KEY_FOCUS_MODE,
47            CameraSettings.KEY_EXPOSURE,
48            CameraSettings.KEY_PICTURE_SIZE,
49            CameraSettings.KEY_JPEG_QUALITY};
50    private static final String ITEM_KEY = "key";
51    private static final String ITEM_TITLE = "text";
52    private static final String ITEM_VALUE = "value";
53    private static final String ITEM_RESTORE = "reset";
54
55    private Context mContext;
56    private Listener mListener;
57    private PreferenceGroup mPreferenceGroup;
58    private ArrayList<HashMap<String, Object>> mListItem =
59            new ArrayList<HashMap<String, Object>>();
60
61    static public interface Listener {
62        public void onOtherSettingChanged();
63        public void onRestorePreferencesClicked();
64    }
65
66    private class OtherSettingsAdapter extends SimpleAdapter {
67
68        OtherSettingsAdapter(Context context,
69                List<? extends Map<String, ?>> data,
70                int resource, String[] from, int[] to) {
71            super(context, data, resource, from, to);
72        }
73
74        @Override
75        public View getView(int position, View convertView, ViewGroup parent) {
76            if (convertView != null) return convertView;
77
78            InLineSettingPicker view = (InLineSettingPicker)
79                    super.getView(position, convertView, parent);
80            TextView restoreSettings =
81                    (TextView) view.findViewById(R.id.restore);
82            View settingItem = view.findViewById(R.id.setting_item);
83
84            // We apply the same View(InLineSettingPicker) as the listview's
85            // components. To show the restore setting line, we control the
86            // visibilities of components in InLineSettingPicker.
87            boolean isRestoreItem = (position == mListItem.size() - 1);
88            settingItem.setVisibility(
89                    isRestoreItem ? View.GONE : View.VISIBLE);
90            restoreSettings.setVisibility(
91                    isRestoreItem ? View.VISIBLE : View.GONE);
92
93            if (!isRestoreItem) {
94                HashMap map = (HashMap) mListItem.get(position);
95                ListPreference pref = (ListPreference) map.get(ITEM_KEY);
96                view.initialize(pref);
97                view.setSettingChangedListener(OtherSettingsPopup.this);
98            }
99            return view;
100        }
101    }
102
103    public void setOtherSettingChangedListener(Listener listener) {
104        mListener = listener;
105    }
106
107    public OtherSettingsPopup(Context context, AttributeSet attrs) {
108        super(context, attrs);
109        mContext = context;
110    }
111
112    public void initialize(PreferenceGroup group) {
113        mPreferenceGroup = group;
114        // Prepare the setting items.
115        for (int i = 0; i < OTHER_SETTING_KEYS.length; ++i) {
116            HashMap<String, Object> map = new HashMap<String, Object>();
117            ListPreference pref = group.findPreference(OTHER_SETTING_KEYS[i]);
118            if (pref != null) {
119                map.put(ITEM_KEY, pref);
120                map.put(ITEM_TITLE, pref.getTitle());
121                map.put(ITEM_VALUE, pref.getEntry());
122                mListItem.add(map);
123            }
124        }
125
126        // Prepare the restore setting line.
127        HashMap<String, Object> map = new HashMap<String, Object>();
128        map.put(ITEM_RESTORE, mContext.getString(R.string.pref_restore_detail));
129        mListItem.add(map);
130
131        SimpleAdapter mListItemAdapter = new OtherSettingsAdapter(mContext,
132                mListItem,
133                R.layout.in_line_setting_picker,
134                new String[] {ITEM_TITLE, ITEM_VALUE, ITEM_RESTORE},
135                new int[] {R.id.title, R.id.current_setting, R.id.restore});
136        ((ListView) mSettingList).setAdapter(mListItemAdapter);
137        ((ListView) mSettingList).setOnItemClickListener(this);
138        ((ListView) mSettingList).setSelector(android.R.color.transparent);
139    }
140
141    public void onSettingChanged() {
142        if (mListener != null) {
143            mListener.onOtherSettingChanged();
144        }
145    }
146
147    // Scene mode can override other camera settings (ex: flash mode).
148    public void overrideSettings(String key, String value) {
149        int count = mSettingList.getChildCount();
150        for (int i = 0; i < count; i++) {
151            if (key.equals(mListItem.get(i).get(ITEM_KEY))) {
152                InLineSettingPicker picker =
153                        (InLineSettingPicker) mSettingList.getChildAt(i);
154                picker.overrideSettings(value);
155            }
156        }
157    }
158
159    @Override
160    public void onItemClick(AdapterView<?> parent, View view, int position,
161            long id) {
162        if ((position == mListItem.size() - 1) && (mListener != null)) {
163            mListener.onRestorePreferencesClicked();
164        }
165    }
166}
167