• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CheckBoxPreference mQuietHours;
76     private Preference mQuietHoursStart;
77     private Preference mQuietHoursEnd;
78 
79     private TimePickerDialog mTimePickerDialog;
80     private TimeSetListener mQuietHoursStartListener;
81     private TimePickerDialog mQuietHoursStartDialog;
82     private TimeSetListener mQuietHoursEndListener;
83     private TimePickerDialog mQuietHoursEndDialog;
84     private boolean mIs24HourMode;
85 
OtherPreferences()86     public OtherPreferences() {
87     }
88 
89     @Override
onCreate(Bundle icicle)90     public void onCreate(Bundle icicle) {
91         super.onCreate(icicle);
92         PreferenceManager manager = getPreferenceManager();
93         manager.setSharedPreferencesName(SHARED_PREFS_NAME);
94         SharedPreferences prefs = manager.getSharedPreferences();
95 
96         addPreferencesFromResource(R.xml.other_preferences);
97         mCopyDb = findPreference(KEY_OTHER_COPY_DB);
98 
99         Activity activity = getActivity();
100         if (activity == null) {
101             Log.d(TAG, "Activity was null");
102         }
103         mIs24HourMode = DateFormat.is24HourFormat(activity);
104 
105         mQuietHours =
106                 (CheckBoxPreference) findPreference(KEY_OTHER_QUIET_HOURS);
107 
108         int startHour = prefs.getInt(KEY_OTHER_QUIET_HOURS_START_HOUR,
109                 QUIET_HOURS_DEFAULT_START_HOUR);
110         int startMinute = prefs.getInt(KEY_OTHER_QUIET_HOURS_START_MINUTE,
111                 QUIET_HOURS_DEFAULT_START_MINUTE);
112         mQuietHoursStart = findPreference(KEY_OTHER_QUIET_HOURS_START);
113         mQuietHoursStartListener = new TimeSetListener(START_LISTENER);
114         mQuietHoursStartDialog = new TimePickerDialog(
115                 activity, mQuietHoursStartListener,
116                 startHour, startMinute, mIs24HourMode);
117         mQuietHoursStart.setSummary(formatTime(startHour, startMinute));
118 
119         int endHour = prefs.getInt(KEY_OTHER_QUIET_HOURS_END_HOUR,
120                 QUIET_HOURS_DEFAULT_END_HOUR);
121         int endMinute = prefs.getInt(KEY_OTHER_QUIET_HOURS_END_MINUTE,
122                 QUIET_HOURS_DEFAULT_END_MINUTE);
123         mQuietHoursEnd = findPreference(KEY_OTHER_QUIET_HOURS_END);
124         mQuietHoursEndListener = new TimeSetListener(END_LISTENER);
125         mQuietHoursEndDialog = new TimePickerDialog(
126                 activity, mQuietHoursEndListener,
127                 endHour, endMinute, mIs24HourMode);
128         mQuietHoursEnd.setSummary(formatTime(endHour, endMinute));
129     }
130 
131     @Override
onPreferenceChange(Preference preference, Object objValue)132     public boolean onPreferenceChange(Preference preference, Object objValue) {
133         return true;
134     }
135 
136     @Override
onPreferenceTreeClick(PreferenceScreen screen, Preference preference)137     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
138         if (preference == mCopyDb) {
139             Intent intent = new Intent(Intent.ACTION_MAIN);
140             intent.setComponent(new ComponentName("com.android.providers.calendar",
141                     "com.android.providers.calendar.CalendarDebugActivity"));
142             startActivity(intent);
143         } else if (preference == mQuietHoursStart) {
144             if (mTimePickerDialog == null) {
145                 mTimePickerDialog = mQuietHoursStartDialog;
146                 mTimePickerDialog.show();
147             } else {
148                 Log.v(TAG, "not null");
149             }
150         } else if (preference == mQuietHoursEnd) {
151             if (mTimePickerDialog == null) {
152                 mTimePickerDialog = mQuietHoursEndDialog;
153                 mTimePickerDialog.show();
154             } else {
155                 Log.v(TAG, "not null");
156             }
157         } else {
158             return super.onPreferenceTreeClick(screen, preference);
159         }
160         return true;
161     }
162 
163     private class TimeSetListener implements TimePickerDialog.OnTimeSetListener {
164         private int mListenerId;
165 
TimeSetListener(int listenerId)166         public TimeSetListener(int listenerId) {
167             mListenerId = listenerId;
168         }
169 
170         @Override
onTimeSet(TimePicker view, int hourOfDay, int minute)171         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
172             mTimePickerDialog = null;
173 
174             SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
175             SharedPreferences.Editor editor = prefs.edit();
176 
177             String summary = formatTime(hourOfDay, minute);
178             switch (mListenerId) {
179                 case (START_LISTENER):
180                     mQuietHoursStart.setSummary(summary);
181                     editor.putInt(KEY_OTHER_QUIET_HOURS_START_HOUR, hourOfDay);
182                     editor.putInt(KEY_OTHER_QUIET_HOURS_START_MINUTE, minute);
183                     break;
184                 case (END_LISTENER):
185                     mQuietHoursEnd.setSummary(summary);
186                     editor.putInt(KEY_OTHER_QUIET_HOURS_END_HOUR, hourOfDay);
187                     editor.putInt(KEY_OTHER_QUIET_HOURS_END_MINUTE, minute);
188                     break;
189                 default:
190                     Log.d(TAG, "Set time for unknown listener: "+mListenerId);
191             }
192 
193             editor.commit();
194         }
195     }
196 
197     /**
198      * @param hourOfDay the hour of the day (0-24)
199      * @param minute
200      * @return human-readable string formatted based on 24-hour mode.
201      */
formatTime(int hourOfDay, int minute)202     private String formatTime(int hourOfDay, int minute) {
203         Time time = new Time();
204         time.hour = hourOfDay;
205         time.minute = minute;
206 
207         String format = mIs24HourMode? format24Hour : format12Hour;
208         return time.format(format);
209     }
210 }
211