CameraSettings.java revision 77b6c6a8ef32eeca4c527ca52b04451e7458599e
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_VIDEO_TIME_LAPSE_QUALITY = "pref_video_time_lapse_quality_key";
42    public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
43    public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
44    public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
45    public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
46    public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
47    public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_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";
52
53    private static final String VIDEO_QUALITY_HIGH = "high";
54    private static final String VIDEO_QUALITY_MMS = "mms";
55    private static final String VIDEO_QUALITY_YOUTUBE = "youtube";
56
57    private static final String VIDEO_TIME_LAPSE_QUALITY_LOW= "low";
58    private static final String VIDEO_TIME_LAPSE_QUALITY_HIGH= "high";
59    private static final String VIDEO_TIME_LAPSE_QUALITY_720P = "720p";
60    private static final String VIDEO_TIME_LAPSE_QUALITY_1080P = "1080p";
61
62    public static final int TIME_LAPSE_VIDEO_QUALITY_LOW = 1;
63    public static final int TIME_LAPSE_VIDEO_QUALITY_HIGH = 2;
64    public static final int TIME_LAPSE_VIDEO_QUALITY_720P = 3;
65    public static final int TIME_LAPSE_VIDEO_QUALITY_1080P = 4;
66
67    public static final String EXPOSURE_DEFAULT_VALUE = "0";
68
69    public static final int CURRENT_VERSION = 4;
70    public static final int CURRENT_LOCAL_VERSION = 1;
71
72    // max video duration in seconds for mms and youtube.
73    private static final int MMS_VIDEO_DURATION = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW).duration;
74    private static final int YOUTUBE_VIDEO_DURATION = 10 * 60; // 10 mins
75    private static final int DEFAULT_VIDEO_DURATION = 30 * 60; // 10 mins
76
77    public static final String DEFAULT_VIDEO_QUALITY_VALUE = "high";
78    public static final String DEFAULT_VIDEO_TIME_LAPSE_QUALITY_VALUE = "high";
79
80    // MMS video length
81    public static final int DEFAULT_VIDEO_DURATION_VALUE = -1;
82
83    @SuppressWarnings("unused")
84    private static final String TAG = "CameraSettings";
85
86    private final Context mContext;
87    private final Parameters mParameters;
88
89    public CameraSettings(Activity activity, Parameters parameters) {
90        mContext = activity;
91        mParameters = parameters;
92    }
93
94    public PreferenceGroup getPreferenceGroup(int preferenceRes) {
95        PreferenceInflater inflater = new PreferenceInflater(mContext);
96        PreferenceGroup group =
97                (PreferenceGroup) inflater.inflate(preferenceRes);
98        initPreference(group);
99        return group;
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 pictureSize = group.findPreference(KEY_PICTURE_SIZE);
145        ListPreference whiteBalance =  group.findPreference(KEY_WHITE_BALANCE);
146        ListPreference colorEffect = group.findPreference(KEY_COLOR_EFFECT);
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        ListPreference videoFlashMode =
152                group.findPreference(KEY_VIDEOCAMERA_FLASH_MODE);
153
154        // Since the screen could be loaded from different resources, we need
155        // to check if the preference is available here
156        if (videoQuality != null) {
157            // Modify video duration settings.
158            // The first entry is for MMS video duration, and we need to fill
159            // in the device-dependent value (in seconds).
160            CharSequence[] entries = videoQuality.getEntries();
161            CharSequence[] values = videoQuality.getEntryValues();
162            for (int i = 0; i < entries.length; ++i) {
163                if (VIDEO_QUALITY_MMS.equals(values[i])) {
164                    entries[i] = entries[i].toString().replace(
165                            "30", Integer.toString(MMS_VIDEO_DURATION));
166                    break;
167                }
168            }
169        }
170
171        // Filter out unsupported settings / options
172        if (pictureSize != null) {
173            filterUnsupportedOptions(group, pictureSize, sizeListToStringList(
174                    mParameters.getSupportedPictureSizes()));
175        }
176        if (whiteBalance != null) {
177            filterUnsupportedOptions(group,
178                    whiteBalance, mParameters.getSupportedWhiteBalance());
179        }
180        if (colorEffect != null) {
181            filterUnsupportedOptions(group,
182                    colorEffect, mParameters.getSupportedColorEffects());
183        }
184        if (sceneMode != null) {
185            filterUnsupportedOptions(group,
186                    sceneMode, mParameters.getSupportedSceneModes());
187        }
188        if (flashMode != null) {
189            filterUnsupportedOptions(group,
190                    flashMode, mParameters.getSupportedFlashModes());
191        }
192        if (focusMode != null) {
193            filterUnsupportedOptions(group,
194                    focusMode, mParameters.getSupportedFocusModes());
195        }
196        if (videoFlashMode != null) {
197            filterUnsupportedOptions(group,
198                    videoFlashMode, mParameters.getSupportedFlashModes());
199        }
200
201        if (exposure != null) {
202            buildExposureCompensation(group, exposure);
203        }
204    }
205
206    private void buildExposureCompensation(
207            PreferenceGroup group, ListPreference exposure) {
208        int max = mParameters.getMaxExposureCompensation();
209        int min = mParameters.getMinExposureCompensation();
210        if (max == 0 && min == 0) {
211            removePreference(group, exposure.getKey());
212            return;
213        }
214        float step = mParameters.getExposureCompensationStep();
215
216        // show only integer values for exposure compensation
217        int maxValue = (int) Math.floor(max * step);
218        int minValue = (int) Math.ceil(min * step);
219        CharSequence entries[] = new CharSequence[maxValue - minValue + 1];
220        CharSequence entryValues[] = new CharSequence[maxValue - minValue + 1];
221        for (int i = minValue; i <= maxValue; ++i) {
222            entryValues[maxValue - i] = Integer.toString(Math.round(i / step));
223            StringBuilder builder = new StringBuilder();
224            if (i > 0) builder.append('+');
225            entries[maxValue - i] = builder.append(i).toString();
226        }
227        exposure.setEntries(entries);
228        exposure.setEntryValues(entryValues);
229    }
230
231    private static boolean removePreference(PreferenceGroup group, String key) {
232        for (int i = 0, n = group.size(); i < n; i++) {
233            CameraPreference child = group.get(i);
234            if (child instanceof PreferenceGroup) {
235                if (removePreference((PreferenceGroup) child, key)) {
236                    return true;
237                }
238            }
239            if (child instanceof ListPreference &&
240                    ((ListPreference) child).getKey().equals(key)) {
241                group.removePreference(i);
242                return true;
243            }
244        }
245        return false;
246    }
247
248    private void filterUnsupportedOptions(PreferenceGroup group,
249            ListPreference pref, List<String> supported) {
250
251        CharSequence[] allEntries = pref.getEntries();
252
253        // Remove the preference if the parameter is not supported or there is
254        // only one options for the settings.
255        if (supported == null || supported.size() <= 1) {
256            removePreference(group, pref.getKey());
257            return;
258        }
259
260        pref.filterUnsupported(supported);
261
262        // Set the value to the first entry if it is invalid.
263        String value = pref.getValue();
264        if (pref.findIndexOfValue(value) == NOT_FOUND) {
265            pref.setValueIndex(0);
266        }
267    }
268
269    private static List<String> sizeListToStringList(List<Size> sizes) {
270        ArrayList<String> list = new ArrayList<String>();
271        for (Size size : sizes) {
272            list.add(String.format("%dx%d", size.width, size.height));
273        }
274        return list;
275    }
276
277    public static void upgradeLocalPreferences(SharedPreferences pref) {
278        int version;
279        try {
280            version = pref.getInt(KEY_LOCAL_VERSION, 0);
281        } catch (Exception ex) {
282            version = 0;
283        }
284        if (version == CURRENT_LOCAL_VERSION) return;
285        SharedPreferences.Editor editor = pref.edit();
286        editor.putInt(KEY_LOCAL_VERSION, CURRENT_LOCAL_VERSION);
287        editor.apply();
288    }
289
290    public static void upgradeGlobalPreferences(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.apply();
333    }
334
335    public static void upgradeAllPreferences(ComboPreferences pref) {
336        upgradeGlobalPreferences(pref.getGlobal());
337        upgradeLocalPreferences(pref.getLocal());
338    }
339
340    public static boolean getVideoQuality(String quality) {
341        return VIDEO_QUALITY_YOUTUBE.equals(
342                quality) || VIDEO_QUALITY_HIGH.equals(quality);
343    }
344
345    public static int getVideoTimeLapseQuality(String quality) {
346        if (VIDEO_TIME_LAPSE_QUALITY_LOW.equals(quality)) {
347            return TIME_LAPSE_VIDEO_QUALITY_LOW;
348        } else if (VIDEO_TIME_LAPSE_QUALITY_HIGH.equals(quality)) {
349            return TIME_LAPSE_VIDEO_QUALITY_HIGH;
350        } else if (VIDEO_TIME_LAPSE_QUALITY_720P.equals(quality)) {
351            return TIME_LAPSE_VIDEO_QUALITY_720P;
352        } else if (VIDEO_TIME_LAPSE_QUALITY_1080P.equals(quality)) {
353            return TIME_LAPSE_VIDEO_QUALITY_1080P;
354        } else {
355            throw new IllegalArgumentException("Unknown quality" + quality);
356        }
357    }
358
359    public static int getVidoeDurationInMillis(String quality) {
360        if (VIDEO_QUALITY_MMS.equals(quality)) {
361            return MMS_VIDEO_DURATION * 1000;
362        } else if (VIDEO_QUALITY_YOUTUBE.equals(quality)) {
363            return YOUTUBE_VIDEO_DURATION * 1000;
364        }
365        return DEFAULT_VIDEO_DURATION * 1000;
366    }
367
368    public static int readPreferredCameraId(SharedPreferences pref) {
369        return pref.getInt(KEY_CAMERA_ID, 0);
370    }
371
372    public static void writePreferredCameraId(SharedPreferences pref,
373            int cameraId) {
374        Editor editor = pref.edit();
375        editor.putInt(KEY_CAMERA_ID, cameraId);
376        editor.apply();
377    }
378}
379