ButtonManager.java revision 58598dc16e1f5a747fe1407ec1641a5529f423e5
1/*
2 * Copyright (C) 2013 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;
18
19import android.view.View;
20import android.widget.ImageButton;
21
22import com.android.camera.settings.SettingsManager;
23
24/**
25 * A  class for generating pre-initialized
26 * {@link #android.widget.ImageButton}s.
27 */
28public class ButtonManager {
29
30    public static final int BUTTON_FLASH = 0;
31    public static final int BUTTON_TORCH = 1;
32    public static final int BUTTON_CAMERA = 2;
33    public static final int BUTTON_HDRPLUS = 3;
34    public static final int BUTTON_CANCEL = 4;
35    public static final int BUTTON_DONE = 5;
36    public static final int BUTTON_RETAKE = 6;
37    public static final int BUTTON_REVIEW = 7;
38
39    /** A reference to the activity for finding button views on demand. */
40    private final CameraActivity mActivity;
41    /** A reference to the application's settings manager. */
42    private final SettingsManager mSettingsManager;
43
44    /**
45     * Get a new global ButtonManager.
46     */
47    public ButtonManager(CameraActivity activity) {
48        mActivity = activity;
49        mSettingsManager = activity.getSettingsManager();
50    }
51
52    /**
53     * A callback executed in the state listener of a button.
54     * Used by a module to set specific behavior when a button's
55     * state changes.
56     */
57    public interface ButtonCallback {
58        public void onStateChanged(int state);
59    }
60
61    private MultiToggleImageButton getButtonOrError(int buttonId, int resId) {
62        MultiToggleImageButton button
63            = (MultiToggleImageButton) mActivity.findViewById(resId);
64        if (button == null) {
65            switch (buttonId) {
66                case BUTTON_FLASH:
67                    throw new IllegalStateException("Flash button could not be found.");
68                case BUTTON_TORCH:
69                    throw new IllegalStateException("Torch button could not be found.");
70                case BUTTON_CAMERA:
71                    throw new IllegalStateException("Camera button could not be found.");
72                case BUTTON_HDRPLUS:
73                    throw new IllegalStateException("Hdr button could not be found.");
74                default:
75                    throw new IllegalArgumentException("button not known by id=" + buttonId);
76            }
77        }
78        return button;
79    }
80
81    private ImageButton getImageButtonOrError(int buttonId, int resId) {
82        ImageButton button = (ImageButton) mActivity.findViewById(resId);
83        if (button == null) {
84            switch (buttonId) {
85                case BUTTON_CANCEL:
86                    throw new IllegalStateException("Cancel button could not be found.");
87                case BUTTON_DONE:
88                    throw new IllegalStateException("Done button could not be found.");
89                case BUTTON_RETAKE:
90                    throw new IllegalStateException("Retake button could not be found.");
91                case BUTTON_REVIEW:
92                    throw new IllegalStateException("Review button could not be found.");
93
94                default:
95                    throw new IllegalArgumentException("button not known by id=" + buttonId);
96            }
97        }
98        return button;
99    }
100
101    /**
102     * Enable a known button by id, with a state change callback and
103     * a resource id that points to an array of drawables.
104     */
105    public void enableButton(int buttonId, int resId, ButtonCallback cb, int resIdImages) {
106        MultiToggleImageButton button = getButtonOrError(buttonId, resId);
107        switch (buttonId) {
108            case BUTTON_FLASH:
109                enableFlashButton(button, cb, resIdImages);
110                break;
111            case BUTTON_CAMERA:
112                enableCameraButton(button, cb, resIdImages);
113                break;
114            case BUTTON_HDRPLUS:
115                enableHdrPlusButton(button, cb, resIdImages);
116                break;
117            case BUTTON_TORCH:
118                enableTorchButton(button, cb, resIdImages);
119                break;
120            default:
121                throw new IllegalArgumentException("button not known by id=" + buttonId);
122        }
123        button.setVisibility(View.VISIBLE);
124    }
125
126    public void enablePushButton(int buttonId, int resId, View.OnClickListener cb) {
127        ImageButton button = getImageButtonOrError(buttonId, resId);
128        button.setOnClickListener(cb);
129        button.setEnabled(true);
130    }
131
132    /**
133     * Disable a known button by id.
134     */
135    public void disableButton(int buttonId, int resId) {
136        MultiToggleImageButton button = getButtonOrError(buttonId, resId);
137        button.setEnabled(false);
138    }
139
140    /**
141     * Enable a flash button.
142     */
143    private void enableFlashButton(MultiToggleImageButton button,
144            final ButtonCallback cb, int resIdImages) {
145
146        if (resIdImages > 0) {
147            button.overrideImageIds(resIdImages);
148        }
149        int index = mSettingsManager.getStringValueIndex(SettingsManager.SETTING_FLASH_MODE);
150        if (index >= 0) {
151            button.setState(index, false);
152        }
153        button.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() {
154                @Override
155                public void stateChanged(View view, int state) {
156                    mSettingsManager.setStringValueIndex(SettingsManager.SETTING_FLASH_MODE, state);
157                    if (cb != null) {
158                        cb.onStateChanged(state);
159                    }
160                }
161            });
162    }
163
164    /**
165     * Enable video torch button
166     */
167    private void enableTorchButton(MultiToggleImageButton button,
168            final ButtonCallback cb, int resIdImages) {
169        if (resIdImages > 0) {
170            button.overrideImageIds(resIdImages);
171        }
172        int index = mSettingsManager.getStringValueIndex(
173                SettingsManager.SETTING_VIDEOCAMERA_FLASH_MODE);
174        if (index >= 0) {
175            button.setState(index, false);
176        }
177        button.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() {
178                @Override
179                public void stateChanged(View view, int state) {
180                    mSettingsManager.setStringValueIndex(
181                            SettingsManager.SETTING_VIDEOCAMERA_FLASH_MODE, state);
182                    if(cb != null) {
183                        cb.onStateChanged(state);
184                    }
185                }
186            });
187    }
188
189    /**
190     * Enable a camera button.
191     */
192    private void enableCameraButton(MultiToggleImageButton button,
193            final ButtonCallback cb, int resIdImages) {
194
195        if (resIdImages > 0) {
196            button.overrideImageIds(resIdImages);
197        }
198
199        int index = mSettingsManager.getStringValueIndex(SettingsManager.SETTING_CAMERA_ID);
200        if (index >= 0) {
201            button.setState(index, false);
202        }
203
204        button.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() {
205                @Override
206                public void stateChanged(View view, int state) {
207                    mSettingsManager.setStringValueIndex(SettingsManager.SETTING_CAMERA_ID, state);
208                    int cameraId = Integer.parseInt(mSettingsManager.get(
209                        SettingsManager.SETTING_CAMERA_ID));
210                    if (cb != null) {
211                        cb.onStateChanged(cameraId);
212                    }
213                }
214            });
215    }
216
217    /**
218     * Enable an hdr plus button.
219     */
220    private void enableHdrPlusButton(MultiToggleImageButton button,
221            final ButtonCallback cb, int resIdImages) {
222
223        if (resIdImages > 0) {
224            button.overrideImageIds(resIdImages);
225        }
226
227        int index = mSettingsManager.getStringValueIndex(SettingsManager.SETTING_CAMERA_HDR);
228        if (index >= 0) {
229            button.setState(index, false);
230        }
231
232        button.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() {
233                @Override
234                public void stateChanged(View view, int state) {
235                    mSettingsManager.setStringValueIndex(SettingsManager.SETTING_CAMERA_HDR, state);
236                    if (cb != null) {
237                        cb.onStateChanged(state);
238                    }
239                }
240            });
241    }
242}