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