/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.calendar; import com.android.calendar.alerts.AlertReceiver; import android.app.Activity; import android.app.backup.BackupManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.os.Vibrator; import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.preference.RingtonePreference; import android.provider.CalendarContract; import android.provider.CalendarContract.CalendarCache; import android.provider.SearchRecentSuggestions; import android.text.TextUtils; import android.widget.Toast; public class GeneralPreferences extends PreferenceFragment implements OnSharedPreferenceChangeListener, OnPreferenceChangeListener { // The name of the shared preferences file. This name must be maintained for historical // reasons, as it's what PreferenceManager assigned the first time the file was created. static final String SHARED_PREFS_NAME = "com.android.calendar_preferences"; // Preference keys public static final String KEY_HIDE_DECLINED = "preferences_hide_declined"; public static final String KEY_WEEK_START_DAY = "preferences_week_start_day"; public static final String KEY_SHOW_WEEK_NUM = "preferences_show_week_num"; public static final String KEY_DAYS_PER_WEEK = "preferences_days_per_week"; public static final String KEY_SKIP_SETUP = "preferences_skip_setup"; public static final String KEY_CLEAR_SEARCH_HISTORY = "preferences_clear_search_history"; public static final String KEY_ALERTS_CATEGORY = "preferences_alerts_category"; public static final String KEY_ALERTS = "preferences_alerts"; public static final String KEY_ALERTS_VIBRATE = "preferences_alerts_vibrate"; public static final String KEY_ALERTS_VIBRATE_WHEN = "preferences_alerts_vibrateWhen"; public static final String KEY_ALERTS_RINGTONE = "preferences_alerts_ringtone"; public static final String KEY_ALERTS_POPUP = "preferences_alerts_popup"; public static final String KEY_DEFAULT_REMINDER = "preferences_default_reminder"; public static final int NO_REMINDER = -1; public static final String NO_REMINDER_STRING = "-1"; public static final int REMINDER_DEFAULT_TIME = 10; // in minutes public static final String KEY_DEFAULT_CELL_HEIGHT = "preferences_default_cell_height"; /** Key to SharePreference for default view (CalendarController.ViewType) */ public static final String KEY_START_VIEW = "preferred_startView"; /** * Key to SharePreference for default detail view (CalendarController.ViewType) * Typically used by widget */ public static final String KEY_DETAILED_VIEW = "preferred_detailedView"; public static final String KEY_DEFAULT_CALENDAR = "preference_defaultCalendar"; // These must be in sync with the array preferences_week_start_day_values public static final String WEEK_START_DEFAULT = "-1"; public static final String WEEK_START_SATURDAY = "7"; public static final String WEEK_START_SUNDAY = "1"; public static final String WEEK_START_MONDAY = "2"; // These keys are kept to enable migrating users from previous versions private static final String KEY_ALERTS_TYPE = "preferences_alerts_type"; private static final String ALERT_TYPE_ALERTS = "0"; private static final String ALERT_TYPE_STATUS_BAR = "1"; private static final String ALERT_TYPE_OFF = "2"; static final String KEY_HOME_TZ_ENABLED = "preferences_home_tz_enabled"; static final String KEY_HOME_TZ = "preferences_home_tz"; // Default preference values public static final int DEFAULT_START_VIEW = CalendarController.ViewType.WEEK; public static final int DEFAULT_DETAILED_VIEW = CalendarController.ViewType.DAY; public static final boolean DEFAULT_SHOW_WEEK_NUM = false; CheckBoxPreference mAlert; ListPreference mVibrateWhen; RingtonePreference mRingtone; CheckBoxPreference mPopup; CheckBoxPreference mUseHomeTZ; CheckBoxPreference mHideDeclined; ListPreference mHomeTZ; ListPreference mWeekStart; ListPreference mDefaultReminder; private static CharSequence[][] mTimezones; /** Return a properly configured SharedPreferences instance */ public static SharedPreferences getSharedPreferences(Context context) { return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE); } /** Set the default shared preferences in the proper context */ public static void setDefaultValues(Context context) { PreferenceManager.setDefaultValues(context, SHARED_PREFS_NAME, Context.MODE_PRIVATE, R.xml.general_preferences, false); } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); final Activity activity = getActivity(); // Make sure to always use the same preferences file regardless of the package name // we're running under final PreferenceManager preferenceManager = getPreferenceManager(); final SharedPreferences sharedPreferences = getSharedPreferences(activity); preferenceManager.setSharedPreferencesName(SHARED_PREFS_NAME); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.general_preferences); final PreferenceScreen preferenceScreen = getPreferenceScreen(); mAlert = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS); mVibrateWhen = (ListPreference) preferenceScreen.findPreference(KEY_ALERTS_VIBRATE_WHEN); Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); if (vibrator == null || !vibrator.hasVibrator()) { PreferenceCategory mAlertGroup = (PreferenceCategory) preferenceScreen .findPreference(KEY_ALERTS_CATEGORY); mAlertGroup.removePreference(mVibrateWhen); } else { mVibrateWhen.setSummary(mVibrateWhen.getEntry()); } mRingtone = (RingtonePreference) preferenceScreen.findPreference(KEY_ALERTS_RINGTONE); mPopup = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS_POPUP); mUseHomeTZ = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HOME_TZ_ENABLED); mHideDeclined = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HIDE_DECLINED); mWeekStart = (ListPreference) preferenceScreen.findPreference(KEY_WEEK_START_DAY); mDefaultReminder = (ListPreference) preferenceScreen.findPreference(KEY_DEFAULT_REMINDER); mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ); String tz = mHomeTZ.getValue(); mWeekStart.setSummary(mWeekStart.getEntry()); mDefaultReminder.setSummary(mDefaultReminder.getEntry()); if (mTimezones == null) { mTimezones = (new TimezoneAdapter(activity, tz)).getAllTimezones(); } mHomeTZ.setEntryValues(mTimezones[0]); mHomeTZ.setEntries(mTimezones[1]); CharSequence tzName = mHomeTZ.getEntry(); if (TextUtils.isEmpty(tzName)) { tzName = Utils.getTimeZone(activity, null); } mHomeTZ.setSummary(tzName); migrateOldPreferences(sharedPreferences); updateChildPreferences(); } @Override public void onResume() { super.onResume(); getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); setPreferenceListeners(this); } /** * Sets up all the preference change listeners to use the specified * listener. */ private void setPreferenceListeners(OnPreferenceChangeListener listener) { mUseHomeTZ.setOnPreferenceChangeListener(listener); mHomeTZ.setOnPreferenceChangeListener(listener); mWeekStart.setOnPreferenceChangeListener(listener); mDefaultReminder.setOnPreferenceChangeListener(listener); mRingtone.setOnPreferenceChangeListener(listener); mHideDeclined.setOnPreferenceChangeListener(listener); mVibrateWhen.setOnPreferenceChangeListener(listener); } @Override public void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); setPreferenceListeners(null); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Activity a = getActivity(); if (key.equals(KEY_ALERTS)) { updateChildPreferences(); if (a != null) { Intent intent = new Intent(); intent.setClass(a, AlertReceiver.class); if (mAlert.isChecked()) { intent.setAction(AlertReceiver.ACTION_DISMISS_OLD_REMINDERS); } else { intent.setAction(CalendarContract.ACTION_EVENT_REMINDER); } a.sendBroadcast(intent); } } if (a != null) { BackupManager.dataChanged(a.getPackageName()); } } /** * Handles time zone preference changes */ @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String tz; if (preference == mUseHomeTZ) { if ((Boolean)newValue) { tz = mHomeTZ.getValue(); } else { tz = CalendarCache.TIMEZONE_TYPE_AUTO; } Utils.setTimeZone(getActivity(), tz); return true; } else if (preference == mHideDeclined) { mHideDeclined.setChecked((Boolean) newValue); Activity act = getActivity(); Intent intent = new Intent(Utils.getWidgetScheduledUpdateAction(act)); intent.setDataAndType(CalendarContract.CONTENT_URI, Utils.APPWIDGET_DATA_TYPE); act.sendBroadcast(intent); return true; } else if (preference == mHomeTZ) { tz = (String) newValue; // We set the value here so we can read back the entry mHomeTZ.setValue(tz); mHomeTZ.setSummary(mHomeTZ.getEntry()); Utils.setTimeZone(getActivity(), tz); } else if (preference == mWeekStart) { mWeekStart.setValue((String) newValue); mWeekStart.setSummary(mWeekStart.getEntry()); } else if (preference == mDefaultReminder) { mDefaultReminder.setValue((String) newValue); mDefaultReminder.setSummary(mDefaultReminder.getEntry()); } else if (preference == mRingtone) { // TODO update this after b/3417832 is fixed return true; } else if (preference == mVibrateWhen) { mVibrateWhen.setValue((String)newValue); mVibrateWhen.setSummary(mVibrateWhen.getEntry()); } else { return true; } return false; } /** * If necessary, upgrades previous versions of preferences to the current * set of keys and values. * @param prefs the preferences to upgrade */ private void migrateOldPreferences(SharedPreferences prefs) { // If needed, migrate vibration setting from a previous version if (!prefs.contains(KEY_ALERTS_VIBRATE_WHEN) && prefs.contains(KEY_ALERTS_VIBRATE)) { int stringId = prefs.getBoolean(KEY_ALERTS_VIBRATE, false) ? R.string.prefDefault_alerts_vibrate_true : R.string.prefDefault_alerts_vibrate_false; mVibrateWhen.setValue(getActivity().getString(stringId)); } // If needed, migrate the old alerts type settin if (!prefs.contains(KEY_ALERTS) && prefs.contains(KEY_ALERTS_TYPE)) { String type = prefs.getString(KEY_ALERTS_TYPE, ALERT_TYPE_STATUS_BAR); if (type.equals(ALERT_TYPE_OFF)) { mAlert.setChecked(false); mPopup.setChecked(false); mPopup.setEnabled(false); } else if (type.equals(ALERT_TYPE_STATUS_BAR)) { mAlert.setChecked(true); mPopup.setChecked(false); mPopup.setEnabled(true); } else if (type.equals(ALERT_TYPE_ALERTS)) { mAlert.setChecked(true); mPopup.setChecked(true); mPopup.setEnabled(true); } // clear out the old setting prefs.edit().remove(KEY_ALERTS_TYPE).commit(); } } /** * Keeps the dependent settings in sync with the parent preference, so for * example, when notifications are turned off, we disable the preferences * for configuring the exact notification behavior. */ private void updateChildPreferences() { if (mAlert.isChecked()) { mVibrateWhen.setEnabled(true); mRingtone.setEnabled(true); mPopup.setEnabled(true); } else { mVibrateWhen.setValue( getActivity().getString(R.string.prefDefault_alerts_vibrate_false)); mVibrateWhen.setEnabled(false); mRingtone.setEnabled(false); mPopup.setEnabled(false); } } @Override public boolean onPreferenceTreeClick( PreferenceScreen preferenceScreen, Preference preference) { final String key = preference.getKey(); if (key.equals(KEY_CLEAR_SEARCH_HISTORY)) { SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(), Utils.getSearchAuthority(getActivity()), CalendarRecentSuggestionsProvider.MODE); suggestions.clearHistory(); Toast.makeText(getActivity(), R.string.search_history_cleared, Toast.LENGTH_SHORT).show(); return true; } return false; } }