ButtonManager.java revision 18e2ef6d2b2d36abcaa7378a0f7df792ecf8ed81
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.app.Activity; 20import android.content.Context; 21import android.util.Log; 22import android.util.SparseArray; 23import android.view.View; 24import android.widget.ImageButton; 25 26import com.android.camera.settings.SettingsManager; 27import com.android.camera2.R; 28 29/** 30 * A class for generating pre-initialized 31 * {@link #android.widget.ImageButton}s. 32 */ 33public class ButtonManager { 34 35 /** 36 * Get a new manager instance. Modules should not share 37 * a button manager instance because modules reference 38 * different button instances. 39 */ 40 public static ButtonManager getInstance(CameraActivity activity) { 41 return new ButtonManager(activity); 42 } 43 44 public static final int BUTTON_FLASH = 0; 45 public static final int BUTTON_CAMERA = 1; 46 public static final int BUTTON_HDRPLUS = 2; 47 48 /** A private store of uninitialized buttons. */ 49 private static SparseArray<MultiToggleImageButton> mButtonCache; 50 /** A reference to the application's settings manager. */ 51 private SettingsManager mSettingsManager; 52 53 private ButtonManager(CameraActivity activity) { 54 mSettingsManager = activity.getSettingsManager(); 55 mButtonCache = getButtonReferences(activity); 56 } 57 58 /** Store uninitialized references to buttons with known keys. */ 59 private static SparseArray<MultiToggleImageButton> getButtonReferences(Activity activity) { 60 SparseArray<MultiToggleImageButton> cache = new SparseArray<MultiToggleImageButton>(); 61 62 MultiToggleImageButton flashToggle 63 = (MultiToggleImageButton) activity.findViewById(R.id.flash_toggle_button); 64 cache.put(BUTTON_FLASH, flashToggle); 65 66 MultiToggleImageButton cameraToggle 67 = (MultiToggleImageButton) activity.findViewById(R.id.camera_toggle_button); 68 cache.put(BUTTON_CAMERA, cameraToggle); 69 70 MultiToggleImageButton hdrPlusToggle 71 = (MultiToggleImageButton) activity.findViewById(R.id.hdr_plus_toggle_button); 72 cache.put(BUTTON_HDRPLUS, hdrPlusToggle); 73 return cache; 74 } 75 76 /** 77 * A callback executed in the state listener of a button. 78 * Used by a module to set specific behavior when button's 79 * state changes. 80 */ 81 public interface ButtonCallback { 82 public void onStateChanged(int state); 83 } 84 85 86 /** 87 * Initialize a known button by id, with a state change callback and 88 * a resource id that points to an array of drawables. 89 */ 90 public ImageButton getButton(int id, ButtonCallback cb, int resIdImages) { 91 switch (id) { 92 case BUTTON_FLASH: 93 return getFlashButton(cb, resIdImages); 94 case BUTTON_CAMERA: 95 return getCameraButton(cb, resIdImages); 96 case BUTTON_HDRPLUS: 97 return getHdrPlusButton(cb, resIdImages); 98 default: 99 throw new IllegalArgumentException("Button not known by id=" + id); 100 } 101 } 102 103 /** 104 * Initialize a flash button. 105 */ 106 private ImageButton getFlashButton(final ButtonCallback cb, int resIdImages) { 107 MultiToggleImageButton flashToggle 108 = (MultiToggleImageButton) mButtonCache.get(BUTTON_FLASH); 109 110 if (flashToggle == null) { 111 throw new IllegalStateException("Flash button could not be initialized."); 112 } 113 114 if (resIdImages > 0) { 115 flashToggle.overrideImageIds(resIdImages); 116 } 117 int index = mSettingsManager.getStringValueIndex(SettingsManager.SETTING_FLASH_MODE); 118 flashToggle.setState(index, false); 119 flashToggle.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() { 120 @Override 121 public void stateChanged(View view, int state) { 122 mSettingsManager.setStringValueIndex(SettingsManager.SETTING_FLASH_MODE, state); 123 if (cb != null) { 124 cb.onStateChanged(state); 125 } 126 } 127 }); 128 129 return flashToggle; 130 } 131 132 /** 133 * Initialize a camera button. 134 */ 135 private ImageButton getCameraButton(final ButtonCallback cb, int resIdImages) { 136 MultiToggleImageButton cameraToggle 137 = (MultiToggleImageButton) mButtonCache.get(BUTTON_CAMERA); 138 final MultiToggleImageButton flashToggle 139 = (MultiToggleImageButton) mButtonCache.get(BUTTON_FLASH); 140 141 if (cameraToggle == null) { 142 throw new IllegalStateException("Camera button could not be initialized."); 143 } 144 145 if (resIdImages > 0) { 146 cameraToggle.overrideImageIds(resIdImages); 147 } 148 int index = mSettingsManager.getStringValueIndex(SettingsManager.SETTING_CAMERA_ID); 149 cameraToggle.setState(index, false); 150 cameraToggle.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() { 151 @Override 152 public void stateChanged(View view, int state) { 153 mSettingsManager.setStringValueIndex(SettingsManager.SETTING_CAMERA_ID, state); 154 int cameraId = Integer.parseInt(mSettingsManager.get( 155 SettingsManager.SETTING_CAMERA_ID)); 156 if (cb != null) { 157 cb.onStateChanged(cameraId); 158 } 159 if (flashToggle != null) { 160 flashToggle.setVisibility(state == 0 ? View.VISIBLE : View.INVISIBLE); 161 } 162 } 163 }); 164 165 if (flashToggle != null) { 166 flashToggle.setVisibility(index == 0 ? View.VISIBLE : View.INVISIBLE); 167 } 168 169 return cameraToggle; 170 } 171 172 /** 173 * Initialize an hdr plus button. 174 */ 175 private ImageButton getHdrPlusButton(final ButtonCallback cb, int resIdImages) { 176 MultiToggleImageButton hdrPlusToggle 177 = (MultiToggleImageButton) mButtonCache.get(BUTTON_HDRPLUS); 178 179 if (hdrPlusToggle == null) { 180 throw new IllegalStateException("Hdr plus button could not be initialized."); 181 } 182 183 if (resIdImages > 0) { 184 hdrPlusToggle.overrideImageIds(resIdImages); 185 } 186 int index = mSettingsManager.getStringValueIndex(SettingsManager.SETTING_CAMERA_HDR); 187 hdrPlusToggle.setState(index, false); 188 hdrPlusToggle.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() { 189 @Override 190 public void stateChanged(View view, int state) { 191 mSettingsManager.setStringValueIndex(SettingsManager.SETTING_CAMERA_HDR, state); 192 if (cb != null) { 193 cb.onStateChanged(state); 194 } 195 } 196 }); 197 198 return hdrPlusToggle; 199 } 200}