IndicatorButton.java revision 4cc009b812557bc312af556e3ed64cae2367d69e
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.IconListPreference;
20import com.android.camera.R;
21
22import android.content.Context;
23import android.util.Log;
24import android.view.LayoutInflater;
25import android.view.ViewGroup;
26
27// An indicator button that represents one camera setting. Ex: flash. Pressing it opens a popup
28// window.
29public class IndicatorButton extends AbstractIndicatorButton implements BasicSettingPopup.Listener {
30    private final String TAG = "IndicatorButton";
31    private IconListPreference mPreference;
32    // Scene mode can override the original preference value.
33    private String mOverrideValue;
34    private Listener mListener;
35
36    static public interface Listener {
37        public void onSettingChanged();
38    }
39
40    public void setSettingChangedListener(Listener listener) {
41        mListener = listener;
42    }
43
44    public IndicatorButton(Context context, IconListPreference pref) {
45        super(context);
46        mPreference = pref;
47        reloadPreference();
48    }
49
50    @Override
51    public void reloadPreference() {
52        int[] iconIds = mPreference.getLargeIconIds();
53        if (iconIds != null) {
54            // Each entry has a corresponding icon.
55            int index;
56            if (mOverrideValue == null) {
57                index = mPreference.findIndexOfValue(mPreference.getValue());
58            } else {
59                index = mPreference.findIndexOfValue(mOverrideValue);
60                if (index == -1) {
61                    // Avoid the crash if camera driver has bugs.
62                    Log.e(TAG, "Fail to find override value=" + mOverrideValue);
63                    mPreference.print();
64                    return;
65                }
66            }
67            setImageResource(iconIds[index]);
68        } else {
69            // The preference only has a single icon to represent it.
70            setImageResource(mPreference.getSingleIcon());
71        }
72        super.reloadPreference();
73    }
74
75    public String getKey() {
76        return mPreference.getKey();
77    }
78
79    @Override
80    public boolean isOverridden() {
81        return mOverrideValue != null;
82    }
83
84    @Override
85    public void overrideSettings(final String ... keyvalues) {
86        mOverrideValue = null;
87        for (int i = 0; i < keyvalues.length; i += 2) {
88            String key = keyvalues[i];
89            String value = keyvalues[i + 1];
90            if (key.equals(getKey())) {
91                mOverrideValue = value;
92                setEnabled(value == null);
93                break;
94            }
95        }
96        reloadPreference();
97    }
98
99    @Override
100    protected void initializePopup() {
101        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
102                Context.LAYOUT_INFLATER_SERVICE);
103        ViewGroup root = (ViewGroup) getRootView().findViewById(R.id.frame);
104
105        BasicSettingPopup popup = (BasicSettingPopup) inflater.inflate(
106                R.layout.basic_setting_popup, root, false);
107        popup.setSettingChangedListener(this);
108        popup.initialize(mPreference);
109        root.addView(popup);
110        mPopup = popup;
111    }
112
113    public void onSettingChanged() {
114        reloadPreference();
115        if (mListener != null) {
116            mListener.onSettingChanged();
117        }
118    }
119}
120