1 /*
2  * Copyright (C) 2012 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.content.ContentResolver;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.os.IBinder;
27 import android.provider.CalendarContract.CalendarAlerts;
28 
29 /**
30  * Service for asynchronously marking a fired alarm as dismissed and scheduling
31  * a new alarm in the future.
32  */
33 public class SnoozeAlarmsService extends IntentService {
34     private static final String[] PROJECTION = new String[] {
35             CalendarAlerts.STATE,
36     };
37     private static final int COLUMN_INDEX_STATE = 0;
38 
SnoozeAlarmsService()39     public SnoozeAlarmsService() {
40         super("SnoozeAlarmsService");
41     }
42 
43     @Override
onBind(Intent intent)44     public IBinder onBind(Intent intent) {
45         return null;
46     }
47 
48     @Override
onHandleIntent(Intent intent)49     public void onHandleIntent(Intent intent) {
50 
51         long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1);
52         long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1);
53         long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1);
54 
55         // The ID reserved for the expired notification digest should never be passed in
56         // here, so use that as a default.
57         int notificationId = intent.getIntExtra(AlertUtils.NOTIFICATION_ID_KEY,
58                 AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID);
59 
60         if (eventId != -1) {
61             ContentResolver resolver = getContentResolver();
62 
63             // Remove notification
64             if (notificationId != AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID) {
65                 NotificationManager nm =
66                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
67                 nm.cancel(notificationId);
68             }
69 
70             // Dismiss current alarm
71             Uri uri = CalendarAlerts.CONTENT_URI;
72             String selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " +
73                     CalendarAlerts.EVENT_ID + "=" + eventId;
74             ContentValues dismissValues = new ContentValues();
75             dismissValues.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
76             resolver.update(uri, dismissValues, selection, null);
77 
78             // Add a new alarm
79             long alarmTime = System.currentTimeMillis() + AlertUtils.SNOOZE_DELAY;
80             ContentValues values = AlertUtils.makeContentValues(eventId, eventStart, eventEnd,
81                     alarmTime, 0);
82             resolver.insert(uri, values);
83             AlertUtils.scheduleAlarm(SnoozeAlarmsService.this, AlertUtils.createAlarmManager(this),
84                     alarmTime);
85         }
86         AlertService.updateAlertNotification(this);
87         stopSelf();
88     }
89 }
90