CameraSettings.java revision 5f6484a74fe019337e436e6e0dcb07375a74af25
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera; 18 19import android.content.SharedPreferences; 20import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 21import android.content.res.Resources; 22import android.hardware.Camera.Parameters; 23import android.hardware.Camera.Size; 24import android.os.Bundle; 25import android.os.SystemProperties; 26import android.preference.ListPreference; 27import android.preference.Preference; 28import android.preference.PreferenceActivity; 29import android.preference.PreferenceGroup; 30 31import java.util.ArrayList; 32import java.util.List; 33 34/** 35 * CameraSettings 36 */ 37public class CameraSettings extends PreferenceActivity implements 38 OnSharedPreferenceChangeListener { 39 public static final String KEY_VIDEO_QUALITY = 40 "pref_camera_videoquality_key"; 41 public static final String KEY_VIDEO_DURATION = 42 "pref_camera_video_duration_key"; 43 public static final String KEY_VERSION = "pref_version_key"; 44 public static final int CURRENT_VERSION = 1; 45 public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; 46 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; 47 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; 48 public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key"; 49 public static final String KEY_WHITE_BALANCE = 50 "pref_camera_whitebalance_key"; 51 public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key"; 52 public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; 53 54 // MMS video length 55 public static final int DEFAULT_VIDEO_DURATION_VALUE = -1; 56 57 // max mms video duration in seconds. 58 public static final int MMS_VIDEO_DURATION = 59 SystemProperties.getInt("ro.media.enc.lprof.duration", 60); 60 61 private ListPreference mVideoQuality; 62 private ListPreference mVideoDuration; 63 private ListPreference mPictureSize; 64 private ListPreference mJpegQuality; 65 private ListPreference mFocusMode; 66 private ListPreference mWhiteBalance; 67 private ListPreference mColorEffect; 68 private Parameters mParameters; 69 70 @Override 71 public void onCreate(Bundle icicle) { 72 super.onCreate(icicle); 73 addPreferencesFromResource(R.xml.camera_preferences); 74 75 initUI(); 76 } 77 78 @Override 79 protected void onResume() { 80 super.onResume(); 81 82 updateVideoQualitySummary(); 83 updateVideoDurationSummary(); 84 updatePictureSizeSummary(); 85 updateJpegQualitySummary(); 86 updateFocusModeSummary(); 87 updateWhiteBalanceSummary(); 88 updateEffectSummary(); 89 } 90 91 private ArrayList<String> sizeToStr(List<Size> sizes) { 92 if (sizes == null) return null; 93 94 ArrayList<String> sizesInString = new ArrayList<String>(); 95 for (Size size : sizes) { 96 sizesInString.add("" + size.width + "x" + size.height); 97 } 98 return sizesInString; 99 } 100 101 private void initUI() { 102 mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY); 103 mVideoDuration = (ListPreference) findPreference(KEY_VIDEO_DURATION); 104 mPictureSize = (ListPreference) findPreference(KEY_PICTURE_SIZE); 105 mJpegQuality = (ListPreference) findPreference(KEY_JPEG_QUALITY); 106 mFocusMode = (ListPreference) findPreference(KEY_FOCUS_MODE); 107 mWhiteBalance = (ListPreference) findPreference(KEY_WHITE_BALANCE); 108 mColorEffect = (ListPreference) findPreference(KEY_COLOR_EFFECT); 109 110 SharedPreferences pref = getPreferenceScreen().getSharedPreferences(); 111 upgradePreferences(pref); 112 pref.registerOnSharedPreferenceChangeListener(this); 113 114 // Get parameters. 115 android.hardware.Camera device; 116 try { 117 device = CameraHolder.instance().open(); 118 } catch (CameraHardwareException e) { 119 Resources ress = getResources(); 120 Util.showFatalErrorAndFinish(this, 121 ress.getString(R.string.camera_error_title), 122 ress.getString(R.string.cannot_connect_camera)); 123 return; 124 } 125 mParameters = device.getParameters(); 126 CameraHolder.instance().release(); 127 128 // Create picture size settings. 129 List<Size> pictureSizes = mParameters.getSupportedPictureSizes(); 130 ArrayList<String> pictureSizesInString = sizeToStr(pictureSizes); 131 createSettings(mPictureSize, pictureSizesInString); 132 133 // Create white balance settings. 134 createSettings(mWhiteBalance, mParameters.getSupportedWhiteBalance()); 135 136 // Create color effect settings. 137 createSettings(mColorEffect, mParameters.getSupportedColorEffects()); 138 139 // Modify video duration settings. 140 // The first entry is for MMS video duration, and we need to fill in the 141 // device-dependent value (in seconds). 142 CharSequence[] entries = mVideoDuration.getEntries(); 143 entries[0] = String.format(entries[0].toString(), MMS_VIDEO_DURATION); 144 145 // Set default JPEG quality value if it is empty. 146 if (mJpegQuality.getValue() == null) { 147 mJpegQuality.setValue(getString( 148 R.string.pref_camera_jpegquality_default)); 149 } 150 151 // Set default focus mode value if it is empty. 152 if (mFocusMode.getValue() == null) { 153 mFocusMode.setValue(getString( 154 R.string.pref_camera_focusmode_default)); 155 } 156 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 157 } 158 159 private boolean removePreference(PreferenceGroup group, Preference remove) { 160 if (group.removePreference(remove)) return true; 161 162 for (int i = 0; i < group.getPreferenceCount(); i++) { 163 final Preference child = group.getPreference(i); 164 if (child instanceof PreferenceGroup) { 165 if (removePreference((PreferenceGroup) child, remove)) { 166 return true; 167 } 168 } 169 } 170 return false; 171 } 172 173 private void createSettings( 174 ListPreference pref, List<String> supportedParam) { 175 // Remove the preference if the parameter is not supported. 176 if (supportedParam == null) { 177 removePreference(getPreferenceScreen(), pref); 178 return; 179 } 180 181 // Prepare setting entries and entry values. 182 CharSequence[] allEntries = pref.getEntries(); 183 CharSequence[] allEntryValues = pref.getEntryValues(); 184 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 185 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 186 for (int i = 0, len = allEntryValues.length; i < len; i++) { 187 int found = supportedParam.indexOf(allEntryValues[i]); 188 if (found != -1) { 189 entries.add(allEntries[i]); 190 entryValues.add(allEntryValues[i]); 191 } 192 } 193 194 // Set entries and entry values to list preference. 195 pref.setEntries(entries.toArray(new CharSequence[entries.size()])); 196 pref.setEntryValues(entryValues.toArray( 197 new CharSequence[entryValues.size()])); 198 199 // Set the value to the first entry if it is invalid. 200 String value = pref.getValue(); 201 int index = pref.findIndexOfValue(value); 202 if (index == -1) { 203 pref.setValueIndex(0); 204 } 205 } 206 207 private void updateVideoQualitySummary() { 208 mVideoQuality.setSummary(mVideoQuality.getEntry()); 209 } 210 211 private void updateVideoDurationSummary() { 212 mVideoDuration.setSummary(mVideoDuration.getEntry()); 213 } 214 215 private void updatePictureSizeSummary() { 216 mPictureSize.setSummary(mPictureSize.getEntry()); 217 } 218 219 private void updateJpegQualitySummary() { 220 mJpegQuality.setSummary(mJpegQuality.getEntry()); 221 } 222 223 private void updateWhiteBalanceSummary() { 224 mWhiteBalance.setSummary(mWhiteBalance.getEntry()); 225 } 226 227 private void updateFocusModeSummary() { 228 mFocusMode.setSummary(mFocusMode.getEntry()); 229 } 230 231 private void updateEffectSummary() { 232 mColorEffect.setSummary(mColorEffect.getEntry()); 233 } 234 235 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 236 String key) { 237 if (key.equals(KEY_VIDEO_QUALITY)) { 238 updateVideoQualitySummary(); 239 } else if (key.equals(KEY_VIDEO_DURATION)) { 240 updateVideoDurationSummary(); 241 } else if (key.equals(KEY_PICTURE_SIZE)) { 242 updatePictureSizeSummary(); 243 } else if (key.equals(KEY_JPEG_QUALITY)) { 244 updateJpegQualitySummary(); 245 } else if (key.equals(KEY_FOCUS_MODE)) { 246 updateFocusModeSummary(); 247 } else if (key.equals(KEY_WHITE_BALANCE)) { 248 updateWhiteBalanceSummary(); 249 } else if (key.equals(KEY_COLOR_EFFECT)) { 250 updateEffectSummary(); 251 } 252 } 253 254 public static void upgradePreferences(SharedPreferences pref) { 255 int version; 256 try { 257 version = pref.getInt(KEY_VERSION, 0); 258 } catch (Exception ex) { 259 version = 0; 260 } 261 262 if (version == 0) { 263 SharedPreferences.Editor editor = pref.edit(); 264 // For old version, change 1 to -1 for video duration preference. 265 if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) { 266 editor.putString(KEY_VIDEO_DURATION, "-1"); 267 } 268 editor.putInt(KEY_VERSION, CURRENT_VERSION); 269 editor.commit(); 270 } 271 } 272} 273