2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.calendar;
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.ContentUris;
25 import android.content.ContentValues;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.SharedPreferences;
29 import android.database.Cursor;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.os.Handler;
33 import android.os.HandlerThread;
34 import android.os.IBinder;
35 import android.os.Looper;
36 import android.os.Message;
37 import android.os.Process;
38 import android.preference.PreferenceManager;
39 import android.provider.Calendar.Attendees;
40 import android.provider.Calendar.CalendarAlerts;
41 import android.text.TextUtils;
42 import android.util.Log;
44 import java.util.HashMap;
47 * This service is used to handle calendar event reminders.
49 public class AlertService extends Service {
50 static final boolean DEBUG = true;
51 private static final String TAG = "AlertService";
53 private volatile Looper mServiceLooper;
54 private volatile ServiceHandler mServiceHandler;
56 private static final String[] ALERT_PROJECTION = new String[] {
57 CalendarAlerts._ID, // 0
58 CalendarAlerts.EVENT_ID, // 1
59 CalendarAlerts.STATE, // 2
60 CalendarAlerts.TITLE, // 3
61 CalendarAlerts.EVENT_LOCATION, // 4
62 CalendarAlerts.SELF_ATTENDEE_STATUS, // 5
63 CalendarAlerts.ALL_DAY, // 6
64 CalendarAlerts.ALARM_TIME, // 7
65 CalendarAlerts.MINUTES, // 8
66 CalendarAlerts.BEGIN, // 9
67 CalendarAlerts.END, // 10
70 private static final int ALERT_INDEX_ID = 0;
71 private static final int ALERT_INDEX_EVENT_ID = 1;
72 private static final int ALERT_INDEX_STATE = 2;
73 private static final int ALERT_INDEX_TITLE = 3;
74 private static final int ALERT_INDEX_EVENT_LOCATION = 4;
75 private static final int ALERT_INDEX_SELF_ATTENDEE_STATUS = 5;
76 private static final int ALERT_INDEX_ALL_DAY = 6;
77 private static final int ALERT_INDEX_ALARM_TIME = 7;
78 private static final int ALERT_INDEX_MINUTES = 8;
79 private static final int ALERT_INDEX_BEGIN = 9;
80 private static final int ALERT_INDEX_END = 10;
82 private static final String ACTIVE_ALERTS_SELECTION = "(" + CalendarAlerts.STATE + "=? OR "
83 + CalendarAlerts.STATE + "=?) AND " + CalendarAlerts.ALARM_TIME + "<=";
85 private static final String[] ACTIVE_ALERTS_SELECTION_ARGS = new String[] {
86 Integer.toString(CalendarAlerts.FIRED), Integer.toString(CalendarAlerts.SCHEDULED)
89 private static final String ACTIVE_ALERTS_SORT = "begin DESC, end DESC";
91 @SuppressWarnings("deprecation")
92 void processMessage(Message msg) {
93 Bundle bundle = (Bundle) msg.obj;
95 // On reboot, update the notification bar with the contents of the
96 // CalendarAlerts table.
97 String action = bundle.getString("action");
99 Log.d(TAG, "" + bundle.getLong(android.provider.Calendar.CalendarAlerts.ALARM_TIME)
100 + " Action = " + action);
103 if (action.equals(Intent.ACTION_BOOT_COMPLETED)
104 || action.equals(Intent.ACTION_TIME_CHANGED)) {
109 if (!action.equals(android.provider.Calendar.EVENT_REMINDER_ACTION)
110 && !action.equals(Intent.ACTION_LOCALE_CHANGED)) {
111 Log.w(TAG, "Invalid action: " + action);
115 updateAlertNotification(this);
118 static boolean updateAlertNotification(Context context) {
119 ContentResolver cr = context.getContentResolver();
120 final long currentTime = System.currentTimeMillis();
122 Cursor alertCursor = CalendarAlerts.query(cr, ALERT_PROJECTION, ACTIVE_ALERTS_SELECTION
123 + currentTime, ACTIVE_ALERTS_SELECTION_ARGS, ACTIVE_ALERTS_SORT);
125 if (alertCursor == null || alertCursor.getCount() == 0) {
126 if (alertCursor != null) {
130 if (DEBUG) Log.d(TAG, "No fired or scheduled alerts");
131 NotificationManager nm =
132 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
138 Log.d(TAG, "alert count:" + alertCursor.getCount());
141 String notificationEventName = null;
142 String notificationEventLocation = null;
143 long notificationEventBegin = 0;
144 int notificationEventStatus = 0;
145 HashMap<Long, Long> eventIds = new HashMap<Long, Long>();
146 int numReminders = 0;
149 while (alertCursor.moveToNext()) {
150 final long alertId = alertCursor.getLong(ALERT_INDEX_ID);
151 final long eventId = alertCursor.getLong(ALERT_INDEX_EVENT_ID);
152 final int minutes = alertCursor.getInt(ALERT_INDEX_MINUTES);
153 final String eventName = alertCursor.getString(ALERT_INDEX_TITLE);
154 final String location = alertCursor.getString(ALERT_INDEX_EVENT_LOCATION);
155 final boolean allDay = alertCursor.getInt(ALERT_INDEX_ALL_DAY) != 0;
156 final int status = alertCursor.getInt(ALERT_INDEX_SELF_ATTENDEE_STATUS);
157 final boolean declined = status == Attendees.ATTENDEE_STATUS_DECLINED;
158 final long beginTime = alertCursor.getLong(ALERT_INDEX_BEGIN);
159 final long endTime = alertCursor.getLong(ALERT_INDEX_END);
160 final Uri alertUri = ContentUris
161 .withAppendedId(CalendarAlerts.CONTENT_URI, alertId);
162 final long alarmTime = alertCursor.getLong(ALERT_INDEX_ALARM_TIME);
163 int state = alertCursor.getInt(ALERT_INDEX_STATE);
166 Log.d(TAG, "alarmTime:" + alarmTime + " alertId:" + alertId
167 + " eventId:" + eventId + " state: " + state + " minutes:" + minutes
168 + " declined:" + declined + " beginTime:" + beginTime
169 + " endTime:" + endTime);
172 ContentValues values = new ContentValues();
175 // Uncomment for the behavior of clearing out alerts after the
176 // events ended. b/1880369
178 // if (endTime < currentTime) {
179 // newState = CalendarAlerts.DISMISSED;
182 // Remove declined events and duplicate alerts for the same event
183 if (!declined && eventIds.put(eventId, beginTime) == null) {
185 if (state == CalendarAlerts.SCHEDULED) {
186 newState = CalendarAlerts.FIRED;
189 // Record the received time in the CalendarAlerts table.
190 // This is useful for finding bugs that cause alarms to be
191 // missed or delayed.
192 values.put(CalendarAlerts.RECEIVED_TIME, currentTime);
195 newState = CalendarAlerts.DISMISSED;
197 if (!declined) Log.d(TAG, "dropping dup alert for event " + eventId);
201 // Update row if state changed
202 if (newState != -1) {
203 values.put(CalendarAlerts.STATE, newState);
207 if (state == CalendarAlerts.FIRED) {
208 // Record the time posting to notification manager.
209 // This is used for debugging missed alarms.
210 values.put(CalendarAlerts.NOTIFY_TIME, currentTime);
213 // Write row to if anything changed
214 if (values.size() > 0) cr.update(alertUri, values, null, null);
216 if (state != CalendarAlerts.FIRED) {
220 // Pick an Event title for the notification panel by the latest
221 // alertTime and give prefer accepted events in case of ties.
224 case Attendees.ATTENDEE_STATUS_ACCEPTED:
227 case Attendees.ATTENDEE_STATUS_TENTATIVE:
234 // TODO Prioritize by "primary" calendar
235 // Assumes alerts are sorted by begin time in reverse
236 if (notificationEventName == null
237 || (notificationEventBegin <= beginTime &&
238 notificationEventStatus < newStatus)) {
239 notificationEventName = eventName;
240 notificationEventLocation = location;
241 notificationEventBegin = beginTime;
242 notificationEventStatus = newStatus;
246 if (alertCursor != null) {
251 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
252 String reminderType = prefs.getString(CalendarPreferenceActivity.KEY_ALERTS_TYPE,
253 CalendarPreferenceActivity.ALERT_TYPE_STATUS_BAR);
255 // TODO check for this before adding stuff to the alerts table.
256 if (reminderType.equals(CalendarPreferenceActivity.ALERT_TYPE_OFF)) {
258 Log.d(TAG, "alert preference is OFF");
263 postNotification(context, prefs, notificationEventName, notificationEventLocation,
264 numReminders, numFired == 0 /* quiet update */);
266 if (numFired > 0 && reminderType.equals(CalendarPreferenceActivity.ALERT_TYPE_ALERTS)) {
267 Intent alertIntent = new Intent();
268 alertIntent.setClass(context, AlertActivity.class);
269 alertIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
270 context.startActivity(alertIntent);
276 private static void postNotification(Context context, SharedPreferences prefs,
277 String eventName, String location, int numReminders, boolean quietUpdate) {
279 Log.d(TAG, "###### creating new alarm notification, numReminders: " + numReminders
280 + (quietUpdate ? " QUIET" : " loud"));
283 NotificationManager nm =
284 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
286 if (numReminders == 0) {
291 Notification notification = AlertReceiver.makeNewAlertNotification(context, eventName,
292 location, numReminders);
293 notification.defaults |= Notification.DEFAULT_LIGHTS;
295 // Quietly update notification bar. Nothing new. Maybe something just got deleted.
297 // Flash ticker in status bar
298 notification.tickerText = eventName;
299 if (!TextUtils.isEmpty(location)) {
300 notification.tickerText = eventName + " - " + location;
303 // Generate either a pop-up dialog, status bar notification, or
304 // neither. Pop-up dialog and status bar notification may include a
305 // sound, an alert, or both. A status bar notification also includes
307 boolean reminderVibrate =
308 prefs.getBoolean(CalendarPreferenceActivity.KEY_ALERTS_VIBRATE, false);
310 // Possibly generate a vibration
311 if (reminderVibrate) {
312 notification.defaults |= Notification.DEFAULT_VIBRATE;
315 // Possibly generate a sound. If 'Silent' is chosen, the ringtone
316 // string will be empty.
317 String reminderRingtone = prefs.getString(
318 CalendarPreferenceActivity.KEY_ALERTS_RINGTONE, null);
319 notification.sound = TextUtils.isEmpty(reminderRingtone) ? null : Uri
320 .parse(reminderRingtone);
323 nm.notify(0, notification);
326 private void doTimeChanged() {
327 ContentResolver cr = getContentResolver();
328 Object service = getSystemService(Context.ALARM_SERVICE);
329 AlarmManager manager = (AlarmManager) service;
330 CalendarAlerts.rescheduleMissedAlarms(cr, this, manager);
331 updateAlertNotification(this);
334 private final class ServiceHandler extends Handler {
335 public ServiceHandler(Looper looper) {
340 public void handleMessage(Message msg) {
342 // NOTE: We MUST not call stopSelf() directly, since we need to
343 // make sure the wake lock acquired by AlertReceiver is released.
344 AlertReceiver.finishStartingService(AlertService.this, msg.arg1);
349 public void onCreate() {
350 HandlerThread thread = new HandlerThread("AlertService",
351 Process.THREAD_PRIORITY_BACKGROUND);
354 mServiceLooper = thread.getLooper();
355 mServiceHandler = new ServiceHandler(mServiceLooper);
359 public int onStartCommand(Intent intent, int flags, int startId) {
360 if (intent != null) {
361 Message msg = mServiceHandler.obtainMessage();
363 msg.obj = intent.getExtras();
364 mServiceHandler.sendMessage(msg);
366 return START_REDELIVER_INTENT;
370 public void onDestroy() {
371 mServiceLooper.quit();
375 public IBinder onBind(Intent intent) {