CameraSettings.java revision e64363e447ce719601f8e5b5de7ff0497a607a09
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_COLOR_EFFECT = "pref_camera_coloreffect_key"; 49 public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key"; 50 public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key"; 51 public static final String KEY_EXPOSURE = "pref_camera_exposure_key"; 52 public static final String KEY_CAMERA_ID = "pref_camera_id_key"; 53 54 public static final String EXPOSURE_DEFAULT_VALUE = "0"; 55 56 public static final int CURRENT_VERSION = 4; 57 public static final int CURRENT_LOCAL_VERSION = 1; 58 59 // max video duration in seconds for mms and youtube. 60 private static final int MMS_VIDEO_DURATION = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW).duration; 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 pictureSize = group.findPreference(KEY_PICTURE_SIZE); 132 ListPreference whiteBalance = group.findPreference(KEY_WHITE_BALANCE); 133 ListPreference colorEffect = group.findPreference(KEY_COLOR_EFFECT); 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 143 // Since the screen could be loaded from different resources, we need 144 // to check if the preference is available here 145 if (videoQuality != null) { 146 initVideoQuality(videoQuality); 147 } 148 149 if (pictureSize != null) { 150 filterUnsupportedOptions(group, pictureSize, sizeListToStringList( 151 mParameters.getSupportedPictureSizes())); 152 } 153 if (whiteBalance != null) { 154 filterUnsupportedOptions(group, 155 whiteBalance, mParameters.getSupportedWhiteBalance()); 156 } 157 if (colorEffect != null) { 158 filterUnsupportedOptions(group, 159 colorEffect, mParameters.getSupportedColorEffects()); 160 } 161 if (sceneMode != null) { 162 filterUnsupportedOptions(group, 163 sceneMode, mParameters.getSupportedSceneModes()); 164 } 165 if (flashMode != null) { 166 filterUnsupportedOptions(group, 167 flashMode, mParameters.getSupportedFlashModes()); 168 } 169 if (focusMode != null) { 170 filterUnsupportedOptions(group, 171 focusMode, mParameters.getSupportedFocusModes()); 172 } 173 if (videoFlashMode != null) { 174 filterUnsupportedOptions(group, 175 videoFlashMode, mParameters.getSupportedFlashModes()); 176 } 177 if (exposure != null) buildExposureCompensation(group, exposure); 178 if (cameraIdPref != null) buildCameraId(group, cameraIdPref); 179 } 180 181 private static List<String> getSupportedTimeLapseProfiles(int cameraId) { 182 ArrayList<String> supportedProfiles = new ArrayList<String>(); 183 if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_TIME_LAPSE_480P)) { 184 supportedProfiles.add(Integer.toString(CamcorderProfile.QUALITY_TIME_LAPSE_480P)); 185 } 186 if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_TIME_LAPSE_720P)) { 187 supportedProfiles.add(Integer.toString(CamcorderProfile.QUALITY_TIME_LAPSE_720P)); 188 } 189 if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_TIME_LAPSE_1080P)) { 190 supportedProfiles.add(Integer.toString(CamcorderProfile.QUALITY_TIME_LAPSE_1080P)); 191 } 192 193 return supportedProfiles; 194 } 195 196 private void buildExposureCompensation( 197 PreferenceGroup group, ListPreference exposure) { 198 int max = mParameters.getMaxExposureCompensation(); 199 int min = mParameters.getMinExposureCompensation(); 200 if (max == 0 && min == 0) { 201 removePreference(group, exposure.getKey()); 202 return; 203 } 204 float step = mParameters.getExposureCompensationStep(); 205 206 // show only integer values for exposure compensation 207 int maxValue = (int) Math.floor(max * step); 208 int minValue = (int) Math.ceil(min * step); 209 CharSequence entries[] = new CharSequence[maxValue - minValue + 1]; 210 CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1]; 211 for (int i = minValue; i <= maxValue; ++i) { 212 entryValues[maxValue - i] = Integer.toString(Math.round(i / step)); 213 StringBuilder builder = new StringBuilder(); 214 if (i > 0) builder.append('+'); 215 entries[maxValue - i] = builder.append(i).toString(); 216 } 217 exposure.setEntries(entries); 218 exposure.setEntryValues(entryValues); 219 } 220 221 private void buildCameraId( 222 PreferenceGroup group, IconListPreference preference) { 223 int numOfCameras = mCameraInfo.length; 224 if (numOfCameras < 2) { 225 removePreference(group, preference.getKey()); 226 return; 227 } 228 229 CharSequence[] entryValues = new CharSequence[2]; 230 for (int i = 0 ; i < mCameraInfo.length ; ++i) { 231 int index = 232 (mCameraInfo[i].facing == CameraInfo.CAMERA_FACING_FRONT) 233 ? CameraInfo.CAMERA_FACING_FRONT 234 : CameraInfo.CAMERA_FACING_BACK; 235 if (entryValues[index] == null) { 236 entryValues[index] = "" + i; 237 if (entryValues[((index == 1) ? 0 : 1)] != null) break; 238 } 239 } 240 preference.setEntryValues(entryValues); 241 } 242 243 private static boolean removePreference(PreferenceGroup group, String key) { 244 for (int i = 0, n = group.size(); i < n; i++) { 245 CameraPreference child = group.get(i); 246 if (child instanceof PreferenceGroup) { 247 if (removePreference((PreferenceGroup) child, key)) { 248 return true; 249 } 250 } 251 if (child instanceof ListPreference && 252 ((ListPreference) child).getKey().equals(key)) { 253 group.removePreference(i); 254 return true; 255 } 256 } 257 return false; 258 } 259 260 private void filterUnsupportedOptions(PreferenceGroup group, 261 ListPreference pref, List<String> supported) { 262 263 // Remove the preference if the parameter is not supported or there is 264 // only one options for the settings. 265 if (supported == null || supported.size() <= 1) { 266 removePreference(group, pref.getKey()); 267 return; 268 } 269 270 pref.filterUnsupported(supported); 271 if (pref.getEntries().length <= 1) { 272 removePreference(group, pref.getKey()); 273 return; 274 } 275 276 // Set the value to the first entry if it is invalid. 277 String value = pref.getValue(); 278 if (pref.findIndexOfValue(value) == NOT_FOUND) { 279 pref.setValueIndex(0); 280 } 281 } 282 283 private static List<String> sizeListToStringList(List<Size> sizes) { 284 ArrayList<String> list = new ArrayList<String>(); 285 for (Size size : sizes) { 286 list.add(String.format("%dx%d", size.width, size.height)); 287 } 288 return list; 289 } 290 291 public static void upgradeLocalPreferences(SharedPreferences pref) { 292 int version; 293 try { 294 version = pref.getInt(KEY_LOCAL_VERSION, 0); 295 } catch (Exception ex) { 296 version = 0; 297 } 298 if (version == CURRENT_LOCAL_VERSION) return; 299 SharedPreferences.Editor editor = pref.edit(); 300 editor.putInt(KEY_LOCAL_VERSION, CURRENT_LOCAL_VERSION); 301 editor.apply(); 302 } 303 304 public static void upgradeGlobalPreferences(SharedPreferences pref) { 305 int version; 306 try { 307 version = pref.getInt(KEY_VERSION, 0); 308 } catch (Exception ex) { 309 version = 0; 310 } 311 if (version == CURRENT_VERSION) return; 312 313 SharedPreferences.Editor editor = pref.edit(); 314 if (version == 0) { 315 // We won't use the preference which change in version 1. 316 // So, just upgrade to version 1 directly 317 version = 1; 318 } 319 if (version == 1) { 320 // Change jpeg quality {65,75,85} to {normal,fine,superfine} 321 String quality = pref.getString(KEY_JPEG_QUALITY, "85"); 322 if (quality.equals("65")) { 323 quality = "normal"; 324 } else if (quality.equals("75")) { 325 quality = "fine"; 326 } else { 327 quality = "superfine"; 328 } 329 editor.putString(KEY_JPEG_QUALITY, quality); 330 version = 2; 331 } 332 if (version == 2) { 333 editor.putString(KEY_RECORD_LOCATION, 334 pref.getBoolean(KEY_RECORD_LOCATION, false) 335 ? RecordLocationPreference.VALUE_ON 336 : RecordLocationPreference.VALUE_NONE); 337 version = 3; 338 } 339 if (version == 3) { 340 // Just use video quality to replace it and 341 // ignore the current settings. 342 editor.remove("pref_camera_videoquality_key"); 343 editor.remove("pref_camera_video_duration_key"); 344 } 345 editor.putInt(KEY_VERSION, CURRENT_VERSION); 346 editor.apply(); 347 } 348 349 public static boolean getVideoQuality(Context context, String quality) { 350 return context.getString(R.string.pref_video_quality_youtube).equals(quality) 351 || context.getString(R.string.pref_video_quality_high).equals(quality); 352 } 353 354 public static int getVideoDurationInMillis(Context context, String quality) { 355 if (context.getString(R.string.pref_video_quality_mms).equals(quality)) { 356 return MMS_VIDEO_DURATION * 1000; 357 } else if (context.getString(R.string.pref_video_quality_youtube).equals(quality)) { 358 return YOUTUBE_VIDEO_DURATION * 1000; 359 } 360 return DEFAULT_VIDEO_DURATION; 361 } 362 363 public static int readPreferredCameraId(SharedPreferences pref) { 364 return Integer.parseInt(pref.getString(KEY_CAMERA_ID, "0")); 365 } 366 367 public static void writePreferredCameraId(SharedPreferences pref, 368 int cameraId) { 369 Editor editor = pref.edit(); 370 editor.putString(KEY_CAMERA_ID, Integer.toString(cameraId)); 371 editor.apply(); 372 } 373 374 375 public static void restorePreferences(Context context, 376 ComboPreferences preferences, Parameters parameters) { 377 int currentCameraId = readPreferredCameraId(preferences); 378 379 // Clear the preferences of both cameras. 380 int backCameraId = CameraHolder.instance().getBackCameraId(); 381 if (backCameraId != -1) { 382 preferences.setLocalId(context, backCameraId); 383 Editor editor = preferences.edit(); 384 editor.clear(); 385 editor.apply(); 386 } 387 int frontCameraId = CameraHolder.instance().getFrontCameraId(); 388 if (frontCameraId != -1) { 389 preferences.setLocalId(context, frontCameraId); 390 Editor editor = preferences.edit(); 391 editor.clear(); 392 editor.apply(); 393 } 394 395 upgradeGlobalPreferences(preferences.getGlobal()); 396 upgradeLocalPreferences(preferences.getLocal()); 397 398 // Write back the current camera id because parameters are related to 399 // the camera. Otherwise, we may switch to the front camera but the 400 // initial picture size is that of the back camera. 401 initialCameraPictureSize(context, parameters); 402 writePreferredCameraId(preferences, currentCameraId); 403 } 404 405 private void initVideoQuality(ListPreference videoQuality) { 406 CharSequence[] entries = videoQuality.getEntries(); 407 CharSequence[] values = videoQuality.getEntryValues(); 408 if (Util.isMmsCapable(mContext)) { 409 // We need to fill in the device-dependent value (in seconds). 410 for (int i = 0; i < entries.length; ++i) { 411 if (mContext.getString(R.string.pref_video_quality_mms).equals(values[i])) { 412 entries[i] = entries[i].toString().replace( 413 "30", Integer.toString(MMS_VIDEO_DURATION)); 414 break; 415 } 416 } 417 } else { 418 // The device does not support mms. Remove it. Using 419 // filterUnsupported is not efficient but adding a new method 420 // for remove is not worth it. 421 ArrayList<String> supported = new ArrayList<String>(); 422 for (int i = 0; i < entries.length; ++i) { 423 if (!mContext.getString(R.string.pref_video_quality_mms).equals(values[i])) { 424 supported.add(values[i].toString()); 425 } 426 } 427 videoQuality.filterUnsupported(supported); 428 } 429 } 430} 431