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.graphics.drawable.Drawable; 20 import android.util.Log; 21 22 import com.android.camera.CameraPreference.OnPreferenceChangedListener; 23 import com.android.camera.drawable.TextDrawable; 24 import com.android.camera.ui.PieItem; 25 import com.android.camera.ui.PieItem.OnClickListener; 26 import com.android.camera.ui.PieRenderer; 27 28 import java.util.ArrayList; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 33 public class PieController { 34 35 private static String TAG = "CAM_piecontrol"; 36 37 protected static final int MODE_PHOTO = 0; 38 protected static final int MODE_VIDEO = 1; 39 40 protected CameraActivity mActivity; 41 protected PreferenceGroup mPreferenceGroup; 42 protected OnPreferenceChangedListener mListener; 43 protected PieRenderer mRenderer; 44 private List<IconListPreference> mPreferences; 45 private Map<IconListPreference, PieItem> mPreferenceMap; 46 private Map<IconListPreference, String> mOverrides; 47 setListener(OnPreferenceChangedListener listener)48 public void setListener(OnPreferenceChangedListener listener) { 49 mListener = listener; 50 } 51 PieController(CameraActivity activity, PieRenderer pie)52 public PieController(CameraActivity activity, PieRenderer pie) { 53 mActivity = activity; 54 mRenderer = pie; 55 mPreferences = new ArrayList<IconListPreference>(); 56 mPreferenceMap = new HashMap<IconListPreference, PieItem>(); 57 mOverrides = new HashMap<IconListPreference, String>(); 58 } 59 initialize(PreferenceGroup group)60 public void initialize(PreferenceGroup group) { 61 mRenderer.clearItems(); 62 setPreferenceGroup(group); 63 } 64 onSettingChanged(ListPreference pref)65 public void onSettingChanged(ListPreference pref) { 66 if (mListener != null) { 67 mListener.onSharedPreferenceChanged(); 68 } 69 } 70 setCameraId(int cameraId)71 protected void setCameraId(int cameraId) { 72 ListPreference pref = mPreferenceGroup.findPreference(CameraSettings.KEY_CAMERA_ID); 73 pref.setValue("" + cameraId); 74 } 75 makeItem(int resId)76 protected PieItem makeItem(int resId) { 77 // We need a mutable version as we change the alpha 78 Drawable d = mActivity.getResources().getDrawable(resId).mutate(); 79 return new PieItem(d, 0); 80 } 81 makeItem(CharSequence value)82 protected PieItem makeItem(CharSequence value) { 83 TextDrawable drawable = new TextDrawable(mActivity.getResources(), value); 84 return new PieItem(drawable, 0); 85 } 86 addItem(String prefKey, float center, float sweep)87 public void addItem(String prefKey, float center, float sweep) { 88 final IconListPreference pref = 89 (IconListPreference) mPreferenceGroup.findPreference(prefKey); 90 if (pref == null) return; 91 int[] iconIds = pref.getLargeIconIds(); 92 int resid = -1; 93 if (!pref.getUseSingleIcon() && iconIds != null) { 94 // Each entry has a corresponding icon. 95 int index = pref.findIndexOfValue(pref.getValue()); 96 resid = iconIds[index]; 97 } else { 98 // The preference only has a single icon to represent it. 99 resid = pref.getSingleIcon(); 100 } 101 PieItem item = makeItem(resid); 102 // use center and sweep to determine layout 103 item.setFixedSlice(center, sweep); 104 mRenderer.addItem(item); 105 mPreferences.add(pref); 106 mPreferenceMap.put(pref, item); 107 int nOfEntries = pref.getEntries().length; 108 if (nOfEntries > 1) { 109 for (int i = 0; i < nOfEntries; i++) { 110 PieItem inner = null; 111 if (iconIds != null) { 112 inner = makeItem(iconIds[i]); 113 } else { 114 inner = makeItem(pref.getEntries()[i]); 115 } 116 item.addItem(inner); 117 final int index = i; 118 inner.setOnClickListener(new OnClickListener() { 119 @Override 120 public void onClick(PieItem item) { 121 pref.setValueIndex(index); 122 reloadPreference(pref); 123 onSettingChanged(pref); 124 } 125 }); 126 } 127 } 128 } 129 setPreferenceGroup(PreferenceGroup group)130 public void setPreferenceGroup(PreferenceGroup group) { 131 mPreferenceGroup = group; 132 } 133 reloadPreferences()134 public void reloadPreferences() { 135 mPreferenceGroup.reloadValue(); 136 for (IconListPreference pref : mPreferenceMap.keySet()) { 137 reloadPreference(pref); 138 } 139 } 140 reloadPreference(IconListPreference pref)141 private void reloadPreference(IconListPreference pref) { 142 if (pref.getUseSingleIcon()) return; 143 PieItem item = mPreferenceMap.get(pref); 144 String overrideValue = mOverrides.get(pref); 145 int[] iconIds = pref.getLargeIconIds(); 146 if (iconIds != null) { 147 // Each entry has a corresponding icon. 148 int index; 149 if (overrideValue == null) { 150 index = pref.findIndexOfValue(pref.getValue()); 151 } else { 152 index = pref.findIndexOfValue(overrideValue); 153 if (index == -1) { 154 // Avoid the crash if camera driver has bugs. 155 Log.e(TAG, "Fail to find override value=" + overrideValue); 156 pref.print(); 157 return; 158 } 159 } 160 item.setImageResource(mActivity, iconIds[index]); 161 } else { 162 // The preference only has a single icon to represent it. 163 item.setImageResource(mActivity, pref.getSingleIcon()); 164 } 165 } 166 167 // Scene mode may override other camera settings (ex: flash mode). overrideSettings(final String ... keyvalues)168 public void overrideSettings(final String ... keyvalues) { 169 if (keyvalues.length % 2 != 0) { 170 throw new IllegalArgumentException(); 171 } 172 for (IconListPreference pref : mPreferenceMap.keySet()) { 173 override(pref, keyvalues); 174 } 175 } 176 override(IconListPreference pref, final String ... keyvalues)177 private void override(IconListPreference pref, final String ... keyvalues) { 178 mOverrides.remove(pref); 179 for (int i = 0; i < keyvalues.length; i += 2) { 180 String key = keyvalues[i]; 181 String value = keyvalues[i + 1]; 182 if (key.equals(pref.getKey())) { 183 mOverrides.put(pref, value); 184 PieItem item = mPreferenceMap.get(pref); 185 item.setEnabled(value == null); 186 break; 187 } 188 } 189 reloadPreference(pref); 190 } 191 } 192