CameraSettings.java revision c6b84a41226d54988f2def467e2986fc2d94dbfa
1/*
2 * Copyright (C) 2007 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.content.SharedPreferences;
20import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
21import android.hardware.Camera.Parameters;
22import android.os.Bundle;
23import android.preference.ListPreference;
24import android.preference.PreferenceActivity;
25
26import java.util.ArrayList;
27import java.util.StringTokenizer;
28
29/**
30 *  CameraSettings
31 */
32public class CameraSettings extends PreferenceActivity implements
33        OnSharedPreferenceChangeListener {
34    public static final String KEY_VIDEO_QUALITY =
35            "pref_camera_videoquality_key";
36    public static final String KEY_WHITE_BALANCE =
37            "pref_camera_whitebalance_key";
38    public static final String KEY_EFFECT = "pref_camera_effect_key";
39    public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
40    public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
41    public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true;
42
43    private ListPreference mVideoQuality;
44    private ListPreference mWhiteBalance;
45    private ListPreference mEffect;
46    private ListPreference mPictureSize;
47    private ListPreference mJpegQuality;
48    private Parameters mParameters;
49
50    @Override
51    public void onCreate(Bundle icicle) {
52        super.onCreate(icicle);
53        addPreferencesFromResource(R.xml.camera_preferences);
54
55        initUI();
56    }
57
58    @Override
59    protected void onResume() {
60        super.onResume();
61
62        updateVideoQualitySummary();
63        updateWhiteBalanceSummary();
64        updateEffectSummary();
65        updatePictureSizeSummary();
66        updateJpegQualitySummary();
67    }
68
69    private void initUI() {
70        mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY);
71        mWhiteBalance = (ListPreference) findPreference(KEY_WHITE_BALANCE);
72        mEffect = (ListPreference) findPreference(KEY_EFFECT);
73        mPictureSize = (ListPreference) findPreference(KEY_PICTURE_SIZE);
74        mJpegQuality = (ListPreference) findPreference(KEY_JPEG_QUALITY);
75        getPreferenceScreen().getSharedPreferences().
76                registerOnSharedPreferenceChangeListener(this);
77
78        // Get parameters.
79        android.hardware.Camera device = android.hardware.Camera.open();
80        mParameters = device.getParameters();
81        device.release();
82
83        // Create white balance settings.
84        createSettings(mWhiteBalance, Camera.SUPPORTED_WHITE_BALANCE,
85                       R.array.pref_camera_whitebalance_entries,
86                       R.array.pref_camera_whitebalance_entryvalues);
87
88        // Create effect settings.
89        createSettings(mEffect, Camera.SUPPORTED_EFFECT,
90                       R.array.pref_camera_effect_entries,
91                       R.array.pref_camera_effect_entryvalues);
92
93        // Create picture size settings.
94        createSettings(mPictureSize, Camera.SUPPORTED_PICTURE_SIZE,
95                       R.array.pref_camera_picturesize_entries,
96                       R.array.pref_camera_picturesize_entryvalues);
97    }
98
99    private void createSettings(
100            ListPreference pref, String paramName, int prefEntriesResId,
101            int prefEntryValuesResId) {
102        // Disable the preference if the parameter is not supported.
103        String supportedParamStr = mParameters.get(paramName);
104        if (supportedParamStr == null) {
105            pref.setEnabled(false);
106            return;
107        }
108
109        // Get the supported parameter settings.
110        StringTokenizer tokenizer = new StringTokenizer(supportedParamStr, ",");
111        ArrayList<CharSequence> supportedParam = new ArrayList<CharSequence>();
112        while (tokenizer.hasMoreElements()) {
113            supportedParam.add(tokenizer.nextToken());
114        }
115
116        // Prepare setting entries and entry values.
117        String[] allEntries = getResources().getStringArray(prefEntriesResId);
118        String[] allEntryValues = getResources().getStringArray(
119                prefEntryValuesResId);
120        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
121        ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
122        for (int i = 0, len = allEntryValues.length; i < len; i++) {
123            int found = supportedParam.indexOf(allEntryValues[i]);
124            if (found != -1) {
125                entries.add(allEntries[i]);
126                entryValues.add(allEntryValues[i]);
127            }
128        }
129
130        // Set entries and entry values to list preference.
131        pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
132        pref.setEntryValues(entryValues.toArray(
133                new CharSequence[entryValues.size()]));
134
135        // Set the value to the first entry if it is invalid.
136        String value = pref.getValue();
137        int index = pref.findIndexOfValue(value);
138        if (index == -1) {
139            pref.setValueIndex(0);
140        }
141    }
142
143    private void updateVideoQualitySummary() {
144        mVideoQuality.setSummary(mVideoQuality.getEntry());
145    }
146
147    private void updateWhiteBalanceSummary() {
148        mWhiteBalance.setSummary(mWhiteBalance.getEntry());
149    }
150
151    private void updateEffectSummary() {
152        mEffect.setSummary(mEffect.getEntry());
153    }
154
155    private void updatePictureSizeSummary() {
156        mPictureSize.setSummary(mPictureSize.getEntry());
157    }
158
159    private void updateJpegQualitySummary() {
160        mJpegQuality.setSummary(mJpegQuality.getEntry());
161    }
162
163    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
164            String key) {
165        if (key.equals(KEY_VIDEO_QUALITY)) {
166            updateVideoQualitySummary();
167        } else if (key.equals(KEY_WHITE_BALANCE)) {
168            updateWhiteBalanceSummary();
169        } else if (key.equals(KEY_EFFECT)) {
170            updateEffectSummary();
171        } else if (key.equals(KEY_PICTURE_SIZE)) {
172            updatePictureSizeSummary();
173        } else if (key.equals(KEY_JPEG_QUALITY)) {
174            updateJpegQualitySummary();
175        }
176    }
177}
178