1 /* 2 * Copyright (C) 2009 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.alerts; 18 19 import android.app.IntentService; 20 import android.app.NotificationManager; 21 import android.app.TaskStackBuilder; 22 import android.content.ContentResolver; 23 import android.content.ContentValues; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.net.Uri; 27 import android.os.IBinder; 28 import android.provider.CalendarContract.CalendarAlerts; 29 30 import com.android.calendar.EventInfoActivity; 31 32 /** 33 * Service for asynchronously marking fired alarms as dismissed. 34 */ 35 public class DismissAlarmsService extends IntentService { 36 private static final String[] PROJECTION = new String[] { 37 CalendarAlerts.STATE, 38 }; 39 private static final int COLUMN_INDEX_STATE = 0; 40 DismissAlarmsService()41 public DismissAlarmsService() { 42 super("DismissAlarmsService"); 43 } 44 45 @Override onBind(Intent intent)46 public IBinder onBind(Intent intent) { 47 return null; 48 } 49 50 @Override onHandleIntent(Intent intent)51 public void onHandleIntent(Intent intent) { 52 53 long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1); 54 long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1); 55 long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1); 56 boolean showEvent = intent.getBooleanExtra(AlertUtils.SHOW_EVENT_KEY, false); 57 long[] eventIds = intent.getLongArrayExtra(AlertUtils.EVENT_IDS_KEY); 58 int notificationId = intent.getIntExtra(AlertUtils.NOTIFICATION_ID_KEY, -1); 59 60 Uri uri = CalendarAlerts.CONTENT_URI; 61 String selection; 62 63 // Dismiss a specific fired alarm if id is present, otherwise, dismiss all alarms 64 if (eventId != -1) { 65 selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " + 66 CalendarAlerts.EVENT_ID + "=" + eventId; 67 } else if (eventIds != null && eventIds.length > 0) { 68 selection = buildMultipleEventsQuery(eventIds); 69 } else { 70 selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED; 71 } 72 73 ContentResolver resolver = getContentResolver(); 74 ContentValues values = new ContentValues(); 75 values.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED); 76 resolver.update(uri, values, selection, null); 77 78 // Remove from notification bar. 79 if (notificationId != -1) { 80 NotificationManager nm = 81 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 82 nm.cancel(notificationId); 83 } 84 85 if (showEvent) { 86 // Show event on Calendar app by building an intent and task stack to start 87 // EventInfoActivity with AllInOneActivity as the parent activity rooted to home. 88 Intent i = AlertUtils.buildEventViewIntent(this, eventId, eventStart, eventEnd); 89 TaskStackBuilder.create(this) 90 .addParentStack(EventInfoActivity.class).addNextIntent(i).startActivities(); 91 } 92 93 // Stop this service 94 stopSelf(); 95 } 96 buildMultipleEventsQuery(long[] eventIds)97 private String buildMultipleEventsQuery(long[] eventIds) { 98 StringBuilder selection = new StringBuilder(); 99 selection.append(CalendarAlerts.STATE); 100 selection.append("="); 101 selection.append(CalendarAlerts.STATE_FIRED); 102 if (eventIds.length > 0) { 103 selection.append(" AND ("); 104 selection.append(CalendarAlerts.EVENT_ID); 105 selection.append("="); 106 selection.append(eventIds[0]); 107 for (int i = 1; i < eventIds.length; i++) { 108 selection.append(" OR "); 109 selection.append(CalendarAlerts.EVENT_ID); 110 selection.append("="); 111 selection.append(eventIds[i]); 112 } 113 selection.append(")"); 114 } 115 return selection.toString(); 116 } 117 } 118