CameraSettings.java revision a8bbcd7ce25aada705561852ea9f356dc38c0f43
1package com.android.camera; 2 3import android.app.Activity; 4import android.content.Context; 5import android.content.SharedPreferences; 6import android.graphics.drawable.Drawable; 7import android.hardware.Camera.Parameters; 8import android.hardware.Camera.Size; 9import android.os.SystemProperties; 10import android.preference.ListPreference; 11import android.preference.Preference; 12import android.preference.PreferenceGroup; 13import android.preference.PreferenceManager; 14import android.preference.PreferenceScreen; 15 16import java.util.ArrayList; 17import java.util.List; 18 19public class CameraSettings { 20 private static final int FIRST_REQUEST_CODE = 100; 21 private static final int NOT_FOUND = -1; 22 23 public static final String KEY_VERSION = "pref_version_key"; 24 public static final String KEY_RECORD_LOCATION = 25 "pref_camera_recordlocation_key"; 26 public static final String KEY_VIDEO_QUALITY = 27 "pref_camera_videoquality_key"; 28 public static final String KEY_VIDEO_DURATION = 29 "pref_camera_video_duration_key"; 30 public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; 31 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; 32 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; 33 public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key"; 34 public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key"; 35 public static final String KEY_WHITE_BALANCE = 36 "pref_camera_whitebalance_key"; 37 public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key"; 38 39 public static final int CURRENT_VERSION = 1; 40 41 // max mms video duration in seconds. 42 public static final int MMS_VIDEO_DURATION = 43 SystemProperties.getInt("ro.media.enc.lprof.duration", 60); 44 45 public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; 46 47 // MMS video length 48 public static final int DEFAULT_VIDEO_DURATION_VALUE = -1; 49 50 @SuppressWarnings("unused") 51 private static final String TAG = "CameraSettings"; 52 53 private final Context mContext; 54 private final Parameters mParameters; 55 private final PreferenceManager mManager; 56 57 public CameraSettings(Activity activity, Parameters parameters) { 58 mContext = activity; 59 mParameters = parameters; 60 mManager = new PreferenceManager(activity, FIRST_REQUEST_CODE); 61 } 62 63 public PreferenceScreen getPreferenceScreen(int preferenceRes) { 64 PreferenceScreen screen = mManager.createPreferenceScreen(mContext); 65 mManager.inflateFromResource(mContext, preferenceRes, screen); 66 initPreference(screen); 67 return screen; 68 } 69 70 public static void initialCameraPictureSize( 71 Context context, Parameters parameters) { 72 // When launching the camera app first time, we will set the picture 73 // size to the first one in the list defined in "arrays.xml" and is also 74 // supported by the driver. 75 List<Size> supported = parameters.getSupportedPictureSizes(); 76 if (supported == null) return; 77 for (String candidate : context.getResources().getStringArray( 78 R.array.pref_camera_picturesize_entryvalues)) { 79 if (setCameraPictureSize(candidate, supported, parameters)) { 80 SharedPreferences.Editor editor = PreferenceManager 81 .getDefaultSharedPreferences(context).edit(); 82 editor.putString(KEY_PICTURE_SIZE, candidate); 83 editor.commit(); 84 return; 85 } 86 } 87 } 88 89 public static boolean setCameraPictureSize( 90 String candidate, List<Size> supported, Parameters parameters) { 91 int index = candidate.indexOf('x'); 92 if (index == NOT_FOUND) return false; 93 int width = Integer.parseInt(candidate.substring(0, index)); 94 int height = Integer.parseInt(candidate.substring(index + 1)); 95 for (Size size: supported) { 96 if (size.width == width && size.height == height) { 97 parameters.setPictureSize(width, height); 98 return true; 99 } 100 } 101 return false; 102 } 103 104 private void initPreference(PreferenceScreen screen) { 105 ListPreference videoDuration = 106 (ListPreference) screen.findPreference(KEY_VIDEO_DURATION); 107 ListPreference pictureSize = 108 (ListPreference) screen.findPreference(KEY_PICTURE_SIZE); 109 ListPreference whiteBalance = 110 (ListPreference) screen.findPreference(KEY_WHITE_BALANCE); 111 ListPreference colorEffect = 112 (ListPreference) screen.findPreference(KEY_COLOR_EFFECT); 113 ListPreference sceneMode = 114 (ListPreference) screen.findPreference(KEY_SCENE_MODE); 115 ListPreference flashMode = 116 (ListPreference) screen.findPreference(KEY_FLASH_MODE); 117 118 // Since the screen could be loaded from different resources, we need 119 // to check if the preference is available here 120 if (videoDuration != null) { 121 // Modify video duration settings. 122 // The first entry is for MMS video duration, and we need to fill 123 // in the device-dependent value (in seconds). 124 CharSequence[] entries = videoDuration.getEntries(); 125 entries[0] = String.format( 126 entries[0].toString(), MMS_VIDEO_DURATION); 127 } 128 129 // Filter out unsupported settings / options 130 if (pictureSize != null) { 131 filterUnsupportedOptions(screen, pictureSize, sizeListToStringList( 132 mParameters.getSupportedPictureSizes())); 133 } 134 if (whiteBalance != null) { 135 filterUnsupportedOptions(screen, 136 whiteBalance, mParameters.getSupportedWhiteBalance()); 137 } 138 if (colorEffect != null) { 139 filterUnsupportedOptions(screen, 140 colorEffect, mParameters.getSupportedColorEffects()); 141 } 142 if (sceneMode != null) { 143 filterUnsupportedOptions(screen, 144 sceneMode, mParameters.getSupportedSceneModes()); 145 } 146 if (flashMode != null) { 147 filterUnsupportedOptions(screen, 148 flashMode, mParameters.getSupportedFlashModes()); 149 } 150 } 151 152 private boolean removePreference(PreferenceGroup group, Preference remove) { 153 if (group.removePreference(remove)) return true; 154 155 for (int i = 0; i < group.getPreferenceCount(); i++) { 156 final Preference child = group.getPreference(i); 157 if (child instanceof PreferenceGroup) { 158 if (removePreference((PreferenceGroup) child, remove)) { 159 return true; 160 } 161 } 162 } 163 return false; 164 } 165 166 private void filterUnsupportedOptions(PreferenceScreen screen, 167 ListPreference pref, List<String> supported) { 168 169 // Remove the preference if the parameter is not supported. 170 if (supported == null) { 171 removePreference(screen, pref); 172 return; 173 } 174 175 // Prepare setting entries and entry values. 176 CharSequence[] allEntries = pref.getEntries(); 177 CharSequence[] allEntryValues = pref.getEntryValues(); 178 Drawable[] allIcons = (pref instanceof IconListPreference) 179 ? ((IconListPreference) pref).getIcons() 180 : null; 181 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 182 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 183 ArrayList<Drawable> icons = 184 allIcons == null ? null : new ArrayList<Drawable>(); 185 for (int i = 0, len = allEntryValues.length; i < len; i++) { 186 if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) { 187 entries.add(allEntries[i]); 188 entryValues.add(allEntryValues[i]); 189 if (allIcons != null) icons.add(allIcons[i]); 190 } 191 } 192 193 // Set entries and entry values to list preference. 194 int size = entries.size(); 195 pref.setEntries(entries.toArray(new CharSequence[size])); 196 pref.setEntryValues(entryValues.toArray(new CharSequence[size])); 197 if (allIcons != null) { 198 ((IconListPreference) pref) 199 .setIcons(icons.toArray(new Drawable[size])); 200 } 201 202 // Set the value to the first entry if it is invalid. 203 String value = pref.getValue(); 204 if (pref.findIndexOfValue(value) == NOT_FOUND) { 205 pref.setValueIndex(0); 206 } 207 } 208 209 private static List<String> sizeListToStringList(List<Size> sizes) { 210 ArrayList<String> list = new ArrayList<String>(); 211 for (Size size : sizes) { 212 list.add(String.format("%dx%d", size.width, size.height)); 213 } 214 return list; 215 } 216 217 public static void upgradePreferences(SharedPreferences pref) { 218 int version; 219 try { 220 version = pref.getInt(KEY_VERSION, 0); 221 } catch (Exception ex) { 222 version = 0; 223 } 224 if (version == CURRENT_VERSION) return; 225 226 SharedPreferences.Editor editor = pref.edit(); 227 if (version == 0) { 228 // For old version, change 1 to -1 for video duration preference. 229 if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { 230 editor.putString(KEY_VIDEO_DURATION, "-1"); 231 } 232 } 233 editor.putInt(KEY_VERSION, CURRENT_VERSION); 234 editor.commit(); 235 } 236} 237