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