CameraSettings.java revision 71e76dcebdbb3e5768fc948bed9c529a5787294e
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) resetIfInvalid(videoEffect); 184 } 185 186 private void buildExposureCompensation( 187 PreferenceGroup group, ListPreference exposure) { 188 int max = mParameters.getMaxExposureCompensation(); 189 int min = mParameters.getMinExposureCompensation(); 190 if (max == 0 && min == 0) { 191 removePreference(group, exposure.getKey()); 192 return; 193 } 194 float step = mParameters.getExposureCompensationStep(); 195 196 // show only integer values for exposure compensation 197 int maxValue = (int) Math.floor(max * step); 198 int minValue = (int) Math.ceil(min * step); 199 CharSequence entries[] = new CharSequence[maxValue - minValue + 1]; 200 CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1]; 201 for (int i = minValue; i <= maxValue; ++i) { 202 entryValues[maxValue - i] = Integer.toString(Math.round(i / step)); 203 StringBuilder builder = new StringBuilder(); 204 if (i > 0) builder.append('+'); 205 entries[maxValue - i] = builder.append(i).toString(); 206 } 207 exposure.setEntries(entries); 208 exposure.setEntryValues(entryValues); 209 } 210 211 private void buildCameraId( 212 PreferenceGroup group, IconListPreference preference) { 213 int numOfCameras = mCameraInfo.length; 214 if (numOfCameras < 2) { 215 removePreference(group, preference.getKey()); 216 return; 217 } 218 219 CharSequence[] entryValues = new CharSequence[2]; 220 for (int i = 0 ; i < mCameraInfo.length ; ++i) { 221 int index = 222 (mCameraInfo[i].facing == CameraInfo.CAMERA_FACING_FRONT) 223 ? CameraInfo.CAMERA_FACING_FRONT 224 : CameraInfo.CAMERA_FACING_BACK; 225 if (entryValues[index] == null) { 226 entryValues[index] = "" + i; 227 if (entryValues[((index == 1) ? 0 : 1)] != null) break; 228 } 229 } 230 preference.setEntryValues(entryValues); 231 } 232 233 private static boolean removePreference(PreferenceGroup group, String key) { 234 for (int i = 0, n = group.size(); i < n; i++) { 235 CameraPreference child = group.get(i); 236 if (child instanceof PreferenceGroup) { 237 if (removePreference((PreferenceGroup) child, key)) { 238 return true; 239 } 240 } 241 if (child instanceof ListPreference && 242 ((ListPreference) child).getKey().equals(key)) { 243 group.removePreference(i); 244 return true; 245 } 246 } 247 return false; 248 } 249 250 private void filterUnsupportedOptions(PreferenceGroup group, 251 ListPreference pref, List<String> supported) { 252 253 // Remove the preference if the parameter is not supported or there is 254 // only one options for the settings. 255 if (supported == null || supported.size() <= 1) { 256 removePreference(group, pref.getKey()); 257 return; 258 } 259 260 pref.filterUnsupported(supported); 261 if (pref.getEntries().length <= 1) { 262 removePreference(group, pref.getKey()); 263 return; 264 } 265 266 resetIfInvalid(pref); 267 } 268 269 private void resetIfInvalid(ListPreference pref) { 270 // Set the value to the first entry if it is invalid. 271 String value = pref.getValue(); 272 if (pref.findIndexOfValue(value) == NOT_FOUND) { 273 pref.setValueIndex(0); 274 } 275 } 276 277 private static List<String> sizeListToStringList(List<Size> sizes) { 278 ArrayList<String> list = new ArrayList<String>(); 279 for (Size size : sizes) { 280 list.add(String.format("%dx%d", size.width, size.height)); 281 } 282 return list; 283 } 284 285 public static void upgradeLocalPreferences(SharedPreferences pref) { 286 int version; 287 try { 288 version = pref.getInt(KEY_LOCAL_VERSION, 0); 289 } catch (Exception ex) { 290 version = 0; 291 } 292 if (version == CURRENT_LOCAL_VERSION) return; 293 SharedPreferences.Editor editor = pref.edit(); 294 editor.putInt(KEY_LOCAL_VERSION, CURRENT_LOCAL_VERSION); 295 editor.apply(); 296 } 297 298 public static void upgradeGlobalPreferences(SharedPreferences pref) { 299 int version; 300 try { 301 version = pref.getInt(KEY_VERSION, 0); 302 } catch (Exception ex) { 303 version = 0; 304 } 305 if (version == CURRENT_VERSION) return; 306 307 SharedPreferences.Editor editor = pref.edit(); 308 if (version == 0) { 309 // We won't use the preference which change in version 1. 310 // So, just upgrade to version 1 directly 311 version = 1; 312 } 313 if (version == 1) { 314 // Change jpeg quality {65,75,85} to {normal,fine,superfine} 315 String quality = pref.getString(KEY_JPEG_QUALITY, "85"); 316 if (quality.equals("65")) { 317 quality = "normal"; 318 } else if (quality.equals("75")) { 319 quality = "fine"; 320 } else { 321 quality = "superfine"; 322 } 323 editor.putString(KEY_JPEG_QUALITY, quality); 324 version = 2; 325 } 326 if (version == 2) { 327 editor.putString(KEY_RECORD_LOCATION, 328 pref.getBoolean(KEY_RECORD_LOCATION, false) 329 ? RecordLocationPreference.VALUE_ON 330 : RecordLocationPreference.VALUE_NONE); 331 version = 3; 332 } 333 if (version == 3) { 334 // Just use video quality to replace it and 335 // ignore the current settings. 336 editor.remove("pref_camera_videoquality_key"); 337 editor.remove("pref_camera_video_duration_key"); 338 } 339 editor.putInt(KEY_VERSION, CURRENT_VERSION); 340 editor.apply(); 341 } 342 343 public static boolean getVideoQuality(Context context, String quality) { 344 return context.getString(R.string.pref_video_quality_youtube).equals(quality) 345 || context.getString(R.string.pref_video_quality_high).equals(quality); 346 } 347 348 public static int getVideoDurationInMillis(Context context, String quality, int cameraId) { 349 if (context.getString(R.string.pref_video_quality_mms).equals(quality)) { 350 int mmsVideoDuration = CamcorderProfile.get(cameraId, 351 CamcorderProfile.QUALITY_LOW).duration; 352 return mmsVideoDuration * 1000; 353 } else if (context.getString(R.string.pref_video_quality_youtube).equals(quality)) { 354 return YOUTUBE_VIDEO_DURATION * 1000; 355 } 356 return DEFAULT_VIDEO_DURATION; 357 } 358 359 public static int readPreferredCameraId(SharedPreferences pref) { 360 return Integer.parseInt(pref.getString(KEY_CAMERA_ID, "0")); 361 } 362 363 public static void writePreferredCameraId(SharedPreferences pref, 364 int cameraId) { 365 Editor editor = pref.edit(); 366 editor.putString(KEY_CAMERA_ID, Integer.toString(cameraId)); 367 editor.apply(); 368 } 369 370 public static int readExposure(ComboPreferences preferences) { 371 String exposure = preferences.getString( 372 CameraSettings.KEY_EXPOSURE, 373 EXPOSURE_DEFAULT_VALUE); 374 try { 375 return Integer.parseInt(exposure); 376 } catch (Exception ex) { 377 Log.e(TAG, "Invalid exposure: " + exposure); 378 } 379 return 0; 380 } 381 382 public static void restorePreferences(Context context, 383 ComboPreferences preferences, Parameters parameters) { 384 int currentCameraId = readPreferredCameraId(preferences); 385 386 // Clear the preferences of both cameras. 387 int backCameraId = CameraHolder.instance().getBackCameraId(); 388 if (backCameraId != -1) { 389 preferences.setLocalId(context, backCameraId); 390 Editor editor = preferences.edit(); 391 editor.clear(); 392 editor.apply(); 393 } 394 int frontCameraId = CameraHolder.instance().getFrontCameraId(); 395 if (frontCameraId != -1) { 396 preferences.setLocalId(context, frontCameraId); 397 Editor editor = preferences.edit(); 398 editor.clear(); 399 editor.apply(); 400 } 401 402 // Switch back to the preferences of the current camera. Otherwise, 403 // we may write the preference to wrong camera later. 404 preferences.setLocalId(context, currentCameraId); 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 int mmsVideoDuration = CamcorderProfile.get(mCameraId, 421 CamcorderProfile.QUALITY_LOW).duration; 422 // We need to fill in the device-dependent value (in seconds). 423 for (int i = 0; i < entries.length; ++i) { 424 if (mContext.getString(R.string.pref_video_quality_mms).equals(values[i])) { 425 entries[i] = entries[i].toString().replace( 426 "30", Integer.toString(mmsVideoDuration)); 427 break; 428 } 429 } 430 } else { 431 // The device does not support mms. Remove it. Using 432 // filterUnsupported is not efficient but adding a new method 433 // for remove is not worth it. 434 ArrayList<String> supported = new ArrayList<String>(); 435 for (int i = 0; i < entries.length; ++i) { 436 if (!mContext.getString(R.string.pref_video_quality_mms).equals(values[i])) { 437 supported.add(values[i].toString()); 438 } 439 } 440 videoQuality.filterUnsupported(supported); 441 } 442 } 443} 444