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