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