DismissAlarmsService.java revision 828df5020067aa477adbe1eefd88afa3fc5de900
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.AllInOneActivity;
31import com.android.calendar.EventInfoActivity;
32
33/**
34 * Service for asynchronously marking all 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
59        Uri uri = CalendarAlerts.CONTENT_URI;
60        String selection;
61
62        // Dismiss a specific fired alarm if id is present, otherwise, dismiss all alarms
63        if (eventId != -1) {
64            selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " +
65            CalendarAlerts.EVENT_ID + "=" + eventId;
66        } else {
67            selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
68        }
69        ContentResolver resolver = getContentResolver();
70
71        ContentValues values = new ContentValues();
72        values.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
73
74        resolver.update(uri, values, selection, null);
75        if (showEvent) {
76            // Remove notification
77            NotificationManager nm =
78                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
79            nm.cancel(AlertUtils.NOTIFICATION_ID);
80            // Show event on Calendar app by building an intent and task stack to start
81            // EventInfoActivity with AllInOneActivity as the parent activity rooted to home.
82
83            Intent i = AlertUtils.buildEventViewIntent(this, eventId, eventStart, eventEnd);
84            TaskStackBuilder.from(this)
85                    .addParentStack(EventInfoActivity.class).addNextIntent(i).startActivities();
86        }
87
88        // Stop this service
89        stopSelf();
90    }
91}
92