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