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