CameraSettings.java revision e1706c13d469a163d811e29d33b86cc5d4bf67e9
1/* 2 * Copyright (C) 2009 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.content.SharedPreferences; 22import android.graphics.drawable.Drawable; 23import android.hardware.Camera.Parameters; 24import android.hardware.Camera.Size; 25import android.os.SystemProperties; 26import android.preference.ListPreference; 27import android.preference.Preference; 28import android.preference.PreferenceGroup; 29import android.preference.PreferenceManager; 30import android.preference.PreferenceScreen; 31import android.util.Log; 32 33import java.util.ArrayList; 34import java.util.List; 35 36/** 37 * Provides utilities and keys for Camera settings. 38 */ 39public class CameraSettings { 40 private static final int FIRST_REQUEST_CODE = 100; 41 private static final int NOT_FOUND = -1; 42 43 public static final String KEY_VERSION = "pref_version_key"; 44 public static final String KEY_RECORD_LOCATION = 45 RecordLocationPreference.KEY; 46 public static final String KEY_VIDEO_QUALITY = 47 "pref_camera_videoquality_key"; 48 public static final String KEY_VIDEO_DURATION = 49 "pref_camera_video_duration_key"; 50 public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; 51 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; 52 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; 53 public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key"; 54 public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key"; 55 public static final String KEY_WHITE_BALANCE = 56 "pref_camera_whitebalance_key"; 57 public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key"; 58 59 public static final int CURRENT_VERSION = 3; 60 61 // max mms video duration in seconds. 62 public static final int MMS_VIDEO_DURATION = 63 SystemProperties.getInt("ro.media.enc.lprof.duration", 60); 64 65 public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; 66 67 // MMS video length 68 public static final int DEFAULT_VIDEO_DURATION_VALUE = -1; 69 70 @SuppressWarnings("unused") 71 private static final String TAG = "CameraSettings"; 72 73 private final Context mContext; 74 private final Parameters mParameters; 75 private final PreferenceManager mManager; 76 77 public CameraSettings(Activity activity, Parameters parameters) { 78 mContext = activity; 79 mParameters = parameters; 80 mManager = new PreferenceManager(activity, FIRST_REQUEST_CODE); 81 } 82 83 public PreferenceScreen getPreferenceScreen(int preferenceRes) { 84 PreferenceScreen screen = mManager.createPreferenceScreen(mContext); 85 mManager.inflateFromResource(mContext, preferenceRes, screen); 86 initPreference(screen); 87 return screen; 88 } 89 90 public static void initialCameraPictureSize( 91 Context context, Parameters parameters) { 92 // When launching the camera app first time, we will set the picture 93 // size to the first one in the list defined in "arrays.xml" and is also 94 // supported by the driver. 95 List<Size> supported = parameters.getSupportedPictureSizes(); 96 if (supported == null) return; 97 for (String candidate : context.getResources().getStringArray( 98 R.array.pref_camera_picturesize_entryvalues)) { 99 if (setCameraPictureSize(candidate, supported, parameters)) { 100 SharedPreferences.Editor editor = PreferenceManager 101 .getDefaultSharedPreferences(context).edit(); 102 editor.putString(KEY_PICTURE_SIZE, candidate); 103 editor.commit(); 104 return; 105 } 106 } 107 } 108 109 public static void removePreferenceFromScreen( 110 PreferenceScreen screen, String key) { 111 Preference pref = screen.findPreference(key); 112 if (pref == null) { 113 Log.i(TAG, "No preference found based the key : " + key); 114 throw new IllegalArgumentException(); 115 } else { 116 removePreference(screen, pref); 117 } 118 } 119 120 public static boolean setCameraPictureSize( 121 String candidate, List<Size> supported, Parameters parameters) { 122 int index = candidate.indexOf('x'); 123 if (index == NOT_FOUND) return false; 124 int width = Integer.parseInt(candidate.substring(0, index)); 125 int height = Integer.parseInt(candidate.substring(index + 1)); 126 for (Size size: supported) { 127 if (size.width == width && size.height == height) { 128 parameters.setPictureSize(width, height); 129 return true; 130 } 131 } 132 return false; 133 } 134 135 private void initPreference(PreferenceScreen screen) { 136 ListPreference videoDuration = 137 (ListPreference) screen.findPreference(KEY_VIDEO_DURATION); 138 ListPreference pictureSize = 139 (ListPreference) screen.findPreference(KEY_PICTURE_SIZE); 140 ListPreference whiteBalance = 141 (ListPreference) screen.findPreference(KEY_WHITE_BALANCE); 142 ListPreference colorEffect = 143 (ListPreference) screen.findPreference(KEY_COLOR_EFFECT); 144 ListPreference sceneMode = 145 (ListPreference) screen.findPreference(KEY_SCENE_MODE); 146 ListPreference flashMode = 147 (ListPreference) screen.findPreference(KEY_FLASH_MODE); 148 ListPreference focusMode = 149 (ListPreference) screen.findPreference(KEY_FOCUS_MODE); 150 151 // Since the screen could be loaded from different resources, we need 152 // to check if the preference is available here 153 if (videoDuration != null) { 154 // Modify video duration settings. 155 // The first entry is for MMS video duration, and we need to fill 156 // in the device-dependent value (in seconds). 157 CharSequence[] entries = videoDuration.getEntries(); 158 entries[0] = String.format( 159 entries[0].toString(), MMS_VIDEO_DURATION); 160 } 161 162 // Filter out unsupported settings / options 163 if (pictureSize != null) { 164 filterUnsupportedOptions(screen, pictureSize, sizeListToStringList( 165 mParameters.getSupportedPictureSizes())); 166 } 167 if (whiteBalance != null) { 168 filterUnsupportedOptions(screen, 169 whiteBalance, mParameters.getSupportedWhiteBalance()); 170 } 171 if (colorEffect != null) { 172 filterUnsupportedOptions(screen, 173 colorEffect, mParameters.getSupportedColorEffects()); 174 } 175 if (sceneMode != null) { 176 filterUnsupportedOptions(screen, 177 sceneMode, mParameters.getSupportedSceneModes()); 178 } 179 if (flashMode != null) { 180 filterUnsupportedOptions(screen, 181 flashMode, mParameters.getSupportedFlashModes()); 182 } 183 if (focusMode != null) { 184 filterUnsupportedOptions(screen, 185 focusMode, mParameters.getSupportedFocusModes()); 186 } 187 } 188 189 private static boolean removePreference(PreferenceGroup group, 190 Preference remove) { 191 if (group.removePreference(remove)) return true; 192 193 for (int i = 0; i < group.getPreferenceCount(); i++) { 194 final Preference child = group.getPreference(i); 195 if (child instanceof PreferenceGroup) { 196 if (removePreference((PreferenceGroup) child, remove)) { 197 return true; 198 } 199 } 200 } 201 return false; 202 } 203 204 private void filterUnsupportedOptions(PreferenceScreen screen, 205 ListPreference pref, List<String> supported) { 206 207 CharSequence[] allEntries = pref.getEntries(); 208 209 // Remove the preference if the parameter is not supported or there is 210 // only one options for the settings. 211 if (supported == null || supported.size() <= 1) { 212 removePreference(screen, pref); 213 return; 214 } 215 216 CharSequence[] allEntryValues = pref.getEntryValues(); 217 Drawable[] allIcons = (pref instanceof IconListPreference) 218 ? ((IconListPreference) pref).getIcons() 219 : null; 220 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 221 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 222 ArrayList<Drawable> icons = 223 allIcons == null ? null : new ArrayList<Drawable>(); 224 for (int i = 0, len = allEntryValues.length; i < len; i++) { 225 if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) { 226 entries.add(allEntries[i]); 227 entryValues.add(allEntryValues[i]); 228 if (allIcons != null) icons.add(allIcons[i]); 229 } 230 } 231 232 // Set entries and entry values to list preference. 233 int size = entries.size(); 234 pref.setEntries(entries.toArray(new CharSequence[size])); 235 pref.setEntryValues(entryValues.toArray(new CharSequence[size])); 236 if (allIcons != null) { 237 ((IconListPreference) pref) 238 .setIcons(icons.toArray(new Drawable[size])); 239 } 240 241 // Set the value to the first entry if it is invalid. 242 String value = pref.getValue(); 243 if (pref.findIndexOfValue(value) == NOT_FOUND) { 244 pref.setValueIndex(0); 245 } 246 } 247 248 private static List<String> sizeListToStringList(List<Size> sizes) { 249 ArrayList<String> list = new ArrayList<String>(); 250 for (Size size : sizes) { 251 list.add(String.format("%dx%d", size.width, size.height)); 252 } 253 return list; 254 } 255 256 public static void upgradePreferences(SharedPreferences pref) { 257 int version; 258 try { 259 version = pref.getInt(KEY_VERSION, 0); 260 } catch (Exception ex) { 261 version = 0; 262 } 263 if (version == CURRENT_VERSION) return; 264 265 SharedPreferences.Editor editor = pref.edit(); 266 if (version == 0) { 267 // For old version, change 1 to 10 for video duration preference. 268 if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { 269 editor.putString(KEY_VIDEO_DURATION, "10"); 270 } 271 version = 1; 272 } 273 if (version == 1) { 274 // Change jpeg quality {65,75,85} to {normal,fine,superfine} 275 String quality = pref.getString(KEY_JPEG_QUALITY, "85"); 276 if (quality.equals("65")) { 277 quality = "normal"; 278 } else if (quality.equals("75")) { 279 quality = "fine"; 280 } else { 281 quality = "superfine"; 282 } 283 editor.putString(KEY_JPEG_QUALITY, quality); 284 version = 2; 285 } 286 if (version == 2) { 287 editor.putString(KEY_RECORD_LOCATION, 288 pref.getBoolean(KEY_RECORD_LOCATION, false) 289 ? RecordLocationPreference.VALUE_ON 290 : RecordLocationPreference.VALUE_NONE); 291 version = 3; 292 } 293 editor.putInt(KEY_VERSION, CURRENT_VERSION); 294 editor.commit(); 295 } 296} 297