CameraSettings.java revision 0a475e11e2ceadbaa70308b52e06f4e6d9e56f12
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.hardware.Camera.Parameters; 22import android.os.Bundle; 23import android.preference.ListPreference; 24import android.preference.PreferenceActivity; 25 26import java.util.ArrayList; 27import java.util.StringTokenizer; 28 29/** 30 * CameraSettings 31 */ 32public class CameraSettings extends PreferenceActivity implements 33 OnSharedPreferenceChangeListener { 34 public static final String KEY_VIDEO_QUALITY = 35 "pref_camera_videoquality_key"; 36 public static final String KEY_WHITE_BALANCE = 37 "pref_camera_whitebalance_key"; 38 public static final String KEY_EFFECT = "pref_camera_effect_key"; 39 public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key"; 40 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key"; 41 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key"; 42 public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true; 43 44 private ListPreference mVideoQuality; 45 private ListPreference mWhiteBalance; 46 private ListPreference mEffect; 47 private ListPreference mPictureSize; 48 private ListPreference mJpegQuality; 49 private ListPreference mFocusMode; 50 private Parameters mParameters; 51 52 @Override 53 public void onCreate(Bundle icicle) { 54 super.onCreate(icicle); 55 addPreferencesFromResource(R.xml.camera_preferences); 56 57 initUI(); 58 } 59 60 @Override 61 protected void onResume() { 62 super.onResume(); 63 64 updateVideoQualitySummary(); 65 updateWhiteBalanceSummary(); 66 updateEffectSummary(); 67 updatePictureSizeSummary(); 68 updateJpegQualitySummary(); 69 updateFocusModeSummary(); 70 } 71 72 private void initUI() { 73 mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY); 74 mWhiteBalance = (ListPreference) findPreference(KEY_WHITE_BALANCE); 75 mEffect = (ListPreference) findPreference(KEY_EFFECT); 76 mPictureSize = (ListPreference) findPreference(KEY_PICTURE_SIZE); 77 mJpegQuality = (ListPreference) findPreference(KEY_JPEG_QUALITY); 78 mFocusMode = (ListPreference) findPreference(KEY_FOCUS_MODE); 79 getPreferenceScreen().getSharedPreferences(). 80 registerOnSharedPreferenceChangeListener(this); 81 82 // Get parameters. 83 android.hardware.Camera device = android.hardware.Camera.open(); 84 mParameters = device.getParameters(); 85 device.release(); 86 87 // Create white balance settings. 88 createSettings(mWhiteBalance, Camera.SUPPORTED_WHITE_BALANCE, 89 R.array.pref_camera_whitebalance_entries, 90 R.array.pref_camera_whitebalance_entryvalues); 91 92 // Create effect settings. 93 createSettings(mEffect, Camera.SUPPORTED_EFFECT, 94 R.array.pref_camera_effect_entries, 95 R.array.pref_camera_effect_entryvalues); 96 97 // Create picture size settings. 98 createSettings(mPictureSize, Camera.SUPPORTED_PICTURE_SIZE, 99 R.array.pref_camera_picturesize_entries, 100 R.array.pref_camera_picturesize_entryvalues); 101 102 // Set default JPEG quality value if it is empty. 103 if (mJpegQuality.getValue() == null) { 104 mJpegQuality.setValue(getString( 105 R.string.pref_camera_jpegquality_default)); 106 } 107 108 // Set default focus mode value if it is empty. 109 if (mFocusMode.getValue() == null) { 110 mFocusMode.setValue(getString( 111 R.string.pref_camera_focusmode_default)); 112 } 113 } 114 115 private void createSettings( 116 ListPreference pref, String paramName, int prefEntriesResId, 117 int prefEntryValuesResId) { 118 // Disable the preference if the parameter is not supported. 119 String supportedParamStr = mParameters.get(paramName); 120 if (supportedParamStr == null) { 121 pref.setEnabled(false); 122 return; 123 } 124 125 // Get the supported parameter settings. 126 StringTokenizer tokenizer = new StringTokenizer(supportedParamStr, ","); 127 ArrayList<CharSequence> supportedParam = new ArrayList<CharSequence>(); 128 while (tokenizer.hasMoreElements()) { 129 supportedParam.add(tokenizer.nextToken()); 130 } 131 132 // Prepare setting entries and entry values. 133 String[] allEntries = getResources().getStringArray(prefEntriesResId); 134 String[] allEntryValues = getResources().getStringArray( 135 prefEntryValuesResId); 136 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); 137 ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>(); 138 for (int i = 0, len = allEntryValues.length; i < len; i++) { 139 int found = supportedParam.indexOf(allEntryValues[i]); 140 if (found != -1) { 141 entries.add(allEntries[i]); 142 entryValues.add(allEntryValues[i]); 143 } 144 } 145 146 // Set entries and entry values to list preference. 147 pref.setEntries(entries.toArray(new CharSequence[entries.size()])); 148 pref.setEntryValues(entryValues.toArray( 149 new CharSequence[entryValues.size()])); 150 151 // Set the value to the first entry if it is invalid. 152 String value = pref.getValue(); 153 int index = pref.findIndexOfValue(value); 154 if (index == -1) { 155 pref.setValueIndex(0); 156 } 157 } 158 159 private void updateVideoQualitySummary() { 160 mVideoQuality.setSummary(mVideoQuality.getEntry()); 161 } 162 163 private void updateWhiteBalanceSummary() { 164 mWhiteBalance.setSummary(mWhiteBalance.getEntry()); 165 } 166 167 private void updateEffectSummary() { 168 mEffect.setSummary(mEffect.getEntry()); 169 } 170 171 private void updatePictureSizeSummary() { 172 mPictureSize.setSummary(mPictureSize.getEntry()); 173 } 174 175 private void updateJpegQualitySummary() { 176 mJpegQuality.setSummary(mJpegQuality.getEntry()); 177 } 178 179 private void updateFocusModeSummary() { 180 mFocusMode.setSummary(mFocusMode.getEntry()); 181 } 182 183 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 184 String key) { 185 if (key.equals(KEY_VIDEO_QUALITY)) { 186 updateVideoQualitySummary(); 187 } else if (key.equals(KEY_WHITE_BALANCE)) { 188 updateWhiteBalanceSummary(); 189 } else if (key.equals(KEY_EFFECT)) { 190 updateEffectSummary(); 191 } else if (key.equals(KEY_PICTURE_SIZE)) { 192 updatePictureSizeSummary(); 193 } else if (key.equals(KEY_JPEG_QUALITY)) { 194 updateJpegQualitySummary(); 195 } else if (key.equals(KEY_FOCUS_MODE)) { 196 updateFocusModeSummary(); 197 } 198 } 199} 200