1 /* 2 * Copyright (C) 2011 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.ui; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 import android.widget.RelativeLayout; 23 24 import com.android.camera.CameraPreference.OnPreferenceChangedListener; 25 import com.android.camera.CameraSettings; 26 import com.android.camera.IconListPreference; 27 import com.android.camera.ListPreference; 28 import com.android.camera.PreferenceGroup; 29 import com.android.camera.R; 30 31 import java.util.ArrayList; 32 33 /** 34 * A view that contains camera setting indicators. 35 */ 36 public abstract class IndicatorControl extends RelativeLayout implements 37 IndicatorButton.Listener, OtherSettingsPopup.Listener, Rotatable { 38 @SuppressWarnings("unused") 39 private static final String TAG = "IndicatorControl"; 40 public static final int MODE_CAMERA = 0; 41 public static final int MODE_VIDEO = 1; 42 43 private OnPreferenceChangedListener mListener; 44 protected OnIndicatorEventListener mOnIndicatorEventListener; 45 protected CameraPicker mCameraPicker; 46 protected ZoomControl mZoomControl; 47 48 private PreferenceGroup mPreferenceGroup; 49 50 protected int mCurrentMode = MODE_CAMERA; 51 52 ArrayList<AbstractIndicatorButton> mIndicators = 53 new ArrayList<AbstractIndicatorButton>(); 54 setListener(OnPreferenceChangedListener listener)55 public void setListener(OnPreferenceChangedListener listener) { 56 mListener = listener; 57 if (mCameraPicker != null) mCameraPicker.setListener(listener); 58 } 59 IndicatorControl(Context context, AttributeSet attrs)60 public IndicatorControl(Context context, AttributeSet attrs) { 61 super(context, attrs); 62 } 63 64 @Override setOrientation(int orientation, boolean animation)65 public void setOrientation(int orientation, boolean animation) { 66 int count = getChildCount(); 67 for (int i = 0 ; i < count ; ++i) { 68 View view = getChildAt(i); 69 if (view instanceof Rotatable) { 70 ((Rotatable) view).setOrientation(orientation, animation); 71 } 72 } 73 } 74 setOnIndicatorEventListener(OnIndicatorEventListener listener)75 public void setOnIndicatorEventListener(OnIndicatorEventListener listener) { 76 mOnIndicatorEventListener = listener; 77 } 78 setPreferenceGroup(PreferenceGroup group)79 public void setPreferenceGroup(PreferenceGroup group) { 80 mPreferenceGroup = group; 81 // Preset the current mode from the title of preference group. 82 String title = group.getTitle(); 83 if (title.equals(getContext().getString( 84 R.string.pref_camcorder_settings_category))) { 85 mCurrentMode = MODE_VIDEO; 86 } 87 } 88 addControls(String[] keys, String[] otherSettingKeys)89 protected void addControls(String[] keys, String[] otherSettingKeys) { 90 if (keys != null) { 91 for (int i = 0; i < keys.length; i++) { 92 IconListPreference pref = 93 (IconListPreference) mPreferenceGroup.findPreference(keys[i]); 94 if (pref != null) { 95 addIndicator(getContext(), pref); 96 } 97 } 98 } 99 100 // Add other settings indicator. 101 if (otherSettingKeys != null) { 102 addOtherSettingIndicator(getContext(), 103 R.drawable.ic_menu_overflow, otherSettingKeys); 104 } 105 } 106 removeControls(int index, int count)107 protected void removeControls(int index, int count) { 108 for (int i = index; i < index + count; i++) { 109 AbstractIndicatorButton b = (AbstractIndicatorButton) getChildAt(i); 110 b.removePopupWindow(); 111 mIndicators.remove(b); 112 } 113 removeViews(index, count); 114 } 115 initializeCameraPicker()116 protected void initializeCameraPicker() { 117 // Ignore if camera picker has been initialized. 118 if (mCameraPicker != null) return; 119 120 ListPreference pref = mPreferenceGroup.findPreference( 121 CameraSettings.KEY_CAMERA_ID); 122 if (pref == null) return; 123 mCameraPicker = new CameraPicker(getContext()); 124 mCameraPicker.initialize(pref); 125 addView(mCameraPicker); 126 } 127 initializeZoomControl(boolean zoomSupported)128 protected void initializeZoomControl(boolean zoomSupported) { 129 if (zoomSupported) { 130 mZoomControl = (ZoomControl) findViewById(R.id.zoom_control); 131 mZoomControl.setVisibility(View.VISIBLE); 132 } else if (mZoomControl != null) { 133 mZoomControl.setVisibility(View.GONE); 134 mZoomControl = null; 135 } 136 } 137 138 @Override shouldDelayChildPressedState()139 public boolean shouldDelayChildPressedState() { 140 // Return false so the pressed feedback of the back/front camera switch 141 // can be showed right away. 142 return false; 143 } 144 addIndicator(Context context, IconListPreference pref)145 public IndicatorButton addIndicator(Context context, IconListPreference pref) { 146 IndicatorButton b = new IndicatorButton(context, pref); 147 b.setSettingChangedListener(this); 148 b.setContentDescription(pref.getTitle()); 149 addView(b); 150 mIndicators.add(b); 151 return b; 152 } 153 addOtherSettingIndicator(Context context, int resId, String[] keys)154 public OtherSettingIndicatorButton addOtherSettingIndicator(Context context, 155 int resId, String[] keys) { 156 OtherSettingIndicatorButton b = new OtherSettingIndicatorButton( 157 context, resId, mPreferenceGroup, keys); 158 b.setSettingChangedListener(this); 159 b.setContentDescription(getResources().getString( 160 R.string.pref_camera_settings_category)); 161 b.setId(R.id.other_setting_indicator); 162 addView(b); 163 mIndicators.add(b); 164 return b; 165 } 166 167 @Override onRestorePreferencesClicked()168 public void onRestorePreferencesClicked() { 169 if (mListener != null) { 170 mListener.onRestorePreferencesClicked(); 171 } 172 } 173 174 @Override onSettingChanged()175 public void onSettingChanged() { 176 if (mListener != null) { 177 mListener.onSharedPreferenceChanged(); 178 } 179 } 180 dismissSettingPopup()181 public boolean dismissSettingPopup() { 182 for (AbstractIndicatorButton v: mIndicators) { 183 if (v.dismissPopup()) { 184 invalidate(); 185 return true; 186 } 187 } 188 return false; 189 } 190 getActiveSettingPopup()191 public View getActiveSettingPopup() { 192 for (AbstractIndicatorButton v: mIndicators) { 193 View result = v.getPopupWindow(); 194 if (result != null) return result; 195 } 196 return null; 197 } 198 199 // Scene mode may override other camera settings (ex: flash mode). overrideSettings(final String ... keyvalues)200 public void overrideSettings(final String ... keyvalues) { 201 if (keyvalues.length % 2 != 0) { 202 throw new IllegalArgumentException(); 203 } 204 205 for (AbstractIndicatorButton b: mIndicators) { 206 b.overrideSettings(keyvalues); 207 } 208 } 209 reloadPreferences()210 public void reloadPreferences() { 211 mPreferenceGroup.reloadValue(); 212 for (AbstractIndicatorButton b: mIndicators) { 213 b.reloadPreference(); 214 } 215 } 216 217 @Override setEnabled(boolean enabled)218 public void setEnabled(boolean enabled) { 219 super.setEnabled(enabled); 220 final int count = getChildCount(); 221 for (int i = 0; i < count; i++) { 222 View v = getChildAt(i); 223 // Zoom buttons and shutter button are controlled by the activity. 224 if (v instanceof AbstractIndicatorButton) { 225 v.setEnabled(enabled); 226 // Show or hide the indicator buttons during recording. 227 if (mCurrentMode == MODE_VIDEO) { 228 v.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); 229 } 230 } 231 } 232 if (mCameraPicker != null) { 233 mCameraPicker.setEnabled(enabled); 234 if (mCurrentMode == MODE_VIDEO) { 235 mCameraPicker.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); 236 } 237 } 238 } 239 setupFilter(boolean enabled)240 public void setupFilter(boolean enabled) { 241 for (int i = 0, count = getChildCount(); i < count; i++) { 242 View v = getChildAt(i); 243 if (v instanceof TwoStateImageView) { 244 ((TwoStateImageView) v).enableFilter(enabled); 245 } 246 } 247 } 248 } 249