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