CameraSettings.java revision d4247bc43702d5ccda5dde40e948fffd6c4445ee
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
33    implements 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 boolean DEFAULT_VIDEO_QUALITY_VALUE = true;
40
41    private ListPreference mVideoQuality;
42    private ListPreference mWhiteBalance;
43    private ListPreference mEffect;
44    private Parameters mParameters;
45
46    /** Called with the activity is first created. */
47    @Override
48    public void onCreate(Bundle icicle) {
49        super.onCreate(icicle);
50        addPreferencesFromResource(R.xml.camera_preferences);
51
52        initUI();
53    }
54
55    @Override
56    protected void onResume() {
57        super.onResume();
58
59        updateVideoQualitySummary();
60        updateWhiteBalanceSummary();
61        updateEffectSummary();
62    }
63
64    private void initUI() {
65        mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY);
66        mWhiteBalance = (ListPreference) findPreference(KEY_WHITE_BALANCE);
67        mEffect = (ListPreference) findPreference(KEY_EFFECT);
68        getPreferenceScreen().getSharedPreferences().
69                registerOnSharedPreferenceChangeListener(this);
70
71        // Get parameters.
72        android.hardware.Camera device = android.hardware.Camera.open();
73        mParameters = device.getParameters();
74        device.release();
75
76        // Create white balance settings.
77        createSettings(mWhiteBalance, Camera.SUPPORTED_WHITE_BALANCE,
78                       R.array.pref_camera_whitebalance_entries,
79                       R.array.pref_camera_whitebalance_entryvalues);
80
81        // Create effect settings.
82        createSettings(mEffect, Camera.SUPPORTED_EFFECT,
83                       R.array.pref_camera_effect_entries,
84                       R.array.pref_camera_effect_entryvalues);
85    }
86
87    private void createSettings(
88            ListPreference pref, String paramName, int prefEntriesResId,
89            int prefEntryValuesResId) {
90        // Get the supported parameter settings.
91        String supportedParamStr = mParameters.get(paramName);
92        StringTokenizer tokenizer = new StringTokenizer(supportedParamStr, ",");
93        ArrayList<CharSequence> supportedParam = new ArrayList<CharSequence>();
94        while (tokenizer.hasMoreElements()) {
95            supportedParam.add(tokenizer.nextToken());
96        }
97
98        // Prepare setting entries and entry values.
99        String[] allEntries = getResources().getStringArray(prefEntriesResId);
100        String[] allEntryValues = getResources().getStringArray(
101                prefEntryValuesResId);
102        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
103        ArrayList<CharSequence> entryValues = new ArrayList<CharSequence>();
104        for (int i = 0, len = allEntryValues.length; i < len; i++) {
105            int found = supportedParam.indexOf(allEntryValues[i]);
106            if (found != -1) {
107                entries.add(allEntries[i]);
108                entryValues.add(allEntryValues[i]);
109            }
110        }
111
112        // Set entries and entry values to list preference.
113        pref.setEntries(entries.toArray(new CharSequence[entries.size()]));
114        pref.setEntryValues(entryValues.toArray(
115                new CharSequence[entryValues.size()]));
116
117        // Set the value to the first entry if it is invalid.
118        String value = pref.getValue();
119        int index = pref.findIndexOfValue(value);
120        if (index == -1) {
121            pref.setValueIndex(0);
122        }
123    }
124
125    private void updateVideoQualitySummary() {
126        mVideoQuality.setSummary(mVideoQuality.getEntry());
127    }
128
129    private void updateWhiteBalanceSummary() {
130        // Set preference summary.
131        mWhiteBalance.setSummary(mWhiteBalance.getEntry());
132    }
133
134    private void updateEffectSummary() {
135        // Set preference summary.
136        mEffect.setSummary(mEffect.getEntry());
137    }
138
139    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
140            String key) {
141        if (key.equals(KEY_VIDEO_QUALITY)) {
142            updateVideoQualitySummary();
143        } else if (key.equals(KEY_WHITE_BALANCE)) {
144            updateWhiteBalanceSummary();
145        } else if (key.equals(KEY_EFFECT)) {
146            updateEffectSummary();
147        }
148    }
149}
150
151