blob: 6ef983d50f798c5245e3f9e527b9533c45ddd92c [file] [log] [blame]
Isaac Katzenelson98819072012-03-01 15:58:30 -08001/*
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
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;
28
29/**
Sara Ting42ba5ef2012-05-04 10:28:56 -070030 * Service for asynchronously marking a fired alarm as dismissed and scheduling
31 * a new alarm in the future.
Isaac Katzenelson98819072012-03-01 15:58:30 -080032 */
33public 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
39 public SnoozeAlarmsService() {
40 super("SnoozeAlarmsService");
41 }
42
43 @Override
44 public IBinder onBind(Intent intent) {
45 return null;
46 }
47
48 @Override
49 public void onHandleIntent(Intent intent) {
50
Isaac Katzenelson98819072012-03-01 15:58:30 -080051 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
Sara Ting42ba5ef2012-05-04 10:28:56 -070055 // 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
Isaac Katzenelson98819072012-03-01 15:58:30 -080060 if (eventId != -1) {
61 ContentResolver resolver = getContentResolver();
62
Sara Ting42ba5ef2012-05-04 10:28:56 -070063 // 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
Isaac Katzenelson98819072012-03-01 15:58:30 -080070 // 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);
Sara Tinga3a27fb2012-08-30 16:35:24 -070083 AlertUtils.scheduleAlarm(SnoozeAlarmsService.this, AlertUtils.createAlarmManager(this),
84 alarmTime);
Isaac Katzenelson98819072012-03-01 15:58:30 -080085 }
Sara Tingbb577102012-05-11 15:55:48 -070086 AlertService.updateAlertNotification(this);
Isaac Katzenelson98819072012-03-01 15:58:30 -080087 stopSelf();
88 }
89}