blob: d98d3e65a9e7d7d73194bec40ce35f041a39863c [file] [log] [blame]
/*
* 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 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.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
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;
import com.android.calendar.alerts.AlertReceiver;
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_RINGTONE = "preferences_alerts_ringtone";
public static final String KEY_ALERTS_POPUP = "preferences_alerts_popup";
public static final String KEY_SHOW_CONTROLS = "preferences_show_controls";
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";
public static final String KEY_VERSION = "preferences_version";
/** 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;
CheckBoxPreference mVibrate;
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);
mVibrate = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS_VIBRATE);
Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator == null || !vibrator.hasVibrator()) {
PreferenceCategory mAlertGroup = (PreferenceCategory) preferenceScreen
.findPreference(KEY_ALERTS_CATEGORY);
mAlertGroup.removePreference(mVibrate);
}
mRingtone = (RingtonePreference) preferenceScreen.findPreference(KEY_ALERTS_RINGTONE);
String ringToneUri = Utils.getSharedPreference(activity, KEY_ALERTS_RINGTONE, "");
if (!TextUtils.isEmpty(ringToneUri)) {
String ringtone = getRingtoneTitleFromUri(getActivity(), ringToneUri);
mRingtone.setSummary(ringtone == null ? "" : 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, System.currentTimeMillis()))
.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 onStart() {
super.onStart();
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);
mVibrate.setOnPreferenceChangeListener(listener);
}
@Override
public void onStop() {
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
setPreferenceListeners(null);
super.onStop();
}
@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) {
if (newValue instanceof String) {
String ringtone = getRingtoneTitleFromUri(getActivity(), (String) newValue);
mRingtone.setSummary(ringtone == null ? "" : ringtone);
}
return true;
} else if (preference == mVibrate) {
mVibrate.setChecked((Boolean) newValue);
return true;
} else {
return true;
}
return false;
}
public String getRingtoneTitleFromUri(Context context, String uri) {
if (TextUtils.isEmpty(uri)) {
return null;
}
Ringtone ring = RingtoneManager.getRingtone(getActivity(), Uri.parse(uri));
if (ring != null) {
return ring.getTitle(context);
}
return null;
}
/**
* 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
mVibrate.setChecked(Utils.getDefaultVibrate(getActivity(), prefs));
// 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()) {
mVibrate.setEnabled(true);
mRingtone.setEnabled(true);
mPopup.setEnabled(true);
} else {
mVibrate.setEnabled(false);
mRingtone.setEnabled(false);
mPopup.setEnabled(false);
}
}
@Override
public boolean onPreferenceTreeClick(
PreferenceScreen preferenceScreen, Preference preference) {
final String key = preference.getKey();
if (KEY_CLEAR_SEARCH_HISTORY.equals(key)) {
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;
} else {
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
}