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