CameraSettings.java revision 9b93bcbe3ccc49db5ce42de19fda1d3a9ef0011b
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 boolean DEFAULT_VIDEO_QUALITY_VALUE = true;
50
51    // MMS video length
52    public static final int DEFAULT_VIDEO_DURATION_VALUE = -1;
53
54    // max mms video duration in seconds.
55    public static final int MMS_VIDEO_DURATION =
56            SystemProperties.getInt("ro.media.enc.lprof.duration", 60);
57
58    private ListPreference mVideoQuality;
59    private ListPreference mVideoDuration;
60    private ListPreference mPictureSize;
61    private ListPreference mJpegQuality;
62    private ListPreference mFocusMode;
63    private Parameters mParameters;
64
65    @Override
66    public void onCreate(Bundle icicle) {
67        super.onCreate(icicle);
68        addPreferencesFromResource(R.xml.camera_preferences);
69
70        initUI();
71    }
72
73    @Override
74    protected void onResume() {
75        super.onResume();
76
77        updateVideoQualitySummary();
78        updateVideoDurationSummary();
79        updatePictureSizeSummary();
80        updateJpegQualitySummary();
81        updateFocusModeSummary();
82    }
83
84    private ArrayList<String> sizeToStr(List<Size> sizes) {
85        if (sizes == null) return null;
86
87        ArrayList<String> sizesInString = new ArrayList<String>();
88        for (Size size : sizes) {
89            sizesInString.add("" + size.width + "x" + size.height);
90        }
91        return sizesInString;
92    }
93
94    private void initUI() {
95        mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY);
96        mVideoDuration = (ListPreference) findPreference(KEY_VIDEO_DURATION);
97        mPictureSize = (ListPreference) findPreference(KEY_PICTURE_SIZE);
98        mJpegQuality = (ListPreference) findPreference(KEY_JPEG_QUALITY);
99        mFocusMode = (ListPreference) findPreference(KEY_FOCUS_MODE);
100        SharedPreferences pref = getPreferenceScreen().getSharedPreferences();
101        upgradePreferences(pref);
102        pref.registerOnSharedPreferenceChangeListener(this);
103
104        // Get parameters.
105        android.hardware.Camera device;
106        try {
107            device = CameraHolder.instance().open();
108        } catch (CameraHardwareException e) {
109            Resources ress = getResources();
110            Util.showFatalErrorAndFinish(this,
111                    ress.getString(R.string.camera_error_title),
112                    ress.getString(R.string.cannot_connect_camera));
113            return;
114        }
115        mParameters = device.getParameters();
116        CameraHolder.instance().release();
117
118        // Create picture size settings.
119        List<Size> pictureSizes = mParameters.getSupportedPictureSizes();
120        ArrayList<String> pictureSizesInString = sizeToStr(pictureSizes);
121        createSettings(mPictureSize, pictureSizesInString);
122
123        // Modify video duration settings.
124        // The first entry is for MMS video duration, and we need to fill in the
125        // device-dependent value (in seconds).
126        CharSequence[] entries = mVideoDuration.getEntries();
127        entries[0] = String.format(entries[0].toString(), MMS_VIDEO_DURATION);
128
129        // Set default JPEG quality value if it is empty.
130        if (mJpegQuality.getValue() == null) {
131            mJpegQuality.setValue(getString(
132                R.string.pref_camera_jpegquality_default));
133        }
134
135        // Set default focus mode value if it is empty.
136        if (mFocusMode.getValue() == null) {
137            mFocusMode.setValue(getString(
138                R.string.pref_camera_focusmode_default));
139        }
140    }
141
142    private boolean removePreference(PreferenceGroup group, Preference remove) {
143        if (group.removePreference(remove)) return true;
144
145        for (int i = 0; i < group.getPreferenceCount(); i++) {
146            final Preference child = group.getPreference(i);
147            if (child instanceof PreferenceGroup) {
148                if (removePreference((PreferenceGroup) child, remove)) {
149                    return true;
150                }
151            }
152        }
153        return false;
154    }
155
156    private void createSettings(ListPreference pref,
157                                List<String> supportedParam) {
158        // Remove the preference if the parameter is not supported.
159        if (supportedParam == null) {
160            removePreference(getPreferenceScreen(), pref);
161            return;
162        }
163
164        // Prepare setting entries and entry values.
165        CharSequence[] allEntries = pref.getEntries();
166        CharSequence[] allEntryValues = pref.getEntryValues();
167        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
168        ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
169        for (int i = 0, len = allEntryValues.length; i < len; i++) {
170            int found = supportedParam.indexOf(allEntryValues[i]);
171            if (found != -1) {
172                entries.add(allEntries[i]);
173                entryValues.add(allEntryValues[i]);
174            }
175        }
176
177        // Set entries and entry values to list preference.
178        pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
179        pref.setEntryValues(entryValues.toArray(
180                new CharSequence[entryValues.size()]));
181
182        // Set the value to the first entry if it is invalid.
183        String value = pref.getValue();
184        int index = pref.findIndexOfValue(value);
185        if (index == -1) {
186            pref.setValueIndex(0);
187        }
188    }
189
190    private void updateVideoQualitySummary() {
191        mVideoQuality.setSummary(mVideoQuality.getEntry());
192    }
193
194    private void updateVideoDurationSummary() {
195        mVideoDuration.setSummary(mVideoDuration.getEntry());
196    }
197
198    private void updatePictureSizeSummary() {
199        mPictureSize.setSummary(mPictureSize.getEntry());
200    }
201
202    private void updateJpegQualitySummary() {
203        mJpegQuality.setSummary(mJpegQuality.getEntry());
204    }
205
206    private void updateFocusModeSummary() {
207        mFocusMode.setSummary(mFocusMode.getEntry());
208    }
209
210    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
211            String key) {
212        if (key.equals(KEY_VIDEO_QUALITY)) {
213            updateVideoQualitySummary();
214        } else if (key.equals(KEY_VIDEO_DURATION)) {
215            updateVideoDurationSummary();
216        } else if (key.equals(KEY_PICTURE_SIZE)) {
217            updatePictureSizeSummary();
218        } else if (key.equals(KEY_JPEG_QUALITY)) {
219            updateJpegQualitySummary();
220        } else if (key.equals(KEY_FOCUS_MODE)) {
221            updateFocusModeSummary();
222        }
223    }
224
225    private static final String TAG = "CameraSettings";
226    public static void upgradePreferences(SharedPreferences pref) {
227        int version;
228        try {
229            version = pref.getInt(KEY_VERSION, 0);
230        } catch (Exception ex) {
231            version = 0;
232        }
233
234        if (version == 0) {
235            SharedPreferences.Editor editor = pref.edit();
236            // For old version, change 1 to -1 for video duration preference.
237            if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) {
238                editor.putString(KEY_VIDEO_DURATION, "-1");
239            }
240            editor.putInt(KEY_VERSION, CURRENT_VERSION);
241            editor.commit();
242        }
243    }
244}
245