IndicatorControl.java revision 95788ace8ad3630312f2f94544690c448c935f9e
1/*
2 * Copyright (C) 2011 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.CameraPreference.OnPreferenceChangedListener;
20import com.android.camera.IconListPreference;
21import com.android.camera.ListPreference;
22import com.android.camera.PreferenceGroup;
23import com.android.camera.CameraSettings;
24import com.android.camera.R;
25
26import android.content.Context;
27import android.util.AttributeSet;
28import android.view.View;
29import android.widget.ImageView;
30import android.widget.RelativeLayout;
31
32import java.util.ArrayList;
33
34/**
35 * A view that contains camera setting indicators. The indicators are spreaded
36 * differently based on the screen resolution.
37 */
38public abstract class IndicatorControl extends RelativeLayout implements
39        IndicatorButton.Listener, OtherSettingsPopup.Listener {
40    private static final String TAG = "IndicatorControl";
41
42    private Context mContext;
43    private OnPreferenceChangedListener mListener;
44    protected OnIndicatorEventListener mOnIndicatorEventListener;
45    protected CameraPicker mCameraPicker;
46
47    private PreferenceGroup mPreferenceGroup;
48    private int mDegree = 0;
49
50    ArrayList<AbstractIndicatorButton> mIndicators =
51            new ArrayList<AbstractIndicatorButton>();
52
53    public void setListener(OnPreferenceChangedListener listener) {
54        mListener = listener;
55        if (mCameraPicker != null) mCameraPicker.setListener(listener);
56    }
57
58    public IndicatorControl(Context context, AttributeSet attrs) {
59        super(context, attrs);
60        mContext = context;
61    }
62
63    public void setDegree(int degree) {
64        mDegree = degree;
65        int count = getChildCount();
66        for (int i = 0 ; i < count ; ++i) {
67            View view = getChildAt(i);
68            if (view instanceof RotateImageView) {
69                ((RotateImageView) view).setDegree(degree);
70            }
71        }
72    }
73
74    public void setOnIndicatorEventListener(OnIndicatorEventListener listener) {
75        mOnIndicatorEventListener = listener;
76    }
77
78    // For the initialization of first-level indicator control.
79    public void initialize(Context context, PreferenceGroup group,
80            String flashSetting, String[] keys, String[] otherSettingKeys) {
81        initialize(context, group, keys, otherSettingKeys);
82    }
83
84    public void initialize(Context context, PreferenceGroup group,
85            String[] keys, String[] otherSettingKeys) {
86        // Reset the variables and states.
87        dismissSettingPopup();
88        removeIndicators();
89
90        // Initialize all variables and icons.
91        mPreferenceGroup = group;
92        // Add other settings indicator.
93        if (otherSettingKeys != null) {
94            addOtherSettingIndicator(context, R.drawable.ic_viewfinder_settings, otherSettingKeys);
95        }
96
97        for (int i = 0; i < keys.length; i++) {
98            IconListPreference pref = (IconListPreference) group.findPreference(keys[i]);
99            if (pref != null) {
100                addIndicator(context, pref);
101            }
102        }
103        requestLayout();
104    }
105
106    public void initializeCameraPicker(Context context, PreferenceGroup group) {
107        ListPreference pref = group.findPreference(
108                CameraSettings.KEY_CAMERA_ID);
109        if (pref == null) return;
110        mCameraPicker = new CameraPicker(context);
111        mCameraPicker.initialize(pref);
112        mCameraPicker.setCameraPickerIcon();
113        addView(mCameraPicker);
114    }
115
116    private void removeIndicators() {
117        for (View v: mIndicators) {
118            removeView(v);
119        }
120        mIndicators.clear();
121    }
122
123    @Override
124    public boolean shouldDelayChildPressedState() {
125        // Return false so the pressed feedback of the back/front camera switch
126        // can be showed right away.
127        return false;
128    }
129
130    public void addIndicator(Context context, IconListPreference pref) {
131        IndicatorButton b = new IndicatorButton(context, pref);
132        b.setSettingChangedListener(this);
133        addView(b);
134        mIndicators.add(b);
135    }
136
137    public void addOtherSettingIndicator(Context context, int resId, String[] keys) {
138        OtherSettingIndicatorButton b = new OtherSettingIndicatorButton(context, resId,
139                mPreferenceGroup, keys);
140        b.setSettingChangedListener(this);
141        addView(b);
142        mIndicators.add(b);
143    }
144
145    @Override
146    public void onRestorePreferencesClicked() {
147        if (mListener != null) {
148            mListener.onRestorePreferencesClicked();
149        }
150    }
151
152    @Override
153    public void onSettingChanged() {
154        if (mListener != null) {
155            mListener.onSharedPreferenceChanged();
156        }
157    }
158
159    public boolean dismissSettingPopup() {
160        for (AbstractIndicatorButton v: mIndicators) {
161            if (v.dismissPopup()) {
162                invalidate();
163                return true;
164            }
165        }
166        return false;
167    }
168
169    public View getActiveSettingPopup() {
170        for (AbstractIndicatorButton v: mIndicators) {
171            View result = v.getPopupWindow();
172            if (result != null) return result;
173        }
174        return null;
175    }
176
177    // Scene mode may override other camera settings (ex: flash mode).
178    public void overrideSettings(final String ... keyvalues) {
179        if (keyvalues.length % 2 != 0) {
180            throw new IllegalArgumentException();
181        }
182
183        for (AbstractIndicatorButton b: mIndicators) {
184            b.overrideSettings(keyvalues);
185        }
186    }
187
188    public void reloadPreferences() {
189        mPreferenceGroup.reloadValue();
190        for (AbstractIndicatorButton b: mIndicators) {
191            b.reloadPreferences();
192        }
193    }
194
195    @Override
196    public void setEnabled(boolean enabled) {
197        super.setEnabled(enabled);
198        final int count = getChildCount();
199        for (int i = 0; i < count; i++) {
200            View v = getChildAt(i);
201            // Zoom buttons and shutter button are controlled by the activity.
202            if (v instanceof AbstractIndicatorButton) {
203                v.setEnabled(enabled);
204            }
205        }
206        if (mCameraPicker != null) mCameraPicker.setEnabled(enabled);
207    }
208}
209