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 com.android.camera.CameraPreference.OnPreferenceChangedListener; 20 import com.android.camera.IconListPreference; 21 import com.android.camera.ListPreference; 22 import com.android.camera.PreferenceGroup; 23 import com.android.camera.CameraSettings; 24 import com.android.camera.R; 25 26 import android.content.Context; 27 import android.util.AttributeSet; 28 import android.view.View; 29 import android.widget.ImageView; 30 import android.widget.RelativeLayout; 31 32 import java.util.ArrayList; 33 34 /** 35 * A view that contains camera setting indicators. 36 */ 37 public abstract class IndicatorControl extends RelativeLayout implements 38 IndicatorButton.Listener, OtherSettingsPopup.Listener { 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 47 private PreferenceGroup mPreferenceGroup; 48 private int mDegree = 0; 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 setDegree(int degree)64 public void setDegree(int degree) { 65 mDegree = degree; 66 int count = getChildCount(); 67 for (int i = 0 ; i < count ; ++i) { 68 View view = getChildAt(i); 69 if (view instanceof RotateImageView) { 70 ((RotateImageView) view).setDegree(degree); 71 } else if (view instanceof ZoomControl) { 72 ((ZoomControl) view).setDegree(degree); 73 } 74 } 75 } 76 setOnIndicatorEventListener(OnIndicatorEventListener listener)77 public void setOnIndicatorEventListener(OnIndicatorEventListener listener) { 78 mOnIndicatorEventListener = listener; 79 } 80 setPreferenceGroup(PreferenceGroup group)81 public void setPreferenceGroup(PreferenceGroup group) { 82 mPreferenceGroup = group; 83 // Preset the current mode from the title of preference group. 84 String title = group.getTitle(); 85 if (title.equals(getContext().getString( 86 R.string.pref_camcorder_settings_category))) { 87 mCurrentMode = MODE_VIDEO; 88 } 89 } 90 addControls(String[] keys, String[] otherSettingKeys)91 protected void addControls(String[] keys, String[] otherSettingKeys) { 92 if (keys != null) { 93 for (int i = 0; i < keys.length; i++) { 94 IconListPreference pref = 95 (IconListPreference) mPreferenceGroup.findPreference(keys[i]); 96 if (pref != null) { 97 addIndicator(getContext(), pref); 98 } 99 } 100 } 101 102 // Add other settings indicator. 103 if (otherSettingKeys != null) { 104 addOtherSettingIndicator(getContext(), 105 R.drawable.ic_menu_overflow, otherSettingKeys); 106 } 107 } 108 initializeCameraPicker()109 protected void initializeCameraPicker() { 110 ListPreference pref = mPreferenceGroup.findPreference( 111 CameraSettings.KEY_CAMERA_ID); 112 if (pref == null) return; 113 mCameraPicker = new CameraPicker(getContext()); 114 mCameraPicker.initialize(pref); 115 addView(mCameraPicker); 116 } 117 118 @Override shouldDelayChildPressedState()119 public boolean shouldDelayChildPressedState() { 120 // Return false so the pressed feedback of the back/front camera switch 121 // can be showed right away. 122 return false; 123 } 124 addIndicator(Context context, IconListPreference pref)125 public IndicatorButton addIndicator(Context context, IconListPreference pref) { 126 IndicatorButton b = new IndicatorButton(context, pref); 127 b.setSettingChangedListener(this); 128 b.setContentDescription(pref.getTitle()); 129 addView(b); 130 mIndicators.add(b); 131 return b; 132 } 133 addOtherSettingIndicator(Context context, int resId, String[] keys)134 public OtherSettingIndicatorButton addOtherSettingIndicator(Context context, 135 int resId, String[] keys) { 136 OtherSettingIndicatorButton b = new OtherSettingIndicatorButton( 137 context, resId, mPreferenceGroup, keys); 138 b.setSettingChangedListener(this); 139 b.setContentDescription(getResources().getString( 140 R.string.pref_camera_settings_category)); 141 addView(b); 142 mIndicators.add(b); 143 return b; 144 } 145 146 @Override onRestorePreferencesClicked()147 public void onRestorePreferencesClicked() { 148 if (mListener != null) { 149 mListener.onRestorePreferencesClicked(); 150 } 151 } 152 153 @Override onSettingChanged()154 public void onSettingChanged() { 155 if (mListener != null) { 156 mListener.onSharedPreferenceChanged(); 157 } 158 } 159 dismissSettingPopup()160 public boolean dismissSettingPopup() { 161 for (AbstractIndicatorButton v: mIndicators) { 162 if (v.dismissPopup()) { 163 invalidate(); 164 return true; 165 } 166 } 167 return false; 168 } 169 getActiveSettingPopup()170 public View getActiveSettingPopup() { 171 for (AbstractIndicatorButton v: mIndicators) { 172 View result = v.getPopupWindow(); 173 if (result != null) return result; 174 } 175 return null; 176 } 177 178 // Scene mode may override other camera settings (ex: flash mode). overrideSettings(final String ... keyvalues)179 public void overrideSettings(final String ... keyvalues) { 180 if (keyvalues.length % 2 != 0) { 181 throw new IllegalArgumentException(); 182 } 183 184 for (AbstractIndicatorButton b: mIndicators) { 185 b.overrideSettings(keyvalues); 186 } 187 } 188 reloadPreferences()189 public void reloadPreferences() { 190 mPreferenceGroup.reloadValue(); 191 for (AbstractIndicatorButton b: mIndicators) { 192 b.reloadPreference(); 193 } 194 } 195 196 @Override setEnabled(boolean enabled)197 public void setEnabled(boolean enabled) { 198 super.setEnabled(enabled); 199 final int count = getChildCount(); 200 for (int i = 0; i < count; i++) { 201 View v = getChildAt(i); 202 // Zoom buttons and shutter button are controlled by the activity. 203 if (v instanceof AbstractIndicatorButton) { 204 v.setEnabled(enabled); 205 // Show or hide the indicator buttons during recording. 206 if (mCurrentMode == MODE_VIDEO) { 207 v.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); 208 } 209 } 210 } 211 if (mCameraPicker != null) { 212 mCameraPicker.setEnabled(enabled); 213 if (mCurrentMode == MODE_VIDEO) { 214 mCameraPicker.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE); 215 } 216 } 217 } 218 } 219