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