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