CameraSettings.java revision 3a4ef93f95bed9f812fe75ef94296450833b3997
1/*
2 * Copyright (C) 2009 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.app.Activity;
20import android.content.Context;
21import android.content.SharedPreferences;
22import android.graphics.drawable.Drawable;
23import android.hardware.Camera.Parameters;
24import android.hardware.Camera.Size;
25import android.media.CamcorderProfile;
26import android.preference.PreferenceManager;
27import android.util.Log;
28
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 *  Provides utilities and keys for Camera settings.
34 */
35public class CameraSettings {
36    private static final int NOT_FOUND = -1;
37
38    public static final String KEY_VERSION = "pref_version_key";
39    public static final String KEY_RECORD_LOCATION = RecordLocationPreference.KEY;
40    public static final String KEY_VIDEO_QUALITY = "pref_video_quality_key";
41    public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
42    public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
43    public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
44    public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
45    public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
46    public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key";
47    public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
48    public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
49    public static final String KEY_QUICK_CAPTURE = "pref_camera_quickcapture_key";
50    public static final String KEY_EXPOSURE = "pref_camera_exposure_key";
51
52    public static final String QUICK_CAPTURE_ON = "on";
53    public static final String QUICK_CAPTURE_OFF = "off";
54
55    private static final String VIDEO_QUALITY_HIGH = "high";
56    private static final String VIDEO_QUALITY_MMS = "mms";
57    private static final String VIDEO_QUALITY_YOUTUBE = "youtube";
58
59    public static final String EXPOSURE_DEFAULT_VALUE = "0";
60
61    public static final int CURRENT_VERSION = 4;
62
63    // max video duration in seconds for mms and youtube.
64    private static final int MMS_VIDEO_DURATION = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW).duration;
65    private static final int YOUTUBE_VIDEO_DURATION = 10 * 60; // 10 mins
66    private static final int DEFAULT_VIDEO_DURATION = 30 * 60; // 10 mins
67
68    public static final String DEFAULT_VIDEO_QUALITY_VALUE = "high";
69
70    // MMS video length
71    public static final int DEFAULT_VIDEO_DURATION_VALUE = -1;
72
73    @SuppressWarnings("unused")
74    private static final String TAG = "CameraSettings";
75
76    private final Context mContext;
77    private final Parameters mParameters;
78
79    public CameraSettings(Activity activity, Parameters parameters) {
80        mContext = activity;
81        mParameters = parameters;
82    }
83
84    public PreferenceGroup getPreferenceGroup(int preferenceRes) {
85        PreferenceInflater inflater = new PreferenceInflater(mContext);
86        PreferenceGroup group =
87                (PreferenceGroup) inflater.inflate(preferenceRes);
88        initPreference(group);
89        return group;
90    }
91
92    public static void initialCameraPictureSize(
93            Context context, Parameters parameters) {
94        // When launching the camera app first time, we will set the picture
95        // size to the first one in the list defined in "arrays.xml" and is also
96        // supported by the driver.
97        List<Size> supported = parameters.getSupportedPictureSizes();
98        if (supported == null) return;
99        for (String candidate : context.getResources().getStringArray(
100                R.array.pref_camera_picturesize_entryvalues)) {
101            if (setCameraPictureSize(candidate, supported, parameters)) {
102                SharedPreferences.Editor editor = PreferenceManager
103                        .getDefaultSharedPreferences(context).edit();
104                editor.putString(KEY_PICTURE_SIZE, candidate);
105                editor.commit();
106                return;
107            }
108        }
109        Log.e(TAG, "No supported picture size found");
110    }
111
112    public static void removePreferenceFromScreen(
113            PreferenceGroup group, String key) {
114        removePreference(group, key);
115    }
116
117    public static boolean setCameraPictureSize(
118            String candidate, List<Size> supported, Parameters parameters) {
119        int index = candidate.indexOf('x');
120        if (index == NOT_FOUND) return false;
121        int width = Integer.parseInt(candidate.substring(0, index));
122        int height = Integer.parseInt(candidate.substring(index + 1));
123        for (Size size: supported) {
124            if (size.width == width && size.height == height) {
125                parameters.setPictureSize(width, height);
126                return true;
127            }
128        }
129        return false;
130    }
131
132    private void initPreference(PreferenceGroup group) {
133        ListPreference videoQuality = group.findPreference(KEY_VIDEO_QUALITY);
134        ListPreference pictureSize = group.findPreference(KEY_PICTURE_SIZE);
135        ListPreference whiteBalance =  group.findPreference(KEY_WHITE_BALANCE);
136        ListPreference colorEffect = group.findPreference(KEY_COLOR_EFFECT);
137        ListPreference sceneMode = group.findPreference(KEY_SCENE_MODE);
138        ListPreference flashMode = group.findPreference(KEY_FLASH_MODE);
139        ListPreference focusMode = group.findPreference(KEY_FOCUS_MODE);
140        ListPreference exposure = group.findPreference(KEY_EXPOSURE);
141        ListPreference videoFlashMode =
142                group.findPreference(KEY_VIDEOCAMERA_FLASH_MODE);
143
144        // Since the screen could be loaded from different resources, we need
145        // to check if the preference is available here
146        if (videoQuality != null) {
147            // Modify video duration settings.
148            // The first entry is for MMS video duration, and we need to fill
149            // in the device-dependent value (in seconds).
150            CharSequence[] entries = videoQuality.getEntries();
151            CharSequence[] values = videoQuality.getEntryValues();
152            for (int i = 0; i < entries.length; ++i) {
153                if (VIDEO_QUALITY_MMS.equals(values[i])) {
154                    entries[i] = entries[i].toString().replace(
155                            "30", Integer.toString(MMS_VIDEO_DURATION));
156                    break;
157                }
158            }
159        }
160
161        // Filter out unsupported settings / options
162        if (pictureSize != null) {
163            filterUnsupportedOptions(group, pictureSize, sizeListToStringList(
164                    mParameters.getSupportedPictureSizes()));
165        }
166        if (whiteBalance != null) {
167            filterUnsupportedOptions(group,
168                    whiteBalance, mParameters.getSupportedWhiteBalance());
169        }
170        if (colorEffect != null) {
171            filterUnsupportedOptions(group,
172                    colorEffect, mParameters.getSupportedColorEffects());
173        }
174        if (sceneMode != null) {
175            filterUnsupportedOptions(group,
176                    sceneMode, mParameters.getSupportedSceneModes());
177        }
178        if (flashMode != null) {
179            filterUnsupportedOptions(group,
180                    flashMode, mParameters.getSupportedFlashModes());
181        }
182        if (focusMode != null) {
183            filterUnsupportedOptions(group,
184                    focusMode, mParameters.getSupportedFocusModes());
185        }
186        if (videoFlashMode != null) {
187            filterUnsupportedOptions(group,
188                    videoFlashMode, mParameters.getSupportedFlashModes());
189        }
190
191        if (exposure != null) {
192            buildExposureCompensation(group, exposure);
193        }
194    }
195
196    private void buildExposureCompensation(
197            PreferenceGroup group, ListPreference exposure) {
198        int max = mParameters.getMaxExposureCompensation();
199        int min = mParameters.getMinExposureCompensation();
200        if (max == 0 && min == 0) {
201            removePreference(group, exposure.getKey());
202            return;
203        }
204        float step = mParameters.getExposureCompensationStep();
205
206        // show only integer values for exposure compensation
207        int maxValue = (int) Math.floor(max * step);
208        int minValue = (int) Math.ceil(min * step);
209        CharSequence entries[] = new CharSequence[maxValue - minValue + 1];
210        CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1];
211        for (int i = minValue; i <= maxValue; ++i) {
212            entryValues[maxValue - i] = Integer.toString(Math.round(i / step));
213            StringBuilder builder = new StringBuilder();
214            if (i > 0) builder.append('+');
215            entries[maxValue - i] = builder.append(i).toString();
216        }
217        exposure.setEntries(entries);
218        exposure.setEntryValues(entryValues);
219    }
220
221    private static boolean removePreference(PreferenceGroup group, String key) {
222        for (int i = 0, n = group.size(); i < n; i++) {
223            CameraPreference child = group.get(i);
224            if (child instanceof PreferenceGroup) {
225                if (removePreference((PreferenceGroup) child, key)) {
226                    return true;
227                }
228            }
229            if (child instanceof ListPreference &&
230                    ((ListPreference) child).getKey().equals(key)) {
231                group.removePreference(i);
232                return true;
233            }
234        }
235        return false;
236    }
237
238    private void filterUnsupportedOptions(PreferenceGroup group,
239            ListPreference pref, List<String> supported) {
240
241        CharSequence[] allEntries = pref.getEntries();
242
243        // Remove the preference if the parameter is not supported or there is
244        // only one options for the settings.
245        if (supported == null || supported.size() <= 1) {
246            removePreference(group, pref.getKey());
247            return;
248        }
249
250        CharSequence[] allEntryValues = pref.getEntryValues();
251        Drawable[] allIcons = (pref instanceof IconListPreference)
252                ? ((IconListPreference) pref).getIcons()
253                : null;
254        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
255        ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
256        ArrayList<Drawable> icons =
257                allIcons == null ? null : new ArrayList<Drawable>();
258        for (int i = 0, len = allEntryValues.length; i < len; i++) {
259            if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) {
260                entries.add(allEntries[i]);
261                entryValues.add(allEntryValues[i]);
262                if (allIcons != null) icons.add(allIcons[i]);
263            }
264        }
265
266        // Set entries and entry values to list preference.
267        int size = entries.size();
268        pref.setEntries(entries.toArray(new CharSequence[size]));
269        pref.setEntryValues(entryValues.toArray(new CharSequence[size]));
270        if (allIcons != null) {
271            ((IconListPreference) pref)
272                    .setIcons(icons.toArray(new Drawable[size]));
273        }
274
275        // Set the value to the first entry if it is invalid.
276        String value = pref.getValue();
277        if (pref.findIndexOfValue(value) == NOT_FOUND) {
278            pref.setValueIndex(0);
279        }
280    }
281
282    private static List<String> sizeListToStringList(List<Size> sizes) {
283        ArrayList<String> list = new ArrayList<String>();
284        for (Size size : sizes) {
285            list.add(String.format("%dx%d", size.width, size.height));
286        }
287        return list;
288    }
289
290    public static void upgradePreferences(SharedPreferences pref) {
291        int version;
292        try {
293            version = pref.getInt(KEY_VERSION, 0);
294        } catch (Exception ex) {
295            version = 0;
296        }
297        if (version == CURRENT_VERSION) return;
298
299        SharedPreferences.Editor editor = pref.edit();
300        if (version == 0) {
301            // We won't use the preference which change in version 1.
302            // So, just upgrade to version 1 directly
303            version = 1;
304        }
305        if (version == 1) {
306            // Change jpeg quality {65,75,85} to {normal,fine,superfine}
307            String quality = pref.getString(KEY_JPEG_QUALITY, "85");
308            if (quality.equals("65")) {
309                quality = "normal";
310            } else if (quality.equals("75")) {
311                quality = "fine";
312            } else {
313                quality = "superfine";
314            }
315            editor.putString(KEY_JPEG_QUALITY, quality);
316            version = 2;
317        }
318        if (version == 2) {
319            editor.putString(KEY_RECORD_LOCATION,
320                    pref.getBoolean(KEY_RECORD_LOCATION, false)
321                    ? RecordLocationPreference.VALUE_ON
322                    : RecordLocationPreference.VALUE_NONE);
323            version = 3;
324        }
325        if (version == 3) {
326            // Just use video quality to replace it and
327            // ignore the current settings.
328            editor.remove("pref_camera_videoquality_key");
329            editor.remove("pref_camera_video_duration_key");
330        }
331        editor.putInt(KEY_VERSION, CURRENT_VERSION);
332        editor.commit();
333    }
334
335    public static boolean getVideoQuality(String quality) {
336        return VIDEO_QUALITY_YOUTUBE.equals(
337                quality) || VIDEO_QUALITY_HIGH.equals(quality);
338    }
339
340    public static int getVidoeDurationInMillis(String quality) {
341        if (VIDEO_QUALITY_MMS.equals(quality)) {
342            return MMS_VIDEO_DURATION * 1000;
343        } else if (VIDEO_QUALITY_YOUTUBE.equals(quality)) {
344            return YOUTUBE_VIDEO_DURATION * 1000;
345        }
346        return DEFAULT_VIDEO_DURATION * 1000;
347    }
348}
349