Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.camera.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.graphics.drawable.Drawable; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 22 | import android.view.View; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 23 | import android.widget.ImageView; |
| 24 | |
| 25 | import com.android.camera.ButtonManager; |
| 26 | import com.android.camera.app.AppController; |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 27 | import com.android.camera.debug.Log; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 28 | import com.android.camera.settings.SettingsManager; |
Erin Dahlgren | d706ea6 | 2014-01-21 16:16:06 -0800 | [diff] [blame] | 29 | import com.android.camera.util.PhotoSphereHelper; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 30 | import 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 | */ |
| 39 | public class IndicatorIconController |
| 40 | implements SettingsManager.OnSettingChangedListener, |
| 41 | ButtonManager.ButtonStatusListener { |
| 42 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 43 | private final static Log.Tag TAG = new Log.Tag("IndicatorIconCtrlr"); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 44 | |
| 45 | private ImageView mFlashIndicator; |
| 46 | private ImageView mHdrIndicator; |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 47 | private ImageView mPanoIndicator; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 48 | |
| 49 | private TypedArray mFlashIndicatorPhotoIcons; |
| 50 | private TypedArray mFlashIndicatorVideoIcons; |
Erin Dahlgren | e7d745c | 2014-01-21 14:04:55 -0800 | [diff] [blame] | 51 | private TypedArray mHdrPlusIndicatorIcons; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 52 | private TypedArray mHdrIndicatorIcons; |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 53 | private TypedArray mPanoIndicatorIcons; |
Erin Dahlgren | 6e07fe2 | 2014-02-03 15:55:31 -0800 | [diff] [blame] | 54 | |
| 55 | private OnIndicatorVisibilityChangedListener mListener; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 56 | |
| 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 Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 64 | mFlashIndicatorPhotoIcons = context.getResources().obtainTypedArray( |
| 65 | R.array.camera_flashmode_indicator_icons); |
| 66 | mFlashIndicatorVideoIcons = context.getResources().obtainTypedArray( |
| 67 | R.array.video_flashmode_indicator_icons); |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 68 | |
| 69 | mHdrIndicator = (ImageView) root.findViewById(R.id.hdr_indicator); |
Erin Dahlgren | e7d745c | 2014-01-21 14:04:55 -0800 | [diff] [blame] | 70 | mHdrPlusIndicatorIcons = context.getResources().obtainTypedArray( |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 71 | R.array.pref_camera_hdr_plus_indicator_icons); |
Erin Dahlgren | e7d745c | 2014-01-21 14:04:55 -0800 | [diff] [blame] | 72 | mHdrIndicatorIcons = context.getResources().obtainTypedArray( |
| 73 | R.array.pref_camera_hdr_indicator_icons); |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 74 | |
| 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 Dahlgren | 6e07fe2 | 2014-02-03 15:55:31 -0800 | [diff] [blame] | 81 | } |
| 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 Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 97 | } |
| 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 Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 115 | case ButtonManager.BUTTON_FLASH: { |
| 116 | syncFlashIndicator(); |
| 117 | break; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 118 | } |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 119 | case ButtonManager.BUTTON_TORCH: { |
| 120 | syncFlashIndicator(); |
| 121 | break; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 122 | } |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 123 | case ButtonManager.BUTTON_HDRPLUS: { |
| 124 | syncHdrIndicator(); |
| 125 | break; |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 126 | } |
Erin Dahlgren | e7d745c | 2014-01-21 14:04:55 -0800 | [diff] [blame] | 127 | case ButtonManager.BUTTON_HDR: { |
| 128 | syncHdrIndicator(); |
| 129 | break; |
| 130 | } |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 131 | case ButtonManager.BUTTON_PANO_ORIENTATION: { |
| 132 | syncPanoIndicator(); |
| 133 | break; |
| 134 | } |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 135 | default: |
| 136 | // Do nothing. The indicator doesn't care |
| 137 | // about button that don't correspond to indicators. |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets all indicators to the correct resource and visibility |
| 143 | * based on the current settings. |
| 144 | */ |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 145 | public void syncIndicators() { |
| 146 | syncFlashIndicator(); |
| 147 | syncHdrIndicator(); |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 148 | 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 Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sync the icon and visibility of the flash indicator. |
| 167 | */ |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 168 | private void syncFlashIndicator() { |
| 169 | ButtonManager buttonManager = mController.getButtonManager(); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 170 | // 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 Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 175 | int modeIndex = mController.getCurrentModuleIndex(); |
| 176 | if (modeIndex == mController.getAndroidContext().getResources() |
Doris Liu | bd1b8f9 | 2014-01-03 17:59:51 -0800 | [diff] [blame] | 177 | .getInteger(R.integer.camera_mode_video)) { |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 178 | setIndicatorState(mController.getSettingsManager(), |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 179 | SettingsManager.SETTING_VIDEOCAMERA_FLASH_MODE, |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 180 | mFlashIndicator, mFlashIndicatorVideoIcons, false); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 181 | } else { |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 182 | setIndicatorState(mController.getSettingsManager(), |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 183 | SettingsManager.SETTING_FLASH_MODE, |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 184 | mFlashIndicator, mFlashIndicatorPhotoIcons, false); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 185 | } |
| 186 | } else { |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 187 | changeVisibility(mFlashIndicator, View.GONE); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
Erin Dahlgren | e7d745c | 2014-01-21 14:04:55 -0800 | [diff] [blame] | 192 | * Sync the icon and the visibility of the hdr/hdrplus indicator. |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 193 | */ |
Erin Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 194 | private void syncHdrIndicator() { |
| 195 | ButtonManager buttonManager = mController.getButtonManager(); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 196 | // 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 Dahlgren | a906809 | 2014-01-09 15:09:05 -0800 | [diff] [blame] | 200 | setIndicatorState(mController.getSettingsManager(), |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 201 | SettingsManager.SETTING_CAMERA_HDR, |
Erin Dahlgren | e7d745c | 2014-01-21 14:04:55 -0800 | [diff] [blame] | 202 | 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 Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 207 | mHdrIndicator, mHdrIndicatorIcons, false); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 208 | } else { |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 209 | 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 Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 234 | * 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 Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 238 | ImageView imageView, TypedArray iconArray, boolean showDefault) { |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 239 | |
| 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 Dahlgren | 6e07fe2 | 2014-02-03 15:55:31 -0800 | [diff] [blame] | 258 | boolean visibilityChanged = false; |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 259 | if (!showDefault && settingsManager.isDefault(id)) { |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 260 | changeVisibility(imageView, View.GONE); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 261 | } else { |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 262 | changeVisibility(imageView, View.VISIBLE); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onSettingChanged(SettingsManager settingsManager, int id) { |
| 268 | switch (id) { |
| 269 | case SettingsManager.SETTING_FLASH_MODE: { |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 270 | syncFlashIndicator(); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | case SettingsManager.SETTING_VIDEOCAMERA_FLASH_MODE: { |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 274 | syncFlashIndicator(); |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | case SettingsManager.SETTING_CAMERA_HDR: { |
Erin Dahlgren | a1fab41 | 2014-01-21 09:31:11 -0800 | [diff] [blame] | 278 | syncHdrIndicator(); |
| 279 | break; |
| 280 | } |
Erin Dahlgren | 71a7429 | 2014-02-19 12:50:05 -0800 | [diff] [blame] | 281 | case SettingsManager.SETTING_CAMERA_PANO_ORIENTATION: { |
| 282 | syncPanoIndicator(); |
| 283 | break; |
| 284 | } |
Erin Dahlgren | e2a1f54 | 2014-01-08 14:34:35 -0800 | [diff] [blame] | 285 | default: { |
| 286 | // Do nothing. |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |