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.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
     22 import android.content.pm.PackageInfo;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.os.Bundle;
     25 import android.preference.CheckBoxPreference;
     26 import android.preference.ListPreference;
     27 import android.preference.Preference;
     28 import android.preference.Preference.OnPreferenceChangeListener;
     29 import android.preference.PreferenceActivity;
     30 import android.preference.PreferenceManager;
     31 import android.preference.PreferenceScreen;
     32 import android.preference.RingtonePreference;
     33 import android.provider.Calendar.CalendarCache;
     34 import android.text.TextUtils;
     35 
     36 public class CalendarPreferenceActivity extends PreferenceActivity implements
     37         OnSharedPreferenceChangeListener, OnPreferenceChangeListener {
     38     private static final String BUILD_VERSION = "build_version";
     39 
     40     // The name of the shared preferences file. This name must be maintained for historical
     41     // reasons, as it's what PreferenceManager assigned the first time the file was created.
     42     private static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
     43 
     44     // Preference keys
     45     static final String KEY_HIDE_DECLINED = "preferences_hide_declined";
     46     static final String KEY_ALERTS_TYPE = "preferences_alerts_type";
     47     static final String KEY_ALERTS_VIBRATE = "preferences_alerts_vibrate";
     48     static final String KEY_ALERTS_VIBRATE_WHEN = "preferences_alerts_vibrateWhen";
     49     static final String KEY_ALERTS_RINGTONE = "preferences_alerts_ringtone";
     50     static final String KEY_DEFAULT_REMINDER = "preferences_default_reminder";
     51     static final String KEY_START_VIEW = "startView";
     52     static final String KEY_DETAILED_VIEW = "preferredDetailedView";
     53     static final String KEY_DEFAULT_CALENDAR = "preference_defaultCalendar";
     54     static final String KEY_HOME_TZ_ENABLED = "preferences_home_tz_enabled";
     55     static final String KEY_HOME_TZ = "preferences_home_tz";
     56 
     57     // These must be in sync with the array preferences_alert_type_values
     58     static final String ALERT_TYPE_ALERTS = "0";
     59     static final String ALERT_TYPE_STATUS_BAR = "1";
     60     static final String ALERT_TYPE_OFF = "2";
     61 
     62     // Default preference values
     63     static final String DEFAULT_START_VIEW =
     64             CalendarApplication.ACTIVITY_NAMES[CalendarApplication.MONTH_VIEW_ID];
     65     static final String DEFAULT_DETAILED_VIEW =
     66             CalendarApplication.ACTIVITY_NAMES[CalendarApplication.DAY_VIEW_ID];
     67 
     68 
     69     ListPreference mAlertType;
     70     ListPreference mVibrateWhen;
     71     RingtonePreference mRingtone;
     72     CheckBoxPreference mUseHomeTZ;
     73     ListPreference mHomeTZ;
     74 
     75     private static CharSequence[][] mTimezones;
     76 
     77     // In case we need to update something later
     78     private Runnable mTZUpdater = null;
     79 
     80     /** Return a properly configured SharedPreferences instance */
     81     public static SharedPreferences getSharedPreferences(Context context) {
     82         return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
     83     }
     84 
     85     /** Set the default shared preferences in the proper context */
     86     public static void setDefaultValues(Context context) {
     87         PreferenceManager.setDefaultValues(context, SHARED_PREFS_NAME, Context.MODE_PRIVATE,
     88                 R.xml.preferences, false);
     89     }
     90 
     91     @Override
     92     protected void onCreate(Bundle icicle) {
     93         super.onCreate(icicle);
     94 
     95         // Make sure to always use the same preferences file regardless of the package name
     96         // we're running under
     97         PreferenceManager preferenceManager = getPreferenceManager();
     98         SharedPreferences sharedPreferences = getSharedPreferences(this);
     99         preferenceManager.setSharedPreferencesName(SHARED_PREFS_NAME);
    100 
    101         // Load the preferences from an XML resource
    102         addPreferencesFromResource(R.xml.preferences);
    103 
    104         PreferenceScreen preferenceScreen = getPreferenceScreen();
    105         preferenceScreen.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    106         mAlertType = (ListPreference) preferenceScreen.findPreference(KEY_ALERTS_TYPE);
    107         mVibrateWhen = (ListPreference) preferenceScreen.findPreference(KEY_ALERTS_VIBRATE_WHEN);
    108         mRingtone = (RingtonePreference) preferenceScreen.findPreference(KEY_ALERTS_RINGTONE);
    109         mUseHomeTZ = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HOME_TZ_ENABLED);
    110         mUseHomeTZ.setOnPreferenceChangeListener(this);
    111         mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ);
    112         String tz = mHomeTZ.getValue();
    113 
    114         if (mTimezones == null) {
    115             mTimezones = (new TimezoneAdapter(this, tz)).getAllTimezones();
    116         }
    117         mHomeTZ.setEntryValues(mTimezones[0]);
    118         mHomeTZ.setEntries(mTimezones[1]);
    119         CharSequence tzName = mHomeTZ.getEntry();
    120         if (!TextUtils.isEmpty(tzName)) {
    121             mHomeTZ.setSummary(tzName);
    122         } else {
    123             mHomeTZ.setSummary(Utils.getTimeZone(this, mTZUpdater));
    124         }
    125         mHomeTZ.setOnPreferenceChangeListener(this);
    126 
    127         // If needed, migrate vibration setting from a previous version
    128         if (!sharedPreferences.contains(KEY_ALERTS_VIBRATE_WHEN) &&
    129                 sharedPreferences.contains(KEY_ALERTS_VIBRATE)) {
    130             int stringId = sharedPreferences.getBoolean(KEY_ALERTS_VIBRATE, false) ?
    131                     R.string.prefDefault_alerts_vibrate_true :
    132                     R.string.prefDefault_alerts_vibrate_false;
    133             mVibrateWhen.setValue(getString(stringId));
    134         }
    135 
    136         try {
    137             PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    138             findPreference(BUILD_VERSION).setSummary(packageInfo.versionName);
    139         } catch (NameNotFoundException e) {
    140             findPreference(BUILD_VERSION).setSummary("?");
    141         }
    142         updateChildPreferences();
    143     }
    144 
    145     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    146         if (key.equals(KEY_ALERTS_TYPE)) {
    147             updateChildPreferences();
    148         }
    149     }
    150 
    151     private void updateChildPreferences() {
    152         if (mAlertType.getValue().equals(ALERT_TYPE_OFF)) {
    153             mVibrateWhen.setValue(getString(R.string.prefDefault_alerts_vibrate_false));
    154             mVibrateWhen.setEnabled(false);
    155             mRingtone.setEnabled(false);
    156         } else {
    157             mVibrateWhen.setEnabled(true);
    158             mRingtone.setEnabled(true);
    159         }
    160     }
    161 
    162     /**
    163      * Handles time zone preference changes
    164      */
    165     @Override
    166     public boolean onPreferenceChange(Preference preference, Object newValue) {
    167         String tz;
    168         if (preference == mUseHomeTZ) {
    169             if ((Boolean)newValue) {
    170                 tz = mHomeTZ.getValue();
    171             } else {
    172                 tz = CalendarCache.TIMEZONE_TYPE_AUTO;
    173             }
    174         } else if (preference == mHomeTZ) {
    175             tz = (String)newValue;
    176             mHomeTZ.setValue(tz);
    177             mHomeTZ.setSummary(mHomeTZ.getEntry());
    178         } else {
    179             return false;
    180         }
    181         Utils.setTimeZone(this, tz);
    182         return true;
    183     }
    184 }
    185