CameraSettings.java revision 4ddee78a1507b3c067745023bd2330b866b8aaf9
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.graphics.drawable.Drawable;
23import android.hardware.Camera.Parameters;
24import android.hardware.Camera.Size;
25import android.os.SystemProperties;
26import android.preference.ListPreference;
27import android.preference.Preference;
28import android.preference.PreferenceGroup;
29import android.preference.PreferenceManager;
30import android.preference.PreferenceScreen;
31import android.util.Log;
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 FIRST_REQUEST_CODE = 100;
41    private static final int NOT_FOUND = -1;
42
43    public static final String KEY_VERSION = "pref_version_key";
44    public static final String KEY_RECORD_LOCATION =
45            RecordLocationPreference.KEY;
46    public static final String KEY_VIDEO_QUALITY =
47            "pref_camera_videoquality_key";
48    public static final String KEY_VIDEO_DURATION =
49            "pref_camera_video_duration_key";
50    public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
51    public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
52    public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
53    public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
54    public static final String KEY_COLOR_EFFECT = "pref_camera_coloreffect_key";
55    public static final String KEY_WHITE_BALANCE =
56            "pref_camera_whitebalance_key";
57    public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
58
59    public static final int CURRENT_VERSION = 3;
60
61    // max mms video duration in seconds.
62    public static final int MMS_VIDEO_DURATION =
63            SystemProperties.getInt("ro.media.enc.lprof.duration", 60);
64
65    public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true;
66
67    // MMS video length
68    public static final int DEFAULT_VIDEO_DURATION_VALUE = -1;
69
70    @SuppressWarnings("unused")
71    private static final String TAG = "CameraSettings";
72
73    private final Context mContext;
74    private final Parameters mParameters;
75    private final PreferenceManager mManager;
76
77    public CameraSettings(Activity activity, Parameters parameters) {
78        mContext = activity;
79        mParameters = parameters;
80        mManager = new PreferenceManager(activity, FIRST_REQUEST_CODE);
81    }
82
83    public PreferenceScreen getPreferenceScreen(int preferenceRes) {
84        PreferenceScreen screen = mManager.createPreferenceScreen(mContext);
85        mManager.inflateFromResource(mContext, preferenceRes, screen);
86        initPreference(screen);
87        return screen;
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 = PreferenceManager
101                        .getDefaultSharedPreferences(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            PreferenceScreen screen, String key) {
112        Preference pref = screen.findPreference(key);
113        if (pref == null) {
114            Log.i(TAG, "No preference found based the key : " + key);
115            throw new IllegalArgumentException();
116        } else {
117            removePreference(screen, pref);
118        }
119    }
120
121    public static boolean setCameraPictureSize(
122            String candidate, List<Size> supported, Parameters parameters) {
123        int index = candidate.indexOf('x');
124        if (index == NOT_FOUND) return false;
125        int width = Integer.parseInt(candidate.substring(0, index));
126        int height = Integer.parseInt(candidate.substring(index + 1));
127        for (Size size: supported) {
128            if (size.width == width && size.height == height) {
129                parameters.setPictureSize(width, height);
130                return true;
131            }
132        }
133        return false;
134    }
135
136    private void initPreference(PreferenceScreen screen) {
137        ListPreference videoDuration =
138                (ListPreference) screen.findPreference(KEY_VIDEO_DURATION);
139        ListPreference pictureSize =
140                (ListPreference) screen.findPreference(KEY_PICTURE_SIZE);
141        ListPreference whiteBalance =
142                (ListPreference) screen.findPreference(KEY_WHITE_BALANCE);
143        ListPreference colorEffect =
144                (ListPreference) screen.findPreference(KEY_COLOR_EFFECT);
145        ListPreference sceneMode =
146                (ListPreference) screen.findPreference(KEY_SCENE_MODE);
147        ListPreference flashMode =
148                (ListPreference) screen.findPreference(KEY_FLASH_MODE);
149        ListPreference focusMode =
150                (ListPreference) screen.findPreference(KEY_FOCUS_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 (videoDuration != 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 = videoDuration.getEntries();
159            entries[0] = String.format(
160                    entries[0].toString(), MMS_VIDEO_DURATION);
161        }
162
163        // Filter out unsupported settings / options
164        if (pictureSize != null) {
165            filterUnsupportedOptions(screen, pictureSize, sizeListToStringList(
166                    mParameters.getSupportedPictureSizes()));
167        }
168        if (whiteBalance != null) {
169            filterUnsupportedOptions(screen,
170                    whiteBalance, mParameters.getSupportedWhiteBalance());
171        }
172        if (colorEffect != null) {
173            filterUnsupportedOptions(screen,
174                    colorEffect, mParameters.getSupportedColorEffects());
175        }
176        if (sceneMode != null) {
177            filterUnsupportedOptions(screen,
178                    sceneMode, mParameters.getSupportedSceneModes());
179        }
180        if (flashMode != null) {
181            filterUnsupportedOptions(screen,
182                    flashMode, mParameters.getSupportedFlashModes());
183        }
184        if (focusMode != null) {
185            filterUnsupportedOptions(screen,
186                    focusMode, mParameters.getSupportedFocusModes());
187        }
188    }
189
190    private static boolean removePreference(PreferenceGroup group,
191            Preference remove) {
192        if (group.removePreference(remove)) return true;
193
194        for (int i = 0; i < group.getPreferenceCount(); i++) {
195            final Preference child = group.getPreference(i);
196            if (child instanceof PreferenceGroup) {
197                if (removePreference((PreferenceGroup) child, remove)) {
198                    return true;
199                }
200            }
201        }
202        return false;
203    }
204
205    private void filterUnsupportedOptions(PreferenceScreen screen,
206            ListPreference pref, List<String> supported) {
207
208        CharSequence[] allEntries = pref.getEntries();
209
210        // Remove the preference if the parameter is not supported or there is
211        // only one options for the settings.
212        if (supported == null || supported.size() <= 1) {
213            removePreference(screen, pref);
214            return;
215        }
216
217        CharSequence[] allEntryValues = pref.getEntryValues();
218        Drawable[] allIcons = (pref instanceof IconListPreference)
219                ? ((IconListPreference) pref).getIcons()
220                : null;
221        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
222        ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
223        ArrayList<Drawable> icons =
224                allIcons == null ? null : new ArrayList<Drawable>();
225        for (int i = 0, len = allEntryValues.length; i < len; i++) {
226            if (supported.indexOf(allEntryValues[i].toString()) != NOT_FOUND) {
227                entries.add(allEntries[i]);
228                entryValues.add(allEntryValues[i]);
229                if (allIcons != null) icons.add(allIcons[i]);
230            }
231        }
232
233        // Set entries and entry values to list preference.
234        int size = entries.size();
235        pref.setEntries(entries.toArray(new CharSequence[size]));
236        pref.setEntryValues(entryValues.toArray(new CharSequence[size]));
237        if (allIcons != null) {
238            ((IconListPreference) pref)
239                    .setIcons(icons.toArray(new Drawable[size]));
240        }
241
242        // Set the value to the first entry if it is invalid.
243        String value = pref.getValue();
244        if (pref.findIndexOfValue(value) == NOT_FOUND) {
245            pref.setValueIndex(0);
246        }
247    }
248
249    private static List<String> sizeListToStringList(List<Size> sizes) {
250        ArrayList<String> list = new ArrayList<String>();
251        for (Size size : sizes) {
252            list.add(String.format("%dx%d", size.width, size.height));
253        }
254        return list;
255    }
256
257    public static void upgradePreferences(SharedPreferences pref) {
258        int version;
259        try {
260            version = pref.getInt(KEY_VERSION, 0);
261        } catch (Exception ex) {
262            version = 0;
263        }
264        if (version == CURRENT_VERSION) return;
265
266        SharedPreferences.Editor editor = pref.edit();
267        if (version == 0) {
268            // For old version, change 1 to 10 for video duration preference.
269            if (pref.getString(KEY_VIDEO_DURATION, "1").equals("1")) {
270                editor.putString(KEY_VIDEO_DURATION, "10");
271            }
272            version = 1;
273        }
274        if (version == 1) {
275            // Change jpeg quality {65,75,85} to {normal,fine,superfine}
276            String quality = pref.getString(KEY_JPEG_QUALITY, "85");
277            if (quality.equals("65")) {
278                quality = "normal";
279            } else if (quality.equals("75")) {
280                quality = "fine";
281            } else {
282                quality = "superfine";
283            }
284            editor.putString(KEY_JPEG_QUALITY, quality);
285            version = 2;
286        }
287        if (version == 2) {
288            editor.putString(KEY_RECORD_LOCATION,
289                    pref.getBoolean(KEY_RECORD_LOCATION, false)
290                    ? RecordLocationPreference.VALUE_ON
291                    : RecordLocationPreference.VALUE_NONE);
292            version = 3;
293        }
294        editor.putInt(KEY_VERSION, CURRENT_VERSION);
295        editor.commit();
296    }
297}
298