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