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