2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.widget;
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.drawable.Drawable;
22 import android.view.View;
23 import android.widget.ImageView;
25 import com.android.camera.ButtonManager;
26 import com.android.camera.app.AppController;
27 import com.android.camera.debug.Log;
28 import com.android.camera.settings.Keys;
29 import com.android.camera.settings.SettingsManager;
30 import com.android.camera.util.PhotoSphereHelper;
31 import com.android.camera2.R;
34 * IndicatorIconController sets the visibility and icon state of
35 * on screen indicators.
37 * Indicators are only visible if they are in a non-default state. The
38 * visibility of an indicator is set when an indicator's setting changes.
40 public class IndicatorIconController
41 implements SettingsManager.OnSettingChangedListener,
42 ButtonManager.ButtonStatusListener {
44 private final static Log.Tag TAG = new Log.Tag("IndicatorIconCtrlr");
46 private ImageView mFlashIndicator;
47 private ImageView mHdrIndicator;
48 private ImageView mPanoIndicator;
49 private ImageView mCountdownTimerIndicator;
51 private ImageView mExposureIndicatorN2;
52 private ImageView mExposureIndicatorN1;
53 private ImageView mExposureIndicatorP1;
54 private ImageView mExposureIndicatorP2;
56 private TypedArray mFlashIndicatorPhotoIcons;
57 private TypedArray mFlashIndicatorVideoIcons;
58 private TypedArray mHdrPlusIndicatorIcons;
59 private TypedArray mHdrIndicatorIcons;
60 private TypedArray mPanoIndicatorIcons;
61 private TypedArray mCountdownTimerIndicatorIcons;
63 private AppController mController;
65 public IndicatorIconController(AppController controller, View root) {
66 mController = controller;
67 Context context = controller.getAndroidContext();
69 mFlashIndicator = (ImageView) root.findViewById(R.id.flash_indicator);
70 mFlashIndicatorPhotoIcons = context.getResources().obtainTypedArray(
71 R.array.camera_flashmode_indicator_icons);
72 mFlashIndicatorVideoIcons = context.getResources().obtainTypedArray(
73 R.array.video_flashmode_indicator_icons);
75 mHdrIndicator = (ImageView) root.findViewById(R.id.hdr_indicator);
76 mHdrPlusIndicatorIcons = context.getResources().obtainTypedArray(
77 R.array.pref_camera_hdr_plus_indicator_icons);
78 mHdrIndicatorIcons = context.getResources().obtainTypedArray(
79 R.array.pref_camera_hdr_indicator_icons);
81 int panoIndicatorArrayId = PhotoSphereHelper.getPanoramaOrientationIndicatorArrayId();
82 if (panoIndicatorArrayId > 0) {
83 mPanoIndicator = (ImageView) root.findViewById(R.id.pano_indicator);
85 context.getResources().obtainTypedArray(panoIndicatorArrayId);
88 mCountdownTimerIndicator = (ImageView) root.findViewById(R.id.countdown_timer_indicator);
89 mCountdownTimerIndicatorIcons = context.getResources().obtainTypedArray(
90 R.array.pref_camera_countdown_indicators);
92 mExposureIndicatorN2 = (ImageView) root.findViewById(R.id.exposure_n2_indicator);
93 mExposureIndicatorN1 = (ImageView) root.findViewById(R.id.exposure_n1_indicator);
94 mExposureIndicatorP1 = (ImageView) root.findViewById(R.id.exposure_p1_indicator);
95 mExposureIndicatorP2 = (ImageView) root.findViewById(R.id.exposure_p2_indicator);
99 public void onButtonVisibilityChanged(ButtonManager buttonManager, int buttonId) {
100 syncIndicatorWithButton(buttonId);
104 public void onButtonEnabledChanged(ButtonManager buttonManager, int buttonId) {
105 syncIndicatorWithButton(buttonId);
109 * Syncs a specific indicator's icon and visibility
110 * based on the enabled state and visibility of a button.
112 private void syncIndicatorWithButton(int buttonId) {
114 case ButtonManager.BUTTON_FLASH: {
115 syncFlashIndicator();
118 case ButtonManager.BUTTON_TORCH: {
119 syncFlashIndicator();
122 case ButtonManager.BUTTON_HDR_PLUS: {
126 case ButtonManager.BUTTON_HDR: {
130 case ButtonManager.BUTTON_EXPOSURE_COMPENSATION: {
131 syncExposureIndicator();
135 // Do nothing. The indicator doesn't care
136 // about button that don't correspond to indicators.
141 * Sets all indicators to the correct resource and visibility
142 * based on the current settings.
144 public void syncIndicators() {
145 syncFlashIndicator();
148 syncExposureIndicator();
149 syncCountdownTimerIndicator();
153 * If the new visibility is different from the current visibility
154 * on a view, change the visibility and call any registered
155 * {@link OnIndicatorVisibilityChangedListener}.
157 private static void changeVisibility(View view, int visibility) {
158 if (view.getVisibility() != visibility) {
159 view.setVisibility(visibility);
164 * Sync the icon and visibility of the flash indicator.
166 private void syncFlashIndicator() {
167 ButtonManager buttonManager = mController.getButtonManager();
168 // If flash isn't an enabled and visible option,
169 // do not show the indicator.
170 if (buttonManager.isEnabled(ButtonManager.BUTTON_FLASH)
171 && buttonManager.isVisible(ButtonManager.BUTTON_FLASH)) {
173 int modeIndex = mController.getCurrentModuleIndex();
174 if (modeIndex == mController.getAndroidContext().getResources()
175 .getInteger(R.integer.camera_mode_video)) {
176 setIndicatorState(mController.getCameraScope(),
177 Keys.KEY_VIDEOCAMERA_FLASH_MODE, mFlashIndicator,
178 mFlashIndicatorVideoIcons, false);
179 } else if (modeIndex == mController.getAndroidContext().getResources()
180 .getInteger(R.integer.camera_mode_gcam)) {
181 setIndicatorState(mController.getCameraScope(),
182 Keys.KEY_HDR_PLUS_FLASH_MODE, mFlashIndicator,
183 mFlashIndicatorPhotoIcons, false);
185 setIndicatorState(mController.getCameraScope(),
186 Keys.KEY_FLASH_MODE, mFlashIndicator,
187 mFlashIndicatorPhotoIcons, false);
190 changeVisibility(mFlashIndicator, View.GONE);
195 * Sync the icon and the visibility of the hdr/hdrplus indicator.
197 private void syncHdrIndicator() {
198 ButtonManager buttonManager = mController.getButtonManager();
199 // If hdr isn't an enabled and visible option,
200 // do not show the indicator.
201 if (buttonManager.isEnabled(ButtonManager.BUTTON_HDR_PLUS)
202 && buttonManager.isVisible(ButtonManager.BUTTON_HDR_PLUS)) {
203 setIndicatorState(SettingsManager.SCOPE_GLOBAL,
204 Keys.KEY_CAMERA_HDR_PLUS, mHdrIndicator,
205 mHdrPlusIndicatorIcons, false);
206 } else if (buttonManager.isEnabled(ButtonManager.BUTTON_HDR)
207 && buttonManager.isVisible(ButtonManager.BUTTON_HDR)) {
208 setIndicatorState(SettingsManager.SCOPE_GLOBAL,
209 Keys.KEY_CAMERA_HDR, mHdrIndicator,
210 mHdrIndicatorIcons, false);
212 changeVisibility(mHdrIndicator, View.GONE);
217 * Sync the icon and the visibility of the pano indicator.
219 private void syncPanoIndicator() {
220 if (mPanoIndicator == null) {
221 Log.w(TAG, "Trying to sync a pano indicator that is not initialized.");
225 ButtonManager buttonManager = mController.getButtonManager();
226 if (buttonManager.isPanoEnabled()) {
227 setIndicatorState(SettingsManager.SCOPE_GLOBAL,
228 Keys.KEY_CAMERA_PANO_ORIENTATION, mPanoIndicator,
229 mPanoIndicatorIcons, true);
231 changeVisibility(mPanoIndicator, View.GONE);
235 private void syncExposureIndicator() {
236 if (mExposureIndicatorN2 == null
237 || mExposureIndicatorN1 == null
238 || mExposureIndicatorP1 == null
239 || mExposureIndicatorP2 == null) {
240 Log.w(TAG, "Trying to sync exposure indicators that are not initialized.");
245 // Reset all exposure indicator icons.
246 changeVisibility(mExposureIndicatorN2, View.GONE);
247 changeVisibility(mExposureIndicatorN1, View.GONE);
248 changeVisibility(mExposureIndicatorP1, View.GONE);
249 changeVisibility(mExposureIndicatorP2, View.GONE);
251 ButtonManager buttonManager = mController.getButtonManager();
252 if (buttonManager.isEnabled(ButtonManager.BUTTON_EXPOSURE_COMPENSATION)
253 && buttonManager.isVisible(ButtonManager.BUTTON_EXPOSURE_COMPENSATION)) {
255 int compValue = mController.getSettingsManager().getInteger(
256 mController.getCameraScope(), Keys.KEY_EXPOSURE);
257 int comp = Math.round(compValue * buttonManager.getExposureCompensationStep());
259 // Turn on the appropriate indicator.
262 changeVisibility(mExposureIndicatorN2, View.VISIBLE);
265 changeVisibility(mExposureIndicatorN1, View.VISIBLE);
271 changeVisibility(mExposureIndicatorP1, View.VISIBLE);
274 changeVisibility(mExposureIndicatorP2, View.VISIBLE);
279 private void syncCountdownTimerIndicator() {
280 ButtonManager buttonManager = mController.getButtonManager();
282 if (buttonManager.isEnabled(ButtonManager.BUTTON_COUNTDOWN)
283 && buttonManager.isVisible(ButtonManager.BUTTON_COUNTDOWN)) {
284 setIndicatorState(SettingsManager.SCOPE_GLOBAL,
285 Keys.KEY_COUNTDOWN_DURATION, mCountdownTimerIndicator,
286 mCountdownTimerIndicatorIcons, false);
288 changeVisibility(mCountdownTimerIndicator, View.GONE);
293 * Sets the image resource and visibility of the indicator
294 * based on the indicator's corresponding setting state.
296 private void setIndicatorState(String scope, String key, ImageView imageView,
297 TypedArray iconArray, boolean showDefault) {
298 SettingsManager settingsManager = mController.getSettingsManager();
300 int valueIndex = settingsManager.getIndexOfCurrentValue(scope, key);
301 if (valueIndex < 0) {
302 // This can happen when the setting is camera dependent
303 // and the camera is not yet open. CameraAppUI.onChangeCamera()
304 // will call this again when the camera is open.
305 Log.w(TAG, "The setting for this indicator is not available.");
306 imageView.setVisibility(View.GONE);
309 Drawable drawable = iconArray.getDrawable(valueIndex);
310 if (drawable == null) {
311 throw new IllegalStateException("Indicator drawable is null.");
313 imageView.setImageDrawable(drawable);
315 // Set the indicator visible if not in default state.
316 boolean visibilityChanged = false;
317 if (!showDefault && settingsManager.isDefault(scope, key)) {
318 changeVisibility(imageView, View.GONE);
320 changeVisibility(imageView, View.VISIBLE);
325 public void onSettingChanged(SettingsManager settingsManager, String key) {
326 if (key.equals(Keys.KEY_FLASH_MODE)) {
327 syncFlashIndicator();
330 if (key.equals(Keys.KEY_VIDEOCAMERA_FLASH_MODE)) {
331 syncFlashIndicator();
334 if (key.equals(Keys.KEY_CAMERA_HDR_PLUS)) {
338 if (key.equals(Keys.KEY_CAMERA_HDR)) {
342 if (key.equals(Keys.KEY_CAMERA_PANO_ORIENTATION)) {
346 if (key.equals(Keys.KEY_EXPOSURE)) {
347 syncExposureIndicator();
350 if (key.equals(Keys.KEY_COUNTDOWN_DURATION)) {
351 syncCountdownTimerIndicator();