CameraSettings.java revision 87341536812081826656040ac81f8f386c6c1407
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;
22import android.hardware.Camera.Parameters;
23import android.os.Bundle;
24import android.preference.ListPreference;
25import android.preference.PreferenceActivity;
26
27import java.util.ArrayList;
28import java.util.StringTokenizer;
29
30/**
31 *  CameraSettings
32 */
33public class CameraSettings extends PreferenceActivity
34    implements OnSharedPreferenceChangeListener
35{
36    public static final String KEY_VIDEO_QUALITY = "pref_camera_videoquality_key";
37    public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
38    public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true;
39
40    private ListPreference mVideoQuality;
41    private ListPreference mWhiteBalance;
42    private Parameters mParameters;
43
44    public CameraSettings()
45    {
46    }
47
48    /** Called with the activity is first created. */
49    @Override
50    public void onCreate(Bundle icicle)
51    {
52        super.onCreate(icicle);
53        addPreferencesFromResource(R.xml.camera_preferences);
54
55        initUI();
56
57        // Get parameters.
58        android.hardware.Camera device = android.hardware.Camera.open();
59        mParameters = device.getParameters();
60        device.release();
61
62        createWhiteBalanceSettings();
63    }
64
65    @Override
66    protected void onResume() {
67        super.onResume();
68
69        updateVideoQuality();
70        updateWhiteBalance();
71    }
72
73    private void initUI() {
74        mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY);
75        mWhiteBalance = (ListPreference) findPreference(KEY_WHITE_BALANCE);
76        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
77    }
78
79    private void updateVideoQuality() {
80        boolean vidQualityValue = getBooleanPreference(mVideoQuality, DEFAULT_VIDEO_QUALITY_VALUE);
81        int vidQualityIndex = vidQualityValue ? 1 : 0;
82        String[] vidQualities =
83            getResources().getStringArray(R.array.pref_camera_videoquality_entries);
84        String vidQuality = vidQualities[vidQualityIndex];
85        mVideoQuality.setSummary(vidQuality);
86    }
87
88    private void createWhiteBalanceSettings() {
89        // Get the supported white balance settings.
90        String supportedWbStr = mParameters.get("whitebalance-values");
91        StringTokenizer tokenizer = new StringTokenizer(supportedWbStr, ",");
92        ArrayList<CharSequence> supportedWb = new ArrayList<CharSequence>();
93        while (tokenizer.hasMoreElements()) {
94            supportedWb.add(tokenizer.nextToken());
95        }
96
97        // Prepare white balance entries and entry values.
98        String[] allWbEntries = getResources().getStringArray(
99                R.array.pref_camera_whitebalance_entries);
100        String[] allWbEntryValues = getResources().getStringArray(
101                R.array.pref_camera_whitebalance_entryvalues);
102        ArrayList<CharSequence> wbEntries = new ArrayList<CharSequence>();
103        ArrayList<CharSequence> wbEntryValues = new ArrayList<CharSequence>();
104        for (int i = 0, len = allWbEntryValues.length; i < len; i++) {
105            int found = supportedWb.indexOf(allWbEntryValues[i]);
106            if (found != -1) {
107                wbEntries.add(allWbEntries[i]);
108                wbEntryValues.add(allWbEntryValues[i]);
109            }
110        }
111
112        // Set white balance entries and entry values to list preference.
113        mWhiteBalance.setEntries(wbEntries.toArray(
114                new CharSequence[wbEntries.size()]));
115        mWhiteBalance.setEntryValues(wbEntryValues.toArray(
116                new CharSequence[wbEntryValues.size()]));
117
118        String value = mWhiteBalance.getValue();
119        int index = mWhiteBalance.findIndexOfValue(value);
120        if (index == -1) {
121            mWhiteBalance.setValueIndex(0);
122        }
123    }
124
125    private void updateWhiteBalance() {
126        // Set preference summary.
127        mWhiteBalance.setSummary(mWhiteBalance.getEntry());
128    }
129
130    private static int getIntPreference(ListPreference preference, int defaultValue) {
131        String s = preference.getValue();
132        int result = defaultValue;
133        try {
134            result = Integer.parseInt(s);
135        } catch (NumberFormatException e) {
136            // Ignore, result is already the default value.
137        }
138        return result;
139    }
140
141    private boolean getBooleanPreference(ListPreference preference, boolean defaultValue) {
142        return getIntPreference(preference, defaultValue ? 1 : 0) != 0;
143    }
144
145    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
146            String key) {
147        if (key.equals(KEY_VIDEO_QUALITY)) {
148            updateVideoQuality();
149        } else if (key.equals(KEY_WHITE_BALANCE)) {
150            updateWhiteBalance();
151        }
152    }
153}
154
155