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