GeneralPreferences.java revision 4ad6302749749149766dcca599380b8f06fa0f12
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.calendar;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.SharedPreferences;
22import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
23import android.content.pm.PackageInfo;
24import android.content.pm.PackageManager.NameNotFoundException;
25import android.os.Bundle;
26import android.preference.CheckBoxPreference;
27import android.preference.ListPreference;
28import android.preference.Preference;
29import android.preference.Preference.OnPreferenceChangeListener;
30import android.preference.PreferenceFragment;
31import android.preference.PreferenceManager;
32import android.preference.PreferenceScreen;
33import android.preference.RingtonePreference;
34import android.provider.SearchRecentSuggestions;
35import android.widget.Toast;
36
37public class GeneralPreferences extends PreferenceFragment implements
38        OnSharedPreferenceChangeListener, OnPreferenceChangeListener {
39    // The name of the shared preferences file. This name must be maintained for historical
40    // reasons, as it's what PreferenceManager assigned the first time the file was created.
41    private static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
42
43    // Preference keys
44    public static final String KEY_HIDE_DECLINED = "preferences_hide_declined";
45    public static final String KEY_WEEK_START_DAY = "preferences_week_start_day";
46
47    public static final String KEY_CLEAR_SEARCH_HISTORY = "preferences_clear_search_history";
48
49    public static final String KEY_ALERTS = "preferences_alerts";
50    public static final String KEY_ALERTS_VIBRATE = "preferences_alerts_vibrate";
51    public static final String KEY_ALERTS_VIBRATE_WHEN = "preferences_alerts_vibrateWhen";
52    public static final String KEY_ALERTS_RINGTONE = "preferences_alerts_ringtone";
53    public static final String KEY_ALERTS_POPUP = "preferences_alerts_popup";
54
55    public static final String KEY_DEFAULT_REMINDER = "preferences_default_reminder";
56
57    /** Key to SharePreference for default view (CalendarController.ViewType) */
58    public static final String KEY_START_VIEW = "preferred_startView";
59    /**
60     *  Key to SharePreference for default detail view (CalendarController.ViewType)
61     *  Typically used by widget
62     */
63    public static final String KEY_DETAILED_VIEW = "preferred_detailedView";
64    public static final String KEY_DEFAULT_CALENDAR = "preference_defaultCalendar";
65
66    // These must be in sync with the array preferences_week_start_day_values
67    public static final String WEEK_START_DEFAULT = "-1";
68    public static final String WEEK_START_SATURDAY = "7";
69    public static final String WEEK_START_SUNDAY = "1";
70    public static final String WEEK_START_MONDAY = "2";
71
72    // These keys are kept to enable migrating users from previous versions
73    private static final String KEY_ALERTS_TYPE = "preferences_alerts_type";
74    private static final String ALERT_TYPE_ALERTS = "0";
75    private static final String ALERT_TYPE_STATUS_BAR = "1";
76    private static final String ALERT_TYPE_OFF = "2";
77    static final String KEY_HOME_TZ_ENABLED = "preferences_home_tz_enabled";
78    static final String KEY_HOME_TZ = "preferences_home_tz";
79
80    // The value to use when setting Calendar to use the device's time zone
81    public static final String LOCAL_TZ = "AUTO";
82
83    // Default preference values
84    public static final int DEFAULT_START_VIEW = CalendarController.ViewType.WEEK;
85    public static final int DEFAULT_DETAILED_VIEW = CalendarController.ViewType.DAY;
86
87    CheckBoxPreference mAlert;
88    ListPreference mVibrateWhen;
89    RingtonePreference mRingtone;
90    CheckBoxPreference mPopup;
91    CheckBoxPreference mUseHomeTZ;
92    ListPreference mHomeTZ;
93
94    /** Return a properly configured SharedPreferences instance */
95    public static SharedPreferences getSharedPreferences(Context context) {
96        return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
97    }
98
99    /** Set the default shared preferences in the proper context */
100    public static void setDefaultValues(Context context) {
101        PreferenceManager.setDefaultValues(context, SHARED_PREFS_NAME, Context.MODE_PRIVATE,
102                R.xml.general_preferences, false);
103    }
104
105    @Override
106    public void onCreate(Bundle icicle) {
107        super.onCreate(icicle);
108
109        final Activity activity = getActivity();
110
111        // Make sure to always use the same preferences file regardless of the package name
112        // we're running under
113        final PreferenceManager preferenceManager = getPreferenceManager();
114        final SharedPreferences sharedPreferences = getSharedPreferences(activity);
115        preferenceManager.setSharedPreferencesName(SHARED_PREFS_NAME);
116
117        // Load the preferences from an XML resource
118        addPreferencesFromResource(R.xml.general_preferences);
119
120        PreferenceScreen preferenceScreen = getPreferenceScreen();
121        preferenceScreen.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
122        mAlert = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS);
123        mVibrateWhen = (ListPreference) preferenceScreen.findPreference(KEY_ALERTS_VIBRATE_WHEN);
124        mRingtone = (RingtonePreference) preferenceScreen.findPreference(KEY_ALERTS_RINGTONE);
125        mPopup = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS_POPUP);
126        mUseHomeTZ = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HOME_TZ_ENABLED);
127        mUseHomeTZ.setOnPreferenceChangeListener(this);
128        mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ);
129        mHomeTZ.setSummary(mHomeTZ.getEntry());
130        mHomeTZ.setOnPreferenceChangeListener(this);
131
132        migrateOldPreferences(sharedPreferences);
133
134        updateChildPreferences();
135    }
136
137    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
138        if (key.equals(KEY_ALERTS)) {
139            updateChildPreferences();
140        }
141    }
142
143    /**
144     * Handles time zone preference changes
145     */
146    @Override
147    public boolean onPreferenceChange(Preference preference, Object newValue) {
148        String tz;
149        if (preference == mUseHomeTZ) {
150            if ((Boolean)newValue) {
151                tz = mHomeTZ.getValue();
152            } else {
153                tz = LOCAL_TZ;
154            }
155        } else if (preference == mHomeTZ) {
156            mHomeTZ.setValue((String)newValue);
157            mHomeTZ.setSummary(mHomeTZ.getEntry());
158            tz = (String)newValue;
159        } else {
160            return false;
161        }
162        Utils.setTimeZone(getActivity(), tz);
163        return true;
164    }
165
166    /**
167     * If necessary, upgrades previous versions of preferences to the current
168     * set of keys and values.
169     * @param prefs the preferences to upgrade
170     */
171    private void migrateOldPreferences(SharedPreferences prefs) {
172        // If needed, migrate vibration setting from a previous version
173        if (!prefs.contains(KEY_ALERTS_VIBRATE_WHEN) &&
174                prefs.contains(KEY_ALERTS_VIBRATE)) {
175            int stringId = prefs.getBoolean(KEY_ALERTS_VIBRATE, false) ?
176                    R.string.prefDefault_alerts_vibrate_true :
177                    R.string.prefDefault_alerts_vibrate_false;
178            mVibrateWhen.setValue(getActivity().getString(stringId));
179        }
180        // If needed, migrate the old alerts type settin
181        if (!prefs.contains(KEY_ALERTS) && prefs.contains(KEY_ALERTS_TYPE)) {
182            String type = prefs.getString(KEY_ALERTS_TYPE, ALERT_TYPE_STATUS_BAR);
183            if (type.equals(ALERT_TYPE_OFF)) {
184                mAlert.setChecked(false);
185                mPopup.setChecked(false);
186                mPopup.setEnabled(false);
187            } else if (type.equals(ALERT_TYPE_STATUS_BAR)) {
188                mAlert.setChecked(true);
189                mPopup.setChecked(false);
190                mPopup.setEnabled(true);
191            } else if (type.equals(ALERT_TYPE_ALERTS)) {
192                mAlert.setChecked(true);
193                mPopup.setChecked(true);
194                mPopup.setEnabled(true);
195            }
196            // clear out the old setting
197            prefs.edit().remove(KEY_ALERTS_TYPE).commit();
198        }
199    }
200
201    /**
202     * Keeps the dependent settings in sync with the parent preference, so for
203     * example, when notifications are turned off, we disable the preferences
204     * for configuring the exact notification behavior.
205     */
206    private void updateChildPreferences() {
207        if (mAlert.isChecked()) {
208            mVibrateWhen.setEnabled(true);
209            mRingtone.setEnabled(true);
210            mPopup.setEnabled(true);
211        } else {
212            mVibrateWhen.setValue(
213                    getActivity().getString(R.string.prefDefault_alerts_vibrate_false));
214            mVibrateWhen.setEnabled(false);
215            mRingtone.setEnabled(false);
216            mPopup.setEnabled(false);
217        }
218    }
219
220
221    @Override @SuppressWarnings("deprecation")
222    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
223        String key = preference.getKey();
224        if (key.equals(KEY_CLEAR_SEARCH_HISTORY)) {
225            SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(),
226                    CalendarRecentSuggestionsProvider.AUTHORITY,
227                    CalendarRecentSuggestionsProvider.MODE);
228            suggestions.clearHistory();
229            Toast.makeText(getActivity(), R.string.search_history_cleared,
230                    Toast.LENGTH_SHORT).show();
231            return true;
232        }
233        return false;
234    }
235
236
237}
238