CameraSettings.java revision 723f84eadb3c1a79337ef20e4d0a014341537adb
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 private void initPreference(PreferenceScreen screen) { 71 ListPreference videoDuration = 72 (ListPreference) screen.findPreference(KEY_VIDEO_DURATION); 73 ListPreference pictureSize = 74 (ListPreference) screen.findPreference(KEY_PICTURE_SIZE); 75 ListPreference whiteBalance = 76 (ListPreference) screen.findPreference(KEY_WHITE_BALANCE); 77 ListPreference colorEffect = 78 (ListPreference) screen.findPreference(KEY_COLOR_EFFECT); 79 ListPreference sceneMode = 80 (ListPreference) screen.findPreference(KEY_SCENE_MODE); 81 ListPreference flashMode = 82 (ListPreference) screen.findPreference(KEY_FLASH_MODE); 83 84 // Since the screen could be loaded from different resources, we need 85 // to check if the preference is available here 86 if (videoDuration != null) { 87 // Modify video duration settings. 88 // The first entry is for MMS video duration, and we need to fill 89 // in the device-dependent value (in seconds). 90 CharSequence[] entries = videoDuration.getEntries(); 91 entries[0] = String.format( 92 entries[0].toString(), MMS_VIDEO_DURATION); 93 } 94 95 // Filter out unsupported settings / options 96 if (pictureSize != null) { 97 filterUnsupportedOptions(screen, pictureSize, sizeListToStringList( 98 mParameters.getSupportedPictureSizes())); 99 } 100 if (whiteBalance != null) { 101 filterUnsupportedOptions(screen, 102 whiteBalance, mParameters.getSupportedWhiteBalance()); 103 } 104 if (colorEffect != null) { 105 filterUnsupportedOptions(screen, 106 colorEffect, mParameters.getSupportedColorEffects()); 107 } 108 if (sceneMode != null) { 109 filterUnsupportedOptions(screen, 110 sceneMode, mParameters.getSupportedSceneModes()); 111 } 112 if (flashMode != null) { 113 filterUnsupportedOptions(screen, 114 flashMode, mParameters.getSupportedFlashModes()); 115 } 116 } 117 118 private boolean removePreference(PreferenceGroup group, Preference remove) { 119 if (group.removePreference(remove)) return true; 120 121 for (int i = 0; i < group.getPreferenceCount(); i++) { 122 final Preference child = group.getPreference(i); 123 if (child instanceof PreferenceGroup) { 124 if (removePreference((PreferenceGroup) child, remove)) { 125 return true; 126 } 127 } 128 } 129 return false; 130 } 131 132 private void filterUnsupportedOptions(PreferenceScreen screen, 133 ListPreference pref, List<String> supported) { 134 135 // Remove the preference if the parameter is not supported. 136 if (supported == null) { 137 removePreference(screen, pref); 138 return; 139 } 140 141 // Prepare setting entries and entry values. 142 CharSequence[] allEntries = pref.getEntries(); 143 CharSequence[] allEntryValues = pref.getEntryValues(); 144 Drawable[] allIcons = (pref instanceof IconListPreference) 145 ? ((IconListPreference) pref).getIcons() 146 : null; 147 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 148 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 149 ArrayList<Drawable> icons = 150 allIcons == null ? null : new ArrayList<Drawable>(); 151 for (int i = 0, len = allEntryValues.length; i < len; i++) { 152 if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) { 153 entries.add(allEntries[i]); 154 entryValues.add(allEntryValues[i]); 155 if (allIcons != null) icons.add(allIcons[i]); 156 } 157 } 158 159 // Set entries and entry values to list preference. 160 int size = entries.size(); 161 pref.setEntries(entries.toArray(new CharSequence[size])); 162 pref.setEntryValues(entryValues.toArray(new CharSequence[size])); 163 if (allIcons != null) { 164 ((IconListPreference) pref) 165 .setIcons(icons.toArray(new Drawable[size])); 166 } 167 168 // Set the value to the first entry if it is invalid. 169 String value = pref.getValue(); 170 if (pref.findIndexOfValue(value) == NOT_FOUND) { 171 pref.setValueIndex(0); 172 } 173 } 174 175 private static List<String> sizeListToStringList(List<Size> sizes) { 176 ArrayList<String> list = new ArrayList<String>(); 177 for (Size size : sizes) { 178 list.add(String.format("%dx%d", size.width, size.height)); 179 } 180 return list; 181 } 182 183 public static void upgradePreferences(SharedPreferences pref) { 184 int version; 185 try { 186 version = pref.getInt(KEY_VERSION, 0); 187 } catch (Exception ex) { 188 version = 0; 189 } 190 191 if (version == 0) { 192 SharedPreferences.Editor editor = pref.edit(); 193 // For old version, change 1 to -1 for video duration preference. 194 if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { 195 editor.putString(KEY_VIDEO_DURATION, "-1"); 196 } 197 editor.putInt(KEY_VERSION, CURRENT_VERSION); 198 editor.commit(); 199 } 200 } 201} 202