1 /* 2 * Copyright (C) 2012 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; 18 19 import android.app.Activity; 20 import android.graphics.drawable.Drawable; 21 import android.util.Log; 22 23 import com.android.camera.CameraPreference.OnPreferenceChangedListener; 24 import com.android.camera.drawable.TextDrawable; 25 import com.android.camera.ui.PieItem; 26 import com.android.camera.ui.PieItem.OnClickListener; 27 import com.android.camera.ui.PieRenderer; 28 29 import java.util.ArrayList; 30 import java.util.HashMap; 31 import java.util.List; 32 import java.util.Map; 33 34 public class PieController { 35 36 private static String TAG = "CAM_piecontrol"; 37 38 protected static final int MODE_PHOTO = 0; 39 protected static final int MODE_VIDEO = 1; 40 41 protected static float CENTER = (float) Math.PI / 2; 42 protected static final float SWEEP = 0.06f; 43 44 protected Activity mActivity; 45 protected PreferenceGroup mPreferenceGroup; 46 protected OnPreferenceChangedListener mListener; 47 protected PieRenderer mRenderer; 48 private List<IconListPreference> mPreferences; 49 private Map<IconListPreference, PieItem> mPreferenceMap; 50 private Map<IconListPreference, String> mOverrides; 51 52 public void setListener(OnPreferenceChangedListener listener) { 53 mListener = listener; 54 } 55 56 public PieController(Activity activity, PieRenderer pie) { 57 mActivity = activity; 58 mRenderer = pie; 59 mPreferences = new ArrayList<IconListPreference>(); 60 mPreferenceMap = new HashMap<IconListPreference, PieItem>(); 61 mOverrides = new HashMap<IconListPreference, String>(); 62 } 63 64 public void initialize(PreferenceGroup group) { 65 mRenderer.clearItems(); 66 mPreferenceMap.clear(); 67 setPreferenceGroup(group); 68 } 69 70 public void onSettingChanged(ListPreference pref) { 71 if (mListener != null) { 72 mListener.onSharedPreferenceChanged(); 73 } 74 } 75 76 protected void setCameraId(int cameraId) { 77 ListPreference pref = mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID); 78 pref.setValue("" + cameraId); 79 } 80 81 protected PieItem makeItem(int resId) { 82 // We need a mutable version as we change the alpha 83 Drawable d = mActivity.getResources().getDrawable(resId).mutate(); 84 return new PieItem(d, 0); 85 } 86 87 protected PieItem makeItem(CharSequence value) { 88 TextDrawable drawable = new TextDrawable(mActivity.getResources(), value); 89 return new PieItem(drawable, 0); 90 } 91 92 public PieItem makeItem(String prefKey) { 93 final IconListPreference pref = 94 (IconListPreference) mPreferenceGroup.findPreference(prefKey); 95 if (pref == null) return null; 96 int[] iconIds = pref.getLargeIconIds(); 97 int resid = -1; 98 if (!pref.getUseSingleIcon() && iconIds != null) { 99 // Each entry has a corresponding icon. 100 int index = pref.findIndexOfValue(pref.getValue()); 101 resid = iconIds[index]; 102 } else { 103 // The preference only has a single icon to represent it. 104 resid = pref.getSingleIcon(); 105 } 106 PieItem item = makeItem(resid); 107 item.setLabel(pref.getTitle().toUpperCase()); 108 mPreferences.add(pref); 109 mPreferenceMap.put(pref, item); 110 int nOfEntries = pref.getEntries().length; 111 if (nOfEntries > 1) { 112 for (int i = 0; i < nOfEntries; i++) { 113 PieItem inner = null; 114 if (iconIds != null) { 115 inner = makeItem(iconIds[i]); 116 } else { 117 inner = makeItem(pref.getEntries()[i]); 118 } 119 inner.setLabel(pref.getLabels()[i]); 120 item.addItem(inner); 121 final int index = i; 122 inner.setOnClickListener(new OnClickListener() { 123 @Override 124 public void onClick(PieItem item) { 125 pref.setValueIndex(index); 126 reloadPreference(pref); 127 onSettingChanged(pref); 128 } 129 }); 130 } 131 } 132 return item; 133 } 134 135 public PieItem makeSwitchItem(final String prefKey, boolean addListener) { 136 final IconListPreference pref = 137 (IconListPreference) mPreferenceGroup.findPreference(prefKey); 138 if (pref == null) return null; 139 int[] iconIds = pref.getLargeIconIds(); 140 int resid = -1; 141 int index = pref.findIndexOfValue(pref.getValue()); 142 if (!pref.getUseSingleIcon() && iconIds != null) { 143 // Each entry has a corresponding icon. 144 resid = iconIds[index]; 145 } else { 146 // The preference only has a single icon to represent it. 147 resid = pref.getSingleIcon(); 148 } 149 PieItem item = makeItem(resid); 150 item.setLabel(pref.getLabels()[index]); 151 item.setImageResource(mActivity, resid); 152 mPreferences.add(pref); 153 mPreferenceMap.put(pref, item); 154 if (addListener) { 155 final PieItem fitem = item; 156 item.setOnClickListener(new OnClickListener() { 157 @Override 158 public void onClick(PieItem item) { 159 if (!item.isEnabled()) { 160 return; 161 } 162 IconListPreference pref = (IconListPreference) mPreferenceGroup 163 .findPreference(prefKey); 164 int index = pref.findIndexOfValue(pref.getValue()); 165 CharSequence[] values = pref.getEntryValues(); 166 index = (index + 1) % values.length; 167 pref.setValueIndex(index); 168 fitem.setLabel(pref.getLabels()[index]); 169 fitem.setImageResource(mActivity, 170 ((IconListPreference) pref).getLargeIconIds()[index]); 171 reloadPreference(pref); 172 onSettingChanged(pref); 173 } 174 }); 175 } 176 return item; 177 } 178 179 180 public PieItem makeDialItem(ListPreference pref, int iconId, float center, float sweep) { 181 PieItem item = makeItem(iconId); 182 return item; 183 } 184 185 public void addItem(String prefKey) { 186 PieItem item = makeItem(prefKey); 187 mRenderer.addItem(item); 188 } 189 190 public void updateItem(PieItem item, String prefKey) { 191 IconListPreference pref = (IconListPreference) mPreferenceGroup 192 .findPreference(prefKey); 193 if (pref != null) { 194 int index = pref.findIndexOfValue(pref.getValue()); 195 item.setLabel(pref.getLabels()[index]); 196 item.setImageResource(mActivity, 197 ((IconListPreference) pref).getLargeIconIds()[index]); 198 } 199 } 200 201 public void setPreferenceGroup(PreferenceGroup group) { 202 mPreferenceGroup = group; 203 } 204 205 public void reloadPreferences() { 206 mPreferenceGroup.reloadValue(); 207 for (IconListPreference pref : mPreferenceMap.keySet()) { 208 reloadPreference(pref); 209 } 210 } 211 212 private void reloadPreference(IconListPreference pref) { 213 if (pref.getUseSingleIcon()) return; 214 PieItem item = mPreferenceMap.get(pref); 215 String overrideValue = mOverrides.get(pref); 216 int[] iconIds = pref.getLargeIconIds(); 217 if (iconIds != null) { 218 // Each entry has a corresponding icon. 219 int index; 220 if (overrideValue == null) { 221 index = pref.findIndexOfValue(pref.getValue()); 222 } else { 223 index = pref.findIndexOfValue(overrideValue); 224 if (index == -1) { 225 // Avoid the crash if camera driver has bugs. 226 Log.e(TAG, "Fail to find override value=" + overrideValue); 227 pref.print(); 228 return; 229 } 230 } 231 item.setImageResource(mActivity, iconIds[index]); 232 } else { 233 // The preference only has a single icon to represent it. 234 item.setImageResource(mActivity, pref.getSingleIcon()); 235 } 236 } 237 238 // Scene mode may override other camera settings (ex: flash mode). 239 public void overrideSettings(final String ... keyvalues) { 240 if (keyvalues.length % 2 != 0) { 241 throw new IllegalArgumentException(); 242 } 243 for (IconListPreference pref : mPreferenceMap.keySet()) { 244 override(pref, keyvalues); 245 } 246 } 247 248 private void override(IconListPreference pref, final String ... keyvalues) { 249 mOverrides.remove(pref); 250 for (int i = 0; i < keyvalues.length; i += 2) { 251 String key = keyvalues[i]; 252 String value = keyvalues[i + 1]; 253 if (key.equals(pref.getKey())) { 254 mOverrides.put(pref, value); 255 PieItem item = mPreferenceMap.get(pref); 256 item.setEnabled(value == null); 257 break; 258 } 259 } 260 reloadPreference(pref); 261 } 262 } 263