1 /* 2 * Copyright (C) 2011 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.Dialog; 21 import android.app.TimePickerDialog; 22 import android.content.ComponentName; 23 import android.content.Intent; 24 import android.content.SharedPreferences; 25 import android.os.Bundle; 26 import android.preference.CheckBoxPreference; 27 import android.preference.ListPreference; 28 import android.preference.Preference; 29 import android.preference.Preference.OnPreferenceChangeListener; 30 import android.preference.PreferenceFragment; 31 import android.preference.PreferenceManager; 32 import android.preference.PreferenceScreen; 33 import android.text.format.DateFormat; 34 import android.text.format.Time; 35 import android.util.Log; 36 import android.widget.TimePicker; 37 38 public class OtherPreferences extends PreferenceFragment implements OnPreferenceChangeListener{ 39 private static final String TAG = "CalendarOtherPreferences"; 40 41 // The name of the shared preferences file. This name must be maintained for 42 // historical reasons, as it's what PreferenceManager assigned the first 43 // time the file was created. 44 static final String SHARED_PREFS_NAME = "com.android.calendar_preferences"; 45 46 // Must be the same keys that are used in the other_preferences.xml file. 47 public static final String KEY_OTHER_COPY_DB = "preferences_copy_db"; 48 public static final String KEY_OTHER_QUIET_HOURS = "preferences_reminders_quiet_hours"; 49 public static final String KEY_OTHER_REMINDERS_RESPONDED = "preferences_reminders_responded"; 50 public static final String KEY_OTHER_QUIET_HOURS_START = 51 "preferences_reminders_quiet_hours_start"; 52 public static final String KEY_OTHER_QUIET_HOURS_START_HOUR = 53 "preferences_reminders_quiet_hours_start_hour"; 54 public static final String KEY_OTHER_QUIET_HOURS_START_MINUTE = 55 "preferences_reminders_quiet_hours_start_minute"; 56 public static final String KEY_OTHER_QUIET_HOURS_END = 57 "preferences_reminders_quiet_hours_end"; 58 public static final String KEY_OTHER_QUIET_HOURS_END_HOUR = 59 "preferences_reminders_quiet_hours_end_hour"; 60 public static final String KEY_OTHER_QUIET_HOURS_END_MINUTE = 61 "preferences_reminders_quiet_hours_end_minute"; 62 public static final String KEY_OTHER_1 = "preferences_tardis_1"; 63 64 public static final int QUIET_HOURS_DEFAULT_START_HOUR = 22; 65 public static final int QUIET_HOURS_DEFAULT_START_MINUTE = 0; 66 public static final int QUIET_HOURS_DEFAULT_END_HOUR = 8; 67 public static final int QUIET_HOURS_DEFAULT_END_MINUTE = 0; 68 69 private static final int START_LISTENER = 1; 70 private static final int END_LISTENER = 2; 71 private static final String format24Hour = "%H:%M"; 72 private static final String format12Hour = "%I:%M%P"; 73 74 private Preference mCopyDb; 75 private ListPreference mSkipReminders; 76 private CheckBoxPreference mQuietHours; 77 private Preference mQuietHoursStart; 78 private Preference mQuietHoursEnd; 79 80 private TimePickerDialog mTimePickerDialog; 81 private TimeSetListener mQuietHoursStartListener; 82 private TimePickerDialog mQuietHoursStartDialog; 83 private TimeSetListener mQuietHoursEndListener; 84 private TimePickerDialog mQuietHoursEndDialog; 85 private boolean mIs24HourMode; 86 OtherPreferences()87 public OtherPreferences() { 88 } 89 90 @Override onCreate(Bundle icicle)91 public void onCreate(Bundle icicle) { 92 super.onCreate(icicle); 93 PreferenceManager manager = getPreferenceManager(); 94 manager.setSharedPreferencesName(SHARED_PREFS_NAME); 95 SharedPreferences prefs = manager.getSharedPreferences(); 96 97 addPreferencesFromResource(R.xml.other_preferences); 98 mCopyDb = findPreference(KEY_OTHER_COPY_DB); 99 mSkipReminders = (ListPreference) findPreference(KEY_OTHER_REMINDERS_RESPONDED); 100 String skipPreferencesValue = null; 101 if (mSkipReminders != null) { 102 skipPreferencesValue = mSkipReminders.getValue(); 103 mSkipReminders.setOnPreferenceChangeListener(this); 104 } 105 updateSkipRemindersSummary(skipPreferencesValue); 106 107 Activity activity = getActivity(); 108 if (activity == null) { 109 Log.d(TAG, "Activity was null"); 110 } 111 mIs24HourMode = DateFormat.is24HourFormat(activity); 112 113 mQuietHours = 114 (CheckBoxPreference) findPreference(KEY_OTHER_QUIET_HOURS); 115 116 int startHour = prefs.getInt(KEY_OTHER_QUIET_HOURS_START_HOUR, 117 QUIET_HOURS_DEFAULT_START_HOUR); 118 int startMinute = prefs.getInt(KEY_OTHER_QUIET_HOURS_START_MINUTE, 119 QUIET_HOURS_DEFAULT_START_MINUTE); 120 mQuietHoursStart = findPreference(KEY_OTHER_QUIET_HOURS_START); 121 mQuietHoursStartListener = new TimeSetListener(START_LISTENER); 122 mQuietHoursStartDialog = new TimePickerDialog( 123 activity, mQuietHoursStartListener, 124 startHour, startMinute, mIs24HourMode); 125 mQuietHoursStart.setSummary(formatTime(startHour, startMinute)); 126 127 int endHour = prefs.getInt(KEY_OTHER_QUIET_HOURS_END_HOUR, 128 QUIET_HOURS_DEFAULT_END_HOUR); 129 int endMinute = prefs.getInt(KEY_OTHER_QUIET_HOURS_END_MINUTE, 130 QUIET_HOURS_DEFAULT_END_MINUTE); 131 mQuietHoursEnd = findPreference(KEY_OTHER_QUIET_HOURS_END); 132 mQuietHoursEndListener = new TimeSetListener(END_LISTENER); 133 mQuietHoursEndDialog = new TimePickerDialog( 134 activity, mQuietHoursEndListener, 135 endHour, endMinute, mIs24HourMode); 136 mQuietHoursEnd.setSummary(formatTime(endHour, endMinute)); 137 } 138 139 @Override onPreferenceChange(Preference preference, Object objValue)140 public boolean onPreferenceChange(Preference preference, Object objValue) { 141 final String key = preference.getKey(); 142 143 if (KEY_OTHER_REMINDERS_RESPONDED.equals(key)) { 144 String value = String.valueOf(objValue); 145 updateSkipRemindersSummary(value); 146 } 147 148 return true; 149 } 150 151 @Override onPreferenceTreeClick(PreferenceScreen screen, Preference preference)152 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { 153 if (preference == mCopyDb) { 154 Intent intent = new Intent(Intent.ACTION_MAIN); 155 intent.setComponent(new ComponentName("com.android.providers.calendar", 156 "com.android.providers.calendar.CalendarDebugActivity")); 157 startActivity(intent); 158 } else if (preference == mQuietHoursStart) { 159 if (mTimePickerDialog == null) { 160 mTimePickerDialog = mQuietHoursStartDialog; 161 mTimePickerDialog.show(); 162 } else { 163 Log.v(TAG, "not null"); 164 } 165 } else if (preference == mQuietHoursEnd) { 166 if (mTimePickerDialog == null) { 167 mTimePickerDialog = mQuietHoursEndDialog; 168 mTimePickerDialog.show(); 169 } else { 170 Log.v(TAG, "not null"); 171 } 172 } else { 173 return super.onPreferenceTreeClick(screen, preference); 174 } 175 return true; 176 } 177 178 private class TimeSetListener implements TimePickerDialog.OnTimeSetListener { 179 private int mListenerId; 180 TimeSetListener(int listenerId)181 public TimeSetListener(int listenerId) { 182 mListenerId = listenerId; 183 } 184 185 @Override onTimeSet(TimePicker view, int hourOfDay, int minute)186 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 187 mTimePickerDialog = null; 188 189 SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); 190 SharedPreferences.Editor editor = prefs.edit(); 191 192 String summary = formatTime(hourOfDay, minute); 193 switch (mListenerId) { 194 case (START_LISTENER): 195 mQuietHoursStart.setSummary(summary); 196 editor.putInt(KEY_OTHER_QUIET_HOURS_START_HOUR, hourOfDay); 197 editor.putInt(KEY_OTHER_QUIET_HOURS_START_MINUTE, minute); 198 break; 199 case (END_LISTENER): 200 mQuietHoursEnd.setSummary(summary); 201 editor.putInt(KEY_OTHER_QUIET_HOURS_END_HOUR, hourOfDay); 202 editor.putInt(KEY_OTHER_QUIET_HOURS_END_MINUTE, minute); 203 break; 204 default: 205 Log.d(TAG, "Set time for unknown listener: "+mListenerId); 206 } 207 208 editor.commit(); 209 } 210 } 211 212 /** 213 * @param hourOfDay the hour of the day (0-24) 214 * @param minute 215 * @return human-readable string formatted based on 24-hour mode. 216 */ formatTime(int hourOfDay, int minute)217 private String formatTime(int hourOfDay, int minute) { 218 Time time = new Time(); 219 time.hour = hourOfDay; 220 time.minute = minute; 221 222 String format = mIs24HourMode? format24Hour : format12Hour; 223 return time.format(format); 224 } 225 226 /** 227 * Update the summary for the SkipReminders preference. 228 * @param value The corresponding value of which summary to set. If null, the default summary 229 * will be set, and the value will be set accordingly too. 230 */ updateSkipRemindersSummary(String value)231 private void updateSkipRemindersSummary(String value) { 232 if (mSkipReminders != null) { 233 // Default to "declined". Must match with R.array.preferences_skip_reminders_values. 234 int index = 0; 235 236 CharSequence[] values = mSkipReminders.getEntryValues(); 237 CharSequence[] entries = mSkipReminders.getEntries(); 238 for(int value_i = 0; value_i < values.length; value_i++) { 239 if (values[value_i].equals(value)) { 240 index = value_i; 241 break; 242 } 243 } 244 mSkipReminders.setSummary(entries[index].toString()); 245 if (value == null) { 246 // Value was not known ahead of time, so the default value will be set. 247 mSkipReminders.setValue(values[index].toString()); 248 } 249 } 250 } 251 } 252