Home | History | Annotate | Download | only in calendar
      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 
     17 package com.android.calendar;
     18 
     19 import android.app.Activity;
     20 import android.app.backup.BackupManager;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
     25 import android.os.Bundle;
     26 import android.os.Vibrator;
     27 import android.preference.CheckBoxPreference;
     28 import android.preference.ListPreference;
     29 import android.preference.Preference;
     30 import android.preference.Preference.OnPreferenceChangeListener;
     31 import android.preference.PreferenceCategory;
     32 import android.preference.PreferenceFragment;
     33 import android.preference.PreferenceManager;
     34 import android.preference.PreferenceScreen;
     35 import android.preference.RingtonePreference;
     36 import android.provider.CalendarContract;
     37 import android.provider.CalendarContract.CalendarCache;
     38 import android.provider.SearchRecentSuggestions;
     39 import android.text.TextUtils;
     40 import android.widget.Toast;
     41 
     42 import com.android.calendar.alerts.AlertReceiver;
     43 
     44 public class GeneralPreferences extends PreferenceFragment implements
     45         OnSharedPreferenceChangeListener, OnPreferenceChangeListener {
     46     // The name of the shared preferences file. This name must be maintained for historical
     47     // reasons, as it's what PreferenceManager assigned the first time the file was created.
     48     static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
     49 
     50     // Preference keys
     51     public static final String KEY_HIDE_DECLINED = "preferences_hide_declined";
     52     public static final String KEY_WEEK_START_DAY = "preferences_week_start_day";
     53     public static final String KEY_SHOW_WEEK_NUM = "preferences_show_week_num";
     54     public static final String KEY_DAYS_PER_WEEK = "preferences_days_per_week";
     55     public static final String KEY_SKIP_SETUP = "preferences_skip_setup";
     56 
     57     public static final String KEY_CLEAR_SEARCH_HISTORY = "preferences_clear_search_history";
     58 
     59     public static final String KEY_ALERTS_CATEGORY = "preferences_alerts_category";
     60     public static final String KEY_ALERTS = "preferences_alerts";
     61     public static final String KEY_ALERTS_VIBRATE = "preferences_alerts_vibrate";
     62     public static final String KEY_ALERTS_VIBRATE_WHEN = "preferences_alerts_vibrateWhen";
     63     public static final String KEY_ALERTS_RINGTONE = "preferences_alerts_ringtone";
     64     public static final String KEY_ALERTS_POPUP = "preferences_alerts_popup";
     65 
     66     public static final String KEY_SHOW_CONTROLS = "preferences_show_controls";
     67 
     68     public static final String KEY_DEFAULT_REMINDER = "preferences_default_reminder";
     69     public static final int NO_REMINDER = -1;
     70     public static final String NO_REMINDER_STRING = "-1";
     71     public static final int REMINDER_DEFAULT_TIME = 10; // in minutes
     72 
     73     public static final String KEY_DEFAULT_CELL_HEIGHT = "preferences_default_cell_height";
     74 
     75     /** Key to SharePreference for default view (CalendarController.ViewType) */
     76     public static final String KEY_START_VIEW = "preferred_startView";
     77     /**
     78      *  Key to SharePreference for default detail view (CalendarController.ViewType)
     79      *  Typically used by widget
     80      */
     81     public static final String KEY_DETAILED_VIEW = "preferred_detailedView";
     82     public static final String KEY_DEFAULT_CALENDAR = "preference_defaultCalendar";
     83 
     84     // These must be in sync with the array preferences_week_start_day_values
     85     public static final String WEEK_START_DEFAULT = "-1";
     86     public static final String WEEK_START_SATURDAY = "7";
     87     public static final String WEEK_START_SUNDAY = "1";
     88     public static final String WEEK_START_MONDAY = "2";
     89 
     90     // These keys are kept to enable migrating users from previous versions
     91     private static final String KEY_ALERTS_TYPE = "preferences_alerts_type";
     92     private static final String ALERT_TYPE_ALERTS = "0";
     93     private static final String ALERT_TYPE_STATUS_BAR = "1";
     94     private static final String ALERT_TYPE_OFF = "2";
     95     static final String KEY_HOME_TZ_ENABLED = "preferences_home_tz_enabled";
     96     static final String KEY_HOME_TZ = "preferences_home_tz";
     97 
     98     // Default preference values
     99     public static final int DEFAULT_START_VIEW = CalendarController.ViewType.WEEK;
    100     public static final int DEFAULT_DETAILED_VIEW = CalendarController.ViewType.DAY;
    101     public static final boolean DEFAULT_SHOW_WEEK_NUM = false;
    102 
    103     CheckBoxPreference mAlert;
    104     ListPreference mVibrateWhen;
    105     RingtonePreference mRingtone;
    106     CheckBoxPreference mPopup;
    107     CheckBoxPreference mUseHomeTZ;
    108     CheckBoxPreference mHideDeclined;
    109     ListPreference mHomeTZ;
    110     ListPreference mWeekStart;
    111     ListPreference mDefaultReminder;
    112 
    113     private static CharSequence[][] mTimezones;
    114 
    115     /** Return a properly configured SharedPreferences instance */
    116     public static SharedPreferences getSharedPreferences(Context context) {
    117         return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
    118     }
    119 
    120     /** Set the default shared preferences in the proper context */
    121     public static void setDefaultValues(Context context) {
    122         PreferenceManager.setDefaultValues(context, SHARED_PREFS_NAME, Context.MODE_PRIVATE,
    123                 R.xml.general_preferences, false);
    124     }
    125 
    126     @Override
    127     public void onCreate(Bundle icicle) {
    128         super.onCreate(icicle);
    129 
    130         final Activity activity = getActivity();
    131 
    132         // Make sure to always use the same preferences file regardless of the package name
    133         // we're running under
    134         final PreferenceManager preferenceManager = getPreferenceManager();
    135         final SharedPreferences sharedPreferences = getSharedPreferences(activity);
    136         preferenceManager.setSharedPreferencesName(SHARED_PREFS_NAME);
    137 
    138         // Load the preferences from an XML resource
    139         addPreferencesFromResource(R.xml.general_preferences);
    140 
    141         final PreferenceScreen preferenceScreen = getPreferenceScreen();
    142         mAlert = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS);
    143         mVibrateWhen = (ListPreference) preferenceScreen.findPreference(KEY_ALERTS_VIBRATE_WHEN);
    144         Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
    145         if (vibrator == null || !vibrator.hasVibrator()) {
    146             PreferenceCategory mAlertGroup = (PreferenceCategory) preferenceScreen
    147                     .findPreference(KEY_ALERTS_CATEGORY);
    148             mAlertGroup.removePreference(mVibrateWhen);
    149         } else {
    150             mVibrateWhen.setSummary(mVibrateWhen.getEntry());
    151         }
    152 
    153         mRingtone = (RingtonePreference) preferenceScreen.findPreference(KEY_ALERTS_RINGTONE);
    154         mPopup = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS_POPUP);
    155         mUseHomeTZ = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HOME_TZ_ENABLED);
    156         mHideDeclined = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HIDE_DECLINED);
    157         mWeekStart = (ListPreference) preferenceScreen.findPreference(KEY_WEEK_START_DAY);
    158         mDefaultReminder = (ListPreference) preferenceScreen.findPreference(KEY_DEFAULT_REMINDER);
    159         mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ);
    160         String tz = mHomeTZ.getValue();
    161 
    162         mWeekStart.setSummary(mWeekStart.getEntry());
    163         mDefaultReminder.setSummary(mDefaultReminder.getEntry());
    164 
    165         if (mTimezones == null) {
    166             mTimezones = (new TimezoneAdapter(activity, tz, System.currentTimeMillis()))
    167                     .getAllTimezones();
    168         }
    169         mHomeTZ.setEntryValues(mTimezones[0]);
    170         mHomeTZ.setEntries(mTimezones[1]);
    171         CharSequence tzName = mHomeTZ.getEntry();
    172         if (TextUtils.isEmpty(tzName)) {
    173             tzName = Utils.getTimeZone(activity, null);
    174         }
    175         mHomeTZ.setSummary(tzName);
    176 
    177         migrateOldPreferences(sharedPreferences);
    178 
    179         updateChildPreferences();
    180     }
    181 
    182     @Override
    183     public void onResume() {
    184         super.onResume();
    185         getPreferenceScreen().getSharedPreferences()
    186                 .registerOnSharedPreferenceChangeListener(this);
    187         setPreferenceListeners(this);
    188     }
    189 
    190     /**
    191      * Sets up all the preference change listeners to use the specified
    192      * listener.
    193      */
    194     private void setPreferenceListeners(OnPreferenceChangeListener listener) {
    195         mUseHomeTZ.setOnPreferenceChangeListener(listener);
    196         mHomeTZ.setOnPreferenceChangeListener(listener);
    197         mWeekStart.setOnPreferenceChangeListener(listener);
    198         mDefaultReminder.setOnPreferenceChangeListener(listener);
    199         mRingtone.setOnPreferenceChangeListener(listener);
    200         mHideDeclined.setOnPreferenceChangeListener(listener);
    201         mVibrateWhen.setOnPreferenceChangeListener(listener);
    202 
    203     }
    204 
    205     @Override
    206     public void onPause() {
    207         super.onPause();
    208         getPreferenceScreen().getSharedPreferences()
    209                 .unregisterOnSharedPreferenceChangeListener(this);
    210         setPreferenceListeners(null);
    211     }
    212 
    213     @Override
    214     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    215         Activity a = getActivity();
    216         if (key.equals(KEY_ALERTS)) {
    217             updateChildPreferences();
    218             if (a != null) {
    219                 Intent intent = new Intent();
    220                 intent.setClass(a, AlertReceiver.class);
    221                 if (mAlert.isChecked()) {
    222                     intent.setAction(AlertReceiver.ACTION_DISMISS_OLD_REMINDERS);
    223                 } else {
    224                     intent.setAction(CalendarContract.ACTION_EVENT_REMINDER);
    225                 }
    226                 a.sendBroadcast(intent);
    227             }
    228         }
    229         if (a != null) {
    230             BackupManager.dataChanged(a.getPackageName());
    231         }
    232     }
    233 
    234     /**
    235      * Handles time zone preference changes
    236      */
    237     @Override
    238     public boolean onPreferenceChange(Preference preference, Object newValue) {
    239         String tz;
    240         if (preference == mUseHomeTZ) {
    241             if ((Boolean)newValue) {
    242                 tz = mHomeTZ.getValue();
    243             } else {
    244                 tz = CalendarCache.TIMEZONE_TYPE_AUTO;
    245             }
    246             Utils.setTimeZone(getActivity(), tz);
    247             return true;
    248         } else if (preference == mHideDeclined) {
    249             mHideDeclined.setChecked((Boolean) newValue);
    250             Activity act = getActivity();
    251             Intent intent = new Intent(Utils.getWidgetScheduledUpdateAction(act));
    252             intent.setDataAndType(CalendarContract.CONTENT_URI, Utils.APPWIDGET_DATA_TYPE);
    253             act.sendBroadcast(intent);
    254             return true;
    255         } else if (preference == mHomeTZ) {
    256             tz = (String) newValue;
    257             // We set the value here so we can read back the entry
    258             mHomeTZ.setValue(tz);
    259             mHomeTZ.setSummary(mHomeTZ.getEntry());
    260             Utils.setTimeZone(getActivity(), tz);
    261         } else if (preference == mWeekStart) {
    262             mWeekStart.setValue((String) newValue);
    263             mWeekStart.setSummary(mWeekStart.getEntry());
    264         } else if (preference == mDefaultReminder) {
    265             mDefaultReminder.setValue((String) newValue);
    266             mDefaultReminder.setSummary(mDefaultReminder.getEntry());
    267         } else if (preference == mRingtone) {
    268             // TODO update this after b/3417832 is fixed
    269             return true;
    270         } else if (preference == mVibrateWhen) {
    271             mVibrateWhen.setValue((String)newValue);
    272             mVibrateWhen.setSummary(mVibrateWhen.getEntry());
    273         } else {
    274             return true;
    275         }
    276         return false;
    277     }
    278 
    279     /**
    280      * If necessary, upgrades previous versions of preferences to the current
    281      * set of keys and values.
    282      * @param prefs the preferences to upgrade
    283      */
    284     private void migrateOldPreferences(SharedPreferences prefs) {
    285         // If needed, migrate vibration setting from a previous version
    286         if (!prefs.contains(KEY_ALERTS_VIBRATE_WHEN) &&
    287                 prefs.contains(KEY_ALERTS_VIBRATE)) {
    288             int stringId = prefs.getBoolean(KEY_ALERTS_VIBRATE, false) ?
    289                     R.string.prefDefault_alerts_vibrate_true :
    290                     R.string.prefDefault_alerts_vibrate_false;
    291             mVibrateWhen.setValue(getActivity().getString(stringId));
    292         }
    293         // If needed, migrate the old alerts type settin
    294         if (!prefs.contains(KEY_ALERTS) && prefs.contains(KEY_ALERTS_TYPE)) {
    295             String type = prefs.getString(KEY_ALERTS_TYPE, ALERT_TYPE_STATUS_BAR);
    296             if (type.equals(ALERT_TYPE_OFF)) {
    297                 mAlert.setChecked(false);
    298                 mPopup.setChecked(false);
    299                 mPopup.setEnabled(false);
    300             } else if (type.equals(ALERT_TYPE_STATUS_BAR)) {
    301                 mAlert.setChecked(true);
    302                 mPopup.setChecked(false);
    303                 mPopup.setEnabled(true);
    304             } else if (type.equals(ALERT_TYPE_ALERTS)) {
    305                 mAlert.setChecked(true);
    306                 mPopup.setChecked(true);
    307                 mPopup.setEnabled(true);
    308             }
    309             // clear out the old setting
    310             prefs.edit().remove(KEY_ALERTS_TYPE).commit();
    311         }
    312     }
    313 
    314     /**
    315      * Keeps the dependent settings in sync with the parent preference, so for
    316      * example, when notifications are turned off, we disable the preferences
    317      * for configuring the exact notification behavior.
    318      */
    319     private void updateChildPreferences() {
    320         if (mAlert.isChecked()) {
    321             mVibrateWhen.setEnabled(true);
    322             mRingtone.setEnabled(true);
    323             mPopup.setEnabled(true);
    324         } else {
    325             mVibrateWhen.setValue(
    326                     getActivity().getString(R.string.prefDefault_alerts_vibrate_false));
    327             mVibrateWhen.setEnabled(false);
    328             mRingtone.setEnabled(false);
    329             mPopup.setEnabled(false);
    330         }
    331     }
    332 
    333 
    334     @Override
    335     public boolean onPreferenceTreeClick(
    336             PreferenceScreen preferenceScreen, Preference preference) {
    337         final String key = preference.getKey();
    338         if (KEY_CLEAR_SEARCH_HISTORY.equals(key)) {
    339             SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(),
    340                     Utils.getSearchAuthority(getActivity()),
    341                     CalendarRecentSuggestionsProvider.MODE);
    342             suggestions.clearHistory();
    343             Toast.makeText(getActivity(), R.string.search_history_cleared,
    344                     Toast.LENGTH_SHORT).show();
    345             return true;
    346         } else {
    347             return super.onPreferenceTreeClick(preferenceScreen, preference);
    348         }
    349     }
    350 
    351 }
    352