summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/OtherSettingsPopup.java
blob: 7e4bedbb4aec542a005df59466e1a1167ac2e014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.ui;

import com.android.camera.ListPreference;
import com.android.camera.PreferenceGroup;
import com.android.camera.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* A popup window that contains several camera settings. */
public class OtherSettingsPopup extends AbstractSettingPopup
        implements InLineSettingPicker.Listener,
        AdapterView.OnItemClickListener {
    private static final String TAG = "OtherSettingsPopup";
    private static final String ITEM_KEY = "key";
    private static final String ITEM_TITLE = "text";
    private static final String ITEM_VALUE = "value";
    private static final String ITEM_RESTORE = "reset";

    private Context mContext;
    private Listener mListener;
    private ArrayList<HashMap<String, Object>> mListItem =
            new ArrayList<HashMap<String, Object>>();

    static public interface Listener {
        public void onSettingChanged();
        public void onRestorePreferencesClicked();
    }

    private class OtherSettingsAdapter extends SimpleAdapter {

        OtherSettingsAdapter(Context context,
                List<? extends Map<String, ?>> data,
                int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView != null) return convertView;

            InLineSettingPicker view = (InLineSettingPicker)
                    super.getView(position, convertView, parent);
            TextView restoreSettings =
                    (TextView) view.findViewById(R.id.restore);
            View settingItem = view.findViewById(R.id.setting_item);

            // We apply the same View(InLineSettingPicker) as the listview's
            // components. To show the restore setting line, we control the
            // visibilities of components in InLineSettingPicker.
            boolean isRestoreItem = (position == mListItem.size() - 1);
            settingItem.setVisibility(
                    isRestoreItem ? View.GONE : View.VISIBLE);
            restoreSettings.setVisibility(
                    isRestoreItem ? View.VISIBLE : View.GONE);

            if (!isRestoreItem) {
                HashMap map = (HashMap) mListItem.get(position);
                ListPreference pref = (ListPreference) map.get(ITEM_KEY);
                view.initialize(pref);
                view.setSettingChangedListener(OtherSettingsPopup.this);
            }
            return view;
        }
    }

    public void setSettingChangedListener(Listener listener) {
        mListener = listener;
    }

    public OtherSettingsPopup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public void initialize(PreferenceGroup group, String[] keys) {
        // Prepare the setting items.
        for (int i = 0; i < keys.length; ++i) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            ListPreference pref = group.findPreference(keys[i]);
            if (pref != null) {
                map.put(ITEM_KEY, pref);
                map.put(ITEM_TITLE, pref.getTitle());
                map.put(ITEM_VALUE, pref.getEntry());
                mListItem.add(map);
            }
        }

        // Prepare the restore setting line.
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(ITEM_RESTORE, mContext.getString(R.string.pref_restore_detail));
        mListItem.add(map);

        SimpleAdapter mListItemAdapter = new OtherSettingsAdapter(mContext,
                mListItem,
                R.layout.in_line_setting_picker,
                new String[] {ITEM_TITLE, ITEM_VALUE, ITEM_RESTORE},
                new int[] {R.id.title, R.id.current_setting, R.id.restore});
        ((ListView) mSettingList).setAdapter(mListItemAdapter);
        ((ListView) mSettingList).setOnItemClickListener(this);
        ((ListView) mSettingList).setSelector(android.R.color.transparent);
    }

    @Override
    public void onSettingChanged() {
        if (mListener != null) {
            mListener.onSettingChanged();
        }
    }

    // Scene mode can override other camera settings (ex: flash mode).
    public void overrideSettings(final String ... keyvalues) {
        int count = mSettingList.getChildCount();
        for (int i = 0; i < keyvalues.length; i += 2) {
            String key = keyvalues[i];
            String value = keyvalues[i + 1];
            for (int j = 0; j < count; j++) {
                ListPreference pref = (ListPreference) mListItem.get(j).get(ITEM_KEY);
                if (pref != null && key.equals(pref.getKey())) {
                    InLineSettingPicker picker =
                            (InLineSettingPicker) mSettingList.getChildAt(j);
                    picker.overrideSettings(value);
                }
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        if ((position == mListItem.size() - 1) && (mListener != null)) {
            mListener.onRestorePreferencesClicked();
        }
    }

    @Override
    public void reloadPreference() {
        int count = mSettingList.getChildCount();
        for (int i = 0; i < count; i++) {
            ListPreference pref = (ListPreference) mListItem.get(i).get(ITEM_KEY);
            if (pref != null) {
                InLineSettingPicker picker =
                        (InLineSettingPicker) mSettingList.getChildAt(i);
                picker.reloadPreference();
            }
        }
    }
}