CameraSettings.java revision 39a796b1c7e5bb579d139dda89029d656cc135f8
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.annotation.TargetApi;
20import android.app.Activity;
21import android.content.Context;
22import android.content.SharedPreferences;
23import android.content.SharedPreferences.Editor;
24import android.hardware.Camera.CameraInfo;
25import android.hardware.Camera.Parameters;
26import android.hardware.Camera.Size;
27import android.media.CamcorderProfile;
28import android.util.FloatMath;
29import android.util.Log;
30
31import com.android.gallery3d.common.ApiHelper;
32
33import java.util.ArrayList;
34import java.util.List;
35
36/**
37 *  Provides utilities and keys for Camera settings.
38 */
39public class CameraSettings {
40    private static final int NOT_FOUND = -1;
41
42    public static final String KEY_VERSION = "pref_version_key";
43    public static final String KEY_LOCAL_VERSION = "pref_local_version_key";
44    public static final String KEY_RECORD_LOCATION = RecordLocationPreference.KEY;
45    public static final String KEY_VIDEO_QUALITY = "pref_video_quality_key";
46    public static final String KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL = "pref_video_time_lapse_frame_interval_key";
47    public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
48    public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
49    public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
50    public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
51    public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
52    public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
53    public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
54    public static final String KEY_EXPOSURE = "pref_camera_exposure_key";
55    public static final String KEY_VIDEO_EFFECT = "pref_video_effect_key";
56    public static final String KEY_CAMERA_ID = "pref_camera_id_key";
57    public static final String KEY_CAMERA_FIRST_USE_HINT_SHOWN = "pref_camera_first_use_hint_shown_key";
58    public static final String KEY_VIDEO_FIRST_USE_HINT_SHOWN = "pref_video_first_use_hint_shown_key";
59
60    public static final String EXPOSURE_DEFAULT_VALUE = "0";
61
62    public static final int CURRENT_VERSION = 5;
63    public static final int CURRENT_LOCAL_VERSION = 2;
64
65    public static final int DEFAULT_VIDEO_DURATION = 0; // no limit
66
67    private static final String TAG = "CameraSettings";
68
69    private final Context mContext;
70    private final Parameters mParameters;
71    private final CameraInfo[] mCameraInfo;
72    private final int mCameraId;
73
74    public CameraSettings(Activity activity, Parameters parameters,
75                          int cameraId, CameraInfo[] cameraInfo) {
76        mContext = activity;
77        mParameters = parameters;
78        mCameraId = cameraId;
79        mCameraInfo = cameraInfo;
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    @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
91    public static String getDefaultVideoQuality(int cameraId,
92            String defaultQuality) {
93        if (ApiHelper.HAS_FINE_RESOLUTION_QUALITY_LEVELS) {
94            if (CamcorderProfile.hasProfile(
95                    cameraId, Integer.valueOf(defaultQuality))) {
96                return defaultQuality;
97            }
98        }
99        return Integer.toString(CamcorderProfile.QUALITY_HIGH);
100    }
101
102    public static void initialCameraPictureSize(
103            Context context, Parameters parameters) {
104        // When launching the camera app first time, we will set the picture
105        // size to the first one in the list defined in "arrays.xml" and is also
106        // supported by the driver.
107        List<Size> supported = parameters.getSupportedPictureSizes();
108        if (supported == null) return;
109        for (String candidate : context.getResources().getStringArray(
110                R.array.pref_camera_picturesize_entryvalues)) {
111            if (setCameraPictureSize(candidate, supported, parameters)) {
112                SharedPreferences.Editor editor = ComboPreferences
113                        .get(context).edit();
114                editor.putString(KEY_PICTURE_SIZE, candidate);
115                editor.apply();
116                return;
117            }
118        }
119        Log.e(TAG, "No supported picture size found");
120    }
121
122    public static void removePreferenceFromScreen(
123            PreferenceGroup group, String key) {
124        removePreference(group, key);
125    }
126
127    public static boolean setCameraPictureSize(
128            String candidate, List<Size> supported, Parameters parameters) {
129        int index = candidate.indexOf('x');
130        if (index == NOT_FOUND) return false;
131        int width = Integer.parseInt(candidate.substring(0, index));
132        int height = Integer.parseInt(candidate.substring(index + 1));
133        for (Size size : supported) {
134            if (size.width == width && size.height == height) {
135                parameters.setPictureSize(width, height);
136                return true;
137            }
138        }
139        return false;
140    }
141
142    private void initPreference(PreferenceGroup group) {
143        ListPreference videoQuality = group.findPreference(KEY_VIDEO_QUALITY);
144        ListPreference timeLapseInterval = group.findPreference(KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL);
145        ListPreference pictureSize = group.findPreference(KEY_PICTURE_SIZE);
146        ListPreference whiteBalance =  group.findPreference(KEY_WHITE_BALANCE);
147        ListPreference sceneMode = group.findPreference(KEY_SCENE_MODE);
148        ListPreference flashMode = group.findPreference(KEY_FLASH_MODE);
149        ListPreference focusMode = group.findPreference(KEY_FOCUS_MODE);
150        ListPreference exposure = group.findPreference(KEY_EXPOSURE);
151        IconListPreference cameraIdPref =
152                (IconListPreference) group.findPreference(KEY_CAMERA_ID);
153        ListPreference videoFlashMode =
154                group.findPreference(KEY_VIDEOCAMERA_FLASH_MODE);
155        ListPreference videoEffect = group.findPreference(KEY_VIDEO_EFFECT);
156
157        // Since the screen could be loaded from different resources, we need
158        // to check if the preference is available here
159        if (videoQuality != null) {
160            filterUnsupportedOptions(group, videoQuality, getSupportedVideoQuality());
161        }
162
163        if (pictureSize != null) {
164            filterUnsupportedOptions(group, pictureSize, sizeListToStringList(
165                    mParameters.getSupportedPictureSizes()));
166            filterSimilarPictureSize(group, pictureSize);
167        }
168        if (whiteBalance != null) {
169            filterUnsupportedOptions(group,
170                    whiteBalance, mParameters.getSupportedWhiteBalance());
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            if (!Util.isFocusAreaSupported(mParameters)) {
182                filterUnsupportedOptions(group,
183                        focusMode, mParameters.getSupportedFocusModes());
184            } else {
185                // Remove the focus mode if we can use tap-to-focus.
186                removePreference(group, focusMode.getKey());
187            }
188        }
189        if (videoFlashMode != null) {
190            filterUnsupportedOptions(group,
191                    videoFlashMode, mParameters.getSupportedFlashModes());
192        }
193        if (exposure != null) buildExposureCompensation(group, exposure);
194        if (cameraIdPref != null) buildCameraId(group, cameraIdPref);
195
196        if (timeLapseInterval != null) {
197            if (ApiHelper.HAS_TIME_LAPSE_RECORDING) {
198                resetIfInvalid(timeLapseInterval);
199            } else {
200                removePreference(group, timeLapseInterval.getKey());
201            }
202        }
203        if (videoEffect != null) {
204            if (ApiHelper.HAS_EFFECTS_RECORDING) {
205                initVideoEffect(group, videoEffect);
206                resetIfInvalid(videoEffect);
207            } else {
208                filterUnsupportedOptions(group, videoEffect, null);
209            }
210        }
211    }
212
213    private void buildExposureCompensation(
214            PreferenceGroup group, ListPreference exposure) {
215        int max = mParameters.getMaxExposureCompensation();
216        int min = mParameters.getMinExposureCompensation();
217        if (max == 0 && min == 0) {
218            removePreference(group, exposure.getKey());
219            return;
220        }
221        float step = mParameters.getExposureCompensationStep();
222
223        // show only integer values for exposure compensation
224        int maxValue = (int) FloatMath.floor(max * step);
225        int minValue = (int) FloatMath.ceil(min * step);
226        CharSequence entries[] = new CharSequence[maxValue - minValue + 1];
227        CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1];
228        for (int i = minValue; i <= maxValue; ++i) {
229            entryValues[maxValue - i] = Integer.toString(Math.round(i / step));
230            StringBuilder builder = new StringBuilder();
231            if (i > 0) builder.append('+');
232            entries[maxValue - i] = builder.append(i).toString();
233        }
234        exposure.setEntries(entries);
235        exposure.setEntryValues(entryValues);
236    }
237
238    private void buildCameraId(
239            PreferenceGroup group, IconListPreference preference) {
240        int numOfCameras = mCameraInfo.length;
241        if (numOfCameras < 2) {
242            removePreference(group, preference.getKey());
243            return;
244        }
245
246        CharSequence[] entryValues = new CharSequence[numOfCameras];
247        for (int i = 0; i < numOfCameras; ++i) {
248            entryValues[i] = "" + i;
249        }
250        preference.setEntryValues(entryValues);
251    }
252
253    private static boolean removePreference(PreferenceGroup group, String key) {
254        for (int i = 0, n = group.size(); i < n; i++) {
255            CameraPreference child = group.get(i);
256            if (child instanceof PreferenceGroup) {
257                if (removePreference((PreferenceGroup) child, key)) {
258                    return true;
259                }
260            }
261            if (child instanceof ListPreference &&
262                    ((ListPreference) child).getKey().equals(key)) {
263                group.removePreference(i);
264                return true;
265            }
266        }
267        return false;
268    }
269
270    private void filterUnsupportedOptions(PreferenceGroup group,
271            ListPreference pref, List<String> supported) {
272
273        // Remove the preference if the parameter is not supported or there is
274        // only one options for the settings.
275        if (supported == null || supported.size() <= 1) {
276            removePreference(group, pref.getKey());
277            return;
278        }
279
280        pref.filterUnsupported(supported);
281        if (pref.getEntries().length <= 1) {
282            removePreference(group, pref.getKey());
283            return;
284        }
285
286        resetIfInvalid(pref);
287    }
288
289    private void filterSimilarPictureSize(PreferenceGroup group,
290            ListPreference pref) {
291        pref.filterDuplicated();
292        if (pref.getEntries().length <= 1) {
293            removePreference(group, pref.getKey());
294            return;
295        }
296        resetIfInvalid(pref);
297    }
298
299    private void resetIfInvalid(ListPreference pref) {
300        // Set the value to the first entry if it is invalid.
301        String value = pref.getValue();
302        if (pref.findIndexOfValue(value) == NOT_FOUND) {
303            pref.setValueIndex(0);
304        }
305    }
306
307    private static List<String> sizeListToStringList(List<Size> sizes) {
308        ArrayList<String> list = new ArrayList<String>();
309        for (Size size : sizes) {
310            list.add(String.format("%dx%d", size.width, size.height));
311        }
312        return list;
313    }
314
315    public static void upgradeLocalPreferences(SharedPreferences pref) {
316        int version;
317        try {
318            version = pref.getInt(KEY_LOCAL_VERSION, 0);
319        } catch (Exception ex) {
320            version = 0;
321        }
322        if (version == CURRENT_LOCAL_VERSION) return;
323
324        SharedPreferences.Editor editor = pref.edit();
325        if (version == 1) {
326            // We use numbers to represent the quality now. The quality definition is identical to
327            // that of CamcorderProfile.java.
328            editor.remove("pref_video_quality_key");
329        }
330        editor.putInt(KEY_LOCAL_VERSION, CURRENT_LOCAL_VERSION);
331        editor.apply();
332    }
333
334    public static void upgradeGlobalPreferences(SharedPreferences pref) {
335        upgradeOldVersion(pref);
336        upgradeCameraId(pref);
337    }
338
339    private static void upgradeOldVersion(SharedPreferences pref) {
340        int version;
341        try {
342            version = pref.getInt(KEY_VERSION, 0);
343        } catch (Exception ex) {
344            version = 0;
345        }
346        if (version == CURRENT_VERSION) return;
347
348        SharedPreferences.Editor editor = pref.edit();
349        if (version == 0) {
350            // We won't use the preference which change in version 1.
351            // So, just upgrade to version 1 directly
352            version = 1;
353        }
354        if (version == 1) {
355            // Change jpeg quality {65,75,85} to {normal,fine,superfine}
356            String quality = pref.getString(KEY_JPEG_QUALITY, "85");
357            if (quality.equals("65")) {
358                quality = "normal";
359            } else if (quality.equals("75")) {
360                quality = "fine";
361            } else {
362                quality = "superfine";
363            }
364            editor.putString(KEY_JPEG_QUALITY, quality);
365            version = 2;
366        }
367        if (version == 2) {
368            editor.putString(KEY_RECORD_LOCATION,
369                    pref.getBoolean(KEY_RECORD_LOCATION, false)
370                    ? RecordLocationPreference.VALUE_ON
371                    : RecordLocationPreference.VALUE_NONE);
372            version = 3;
373        }
374        if (version == 3) {
375            // Just use video quality to replace it and
376            // ignore the current settings.
377            editor.remove("pref_camera_videoquality_key");
378            editor.remove("pref_camera_video_duration_key");
379        }
380
381        editor.putInt(KEY_VERSION, CURRENT_VERSION);
382        editor.apply();
383    }
384
385    private static void upgradeCameraId(SharedPreferences pref) {
386        // The id stored in the preference may be out of range if we are running
387        // inside the emulator and a webcam is removed.
388        // Note: This method accesses the global preferences directly, not the
389        // combo preferences.
390        int cameraId = readPreferredCameraId(pref);
391        if (cameraId == 0) return;  // fast path
392
393        int n = CameraHolder.instance().getNumberOfCameras();
394        if (cameraId < 0 || cameraId >= n) {
395            writePreferredCameraId(pref, 0);
396        }
397    }
398
399    public static int readPreferredCameraId(SharedPreferences pref) {
400        return Integer.parseInt(pref.getString(KEY_CAMERA_ID, "0"));
401    }
402
403    public static void writePreferredCameraId(SharedPreferences pref,
404            int cameraId) {
405        Editor editor = pref.edit();
406        editor.putString(KEY_CAMERA_ID, Integer.toString(cameraId));
407        editor.apply();
408    }
409
410    public static int readExposure(ComboPreferences preferences) {
411        String exposure = preferences.getString(
412                CameraSettings.KEY_EXPOSURE,
413                EXPOSURE_DEFAULT_VALUE);
414        try {
415            return Integer.parseInt(exposure);
416        } catch (Exception ex) {
417            Log.e(TAG, "Invalid exposure: " + exposure);
418        }
419        return 0;
420    }
421
422    public static int readEffectType(SharedPreferences pref) {
423        String effectSelection = pref.getString(KEY_VIDEO_EFFECT, "none");
424        if (effectSelection.equals("none")) {
425            return EffectsRecorder.EFFECT_NONE;
426        } else if (effectSelection.startsWith("goofy_face")) {
427            return EffectsRecorder.EFFECT_GOOFY_FACE;
428        } else if (effectSelection.startsWith("backdropper")) {
429            return EffectsRecorder.EFFECT_BACKDROPPER;
430        }
431        Log.e(TAG, "Invalid effect selection: " + effectSelection);
432        return EffectsRecorder.EFFECT_NONE;
433    }
434
435    public static Object readEffectParameter(SharedPreferences pref) {
436        String effectSelection = pref.getString(KEY_VIDEO_EFFECT, "none");
437        if (effectSelection.equals("none")) {
438            return null;
439        }
440        int separatorIndex = effectSelection.indexOf('/');
441        String effectParameter =
442                effectSelection.substring(separatorIndex + 1);
443        if (effectSelection.startsWith("goofy_face")) {
444            if (effectParameter.equals("squeeze")) {
445                return EffectsRecorder.EFFECT_GF_SQUEEZE;
446            } else if (effectParameter.equals("big_eyes")) {
447                return EffectsRecorder.EFFECT_GF_BIG_EYES;
448            } else if (effectParameter.equals("big_mouth")) {
449                return EffectsRecorder.EFFECT_GF_BIG_MOUTH;
450            } else if (effectParameter.equals("small_mouth")) {
451                return EffectsRecorder.EFFECT_GF_SMALL_MOUTH;
452            } else if (effectParameter.equals("big_nose")) {
453                return EffectsRecorder.EFFECT_GF_BIG_NOSE;
454            } else if (effectParameter.equals("small_eyes")) {
455                return EffectsRecorder.EFFECT_GF_SMALL_EYES;
456            }
457        } else if (effectSelection.startsWith("backdropper")) {
458            // Parameter is a string that either encodes the URI to use,
459            // or specifies 'gallery'.
460            return effectParameter;
461        }
462
463        Log.e(TAG, "Invalid effect selection: " + effectSelection);
464        return null;
465    }
466
467
468    public static void restorePreferences(Context context,
469            ComboPreferences preferences, Parameters parameters) {
470        int currentCameraId = readPreferredCameraId(preferences);
471
472        // Clear the preferences of both cameras.
473        int backCameraId = CameraHolder.instance().getBackCameraId();
474        if (backCameraId != -1) {
475            preferences.setLocalId(context, backCameraId);
476            Editor editor = preferences.edit();
477            editor.clear();
478            editor.apply();
479        }
480        int frontCameraId = CameraHolder.instance().getFrontCameraId();
481        if (frontCameraId != -1) {
482            preferences.setLocalId(context, frontCameraId);
483            Editor editor = preferences.edit();
484            editor.clear();
485            editor.apply();
486        }
487
488        // Switch back to the preferences of the current camera. Otherwise,
489        // we may write the preference to wrong camera later.
490        preferences.setLocalId(context, currentCameraId);
491
492        upgradeGlobalPreferences(preferences.getGlobal());
493        upgradeLocalPreferences(preferences.getLocal());
494
495        // Write back the current camera id because parameters are related to
496        // the camera. Otherwise, we may switch to the front camera but the
497        // initial picture size is that of the back camera.
498        initialCameraPictureSize(context, parameters);
499        writePreferredCameraId(preferences, currentCameraId);
500    }
501
502    private ArrayList<String> getSupportedVideoQuality() {
503        ArrayList<String> supported = new ArrayList<String>();
504        // Check for supported quality
505        if (ApiHelper.HAS_FINE_RESOLUTION_QUALITY_LEVELS) {
506            getFineResolutionQuality(supported);
507        } else {
508            supported.add(Integer.toString(CamcorderProfile.QUALITY_HIGH));
509            CamcorderProfile high = CamcorderProfile.get(
510                    mCameraId, CamcorderProfile.QUALITY_HIGH);
511            CamcorderProfile low = CamcorderProfile.get(
512                    mCameraId, CamcorderProfile.QUALITY_LOW);
513            if (high.videoFrameHeight * high.videoFrameWidth >
514                    low.videoFrameHeight * low.videoFrameWidth) {
515                supported.add(Integer.toString(CamcorderProfile.QUALITY_LOW));
516            }
517        }
518
519        return supported;
520    }
521
522    @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
523    private void getFineResolutionQuality(ArrayList<String> supported) {
524        if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_1080P)) {
525            supported.add(Integer.toString(CamcorderProfile.QUALITY_1080P));
526        }
527        if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_720P)) {
528            supported.add(Integer.toString(CamcorderProfile.QUALITY_720P));
529        }
530        if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_480P)) {
531            supported.add(Integer.toString(CamcorderProfile.QUALITY_480P));
532        }
533    }
534
535    private void initVideoEffect(PreferenceGroup group, ListPreference videoEffect) {
536        CharSequence[] values = videoEffect.getEntryValues();
537
538        boolean goofyFaceSupported =
539                EffectsRecorder.isEffectSupported(EffectsRecorder.EFFECT_GOOFY_FACE);
540        boolean backdropperSupported =
541                EffectsRecorder.isEffectSupported(EffectsRecorder.EFFECT_BACKDROPPER) &&
542                Util.isAutoExposureLockSupported(mParameters) &&
543                Util.isAutoWhiteBalanceLockSupported(mParameters);
544
545        ArrayList<String> supported = new ArrayList<String>();
546        for (CharSequence value : values) {
547            String effectSelection = value.toString();
548            if (!goofyFaceSupported && effectSelection.startsWith("goofy_face")) continue;
549            if (!backdropperSupported && effectSelection.startsWith("backdropper")) continue;
550            supported.add(effectSelection);
551        }
552
553        filterUnsupportedOptions(group, videoEffect, supported);
554    }
555}
556