CameraSettings.java revision 003dd5a52457c024a0f99a2bb222bfc6ad70bbe5
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.CameraInfo;
24import android.hardware.Camera.Parameters;
25import android.hardware.Camera.Size;
26import android.media.CamcorderProfile;
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_LOCAL_VERSION = "pref_local_version_key";
40    public static final String KEY_RECORD_LOCATION = RecordLocationPreference.KEY;
41    public static final String KEY_VIDEO_QUALITY = "pref_video_quality_key";
42    public static final String KEY_VIDEO_TIME_LAPSE_QUALITY = "pref_video_time_lapse_quality_key";
43    public static final String KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL = "pref_video_time_lapse_frame_interval_key";
44    public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
45    public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
46    public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
47    public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
48    public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
49    public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key";
50    public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
51    public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
52    public static final String KEY_EXPOSURE = "pref_camera_exposure_key";
53    public static final String KEY_CAMERA_ID = "pref_camera_id_key";
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    public static final int CURRENT_LOCAL_VERSION = 1;
63
64    // max video duration in seconds for mms and youtube.
65    private static final int MMS_VIDEO_DURATION = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW).duration;
66    private static final int YOUTUBE_VIDEO_DURATION = 10 * 60; // 10 mins
67    private static final int DEFAULT_VIDEO_DURATION = 30 * 60; // 10 mins
68
69    public static final String DEFAULT_VIDEO_QUALITY_VALUE = "high";
70
71    // MMS video length
72    public static final int DEFAULT_VIDEO_DURATION_VALUE = -1;
73
74    private static final String TAG = "CameraSettings";
75
76    private final Context mContext;
77    private final Parameters mParameters;
78    private final CameraInfo[] mCameraInfo;
79    private final int mCameraId;
80
81    public CameraSettings(Activity activity, Parameters parameters,
82                          int cameraId, CameraInfo[] cameraInfo) {
83        mContext = activity;
84        mParameters = parameters;
85        mCameraId = cameraId;
86        mCameraInfo = cameraInfo;
87    }
88
89    public PreferenceGroup getPreferenceGroup(int preferenceRes) {
90        PreferenceInflater inflater = new PreferenceInflater(mContext);
91        PreferenceGroup group =
92                (PreferenceGroup) inflater.inflate(preferenceRes);
93        initPreference(group);
94        return group;
95    }
96
97    public static void initialCameraPictureSize(
98            Context context, Parameters parameters) {
99        // When launching the camera app first time, we will set the picture
100        // size to the first one in the list defined in "arrays.xml" and is also
101        // supported by the driver.
102        List<Size> supported = parameters.getSupportedPictureSizes();
103        if (supported == null) return;
104        for (String candidate : context.getResources().getStringArray(
105                R.array.pref_camera_picturesize_entryvalues)) {
106            if (setCameraPictureSize(candidate, supported, parameters)) {
107                SharedPreferences.Editor editor = ComboPreferences
108                        .get(context).edit();
109                editor.putString(KEY_PICTURE_SIZE, candidate);
110                editor.apply();
111                return;
112            }
113        }
114        Log.e(TAG, "No supported picture size found");
115    }
116
117    public static void removePreferenceFromScreen(
118            PreferenceGroup group, String key) {
119        removePreference(group, key);
120    }
121
122    public static boolean setCameraPictureSize(
123            String candidate, List<Size> supported, Parameters parameters) {
124        int index = candidate.indexOf('x');
125        if (index == NOT_FOUND) return false;
126        int width = Integer.parseInt(candidate.substring(0, index));
127        int height = Integer.parseInt(candidate.substring(index + 1));
128        for (Size size: supported) {
129            if (size.width == width && size.height == height) {
130                parameters.setPictureSize(width, height);
131                return true;
132            }
133        }
134        return false;
135    }
136
137    private void initPreference(PreferenceGroup group) {
138        ListPreference videoQuality = group.findPreference(KEY_VIDEO_QUALITY);
139        ListPreference videoTimeLapseQuality = group.findPreference(KEY_VIDEO_TIME_LAPSE_QUALITY);
140        ListPreference pictureSize = group.findPreference(KEY_PICTURE_SIZE);
141        ListPreference whiteBalance =  group.findPreference(KEY_WHITE_BALANCE);
142        ListPreference colorEffect = group.findPreference(KEY_COLOR_EFFECT);
143        ListPreference sceneMode = group.findPreference(KEY_SCENE_MODE);
144        ListPreference flashMode = group.findPreference(KEY_FLASH_MODE);
145        ListPreference focusMode = group.findPreference(KEY_FOCUS_MODE);
146        ListPreference exposure = group.findPreference(KEY_EXPOSURE);
147        IconListPreference cameraIdPref =
148                (IconListPreference)group.findPreference(KEY_CAMERA_ID);
149        ListPreference videoFlashMode =
150                group.findPreference(KEY_VIDEOCAMERA_FLASH_MODE);
151
152        // Since the screen could be loaded from different resources, we need
153        // to check if the preference is available here
154        if (videoQuality != null) {
155            // Modify video duration settings.
156            // The first entry is for MMS video duration, and we need to fill
157            // in the device-dependent value (in seconds).
158            CharSequence[] entries = videoQuality.getEntries();
159            CharSequence[] values = videoQuality.getEntryValues();
160            for (int i = 0; i < entries.length; ++i) {
161                if (VIDEO_QUALITY_MMS.equals(values[i])) {
162                    entries[i] = entries[i].toString().replace(
163                            "30", Integer.toString(MMS_VIDEO_DURATION));
164                    break;
165                }
166            }
167        }
168
169        // Filter out unsupported settings / options
170        if (videoTimeLapseQuality != null) {
171            filterUnsupportedOptions(group, videoTimeLapseQuality,
172                    getSupportedTimeLapseProfiles(mCameraId));
173        }
174        if (pictureSize != null) {
175            filterUnsupportedOptions(group, pictureSize, sizeListToStringList(
176                    mParameters.getSupportedPictureSizes()));
177        }
178        if (whiteBalance != null) {
179            filterUnsupportedOptions(group,
180                    whiteBalance, mParameters.getSupportedWhiteBalance());
181        }
182        if (colorEffect != null) {
183            filterUnsupportedOptions(group,
184                    colorEffect, mParameters.getSupportedColorEffects());
185        }
186        if (sceneMode != null) {
187            filterUnsupportedOptions(group,
188                    sceneMode, mParameters.getSupportedSceneModes());
189        }
190        if (flashMode != null) {
191            filterUnsupportedOptions(group,
192                    flashMode, mParameters.getSupportedFlashModes());
193        }
194        if (focusMode != null) {
195            filterUnsupportedOptions(group,
196                    focusMode, mParameters.getSupportedFocusModes());
197        }
198        if (videoFlashMode != null) {
199            filterUnsupportedOptions(group,
200                    videoFlashMode, mParameters.getSupportedFlashModes());
201        }
202        if (exposure != null) buildExposureCompensation(group, exposure);
203        if (cameraIdPref != null) buildCameraId(group, cameraIdPref);
204    }
205
206    private static List<String> getSupportedTimeLapseProfiles(int cameraId) {
207        ArrayList<String> supportedProfiles = new ArrayList<String>();
208        if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_TIME_LAPSE_480P)) {
209            supportedProfiles.add(Integer.toString(CamcorderProfile.QUALITY_TIME_LAPSE_480P));
210        }
211        if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_TIME_LAPSE_720P)) {
212            supportedProfiles.add(Integer.toString(CamcorderProfile.QUALITY_TIME_LAPSE_720P));
213        }
214        if (CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_TIME_LAPSE_1080P)) {
215            supportedProfiles.add(Integer.toString(CamcorderProfile.QUALITY_TIME_LAPSE_1080P));
216        }
217
218        return supportedProfiles;
219    }
220
221    private void buildExposureCompensation(
222            PreferenceGroup group, ListPreference exposure) {
223        int max = mParameters.getMaxExposureCompensation();
224        int min = mParameters.getMinExposureCompensation();
225        if (max == 0 && min == 0) {
226            removePreference(group, exposure.getKey());
227            return;
228        }
229        float step = mParameters.getExposureCompensationStep();
230
231        // show only integer values for exposure compensation
232        int maxValue = (int) Math.floor(max * step);
233        int minValue = (int) Math.ceil(min * step);
234        CharSequence entries[] = new CharSequence[maxValue - minValue + 1];
235        CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1];
236        for (int i = minValue; i <= maxValue; ++i) {
237            entryValues[maxValue - i] = Integer.toString(Math.round(i / step));
238            StringBuilder builder = new StringBuilder();
239            if (i > 0) builder.append('+');
240            entries[maxValue - i] = builder.append(i).toString();
241        }
242        exposure.setEntries(entries);
243        exposure.setEntryValues(entryValues);
244    }
245
246    private void buildCameraId(
247            PreferenceGroup group, IconListPreference preference) {
248        int numOfCameras = mCameraInfo.length;
249        if (numOfCameras < 2) {
250            removePreference(group, preference.getKey());
251            return;
252        }
253
254        CharSequence[] entryValues = new CharSequence[2];
255        for (int i = 0 ; i < mCameraInfo.length ; ++i) {
256            int index =
257                    (mCameraInfo[i].facing == CameraInfo.CAMERA_FACING_FRONT)
258                    ? CameraInfo.CAMERA_FACING_FRONT
259                    : CameraInfo.CAMERA_FACING_BACK;
260            if (entryValues[index] == null) {
261                entryValues[index] = "" + i;
262                if (entryValues[((index == 1) ? 0 : 1)] != null) break;
263            }
264        }
265        preference.setEntryValues(entryValues);
266    }
267
268    private static boolean removePreference(PreferenceGroup group, String key) {
269        for (int i = 0, n = group.size(); i < n; i++) {
270            CameraPreference child = group.get(i);
271            if (child instanceof PreferenceGroup) {
272                if (removePreference((PreferenceGroup) child, key)) {
273                    return true;
274                }
275            }
276            if (child instanceof ListPreference &&
277                    ((ListPreference) child).getKey().equals(key)) {
278                group.removePreference(i);
279                return true;
280            }
281        }
282        return false;
283    }
284
285    private void filterUnsupportedOptions(PreferenceGroup group,
286            ListPreference pref, List<String> supported) {
287
288        // Remove the preference if the parameter is not supported or there is
289        // only one options for the settings.
290        if (supported == null || supported.size() <= 1) {
291            removePreference(group, pref.getKey());
292            return;
293        }
294
295        pref.filterUnsupported(supported);
296        if (pref.getEntries().length <= 1) {
297            removePreference(group, pref.getKey());
298            return;
299        }
300
301        // Set the value to the first entry if it is invalid.
302        String value = pref.getValue();
303        if (pref.findIndexOfValue(value) == NOT_FOUND) {
304            pref.setValueIndex(0);
305        }
306    }
307
308    private static List<String> sizeListToStringList(List<Size> sizes) {
309        ArrayList<String> list = new ArrayList<String>();
310        for (Size size : sizes) {
311            list.add(String.format("%dx%d", size.width, size.height));
312        }
313        return list;
314    }
315
316    public static void upgradeLocalPreferences(SharedPreferences pref) {
317        int version;
318        try {
319            version = pref.getInt(KEY_LOCAL_VERSION, 0);
320        } catch (Exception ex) {
321            version = 0;
322        }
323        if (version == CURRENT_LOCAL_VERSION) return;
324        SharedPreferences.Editor editor = pref.edit();
325        editor.putInt(KEY_LOCAL_VERSION, CURRENT_LOCAL_VERSION);
326        editor.apply();
327    }
328
329    public static void upgradeGlobalPreferences(SharedPreferences pref) {
330        int version;
331        try {
332            version = pref.getInt(KEY_VERSION, 0);
333        } catch (Exception ex) {
334            version = 0;
335        }
336        if (version == CURRENT_VERSION) return;
337
338        SharedPreferences.Editor editor = pref.edit();
339        if (version == 0) {
340            // We won't use the preference which change in version 1.
341            // So, just upgrade to version 1 directly
342            version = 1;
343        }
344        if (version == 1) {
345            // Change jpeg quality {65,75,85} to {normal,fine,superfine}
346            String quality = pref.getString(KEY_JPEG_QUALITY, "85");
347            if (quality.equals("65")) {
348                quality = "normal";
349            } else if (quality.equals("75")) {
350                quality = "fine";
351            } else {
352                quality = "superfine";
353            }
354            editor.putString(KEY_JPEG_QUALITY, quality);
355            version = 2;
356        }
357        if (version == 2) {
358            editor.putString(KEY_RECORD_LOCATION,
359                    pref.getBoolean(KEY_RECORD_LOCATION, false)
360                    ? RecordLocationPreference.VALUE_ON
361                    : RecordLocationPreference.VALUE_NONE);
362            version = 3;
363        }
364        if (version == 3) {
365            // Just use video quality to replace it and
366            // ignore the current settings.
367            editor.remove("pref_camera_videoquality_key");
368            editor.remove("pref_camera_video_duration_key");
369        }
370        editor.putInt(KEY_VERSION, CURRENT_VERSION);
371        editor.apply();
372    }
373
374    public static boolean getVideoQuality(String quality) {
375        return VIDEO_QUALITY_YOUTUBE.equals(
376                quality) || VIDEO_QUALITY_HIGH.equals(quality);
377    }
378
379    public static int getVidoeDurationInMillis(String quality) {
380        if (VIDEO_QUALITY_MMS.equals(quality)) {
381            return MMS_VIDEO_DURATION * 1000;
382        } else if (VIDEO_QUALITY_YOUTUBE.equals(quality)) {
383            return YOUTUBE_VIDEO_DURATION * 1000;
384        }
385        return DEFAULT_VIDEO_DURATION * 1000;
386    }
387
388    public static int readPreferredCameraId(SharedPreferences pref) {
389        return Integer.parseInt(pref.getString(KEY_CAMERA_ID, "0"));
390    }
391
392    public static void writePreferredCameraId(SharedPreferences pref,
393            int cameraId) {
394        Editor editor = pref.edit();
395        editor.putString(KEY_CAMERA_ID, Integer.toString(cameraId));
396        editor.apply();
397    }
398
399
400    public static void restorePreferences(Context context,
401            ComboPreferences preferences, Parameters parameters) {
402        int currentCameraId = readPreferredCameraId(preferences);
403
404        // Clear the preferences of both cameras.
405        int backCameraId = CameraHolder.instance().getBackCameraId();
406        if (backCameraId != -1) {
407            preferences.setLocalId(context, backCameraId);
408            Editor editor = preferences.edit();
409            editor.clear();
410            editor.apply();
411        }
412        int frontCameraId = CameraHolder.instance().getFrontCameraId();
413        if (frontCameraId != -1) {
414            preferences.setLocalId(context, frontCameraId);
415            Editor editor = preferences.edit();
416            editor.clear();
417            editor.apply();
418        }
419
420        upgradeGlobalPreferences(preferences.getGlobal());
421        upgradeLocalPreferences(preferences.getLocal());
422
423        // Write back the current camera id because parameters are related to
424        // the camera. Otherwise, we may switch to the front camera but the
425        // initial picture size is that of the back camera.
426        initialCameraPictureSize(context, parameters);
427        writePreferredCameraId(preferences, currentCameraId);
428    }
429}
430