GeneralPreferences.java revision f983c75b447485fc64c073ab57239c5d15834b66
1/*
2 * Copyright (C) 2010 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.email.activity.setup;
18
19import com.android.email.Preferences;
20import com.android.email.R;
21
22import android.os.Bundle;
23import android.preference.CheckBoxPreference;
24import android.preference.ListPreference;
25import android.preference.Preference;
26import android.preference.Preference.OnPreferenceChangeListener;
27import android.preference.PreferenceFragment;
28
29public class GeneralPreferences extends PreferenceFragment implements OnPreferenceChangeListener  {
30
31    private static final String PREFERENCE_KEY_AUTO_ADVANCE = "auto_advance";
32    private static final String PREFERENCE_KEY_TEXT_ZOOM = "text_zoom";
33    private static final String PREFERENCE_KEY_BACKGROUND_ATTACHMENTS = "background_attachments";
34
35    private Preferences mPreferences;
36    private ListPreference mAutoAdvance;
37    private ListPreference mTextZoom;
38    private CheckBoxPreference mBackgroundAttachments;
39
40    CharSequence[] mSizeSummaries;
41
42    @Override
43    public void onCreate(Bundle savedInstanceState) {
44        super.onCreate(savedInstanceState);
45
46        // Load the preferences from an XML resource
47        addPreferencesFromResource(R.xml.general_preferences);
48    }
49
50    @Override
51    public void onResume() {
52        loadSettings();
53        super.onResume();
54    }
55
56    @Override
57    public boolean onPreferenceChange(Preference preference, Object newValue) {
58        String key = preference.getKey();
59
60        if (PREFERENCE_KEY_AUTO_ADVANCE.equals(key)) {
61            mPreferences.setAutoAdvanceDirection(mAutoAdvance.findIndexOfValue((String) newValue));
62            return true;
63        } else if (PREFERENCE_KEY_TEXT_ZOOM.equals(key)) {
64            mPreferences.setTextZoom(mTextZoom.findIndexOfValue((String) newValue));
65            reloadDynamicSummaries();
66            return true;
67        } else if (PREFERENCE_KEY_BACKGROUND_ATTACHMENTS.equals(key)) {
68            mPreferences.setBackgroundAttachments((Boolean) newValue);
69            return true;
70        }
71        return false;
72    }
73
74    private void loadSettings() {
75        mPreferences = Preferences.getPreferences(getActivity());
76        mAutoAdvance = (ListPreference) findPreference(PREFERENCE_KEY_AUTO_ADVANCE);
77        mAutoAdvance.setValueIndex(mPreferences.getAutoAdvanceDirection());
78        mAutoAdvance.setOnPreferenceChangeListener(this);
79
80        mTextZoom = (ListPreference) findPreference(PREFERENCE_KEY_TEXT_ZOOM);
81        mTextZoom.setValueIndex(mPreferences.getTextZoom());
82        mTextZoom.setOnPreferenceChangeListener(this);
83
84        mBackgroundAttachments = (CheckBoxPreference)
85                findPreference(PREFERENCE_KEY_BACKGROUND_ATTACHMENTS);
86        mBackgroundAttachments.setChecked(mPreferences.getBackgroundAttachments());
87        mBackgroundAttachments.setOnPreferenceChangeListener(this);
88        reloadDynamicSummaries();
89    }
90
91    /**
92     * Reload any preference summaries that are updated dynamically
93     */
94    private void reloadDynamicSummaries() {
95        int textZoomIndex = mPreferences.getTextZoom();
96        // Update summary - but only load the array once
97        if (mSizeSummaries == null) {
98            mSizeSummaries = getActivity().getResources()
99                    .getTextArray(R.array.general_preference_text_zoom_summary_array);
100        }
101        CharSequence summary = null;
102        if (textZoomIndex >= 0 && textZoomIndex < mSizeSummaries.length) {
103            summary = mSizeSummaries[textZoomIndex];
104        }
105        mTextZoom.setSummary(summary);
106    }
107}
108