SnoozeAlarmsService.java revision 9881907c47b2658fa85954bfb339c4b1eab9fc8e
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 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/** 30 * Service for asynchronously marking all fired alarms as dismissed. 31 */ 32public class SnoozeAlarmsService extends IntentService { 33 private static final String[] PROJECTION = new String[] { 34 CalendarAlerts.STATE, 35 }; 36 private static final int COLUMN_INDEX_STATE = 0; 37 38 public SnoozeAlarmsService() { 39 super("SnoozeAlarmsService"); 40 } 41 42 @Override 43 public IBinder onBind(Intent intent) { 44 return null; 45 } 46 47 @Override 48 public void onHandleIntent(Intent intent) { 49 50 // Remove notification 51 NotificationManager nm = 52 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 53 nm.cancel(AlertUtils.NOTIFICATION_ID); 54 55 long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1); 56 long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1); 57 long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1); 58 59 if (eventId != -1) { 60 ContentResolver resolver = getContentResolver(); 61 62 // Dismiss current alarm 63 Uri uri = CalendarAlerts.CONTENT_URI; 64 String selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " + 65 CalendarAlerts.EVENT_ID + "=" + eventId; 66 ContentValues dismissValues = new ContentValues(); 67 dismissValues.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED); 68 resolver.update(uri, dismissValues, selection, null); 69 70 // Add a new alarm 71 long alarmTime = System.currentTimeMillis() + AlertUtils.SNOOZE_DELAY; 72 ContentValues values = AlertUtils.makeContentValues(eventId, eventStart, eventEnd, 73 alarmTime, 0); 74 resolver.insert(uri, values); 75 AlertUtils.scheduleAlarm(SnoozeAlarmsService.this, null, alarmTime); 76 } 77 AlertService.updateAlertNotification(this); 78 stopSelf(); 79 } 80} 81