CameraSettings.java revision 926405a84a882ab78f2f5aa1e88c4961640724f7
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.content.SharedPreferences.Editor; 23import android.hardware.Camera.CameraInfo; 24import android.hardware.Camera.Parameters; 25import android.hardware.Camera.Size; 26import android.media.CamcorderProfile; 27import android.util.Log; 28 29import java.util.ArrayList; 30import java.util.List; 31 32/** 33 * Provides utilities and keys for Camera settings. 34 */ 35public class CameraSettings { 36 private static final int NOT_FOUND = -1; 37 38 public static final String KEY_VERSION = "pref_version_key"; 39 public static final String KEY_LOCAL_VERSION = "pref_local_version_key"; 40 public static final String KEY_RECORD_LOCATION = RecordLocationPreference.KEY; 41 public static final String KEY_VIDEO_QUALITY = "pref_video_quality_key"; 42 public static final String KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL = "pref_video_time_lapse_frame_interval_key"; 43 public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; 44 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; 45 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; 46 public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key"; 47 public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key"; 48 public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key"; 49 public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key"; 50 public static final String KEY_EXPOSURE = "pref_camera_exposure_key"; 51 public static final String KEY_VIDEO_EFFECT = "pref_video_effect_key"; 52 public static final String KEY_CAMERA_ID = "pref_camera_id_key"; 53 public static final String KEY_TAP_TO_FOCUS_PROMPT_SHOWN = "pref_tap_to_focus_prompt_shown_key"; 54 55 public static final String EXPOSURE_DEFAULT_VALUE = "0"; 56 57 public static final int CURRENT_VERSION = 5; 58 public static final int CURRENT_LOCAL_VERSION = 1; 59 60 public static final int DEFAULT_VIDEO_DURATION = 0; // no limit 61 62 private static final String TAG = "CameraSettings"; 63 64 private final Context mContext; 65 private final Parameters mParameters; 66 private final CameraInfo[] mCameraInfo; 67 private final int mCameraId; 68 69 public CameraSettings(Activity activity, Parameters parameters, 70 int cameraId, CameraInfo[] cameraInfo) { 71 mContext = activity; 72 mParameters = parameters; 73 mCameraId = cameraId; 74 mCameraInfo = cameraInfo; 75 } 76 77 public PreferenceGroup getPreferenceGroup(int preferenceRes) { 78 PreferenceInflater inflater = new PreferenceInflater(mContext); 79 PreferenceGroup group = 80 (PreferenceGroup) inflater.inflate(preferenceRes); 81 initPreference(group); 82 return group; 83 } 84 85 public static String getDefaultVideoQuality(int cameraId, 86 String defaultQuality) { 87 int quality = -1; 88 try { 89 quality = Integer.valueOf(defaultQuality); 90 } catch(NumberFormatException e) { 91 Log.e(TAG, "Cannot convert default quality setting '" 92 + defaultQuality 93 + "' into CamcorderProfile quality. Using fallback"); 94 } 95 if (CamcorderProfile.hasProfile(cameraId, quality)) { 96 return defaultQuality; 97 } 98 return Integer.toString(CamcorderProfile.QUALITY_HIGH); 99 } 100 101 public static void initialCameraPictureSize( 102 Context context, Parameters parameters) { 103 // When launching the camera app first time, we will set the picture 104 // size to the first one in the list defined in "arrays.xml" and is also 105 // supported by the driver. 106 List<Size> supported = parameters.getSupportedPictureSizes(); 107 if (supported == null) return; 108 for (String candidate : context.getResources().getStringArray( 109 R.array.pref_camera_picturesize_entryvalues)) { 110 if (setCameraPictureSize(candidate, supported, parameters)) { 111 SharedPreferences.Editor editor = ComboPreferences 112 .get(context).edit(); 113 editor.putString(KEY_PICTURE_SIZE, candidate); 114 editor.apply(); 115 return; 116 } 117 } 118 Log.e(TAG, "No supported picture size found"); 119 } 120 121 public static void removePreferenceFromScreen( 122 PreferenceGroup group, String key) { 123 removePreference(group, key); 124 } 125 126 public static boolean setCameraPictureSize( 127 String candidate, List<Size> supported, Parameters parameters) { 128 int index = candidate.indexOf('x'); 129 if (index == NOT_FOUND) return false; 130 int width = Integer.parseInt(candidate.substring(0, index)); 131 int height = Integer.parseInt(candidate.substring(index + 1)); 132 for (Size size : supported) { 133 if (size.width == width && size.height == height) { 134 parameters.setPictureSize(width, height); 135 return true; 136 } 137 } 138 return false; 139 } 140 141 private void initPreference(PreferenceGroup group) { 142 ListPreference videoQuality = group.findPreference(KEY_VIDEO_QUALITY); 143 ListPreference timeLapseInterval = group.findPreference(KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL); 144 ListPreference pictureSize = group.findPreference(KEY_PICTURE_SIZE); 145 ListPreference whiteBalance = group.findPreference(KEY_WHITE_BALANCE); 146 ListPreference sceneMode = group.findPreference(KEY_SCENE_MODE); 147 ListPreference flashMode = group.findPreference(KEY_FLASH_MODE); 148 ListPreference focusMode = group.findPreference(KEY_FOCUS_MODE); 149 ListPreference exposure = group.findPreference(KEY_EXPOSURE); 150 IconListPreference cameraIdPref = 151 (IconListPreference) group.findPreference(KEY_CAMERA_ID); 152 ListPreference videoFlashMode = 153 group.findPreference(KEY_VIDEOCAMERA_FLASH_MODE); 154 ListPreference videoEffect = group.findPreference(KEY_VIDEO_EFFECT); 155 156 // Since the screen could be loaded from different resources, we need 157 // to check if the preference is available here 158 if (videoQuality != null) { 159 filterUnsupportedOptions(group, videoQuality, getSupportedVideoQuality()); 160 } 161 162 if (pictureSize != null) { 163 filterUnsupportedOptions(group, pictureSize, sizeListToStringList( 164 mParameters.getSupportedPictureSizes())); 165 } 166 if (whiteBalance != null) { 167 filterUnsupportedOptions(group, 168 whiteBalance, mParameters.getSupportedWhiteBalance()); 169 } 170 if (sceneMode != null) { 171 filterUnsupportedOptions(group, 172 sceneMode, mParameters.getSupportedSceneModes()); 173 } 174 if (flashMode != null) { 175 filterUnsupportedOptions(group, 176 flashMode, mParameters.getSupportedFlashModes()); 177 } 178 if (focusMode != null) { 179 if (mParameters.getMaxNumFocusAreas() == 0) { 180 filterUnsupportedOptions(group, 181 focusMode, mParameters.getSupportedFocusModes()); 182 } else { 183 // Remove the focus mode if we can use tap-to-focus. 184 removePreference(group, focusMode.getKey()); 185 } 186 } 187 if (videoFlashMode != null) { 188 filterUnsupportedOptions(group, 189 videoFlashMode, mParameters.getSupportedFlashModes()); 190 } 191 if (exposure != null) buildExposureCompensation(group, exposure); 192 if (cameraIdPref != null) buildCameraId(group, cameraIdPref); 193 194 if (timeLapseInterval != null) resetIfInvalid(timeLapseInterval); 195 if (videoEffect != null) { 196 initVideoEffect(group, videoEffect); 197 resetIfInvalid(videoEffect); 198 } 199 } 200 201 private void buildExposureCompensation( 202 PreferenceGroup group, ListPreference exposure) { 203 int max = mParameters.getMaxExposureCompensation(); 204 int min = mParameters.getMinExposureCompensation(); 205 if (max == 0 && min == 0) { 206 removePreference(group, exposure.getKey()); 207 return; 208 } 209 float step = mParameters.getExposureCompensationStep(); 210 211 // show only integer values for exposure compensation 212 int maxValue = (int) Math.floor(max * step); 213 int minValue = (int) Math.ceil(min * step); 214 CharSequence entries[] = new CharSequence[maxValue - minValue + 1]; 215 CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1]; 216 for (int i = minValue; i <= maxValue; ++i) { 217 entryValues[maxValue - i] = Integer.toString(Math.round(i / step)); 218 StringBuilder builder = new StringBuilder(); 219 if (i > 0) builder.append('+'); 220 entries[maxValue - i] = builder.append(i).toString(); 221 } 222 exposure.setEntries(entries); 223 exposure.setEntryValues(entryValues); 224 } 225 226 private void buildCameraId( 227 PreferenceGroup group, IconListPreference preference) { 228 int numOfCameras = mCameraInfo.length; 229 if (numOfCameras < 2) { 230 removePreference(group, preference.getKey()); 231 return; 232 } 233 234 CharSequence[] entryValues = new CharSequence[2]; 235 for (int i = 0; i < mCameraInfo.length; ++i) { 236 int index = 237 (mCameraInfo[i].facing == CameraInfo.CAMERA_FACING_FRONT) 238 ? CameraInfo.CAMERA_FACING_FRONT 239 : CameraInfo.CAMERA_FACING_BACK; 240 if (entryValues[index] == null) { 241 entryValues[index] = "" + i; 242 if (entryValues[((index == 1) ? 0 : 1)] != null) break; 243 } 244 } 245 preference.setEntryValues(entryValues); 246 } 247 248 private static boolean removePreference(PreferenceGroup group, String key) { 249 for (int i = 0, n = group.size(); i < n; i++) { 250 CameraPreference child = group.get(i); 251 if (child instanceof PreferenceGroup) { 252 if (removePreference((PreferenceGroup) child, key)) { 253 return true; 254 } 255 } 256 if (child instanceof ListPreference && 257 ((ListPreference) child).getKey().equals(key)) { 258 group.removePreference(i); 259 return true; 260 } 261 } 262 return false; 263 } 264 265 private void filterUnsupportedOptions(PreferenceGroup group, 266 ListPreference pref, List<String> supported) { 267 268 // Remove the preference if the parameter is not supported or there is 269 // only one options for the settings. 270 if (supported == null || supported.size() <= 1) { 271 removePreference(group, pref.getKey()); 272 return; 273 } 274 275 pref.filterUnsupported(supported); 276 if (pref.getEntries().length <= 1) { 277 removePreference(group, pref.getKey()); 278 return; 279 } 280 281 resetIfInvalid(pref); 282 } 283 284 private void resetIfInvalid(ListPreference pref) { 285 // Set the value to the first entry if it is invalid. 286 String value = pref.getValue(); 287 if (pref.findIndexOfValue(value) == NOT_FOUND) { 288 pref.setValueIndex(0); 289 } 290 } 291 292 private static List<String> sizeListToStringList(List<Size> sizes) { 293 ArrayList<String> list = new ArrayList<String>(); 294 for (Size size : sizes) { 295 list.add(String.format("%dx%d", size.width, size.height)); 296 } 297 return list; 298 } 299 300 public static void upgradeLocalPreferences(SharedPreferences pref) { 301 int version; 302 try { 303 version = pref.getInt(KEY_LOCAL_VERSION, 0); 304 } catch (Exception ex) { 305 version = 0; 306 } 307 if (version == CURRENT_LOCAL_VERSION) return; 308 SharedPreferences.Editor editor = pref.edit(); 309 editor.putInt(KEY_LOCAL_VERSION, CURRENT_LOCAL_VERSION); 310 editor.apply(); 311 } 312 313 public static void upgradeGlobalPreferences(SharedPreferences pref) { 314 int version; 315 try { 316 version = pref.getInt(KEY_VERSION, 0); 317 } catch (Exception ex) { 318 version = 0; 319 } 320 if (version == CURRENT_VERSION) return; 321 322 SharedPreferences.Editor editor = pref.edit(); 323 if (version == 0) { 324 // We won't use the preference which change in version 1. 325 // So, just upgrade to version 1 directly 326 version = 1; 327 } 328 if (version == 1) { 329 // Change jpeg quality {65,75,85} to {normal,fine,superfine} 330 String quality = pref.getString(KEY_JPEG_QUALITY, "85"); 331 if (quality.equals("65")) { 332 quality = "normal"; 333 } else if (quality.equals("75")) { 334 quality = "fine"; 335 } else { 336 quality = "superfine"; 337 } 338 editor.putString(KEY_JPEG_QUALITY, quality); 339 version = 2; 340 } 341 if (version == 2) { 342 editor.putString(KEY_RECORD_LOCATION, 343 pref.getBoolean(KEY_RECORD_LOCATION, false) 344 ? RecordLocationPreference.VALUE_ON 345 : RecordLocationPreference.VALUE_NONE); 346 version = 3; 347 } 348 if (version == 3) { 349 // Just use video quality to replace it and 350 // ignore the current settings. 351 editor.remove("pref_camera_videoquality_key"); 352 editor.remove("pref_camera_video_duration_key"); 353 version = 4; 354 } 355 if (version == 4) { 356 // We use numbers to represent the quality now. The quality definition is identical to 357 // that of CamcorderProfile.java. 358 editor.remove("pref_video_quality_key"); 359 } 360 361 editor.putInt(KEY_VERSION, CURRENT_VERSION); 362 editor.apply(); 363 } 364 365 public static int readPreferredCameraId(SharedPreferences pref) { 366 return Integer.parseInt(pref.getString(KEY_CAMERA_ID, "0")); 367 } 368 369 public static void writePreferredCameraId(SharedPreferences pref, 370 int cameraId) { 371 Editor editor = pref.edit(); 372 editor.putString(KEY_CAMERA_ID, Integer.toString(cameraId)); 373 editor.apply(); 374 } 375 376 public static int readExposure(ComboPreferences preferences) { 377 String exposure = preferences.getString( 378 CameraSettings.KEY_EXPOSURE, 379 EXPOSURE_DEFAULT_VALUE); 380 try { 381 return Integer.parseInt(exposure); 382 } catch (Exception ex) { 383 Log.e(TAG, "Invalid exposure: " + exposure); 384 } 385 return 0; 386 } 387 388 public static int readEffectType(SharedPreferences pref) { 389 String effectSelection = pref.getString(KEY_VIDEO_EFFECT, "none"); 390 if (effectSelection.equals("none")) { 391 return EffectsRecorder.EFFECT_NONE; 392 } else if (effectSelection.startsWith("goofy_face")) { 393 return EffectsRecorder.EFFECT_GOOFY_FACE; 394 } else if (effectSelection.startsWith("backdropper")) { 395 return EffectsRecorder.EFFECT_BACKDROPPER; 396 } 397 Log.e(TAG, "Invalid effect selection: " + effectSelection); 398 return EffectsRecorder.EFFECT_NONE; 399 } 400 401 public static Object readEffectParameter(SharedPreferences pref) { 402 String effectSelection = pref.getString(KEY_VIDEO_EFFECT, "none"); 403 if (effectSelection.equals("none")) { 404 return null; 405 } 406 int separatorIndex = effectSelection.indexOf('/'); 407 String effectParameter = 408 effectSelection.substring(separatorIndex + 1); 409 if (effectSelection.startsWith("goofy_face")) { 410 if (effectParameter.equals("squeeze")) { 411 return EffectsRecorder.EFFECT_GF_SQUEEZE; 412 } else if (effectParameter.equals("big_eyes")) { 413 return EffectsRecorder.EFFECT_GF_BIG_EYES; 414 } else if (effectParameter.equals("big_mouth")) { 415 return EffectsRecorder.EFFECT_GF_BIG_MOUTH; 416 } else if (effectParameter.equals("small_mouth")) { 417 return EffectsRecorder.EFFECT_GF_SMALL_MOUTH; 418 } else if (effectParameter.equals("big_nose")) { 419 return EffectsRecorder.EFFECT_GF_BIG_NOSE; 420 } else if (effectParameter.equals("small_eyes")) { 421 return EffectsRecorder.EFFECT_GF_SMALL_EYES; 422 } 423 } else if (effectSelection.startsWith("backdropper")) { 424 // Parameter is a string that either encodes the URI to use, 425 // or specifies 'gallery'. 426 return effectParameter; 427 } 428 429 Log.e(TAG, "Invalid effect selection: " + effectSelection); 430 return null; 431 } 432 433 434 public static void restorePreferences(Context context, 435 ComboPreferences preferences, Parameters parameters) { 436 int currentCameraId = readPreferredCameraId(preferences); 437 438 // Clear the preferences of both cameras. 439 int backCameraId = CameraHolder.instance().getBackCameraId(); 440 if (backCameraId != -1) { 441 preferences.setLocalId(context, backCameraId); 442 Editor editor = preferences.edit(); 443 editor.clear(); 444 editor.apply(); 445 } 446 int frontCameraId = CameraHolder.instance().getFrontCameraId(); 447 if (frontCameraId != -1) { 448 preferences.setLocalId(context, frontCameraId); 449 Editor editor = preferences.edit(); 450 editor.clear(); 451 editor.apply(); 452 } 453 454 // Switch back to the preferences of the current camera. Otherwise, 455 // we may write the preference to wrong camera later. 456 preferences.setLocalId(context, currentCameraId); 457 458 upgradeGlobalPreferences(preferences.getGlobal()); 459 upgradeLocalPreferences(preferences.getLocal()); 460 461 // Write back the current camera id because parameters are related to 462 // the camera. Otherwise, we may switch to the front camera but the 463 // initial picture size is that of the back camera. 464 initialCameraPictureSize(context, parameters); 465 writePreferredCameraId(preferences, currentCameraId); 466 } 467 468 private ArrayList<String> getSupportedVideoQuality() { 469 ArrayList<String> supported = new ArrayList<String>(); 470 // Check for supported quality 471 if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_1080P)) { 472 supported.add(Integer.toString(CamcorderProfile.QUALITY_1080P)); 473 } 474 if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_720P)) { 475 supported.add(Integer.toString(CamcorderProfile.QUALITY_720P)); 476 } 477 if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_480P)) { 478 supported.add(Integer.toString(CamcorderProfile.QUALITY_480P)); 479 } 480 481 return supported; 482 } 483 484 private void initVideoEffect(PreferenceGroup group, ListPreference videoEffect) { 485 CharSequence[] values = videoEffect.getEntryValues(); 486 487 boolean goofyFaceSupported = 488 EffectsRecorder.isEffectSupported(EffectsRecorder.EFFECT_GOOFY_FACE); 489 boolean backdropperSupported = 490 EffectsRecorder.isEffectSupported(EffectsRecorder.EFFECT_BACKDROPPER) && 491 mParameters.isAutoExposureLockSupported() && 492 mParameters.isAutoWhiteBalanceLockSupported(); 493 494 ArrayList<String> supported = new ArrayList<String>(); 495 for (CharSequence value : values) { 496 String effectSelection = value.toString(); 497 if (!goofyFaceSupported && effectSelection.startsWith("goofy_face")) continue; 498 if (!backdropperSupported && effectSelection.startsWith("backdropper")) continue; 499 supported.add(effectSelection); 500 } 501 502 filterUnsupportedOptions(group, videoEffect, supported); 503 } 504} 505