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