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