OSDN Git Service

am e0f5f762: am 9b67695b: Merge change I404f895f into eclair
[android-x86/packages-apps-Calendar.git] / src / com / android / calendar / AlertService.java
1 /*
2  * Copyright (C) 2008 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.AlarmManager;
20 import android.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.Service;
23 import android.content.ContentResolver;
24 import android.content.ContentValues;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.database.Cursor;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.HandlerThread;
33 import android.os.IBinder;
34 import android.os.Looper;
35 import android.os.Message;
36 import android.os.Process;
37 import android.preference.PreferenceManager;
38 import android.provider.Calendar;
39 import android.provider.Calendar.Attendees;
40 import android.provider.Calendar.CalendarAlerts;
41 import android.provider.Calendar.Instances;
42 import android.provider.Calendar.Reminders;
43 import android.text.TextUtils;
44 import android.text.format.DateUtils;
45 import android.util.Log;
46 import android.view.LayoutInflater;
47 import android.view.View;
48
49 /**
50  * This service is used to handle calendar event reminders.
51  */
52 public class AlertService extends Service {
53     private static final String TAG = "AlertService";
54     
55     private volatile Looper mServiceLooper;
56     private volatile ServiceHandler mServiceHandler;
57     
58     private static final String[] ALERT_PROJECTION = new String[] { 
59         CalendarAlerts._ID,                     // 0
60         CalendarAlerts.EVENT_ID,                // 1
61         CalendarAlerts.STATE,                   // 2
62         CalendarAlerts.TITLE,                   // 3
63         CalendarAlerts.EVENT_LOCATION,          // 4
64         CalendarAlerts.SELF_ATTENDEE_STATUS,    // 5
65         CalendarAlerts.ALL_DAY,                 // 6
66         CalendarAlerts.ALARM_TIME,              // 7
67         CalendarAlerts.MINUTES,                 // 8
68         CalendarAlerts.BEGIN,                   // 9
69     };
70
71     // We just need a simple projection that returns any column
72     private static final String[] ALERT_PROJECTION_SMALL = new String[] { 
73         CalendarAlerts._ID,                     // 0
74     };
75     
76     private static final int ALERT_INDEX_ID = 0;
77     private static final int ALERT_INDEX_EVENT_ID = 1;
78     private static final int ALERT_INDEX_STATE = 2;
79     private static final int ALERT_INDEX_TITLE = 3;
80     private static final int ALERT_INDEX_EVENT_LOCATION = 4;
81     private static final int ALERT_INDEX_SELF_ATTENDEE_STATUS = 5;
82     private static final int ALERT_INDEX_ALL_DAY = 6;
83     private static final int ALERT_INDEX_ALARM_TIME = 7;
84     private static final int ALERT_INDEX_MINUTES = 8;
85     private static final int ALERT_INDEX_BEGIN = 9;
86
87     private String[] INSTANCE_PROJECTION = { Instances.BEGIN, Instances.END };
88     private static final int INSTANCES_INDEX_BEGIN = 0;
89     private static final int INSTANCES_INDEX_END = 1;
90
91     // We just need a simple projection that returns any column
92     private static final String[] REMINDER_PROJECTION_SMALL = new String[] { 
93         Reminders._ID,                     // 0
94     };
95     
96     @SuppressWarnings("deprecation")
97     void processMessage(Message msg) {
98         Bundle bundle = (Bundle) msg.obj;
99         
100         // On reboot, update the notification bar with the contents of the
101         // CalendarAlerts table.
102         String action = bundle.getString("action");
103         if (action.equals(Intent.ACTION_BOOT_COMPLETED)
104                 || action.equals(Intent.ACTION_TIME_CHANGED)) {
105             doTimeChanged();
106             return;
107         }
108
109         // The Uri specifies an entry in the CalendarAlerts table
110         Uri alertUri = Uri.parse(bundle.getString("uri"));
111         if (Log.isLoggable(TAG, Log.DEBUG)) {
112             Log.d(TAG, "uri: " + alertUri);
113         }
114
115         if (alertUri != null) {
116             if (Calendar.AUTHORITY.equals(alertUri.getAuthority())) {
117                 Log.w(TAG, "Invalid AUTHORITY uri: " + alertUri);
118                 return;
119             }
120
121             // Record the received time in the CalendarAlerts table.
122             // This is useful for finding bugs that cause alarms to be
123             // missed or delayed.
124             ContentValues values = new ContentValues();
125             values.put(CalendarAlerts.RECEIVED_TIME, System.currentTimeMillis());
126             getContentResolver().update(alertUri, values, null /* where */, null /* args */);
127         }
128         
129         ContentResolver cr = getContentResolver();
130         Cursor alertCursor = cr.query(alertUri, ALERT_PROJECTION,
131                 null /* selection */, null, null /* sort order */);
132         
133         long alertId, eventId, alarmTime;
134         int minutes;
135         String eventName;
136         String location;
137         boolean allDay;
138         boolean declined = false;
139         try {
140             if (alertCursor == null || !alertCursor.moveToFirst()) {
141                 // This can happen if the event was deleted.
142                 if (Log.isLoggable(TAG, Log.DEBUG)) {
143                     Log.d(TAG, "alert not found");
144                 }
145                 return;
146             }
147             alertId = alertCursor.getLong(ALERT_INDEX_ID);
148             eventId = alertCursor.getLong(ALERT_INDEX_EVENT_ID);
149             minutes = alertCursor.getInt(ALERT_INDEX_MINUTES);
150             eventName = alertCursor.getString(ALERT_INDEX_TITLE);
151             location = alertCursor.getString(ALERT_INDEX_EVENT_LOCATION);
152             allDay = alertCursor.getInt(ALERT_INDEX_ALL_DAY) != 0;
153             alarmTime = alertCursor.getLong(ALERT_INDEX_ALARM_TIME);
154             declined = alertCursor.getInt(ALERT_INDEX_SELF_ATTENDEE_STATUS) == 
155                     Attendees.ATTENDEE_STATUS_DECLINED;
156             
157             // If the event was declined, then mark the alarm DISMISSED,
158             // otherwise, mark the alarm FIRED.
159             int newState = CalendarAlerts.FIRED;
160             if (declined) {
161                 newState = CalendarAlerts.DISMISSED;
162             }
163             alertCursor.updateInt(ALERT_INDEX_STATE, newState);
164             alertCursor.commitUpdates();
165         } finally {
166             if (alertCursor != null) {
167                 alertCursor.close();
168             }
169         }
170         
171         // Do not show an alert if the event was declined
172         if (declined) {
173             if (Log.isLoggable(TAG, Log.DEBUG)) {
174                 Log.d(TAG, "event declined, alert cancelled");
175             }
176             return;
177         }
178         
179         long beginTime = bundle.getLong(Calendar.EVENT_BEGIN_TIME, 0);
180         long endTime = bundle.getLong(Calendar.EVENT_END_TIME, 0);
181         
182         // Check if this alarm is still valid.  The time of the event may
183         // have been changed, or the reminder may have been changed since
184         // this alarm was set. First, search for an instance in the Instances
185         // that has the same event id and the same begin and end time.
186         // Then check for a reminder in the Reminders table to ensure that
187         // the reminder minutes is consistent with this alarm.
188         String selection = Instances.EVENT_ID + "=" + eventId;
189         Cursor instanceCursor = Instances.query(cr, INSTANCE_PROJECTION,
190                 beginTime, endTime, selection, Instances.DEFAULT_SORT_ORDER);
191         long instanceBegin = 0, instanceEnd = 0;
192         try {
193             if (instanceCursor == null || !instanceCursor.moveToFirst()) {
194                 // Delete this alarm from the CalendarAlerts table
195                 cr.delete(alertUri, null /* selection */, null /* selection args */);
196                 if (Log.isLoggable(TAG, Log.DEBUG)) {
197                     Log.d(TAG, "instance not found, alert cancelled");
198                 }
199                 return;
200             }
201             instanceBegin = instanceCursor.getLong(INSTANCES_INDEX_BEGIN);
202             instanceEnd = instanceCursor.getLong(INSTANCES_INDEX_END);
203         } finally {
204             if (instanceCursor != null) {
205                 instanceCursor.close();
206             }
207         }
208         
209         // Check that a reminder for this event exists with the same number
210         // of minutes.  But snoozed alarms have minutes = 0, so don't do this
211         // check for snoozed alarms.
212         if (minutes > 0) {
213             selection = Reminders.EVENT_ID + "=" + eventId
214                 + " AND " + Reminders.MINUTES + "=" + minutes;
215             Cursor reminderCursor = cr.query(Reminders.CONTENT_URI, REMINDER_PROJECTION_SMALL,
216                     selection, null /* selection args */, null /* sort order */);
217             try {
218                 if (reminderCursor == null || reminderCursor.getCount() == 0) {
219                     // Delete this alarm from the CalendarAlerts table
220                     cr.delete(alertUri, null /* selection */, null /* selection args */);
221                     if (Log.isLoggable(TAG, Log.DEBUG)) {
222                         Log.d(TAG, "reminder not found, alert cancelled");
223                     }
224                     return;
225                 }
226             } finally {
227                 if (reminderCursor != null) {
228                     reminderCursor.close();
229                 }
230             }
231         }
232         
233         // If the event time was changed and the event has already ended,
234         // then don't sound the alarm.
235         if (alarmTime > instanceEnd) {
236             // Delete this alarm from the CalendarAlerts table
237             cr.delete(alertUri, null /* selection */, null /* selection args */);
238             if (Log.isLoggable(TAG, Log.DEBUG)) {
239                 Log.d(TAG, "event ended, alert cancelled");
240             }
241             return;
242         }
243
244         // If minutes > 0, then this is a normal alarm (not a snoozed alarm)
245         // so check for duplicate alarms.  A duplicate alarm can occur when
246         // the start time of an event is changed to an earlier time.  The
247         // later alarm (that was first scheduled for the later event time)
248         // should be discarded.
249         long computedAlarmTime = instanceBegin - minutes * DateUtils.MINUTE_IN_MILLIS;
250         if (minutes > 0 && computedAlarmTime != alarmTime) {
251             // If the event time was changed to a later time, then the computed
252             // alarm time is in the future and we shouldn't sound this alarm.
253             if (computedAlarmTime > alarmTime) {
254                 // Delete this alarm from the CalendarAlerts table
255                 cr.delete(alertUri, null /* selection */, null /* selection args */);
256                 if (Log.isLoggable(TAG, Log.DEBUG)) {
257                     Log.d(TAG, "event postponed, alert cancelled");
258                 }
259                 return;
260             }
261             
262             // Check for another alarm in the CalendarAlerts table that has the
263             // same event id and the same "minutes".  This can occur
264             // if the event start time was changed to an earlier time and the
265             // alarm for the later time goes off.  To avoid discarding alarms
266             // for repeating events (that have the same event id), we check
267             // that the other alarm fired recently (within an hour of this one).
268             long recently = alarmTime - 60 * DateUtils.MINUTE_IN_MILLIS;
269             selection = CalendarAlerts.EVENT_ID + "=" + eventId
270                     + " AND " + CalendarAlerts.TABLE_NAME + "." + CalendarAlerts._ID
271                     + "!=" + alertId
272                     + " AND " + CalendarAlerts.MINUTES + "=" + minutes
273                     + " AND " + CalendarAlerts.ALARM_TIME + ">" + recently
274                     + " AND " + CalendarAlerts.ALARM_TIME + "<=" + alarmTime;
275             alertCursor = CalendarAlerts.query(cr, ALERT_PROJECTION_SMALL, selection, null);
276             if (alertCursor != null) {
277                 try {
278                     if (alertCursor.getCount() > 0) {
279                         // Delete this alarm from the CalendarAlerts table
280                         cr.delete(alertUri, null /* selection */, null /* selection args */);
281                         if (Log.isLoggable(TAG, Log.DEBUG)) {
282                             Log.d(TAG, "duplicate alarm, alert cancelled");
283                         }
284                         return;
285                     }
286                 } finally {
287                     alertCursor.close();
288                 }
289             }
290         }
291         
292         // Find all the alerts that have fired but have not been dismissed
293         selection = CalendarAlerts.STATE + "=" + CalendarAlerts.FIRED;
294         alertCursor = CalendarAlerts.query(cr, ALERT_PROJECTION, selection, null);
295         
296         if (alertCursor == null || alertCursor.getCount() == 0) {
297             if (Log.isLoggable(TAG, Log.DEBUG)) {
298                 Log.d(TAG, "no fired alarms found");
299             }
300             return;
301         }
302
303         int numReminders = alertCursor.getCount();
304         try {
305             while (alertCursor.moveToNext()) {
306                 long otherEventId = alertCursor.getLong(ALERT_INDEX_EVENT_ID);
307                 long otherAlertId = alertCursor.getLong(ALERT_INDEX_ID);
308                 int otherAlarmState = alertCursor.getInt(ALERT_INDEX_STATE);
309                 long otherBeginTime = alertCursor.getLong(ALERT_INDEX_BEGIN);
310                 if (otherEventId == eventId && otherAlertId != alertId
311                         && otherAlarmState == CalendarAlerts.FIRED
312                         && otherBeginTime == beginTime) {
313                     // This event already has an alert that fired and has not
314                     // been dismissed.  This can happen if an event has
315                     // multiple reminders.  Do not count this as a separate
316                     // reminder.  But we do want to sound the alarm and vibrate
317                     // the phone, if necessary.
318                     if (Log.isLoggable(TAG, Log.DEBUG)) {
319                         Log.d(TAG, "multiple alarms for this event");
320                     }
321                     numReminders -= 1;
322                 }
323             }
324         } finally {
325             alertCursor.close();
326         }
327         
328         if (Log.isLoggable(TAG, Log.DEBUG)) {
329             Log.d(TAG, "creating new alarm notification, numReminders: " + numReminders);
330         }
331         Notification notification = AlertReceiver.makeNewAlertNotification(this, eventName,
332                 location, numReminders);
333         
334         // Generate either a pop-up dialog, status bar notification, or
335         // neither. Pop-up dialog and status bar notification may include a
336         // sound, an alert, or both. A status bar notification also includes
337         // a toast.
338         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
339         String reminderType = prefs.getString(CalendarPreferenceActivity.KEY_ALERTS_TYPE,
340                 CalendarPreferenceActivity.ALERT_TYPE_STATUS_BAR);
341         
342         if (reminderType.equals(CalendarPreferenceActivity.ALERT_TYPE_OFF)) {
343             if (Log.isLoggable(TAG, Log.DEBUG)) {
344                 Log.d(TAG, "alert preference is OFF");
345             }
346             return;
347         }
348         
349         NotificationManager nm = 
350             (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
351         boolean reminderVibrate = 
352                 prefs.getBoolean(CalendarPreferenceActivity.KEY_ALERTS_VIBRATE, false);
353         String reminderRingtone =
354                 prefs.getString(CalendarPreferenceActivity.KEY_ALERTS_RINGTONE, null);
355
356         // Possibly generate a vibration
357         if (reminderVibrate) {
358             notification.defaults |= Notification.DEFAULT_VIBRATE;
359         }
360         
361         // Possibly generate a sound.  If 'Silent' is chosen, the ringtone string will be empty.
362         notification.sound = TextUtils.isEmpty(reminderRingtone) ? null : Uri
363                 .parse(reminderRingtone);
364         
365         if (reminderType.equals(CalendarPreferenceActivity.ALERT_TYPE_ALERTS)) {
366             Intent alertIntent = new Intent();
367             alertIntent.setClass(this, AlertActivity.class);
368             alertIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
369             startActivity(alertIntent);
370         } else {
371             LayoutInflater inflater;
372             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
373             View view = inflater.inflate(R.layout.alert_toast, null);
374             
375             AlertAdapter.updateView(this, view, eventName, location, beginTime, endTime, allDay);
376         }
377         
378         // Record the notify time in the CalendarAlerts table.
379         // This is used for debugging missed alarms.
380         ContentValues values = new ContentValues();
381         long currentTime = System.currentTimeMillis();
382         values.put(CalendarAlerts.NOTIFY_TIME, currentTime);
383         cr.update(alertUri, values, null /* where */, null /* args */);
384         
385         // The notification time should be pretty close to the reminder time
386         // that the user set for this event.  If the notification is late, then
387         // that's a bug and we should log an error.
388         if (currentTime > alarmTime + DateUtils.MINUTE_IN_MILLIS) {
389             long minutesLate = (currentTime - alarmTime) / DateUtils.MINUTE_IN_MILLIS;
390             int flags = DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_TIME;
391             String alarmTimeStr = DateUtils.formatDateTime(this, alarmTime, flags);
392             String currentTimeStr = DateUtils.formatDateTime(this, currentTime, flags);
393             Log.w(TAG, "Calendar reminder alarm for event id " + eventId
394                     + " is " + minutesLate + " minute(s) late;"
395                     + " expected alarm at: " + alarmTimeStr
396                     + " but got it at: " + currentTimeStr);
397         }
398
399         nm.notify(0, notification);
400     }
401     
402     private void doTimeChanged() {
403         ContentResolver cr = getContentResolver();
404         Object service = getSystemService(Context.ALARM_SERVICE);
405         AlarmManager manager = (AlarmManager) service;
406         CalendarAlerts.rescheduleMissedAlarms(cr, this, manager);
407         AlertReceiver.updateAlertNotification(this);
408     }
409     
410     private final class ServiceHandler extends Handler {
411         public ServiceHandler(Looper looper) {
412             super(looper);
413         }
414         
415         @Override
416         public void handleMessage(Message msg) {
417             processMessage(msg);
418             // NOTE: We MUST not call stopSelf() directly, since we need to
419             // make sure the wake lock acquired by AlertReceiver is released.
420             AlertReceiver.finishStartingService(AlertService.this, msg.arg1);
421         } 
422     };
423
424     @Override
425     public void onCreate() {
426         HandlerThread thread = new HandlerThread("AlertService",
427                 Process.THREAD_PRIORITY_BACKGROUND);
428         thread.start();
429         
430         mServiceLooper = thread.getLooper();
431         mServiceHandler = new ServiceHandler(mServiceLooper);
432     }
433
434     @Override
435     public int onStartCommand(Intent intent, int flags, int startId) {
436         if (intent != null) {
437             Message msg = mServiceHandler.obtainMessage();
438             msg.arg1 = startId;
439             msg.obj = intent.getExtras();
440             mServiceHandler.sendMessage(msg);
441         }
442         return START_REDELIVER_INTENT;
443     }
444
445     @Override
446     public void onDestroy() {
447         mServiceLooper.quit();
448     }
449
450     @Override
451     public IBinder onBind(Intent intent) {
452         return null;
453     }
454 }