CameraSettings.java revision b97ccf3f20bee44daf70f10966809e39e30ab4f7
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.os.Bundle;
22import android.preference.ListPreference;
23import android.preference.PreferenceActivity;
24
25/**
26 *  CameraSettings
27 */
28public class CameraSettings extends PreferenceActivity
29    implements OnSharedPreferenceChangeListener
30{
31    public static final String KEY_VIDEO_QUALITY = "pref_camera_videoquality_key";
32    public static final boolean DEFAULT_VIDEO_QUALITY_VALUE = true;
33
34    private ListPreference mVideoQuality;
35
36    public CameraSettings()
37    {
38    }
39
40    /** Called with the activity is first created. */
41    @Override
42    public void onCreate(Bundle icicle)
43    {
44        super.onCreate(icicle);
45        addPreferencesFromResource(R.xml.camera_preferences);
46
47        initUI();
48    }
49
50    @Override
51    protected void onResume() {
52        super.onResume();
53        updateVideoQuality();
54    }
55
56    private void initUI() {
57        mVideoQuality = (ListPreference) findPreference(KEY_VIDEO_QUALITY);
58        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
59    }
60
61    private void updateVideoQuality() {
62        boolean vidQualityValue = getBooleanPreference(mVideoQuality, DEFAULT_VIDEO_QUALITY_VALUE);
63        int vidQualityIndex = vidQualityValue ? 1 : 0;
64        String[] vidQualities =
65            getResources().getStringArray(R.array.pref_camera_videoquality_entries);
66        String vidQuality = vidQualities[vidQualityIndex];
67        mVideoQuality.setSummary(vidQuality);
68    }
69
70    private static int getIntPreference(ListPreference preference, int defaultValue) {
71        String s = preference.getValue();
72        int result = defaultValue;
73        try {
74            result = Integer.parseInt(s);
75        } catch (NumberFormatException e) {
76            // Ignore, result is already the default value.
77        }
78        return result;
79    }
80
81    private boolean getBooleanPreference(ListPreference preference, boolean defaultValue) {
82        return getIntPreference(preference, defaultValue ? 1 : 0) != 0;
83    }
84
85    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
86            String key) {
87           if (key.equals(KEY_VIDEO_QUALITY)) {
88               updateVideoQuality();
89           }
90
91    }
92}
93
94