CameraSettings.java revision e8697d7437c343e31e815fd4714238afafc7b1ac
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.graphics.drawable.Drawable; 23import android.media.CamcorderProfile; 24import android.media.CamcorderProfile.Quality; 25import android.hardware.Camera.Parameters; 26import android.hardware.Camera.Size; 27import android.preference.PreferenceManager; 28import android.util.Log; 29 30import java.util.ArrayList; 31import java.util.List; 32 33/** 34 * Provides utilities and keys for Camera settings. 35 */ 36public class CameraSettings { 37 private static final int NOT_FOUND = -1; 38 39 public static final String KEY_VERSION = "pref_version_key"; 40 public static final String KEY_RECORD_LOCATION = 41 RecordLocationPreference.KEY; 42 public static final String KEY_VIDEO_QUALITY = 43 "pref_camera_videoquality_key"; 44 public static final String KEY_VIDEO_DURATION = 45 "pref_camera_video_duration_key"; 46 public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; 47 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; 48 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; 49 public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key"; 50 public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key"; 51 public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key"; 52 public static final String KEY_WHITE_BALANCE = 53 "pref_camera_whitebalance_key"; 54 public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key"; 55 public static final String KEY_QUICK_CAPTURE = 56 "pref_camera_quickcapture_key"; 57 58 public static final String QUICK_CAPTURE_ON = "on"; 59 public static final String QUICK_CAPTURE_OFF = "off"; 60 61 public static final int CURRENT_VERSION = 3; 62 63 // max mms video duration in seconds. 64 public static final int MMS_VIDEO_DURATION = CamcorderProfile.getMmsRecordingDurationInSeconds(); 65 66 public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; 67 68 // MMS video length 69 public static final int DEFAULT_VIDEO_DURATION_VALUE = -1; 70 71 @SuppressWarnings("unused") 72 private static final String TAG = "CameraSettings"; 73 74 private final Context mContext; 75 private final Parameters mParameters; 76 77 public CameraSettings(Activity activity, Parameters parameters) { 78 mContext = activity; 79 mParameters = parameters; 80 } 81 82 public PreferenceGroup getPreferenceGroup(int preferenceRes) { 83 PreferenceInflater inflater = new PreferenceInflater(mContext); 84 PreferenceGroup group = 85 (PreferenceGroup) inflater.inflate(preferenceRes); 86 initPreference(group); 87 return group; 88 } 89 90 public static void initialCameraPictureSize( 91 Context context, Parameters parameters) { 92 // When launching the camera app first time, we will set the picture 93 // size to the first one in the list defined in "arrays.xml" and is also 94 // supported by the driver. 95 List<Size> supported = parameters.getSupportedPictureSizes(); 96 if (supported == null) return; 97 for (String candidate : context.getResources().getStringArray( 98 R.array.pref_camera_picturesize_entryvalues)) { 99 if (setCameraPictureSize(candidate, supported, parameters)) { 100 SharedPreferences.Editor editor = PreferenceManager 101 .getDefaultSharedPreferences(context).edit(); 102 editor.putString(KEY_PICTURE_SIZE, candidate); 103 editor.commit(); 104 return; 105 } 106 } 107 Log.e(TAG, "No supported picture size found"); 108 } 109 110 public static void removePreferenceFromScreen( 111 PreferenceGroup group, String key) { 112 removePreference(group, key); 113 } 114 115 public static boolean setCameraPictureSize( 116 String candidate, List<Size> supported, Parameters parameters) { 117 int index = candidate.indexOf('x'); 118 if (index == NOT_FOUND) return false; 119 int width = Integer.parseInt(candidate.substring(0, index)); 120 int height = Integer.parseInt(candidate.substring(index + 1)); 121 for (Size size: supported) { 122 if (size.width == width && size.height == height) { 123 parameters.setPictureSize(width, height); 124 return true; 125 } 126 } 127 return false; 128 } 129 130 private void initPreference(PreferenceGroup group) { 131 ListPreference videoDuration = group.findPreference(KEY_VIDEO_DURATION); 132 ListPreference pictureSize = group.findPreference(KEY_PICTURE_SIZE); 133 ListPreference whiteBalance = group.findPreference(KEY_WHITE_BALANCE); 134 ListPreference colorEffect = group.findPreference(KEY_COLOR_EFFECT); 135 ListPreference sceneMode = group.findPreference(KEY_SCENE_MODE); 136 ListPreference flashMode = group.findPreference(KEY_FLASH_MODE); 137 ListPreference focusMode = group.findPreference(KEY_FOCUS_MODE); 138 139 // Since the screen could be loaded from different resources, we need 140 // to check if the preference is available here 141 if (videoDuration != null) { 142 // Modify video duration settings. 143 // The first entry is for MMS video duration, and we need to fill 144 // in the device-dependent value (in seconds). 145 CharSequence[] entries = videoDuration.getEntries(); 146 entries[0] = String.format( 147 entries[0].toString(), MMS_VIDEO_DURATION); 148 } 149 150 // Filter out unsupported settings / options 151 if (pictureSize != null) { 152 filterUnsupportedOptions(group, pictureSize, sizeListToStringList( 153 mParameters.getSupportedPictureSizes())); 154 } 155 if (whiteBalance != null) { 156 filterUnsupportedOptions(group, 157 whiteBalance, mParameters.getSupportedWhiteBalance()); 158 } 159 if (colorEffect != null) { 160 filterUnsupportedOptions(group, 161 colorEffect, mParameters.getSupportedColorEffects()); 162 } 163 if (sceneMode != null) { 164 filterUnsupportedOptions(group, 165 sceneMode, mParameters.getSupportedSceneModes()); 166 } 167 if (flashMode != null) { 168 filterUnsupportedOptions(group, 169 flashMode, mParameters.getSupportedFlashModes()); 170 } 171 if (focusMode != null) { 172 filterUnsupportedOptions(group, 173 focusMode, mParameters.getSupportedFocusModes()); 174 } 175 } 176 177 private static boolean removePreference(PreferenceGroup group, String key) { 178 for (int i = 0, n = group.size(); i < n; i++) { 179 CameraPreference child = group.get(i); 180 if (child instanceof PreferenceGroup) { 181 if (removePreference((PreferenceGroup) child, key)) { 182 return true; 183 } 184 } 185 if (child instanceof ListPreference && 186 ((ListPreference) child).getKey().equals(key)) { 187 group.removePreference(i); 188 return true; 189 } 190 } 191 return false; 192 } 193 194 private void filterUnsupportedOptions(PreferenceGroup group, 195 ListPreference pref, List<String> supported) { 196 197 CharSequence[] allEntries = pref.getEntries(); 198 199 // Remove the preference if the parameter is not supported or there is 200 // only one options for the settings. 201 if (supported == null || supported.size() <= 1) { 202 removePreference(group, pref.getKey()); 203 return; 204 } 205 206 CharSequence[] allEntryValues = pref.getEntryValues(); 207 Drawable[] allIcons = (pref instanceof IconListPreference) 208 ? ((IconListPreference) pref).getIcons() 209 : null; 210 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 211 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 212 ArrayList<Drawable> icons = 213 allIcons == null ? null : new ArrayList<Drawable>(); 214 for (int i = 0, len = allEntryValues.length; i < len; i++) { 215 if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) { 216 entries.add(allEntries[i]); 217 entryValues.add(allEntryValues[i]); 218 if (allIcons != null) icons.add(allIcons[i]); 219 } 220 } 221 222 // Set entries and entry values to list preference. 223 int size = entries.size(); 224 pref.setEntries(entries.toArray(new CharSequence[size])); 225 pref.setEntryValues(entryValues.toArray(new CharSequence[size])); 226 if (allIcons != null) { 227 ((IconListPreference) pref) 228 .setIcons(icons.toArray(new Drawable[size])); 229 } 230 231 // Set the value to the first entry if it is invalid. 232 String value = pref.getValue(); 233 if (pref.findIndexOfValue(value) == NOT_FOUND) { 234 pref.setValueIndex(0); 235 } 236 } 237 238 private static List<String> sizeListToStringList(List<Size> sizes) { 239 ArrayList<String> list = new ArrayList<String>(); 240 for (Size size : sizes) { 241 list.add(String.format("%dx%d", size.width, size.height)); 242 } 243 return list; 244 } 245 246 public static void upgradePreferences(SharedPreferences pref) { 247 int version; 248 try { 249 version = pref.getInt(KEY_VERSION, 0); 250 } catch (Exception ex) { 251 version = 0; 252 } 253 if (version == CURRENT_VERSION) return; 254 255 SharedPreferences.Editor editor = pref.edit(); 256 if (version == 0) { 257 // For old version, change 1 to 10 for video duration preference. 258 if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { 259 editor.putString(KEY_VIDEO_DURATION, "10"); 260 } 261 version = 1; 262 } 263 if (version == 1) { 264 // Change jpeg quality {65,75,85} to {normal,fine,superfine} 265 String quality = pref.getString(KEY_JPEG_QUALITY, "85"); 266 if (quality.equals("65")) { 267 quality = "normal"; 268 } else if (quality.equals("75")) { 269 quality = "fine"; 270 } else { 271 quality = "superfine"; 272 } 273 editor.putString(KEY_JPEG_QUALITY, quality); 274 version = 2; 275 } 276 if (version == 2) { 277 editor.putString(KEY_RECORD_LOCATION, 278 pref.getBoolean(KEY_RECORD_LOCATION, false) 279 ? RecordLocationPreference.VALUE_ON 280 : RecordLocationPreference.VALUE_NONE); 281 version = 3; 282 } 283 editor.putInt(KEY_VERSION, CURRENT_VERSION); 284 editor.commit(); 285 } 286} 287