summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/widget/IndicatorIconController.java
blob: e99bfe438765651d7c70bd3bff30db69cc8eb1bd (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (C) 2014 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.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.android.camera.ButtonManager;
import com.android.camera.app.AppController;
import com.android.camera.debug.Log;
import com.android.camera.settings.Keys;
import com.android.camera.settings.SettingsManager;
import com.android.camera.util.PhotoSphereHelper;
import com.android.camera2.R;

/**
 * IndicatorIconController sets the visibility and icon state of
 * on screen indicators.
 *
 * Indicators are only visible if they are in a non-default state.  The
 * visibility of an indicator is set when an indicator's setting changes.
 */
public class IndicatorIconController
    implements SettingsManager.OnSettingChangedListener,
               ButtonManager.ButtonStatusListener {

    private final static Log.Tag TAG = new Log.Tag("IndicatorIconCtrlr");

    private ImageView mFlashIndicator;
    private ImageView mHdrIndicator;
    private ImageView mPanoIndicator;
    private ImageView mCountdownTimerIndicator;

    private ImageView mExposureIndicatorN2;
    private ImageView mExposureIndicatorN1;
    private ImageView mExposureIndicatorP1;
    private ImageView mExposureIndicatorP2;

    private TypedArray mFlashIndicatorPhotoIcons;
    private TypedArray mFlashIndicatorVideoIcons;
    private TypedArray mHdrPlusIndicatorIcons;
    private TypedArray mHdrIndicatorIcons;
    private TypedArray mPanoIndicatorIcons;
    private TypedArray mCountdownTimerIndicatorIcons;

    private AppController mController;

    public IndicatorIconController(AppController controller, View root) {
        mController = controller;
        Context context = controller.getAndroidContext();

        mFlashIndicator = (ImageView) root.findViewById(R.id.flash_indicator);
        mFlashIndicatorPhotoIcons = context.getResources().obtainTypedArray(
            R.array.camera_flashmode_indicator_icons);
        mFlashIndicatorVideoIcons = context.getResources().obtainTypedArray(
            R.array.video_flashmode_indicator_icons);

        mHdrIndicator = (ImageView) root.findViewById(R.id.hdr_indicator);
        mHdrPlusIndicatorIcons = context.getResources().obtainTypedArray(
            R.array.pref_camera_hdr_plus_indicator_icons);
        mHdrIndicatorIcons = context.getResources().obtainTypedArray(
            R.array.pref_camera_hdr_indicator_icons);

        int panoIndicatorArrayId = PhotoSphereHelper.getPanoramaOrientationIndicatorArrayId();
        if (panoIndicatorArrayId > 0) {
            mPanoIndicator = (ImageView) root.findViewById(R.id.pano_indicator);
            mPanoIndicatorIcons =
                context.getResources().obtainTypedArray(panoIndicatorArrayId);
        }

        mCountdownTimerIndicator = (ImageView) root.findViewById(R.id.countdown_timer_indicator);
        mCountdownTimerIndicatorIcons = context.getResources().obtainTypedArray(
                R.array.pref_camera_countdown_indicators);

        mExposureIndicatorN2 = (ImageView) root.findViewById(R.id.exposure_n2_indicator);
        mExposureIndicatorN1 = (ImageView) root.findViewById(R.id.exposure_n1_indicator);
        mExposureIndicatorP1 = (ImageView) root.findViewById(R.id.exposure_p1_indicator);
        mExposureIndicatorP2 = (ImageView) root.findViewById(R.id.exposure_p2_indicator);
    }

    @Override
    public void onButtonVisibilityChanged(ButtonManager buttonManager, int buttonId) {
        syncIndicatorWithButton(buttonId);
    }

    @Override
    public void onButtonEnabledChanged(ButtonManager buttonManager, int buttonId) {
        syncIndicatorWithButton(buttonId);
    }

    /**
     * Syncs a specific indicator's icon and visibility
     * based on the enabled state and visibility of a button.
     */
    private void syncIndicatorWithButton(int buttonId) {
        switch (buttonId) {
            case ButtonManager.BUTTON_FLASH: {
                syncFlashIndicator();
                break;
            }
            case ButtonManager.BUTTON_TORCH: {
                syncFlashIndicator();
                break;
            }
            case ButtonManager.BUTTON_HDR_PLUS: {
                syncHdrIndicator();
                break;
            }
            case ButtonManager.BUTTON_HDR: {
                syncHdrIndicator();
                break;
            }
            case ButtonManager.BUTTON_EXPOSURE_COMPENSATION: {
                syncExposureIndicator();
                break;
            }
            default:
                // Do nothing.  The indicator doesn't care
                // about button that don't correspond to indicators.
        }
    }

    /**
     * Sets all indicators to the correct resource and visibility
     * based on the current settings.
     */
    public void syncIndicators() {
        syncFlashIndicator();
        syncHdrIndicator();
        syncPanoIndicator();
        syncExposureIndicator();
        syncCountdownTimerIndicator();
    }

    /**
     * If the new visibility is different from the current visibility
     * on a view, change the visibility and call any registered
     * {@link OnIndicatorVisibilityChangedListener}.
     */
    private static void changeVisibility(View view, int visibility) {
        if (view.getVisibility() != visibility) {
            view.setVisibility(visibility);
        }
    }

    /**
     * Sync the icon and visibility of the flash indicator.
     */
    private void syncFlashIndicator() {
        ButtonManager buttonManager = mController.getButtonManager();
        // If flash isn't an enabled and visible option,
        // do not show the indicator.
        if (buttonManager.isEnabled(ButtonManager.BUTTON_FLASH)
                && buttonManager.isVisible(ButtonManager.BUTTON_FLASH)) {

            int modeIndex = mController.getCurrentModuleIndex();
            if (modeIndex == mController.getAndroidContext().getResources()
                    .getInteger(R.integer.camera_mode_video)) {
                setIndicatorState(mController.getCameraScope(),
                                  Keys.KEY_VIDEOCAMERA_FLASH_MODE, mFlashIndicator,
                                  mFlashIndicatorVideoIcons, false);
            } else if (modeIndex == mController.getAndroidContext().getResources()
                    .getInteger(R.integer.camera_mode_gcam)) {
                setIndicatorState(mController.getCameraScope(),
                                  Keys.KEY_HDR_PLUS_FLASH_MODE, mFlashIndicator,
                                  mFlashIndicatorPhotoIcons, false);
            } else {
                setIndicatorState(mController.getCameraScope(),
                                  Keys.KEY_FLASH_MODE, mFlashIndicator,
                                  mFlashIndicatorPhotoIcons, false);
            }
        } else {
            changeVisibility(mFlashIndicator, View.GONE);
        }
    }

    /**
     * Sync the icon and the visibility of the hdr/hdrplus indicator.
     */
    private void syncHdrIndicator() {
        ButtonManager buttonManager = mController.getButtonManager();
        // If hdr isn't an enabled and visible option,
        // do not show the indicator.
        if (buttonManager.isEnabled(ButtonManager.BUTTON_HDR_PLUS)
                && buttonManager.isVisible(ButtonManager.BUTTON_HDR_PLUS)) {
            setIndicatorState(SettingsManager.SCOPE_GLOBAL,
                              Keys.KEY_CAMERA_HDR_PLUS, mHdrIndicator,
                              mHdrPlusIndicatorIcons, false);
        } else if (buttonManager.isEnabled(ButtonManager.BUTTON_HDR)
                && buttonManager.isVisible(ButtonManager.BUTTON_HDR)) {
            setIndicatorState(SettingsManager.SCOPE_GLOBAL,
                              Keys.KEY_CAMERA_HDR_PLUS, mHdrIndicator,
                              mHdrIndicatorIcons, false);
        } else {
            changeVisibility(mHdrIndicator, View.GONE);
        }
    }

    /**
     * Sync the icon and the visibility of the pano indicator.
     */
    private void syncPanoIndicator() {
        if (mPanoIndicator == null) {
            Log.w(TAG, "Trying to sync a pano indicator that is not initialized.");
            return;
        }

        ButtonManager buttonManager = mController.getButtonManager();
        if (buttonManager.isPanoEnabled()) {
            setIndicatorState(SettingsManager.SCOPE_GLOBAL,
                              Keys.KEY_CAMERA_PANO_ORIENTATION, mPanoIndicator,
                              mPanoIndicatorIcons, true);
        } else {
            changeVisibility(mPanoIndicator, View.GONE);
        }
    }

    private void syncExposureIndicator() {
        if (mExposureIndicatorN2 == null
            || mExposureIndicatorN1 == null
            || mExposureIndicatorP1 == null
            || mExposureIndicatorP2 == null) {
            Log.w(TAG, "Trying to sync exposure indicators that are not initialized.");
            return;
        }


        // Reset all exposure indicator icons.
        changeVisibility(mExposureIndicatorN2, View.GONE);
        changeVisibility(mExposureIndicatorN1, View.GONE);
        changeVisibility(mExposureIndicatorP1, View.GONE);
        changeVisibility(mExposureIndicatorP2, View.GONE);

        ButtonManager buttonManager = mController.getButtonManager();
        if (buttonManager.isEnabled(ButtonManager.BUTTON_EXPOSURE_COMPENSATION)
                && buttonManager.isVisible(ButtonManager.BUTTON_EXPOSURE_COMPENSATION)) {

            int compValue = mController.getSettingsManager().getInteger(
                    mController.getCameraScope(), Keys.KEY_EXPOSURE);
            int comp = Math.round(compValue * buttonManager.getExposureCompensationStep());

            // Turn on the appropriate indicator.
            switch (comp) {
                case -2:
                    changeVisibility(mExposureIndicatorN2, View.VISIBLE);
                    break;
                case -1:
                    changeVisibility(mExposureIndicatorN1, View.VISIBLE);
                    break;
                case 0:
                    // Do nothing.
                    break;
                case 1:
                    changeVisibility(mExposureIndicatorP1, View.VISIBLE);
                    break;
                case 2:
                    changeVisibility(mExposureIndicatorP2, View.VISIBLE);
            }
        }
    }

    private void syncCountdownTimerIndicator() {
        ButtonManager buttonManager = mController.getButtonManager();

        if (buttonManager.isEnabled(ButtonManager.BUTTON_COUNTDOWN)
            && buttonManager.isVisible(ButtonManager.BUTTON_COUNTDOWN)) {
            setIndicatorState(SettingsManager.SCOPE_GLOBAL,
                              Keys.KEY_COUNTDOWN_DURATION, mCountdownTimerIndicator,
                              mCountdownTimerIndicatorIcons, false);
        } else {
            changeVisibility(mCountdownTimerIndicator, View.GONE);
        }
    }

    /**
     * Sets the image resource and visibility of the indicator
     * based on the indicator's corresponding setting state.
     */
    private void setIndicatorState(String scope, String key, ImageView imageView,
                                   TypedArray iconArray, boolean showDefault) {
        SettingsManager settingsManager = mController.getSettingsManager();

        int valueIndex = settingsManager.getIndexOfCurrentValue(scope, key);
        if (valueIndex < 0) {
            // This can happen when the setting is camera dependent
            // and the camera is not yet open.  CameraAppUI.onChangeCamera()
            // will call this again when the camera is open.
            Log.w(TAG, "The setting for this indicator is not available.");
            imageView.setVisibility(View.GONE);
            return;
        }
        Drawable drawable = iconArray.getDrawable(valueIndex);
        if (drawable == null) {
            throw new IllegalStateException("Indicator drawable is null.");
        }
        imageView.setImageDrawable(drawable);

        // Set the indicator visible if not in default state.
        boolean visibilityChanged = false;
        if (!showDefault && settingsManager.isDefault(scope, key)) {
            changeVisibility(imageView, View.GONE);
        } else {
            changeVisibility(imageView, View.VISIBLE);
        }
    }

    @Override
    public void onSettingChanged(SettingsManager settingsManager, String key) {
        if (key.equals(Keys.KEY_FLASH_MODE)) {
            syncFlashIndicator();
            return;
        }
        if (key.equals(Keys.KEY_VIDEOCAMERA_FLASH_MODE)) {
            syncFlashIndicator();
            return;
        }
        if (key.equals(Keys.KEY_CAMERA_HDR_PLUS)) {
            syncHdrIndicator();
            return;
        }
        if (key.equals(Keys.KEY_CAMERA_HDR)) {
            syncHdrIndicator();
            return;
        }
        if (key.equals(Keys.KEY_CAMERA_PANO_ORIENTATION)) {
            syncPanoIndicator();
            return;
        }
        if (key.equals(Keys.KEY_EXPOSURE)) {
            syncExposureIndicator();
            return;
        }
        if (key.equals(Keys.KEY_COUNTDOWN_DURATION)) {
            syncCountdownTimerIndicator();
            return;
        }
    }

}