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