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