CameraSettings.java revision 6d12b828743cbd81767227229d6351ce412d5698
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 String VALUE_FOCUS_INFINITY = "infinity"; 39 public static final String VALUE_FOCUS_AUTO = "auto"; 40 41 public static final int CURRENT_VERSION = 1; 42 43 // max mms video duration in seconds. 44 public static final int MMS_VIDEO_DURATION = 45 SystemProperties.getInt("ro.media.enc.lprof.duration", 60); 46 47 public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; 48 49 // MMS video length 50 public static final int DEFAULT_VIDEO_DURATION_VALUE = -1; 51 52 private static final String TAG = "CameraSettings"; 53 54 private final Context mContext; 55 private final Parameters mParameters; 56 private final PreferenceManager mManager; 57 58 public CameraSettings(Activity activity, Parameters parameters) { 59 mContext = activity; 60 mParameters = parameters; 61 mManager = new PreferenceManager(activity, FIRST_REQUEST_CODE); 62 } 63 64 public PreferenceScreen getPreferenceScreen(int preferenceRes) { 65 PreferenceScreen screen = mManager.createPreferenceScreen(mContext); 66 mManager.inflateFromResource(mContext, preferenceRes, screen); 67 initPreference(screen); 68 return screen; 69 } 70 71 private void initPreference(PreferenceScreen screen) { 72 ListPreference videoDuration = 73 (ListPreference) screen.findPreference(KEY_VIDEO_DURATION); 74 ListPreference pictureSize = 75 (ListPreference) screen.findPreference(KEY_PICTURE_SIZE); 76 ListPreference whiteBalance = 77 (ListPreference) screen.findPreference(KEY_WHITE_BALANCE); 78 ListPreference colorEffect = 79 (ListPreference) screen.findPreference(KEY_COLOR_EFFECT); 80 ListPreference sceneMode = 81 (ListPreference) screen.findPreference(KEY_SCENE_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 } 112 113 private boolean removePreference(PreferenceGroup group, Preference remove) { 114 if (group.removePreference(remove)) return true; 115 116 for (int i = 0; i < group.getPreferenceCount(); i++) { 117 final Preference child = group.getPreference(i); 118 if (child instanceof PreferenceGroup) { 119 if (removePreference((PreferenceGroup) child, remove)) { 120 return true; 121 } 122 } 123 } 124 return false; 125 } 126 127 private void filterUnsupportedOptions(PreferenceScreen screen, 128 ListPreference pref, List<String> supported) { 129 130 // Remove the preference if the parameter is not supported. 131 if (supported == null) { 132 removePreference(screen, pref); 133 return; 134 } 135 136 // Prepare setting entries and entry values. 137 CharSequence[] allEntries = pref.getEntries(); 138 CharSequence[] allEntryValues = pref.getEntryValues(); 139 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 140 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 141 for (int i = 0, len = allEntryValues.length; i < len; i++) { 142 if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) { 143 entries.add(allEntries[i]); 144 entryValues.add(allEntryValues[i]); 145 } 146 } 147 148 // Set entries and entry values to list preference. 149 pref.setEntries(entries.toArray(new CharSequence[entries.size()])); 150 pref.setEntryValues(entryValues.toArray( 151 new CharSequence[entryValues.size()])); 152 153 // Set the value to the first entry if it is invalid. 154 String value = pref.getValue(); 155 if (pref.findIndexOfValue(value) == NOT_FOUND) { 156 pref.setValueIndex(0); 157 } 158 } 159 160 private static List<String> sizeListToStringList(List<Size> sizes) { 161 ArrayList<String> list = new ArrayList<String>(); 162 for (Size size : sizes) { 163 list.add(String.format("%dx%d", size.width, size.height)); 164 } 165 return list; 166 } 167 168 public static void upgradePreferences(SharedPreferences pref) { 169 int version; 170 try { 171 version = pref.getInt(KEY_VERSION, 0); 172 } catch (Exception ex) { 173 version = 0; 174 } 175 176 if (version == 0) { 177 SharedPreferences.Editor editor = pref.edit(); 178 // For old version, change 1 to -1 for video duration preference. 179 if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { 180 editor.putString(KEY_VIDEO_DURATION, "-1"); 181 } 182 editor.putInt(KEY_VERSION, CURRENT_VERSION); 183 editor.commit(); 184 } 185 } 186} 187