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