blob: e226ccddb106b15450a0c948cbc7cbdcc1715a09 [file] [log] [blame]
Erin Dahlgrene2a1f542014-01-08 14:34:35 -08001/*
2 * Copyright (C) 2014 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.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080022import android.view.View;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080023import android.widget.ImageView;
24
25import com.android.camera.ButtonManager;
26import com.android.camera.app.AppController;
Angus Kong2bca2102014-03-11 16:27:30 -070027import com.android.camera.debug.Log;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080028import com.android.camera.settings.SettingsManager;
Erin Dahlgrend706ea62014-01-21 16:16:06 -080029import com.android.camera.util.PhotoSphereHelper;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080030import com.android.camera2.R;
31
32/**
33 * IndicatorIconController sets the visibility and icon state of
34 * on screen indicators.
35 *
36 * Indicators are only visible if they are in a non-default state. The
37 * visibility of an indicator is set when an indicator's setting changes.
38 */
39public class IndicatorIconController
40 implements SettingsManager.OnSettingChangedListener,
41 ButtonManager.ButtonStatusListener {
42
Angus Kong2bca2102014-03-11 16:27:30 -070043 private final static Log.Tag TAG = new Log.Tag("IndicatorIconCtrlr");
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080044
45 private ImageView mFlashIndicator;
46 private ImageView mHdrIndicator;
Erin Dahlgren71a74292014-02-19 12:50:05 -080047 private ImageView mPanoIndicator;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080048
49 private TypedArray mFlashIndicatorPhotoIcons;
50 private TypedArray mFlashIndicatorVideoIcons;
Erin Dahlgrene7d745c2014-01-21 14:04:55 -080051 private TypedArray mHdrPlusIndicatorIcons;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080052 private TypedArray mHdrIndicatorIcons;
Erin Dahlgren71a74292014-02-19 12:50:05 -080053 private TypedArray mPanoIndicatorIcons;
Erin Dahlgren6e07fe22014-02-03 15:55:31 -080054
55 private OnIndicatorVisibilityChangedListener mListener;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080056
57 private AppController mController;
58
59 public IndicatorIconController(AppController controller, View root) {
60 mController = controller;
61 Context context = controller.getAndroidContext();
62
63 mFlashIndicator = (ImageView) root.findViewById(R.id.flash_indicator);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080064 mFlashIndicatorPhotoIcons = context.getResources().obtainTypedArray(
65 R.array.camera_flashmode_indicator_icons);
66 mFlashIndicatorVideoIcons = context.getResources().obtainTypedArray(
67 R.array.video_flashmode_indicator_icons);
Erin Dahlgren71a74292014-02-19 12:50:05 -080068
69 mHdrIndicator = (ImageView) root.findViewById(R.id.hdr_indicator);
Erin Dahlgrene7d745c2014-01-21 14:04:55 -080070 mHdrPlusIndicatorIcons = context.getResources().obtainTypedArray(
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080071 R.array.pref_camera_hdr_plus_indicator_icons);
Erin Dahlgrene7d745c2014-01-21 14:04:55 -080072 mHdrIndicatorIcons = context.getResources().obtainTypedArray(
73 R.array.pref_camera_hdr_indicator_icons);
Erin Dahlgren71a74292014-02-19 12:50:05 -080074
75 int panoIndicatorArrayId = PhotoSphereHelper.getPanoramaOrientationIndicatorArrayId();
76 if (panoIndicatorArrayId > 0) {
77 mPanoIndicator = (ImageView) root.findViewById(R.id.pano_indicator);
78 mPanoIndicatorIcons =
79 context.getResources().obtainTypedArray(panoIndicatorArrayId);
80 }
Erin Dahlgren6e07fe22014-02-03 15:55:31 -080081 }
82
83 /**
84 * A listener for responding to changes in indicator visibility.
85 */
86 public interface OnIndicatorVisibilityChangedListener {
87 public void onIndicatorVisibilityChanged(View indicator);
88 }
89
90 /**
91 * Set an {@link OnIndicatorVisibilityChangedListener} which will be
92 * called whenever an indicator changes visibility, caused by this
93 * controller.
94 */
95 public void setListener(OnIndicatorVisibilityChangedListener listener) {
96 mListener = listener;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -080097 }
98
99 @Override
100 public void onButtonVisibilityChanged(ButtonManager buttonManager, int buttonId) {
101 syncIndicatorWithButton(buttonId);
102 }
103
104 @Override
105 public void onButtonEnabledChanged(ButtonManager buttonManager, int buttonId) {
106 syncIndicatorWithButton(buttonId);
107 }
108
109 /**
110 * Syncs a specific indicator's icon and visibility
111 * based on the enabled state and visibility of a button.
112 */
113 private void syncIndicatorWithButton(int buttonId) {
114 switch (buttonId) {
Erin Dahlgrena9068092014-01-09 15:09:05 -0800115 case ButtonManager.BUTTON_FLASH: {
116 syncFlashIndicator();
117 break;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800118 }
Erin Dahlgrena9068092014-01-09 15:09:05 -0800119 case ButtonManager.BUTTON_TORCH: {
120 syncFlashIndicator();
121 break;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800122 }
Erin Dahlgrena9068092014-01-09 15:09:05 -0800123 case ButtonManager.BUTTON_HDRPLUS: {
124 syncHdrIndicator();
125 break;
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800126 }
Erin Dahlgrene7d745c2014-01-21 14:04:55 -0800127 case ButtonManager.BUTTON_HDR: {
128 syncHdrIndicator();
129 break;
130 }
Erin Dahlgren71a74292014-02-19 12:50:05 -0800131 case ButtonManager.BUTTON_PANO_ORIENTATION: {
132 syncPanoIndicator();
133 break;
134 }
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800135 default:
136 // Do nothing. The indicator doesn't care
137 // about button that don't correspond to indicators.
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800138 }
139 }
140
141 /**
142 * Sets all indicators to the correct resource and visibility
143 * based on the current settings.
144 */
Erin Dahlgrena9068092014-01-09 15:09:05 -0800145 public void syncIndicators() {
146 syncFlashIndicator();
147 syncHdrIndicator();
Erin Dahlgren71a74292014-02-19 12:50:05 -0800148 syncPanoIndicator();
149 }
150
151 /**
152 * If the new visibility is different from the current visibility
153 * on a view, change the visibility and call any registered
154 * {@link OnIndicatorVisibilityChangedListener}.
155 */
156 private void changeVisibility(View view, int visibility) {
157 if (view.getVisibility() != visibility) {
158 view.setVisibility(visibility);
159 if (mListener != null) {
160 mListener.onIndicatorVisibilityChanged(view);
161 }
162 }
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800163 }
164
165 /**
166 * Sync the icon and visibility of the flash indicator.
167 */
Erin Dahlgrena9068092014-01-09 15:09:05 -0800168 private void syncFlashIndicator() {
169 ButtonManager buttonManager = mController.getButtonManager();
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800170 // If flash isn't an enabled and visible option,
171 // do not show the indicator.
172 if (buttonManager.isEnabled(ButtonManager.BUTTON_FLASH)
173 && buttonManager.isVisible(ButtonManager.BUTTON_FLASH)) {
174
Erin Dahlgrena9068092014-01-09 15:09:05 -0800175 int modeIndex = mController.getCurrentModuleIndex();
176 if (modeIndex == mController.getAndroidContext().getResources()
Doris Liubd1b8f92014-01-03 17:59:51 -0800177 .getInteger(R.integer.camera_mode_video)) {
Erin Dahlgrena9068092014-01-09 15:09:05 -0800178 setIndicatorState(mController.getSettingsManager(),
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800179 SettingsManager.SETTING_VIDEOCAMERA_FLASH_MODE,
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800180 mFlashIndicator, mFlashIndicatorVideoIcons, false);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800181 } else {
Erin Dahlgrena9068092014-01-09 15:09:05 -0800182 setIndicatorState(mController.getSettingsManager(),
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800183 SettingsManager.SETTING_FLASH_MODE,
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800184 mFlashIndicator, mFlashIndicatorPhotoIcons, false);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800185 }
186 } else {
Erin Dahlgren71a74292014-02-19 12:50:05 -0800187 changeVisibility(mFlashIndicator, View.GONE);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800188 }
189 }
190
191 /**
Erin Dahlgrene7d745c2014-01-21 14:04:55 -0800192 * Sync the icon and the visibility of the hdr/hdrplus indicator.
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800193 */
Erin Dahlgrena9068092014-01-09 15:09:05 -0800194 private void syncHdrIndicator() {
195 ButtonManager buttonManager = mController.getButtonManager();
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800196 // If hdr isn't an enabled and visible option,
197 // do not show the indicator.
198 if (buttonManager.isEnabled(ButtonManager.BUTTON_HDRPLUS)
199 && buttonManager.isVisible(ButtonManager.BUTTON_HDRPLUS)) {
Erin Dahlgrena9068092014-01-09 15:09:05 -0800200 setIndicatorState(mController.getSettingsManager(),
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800201 SettingsManager.SETTING_CAMERA_HDR,
Erin Dahlgrene7d745c2014-01-21 14:04:55 -0800202 mHdrIndicator, mHdrPlusIndicatorIcons, false);
203 } else if (buttonManager.isEnabled(ButtonManager.BUTTON_HDR)
204 && buttonManager.isVisible(ButtonManager.BUTTON_HDR)) {
205 setIndicatorState(mController.getSettingsManager(),
206 SettingsManager.SETTING_CAMERA_HDR,
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800207 mHdrIndicator, mHdrIndicatorIcons, false);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800208 } else {
Erin Dahlgren71a74292014-02-19 12:50:05 -0800209 changeVisibility(mHdrIndicator, View.GONE);
210 }
211 }
212
213 /**
214 * Sync the icon and the visibility of the pano indicator.
215 */
216 private void syncPanoIndicator() {
217 if (mPanoIndicator == null) {
218 Log.w(TAG, "Trying to sync a pano indicator that is not initialized.");
219 return;
220 }
221
222 ButtonManager buttonManager = mController.getButtonManager();
223 if (buttonManager.isEnabled(ButtonManager.BUTTON_PANO_ORIENTATION)
224 && buttonManager.isVisible(ButtonManager.BUTTON_PANO_ORIENTATION)) {
225 setIndicatorState(mController.getSettingsManager(),
226 SettingsManager.SETTING_CAMERA_PANO_ORIENTATION,
227 mPanoIndicator, mPanoIndicatorIcons, true);
228 } else {
229 changeVisibility(mPanoIndicator, View.GONE);
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800230 }
231 }
232
233 /**
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800234 * Sets the image resource and visibility of the indicator
235 * based on the indicator's corresponding setting state.
236 */
237 private void setIndicatorState(SettingsManager settingsManager, int id,
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800238 ImageView imageView, TypedArray iconArray, boolean showDefault) {
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800239
240 // Set the correct image src.
241 String value = settingsManager.get(id);
242 int valueIndex = settingsManager.getStringValueIndex(id);
243 if (valueIndex < 0) {
244 // This can happen when the setting is camera dependent
245 // and the camera is not yet open. CameraAppUI.onChangeCamera()
246 // will call this again when the camera is open.
247 Log.w(TAG, "The setting for this indicator is not available.");
248 imageView.setVisibility(View.GONE);
249 return;
250 }
251 Drawable drawable = iconArray.getDrawable(valueIndex);
252 if (drawable == null) {
253 throw new IllegalStateException("Indicator drawable is null.");
254 }
255 imageView.setImageDrawable(drawable);
256
257 // Set the indicator visible if not in default state.
Erin Dahlgren6e07fe22014-02-03 15:55:31 -0800258 boolean visibilityChanged = false;
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800259 if (!showDefault && settingsManager.isDefault(id)) {
Erin Dahlgren71a74292014-02-19 12:50:05 -0800260 changeVisibility(imageView, View.GONE);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800261 } else {
Erin Dahlgren71a74292014-02-19 12:50:05 -0800262 changeVisibility(imageView, View.VISIBLE);
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800263 }
264 }
265
266 @Override
267 public void onSettingChanged(SettingsManager settingsManager, int id) {
268 switch (id) {
269 case SettingsManager.SETTING_FLASH_MODE: {
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800270 syncFlashIndicator();
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800271 break;
272 }
273 case SettingsManager.SETTING_VIDEOCAMERA_FLASH_MODE: {
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800274 syncFlashIndicator();
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800275 break;
276 }
277 case SettingsManager.SETTING_CAMERA_HDR: {
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800278 syncHdrIndicator();
279 break;
280 }
Erin Dahlgren71a74292014-02-19 12:50:05 -0800281 case SettingsManager.SETTING_CAMERA_PANO_ORIENTATION: {
282 syncPanoIndicator();
283 break;
284 }
Erin Dahlgrene2a1f542014-01-08 14:34:35 -0800285 default: {
286 // Do nothing.
287 }
288 }
289 }
290}