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.content.ContentResolver;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.net.Uri;
26import android.os.IBinder;
27import android.provider.CalendarContract.CalendarAlerts;
28import android.support.v4.app.TaskStackBuilder;
29
30import com.android.calendar.EventInfoActivity;
31
32/**
33 * Service for asynchronously marking fired alarms as dismissed.
34 */
35public 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
41    public DismissAlarmsService() {
42        super("DismissAlarmsService");
43    }
44
45    @Override
46    public IBinder onBind(Intent intent) {
47        return null;
48    }
49
50    @Override
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
90            TaskStackBuilder.create(this)
91                    .addParentStack(EventInfoActivity.class).addNextIntent(i).startActivities();
92        }
93
94        // Stop this service
95        stopSelf();
96    }
97
98    private String buildMultipleEventsQuery(long[] eventIds) {
99        StringBuilder selection = new StringBuilder();
100        selection.append(CalendarAlerts.STATE);
101        selection.append("=");
102        selection.append(CalendarAlerts.STATE_FIRED);
103        if (eventIds.length > 0) {
104            selection.append(" AND (");
105            selection.append(CalendarAlerts.EVENT_ID);
106            selection.append("=");
107            selection.append(eventIds[0]);
108            for (int i = 1; i < eventIds.length; i++) {
109                selection.append(" OR ");
110                selection.append(CalendarAlerts.EVENT_ID);
111                selection.append("=");
112                selection.append(eventIds[i]);
113            }
114            selection.append(")");
115        }
116        return selection.toString();
117    }
118}
119