Home | History | Annotate | Download | only in alerts
      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 
     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 import android.support.v4.app.TaskStackBuilder;
     29 
     30 import com.android.calendar.EventInfoActivity;
     31 import com.android.calendar.alerts.GlobalDismissManager.AlarmId;
     32 
     33 import java.util.LinkedList;
     34 import java.util.List;
     35 
     36 /**
     37  * Service for asynchronously marking fired alarms as dismissed.
     38  */
     39 public class DismissAlarmsService extends IntentService {
     40     private static final String[] PROJECTION = new String[] {
     41             CalendarAlerts.STATE,
     42     };
     43     private static final int COLUMN_INDEX_STATE = 0;
     44 
     45     public DismissAlarmsService() {
     46         super("DismissAlarmsService");
     47     }
     48 
     49     @Override
     50     public IBinder onBind(Intent intent) {
     51         return null;
     52     }
     53 
     54     @Override
     55     public void onHandleIntent(Intent intent) {
     56 
     57         long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1);
     58         long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1);
     59         long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1);
     60         boolean showEvent = intent.getBooleanExtra(AlertUtils.SHOW_EVENT_KEY, false);
     61         long[] eventIds = intent.getLongArrayExtra(AlertUtils.EVENT_IDS_KEY);
     62         long[] eventStarts = intent.getLongArrayExtra(AlertUtils.EVENT_STARTS_KEY);
     63         int notificationId = intent.getIntExtra(AlertUtils.NOTIFICATION_ID_KEY, -1);
     64         List<AlarmId> alarmIds = new LinkedList<AlarmId>();
     65 
     66         Uri uri = CalendarAlerts.CONTENT_URI;
     67         String selection;
     68 
     69         // Dismiss a specific fired alarm if id is present, otherwise, dismiss all alarms
     70         if (eventId != -1) {
     71             alarmIds.add(new AlarmId(eventId, eventStart));
     72             selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " +
     73             CalendarAlerts.EVENT_ID + "=" + eventId;
     74         } else if (eventIds != null && eventIds.length > 0 &&
     75                 eventStarts != null && eventIds.length == eventStarts.length) {
     76             selection = buildMultipleEventsQuery(eventIds);
     77             for (int i = 0; i < eventIds.length; i++) {
     78                 alarmIds.add(new AlarmId(eventIds[i], eventStarts[i]));
     79             }
     80         } else {
     81             // NOTE: I don't believe that this ever happens.
     82             selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
     83         }
     84 
     85         GlobalDismissManager.dismissGlobally(getApplicationContext(), alarmIds);
     86 
     87         ContentResolver resolver = getContentResolver();
     88         ContentValues values = new ContentValues();
     89         values.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
     90         resolver.update(uri, values, selection, null);
     91 
     92         // Remove from notification bar.
     93         if (notificationId != -1) {
     94             NotificationManager nm =
     95                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     96             nm.cancel(notificationId);
     97         }
     98 
     99         if (showEvent) {
    100             // Show event on Calendar app by building an intent and task stack to start
    101             // EventInfoActivity with AllInOneActivity as the parent activity rooted to home.
    102             Intent i = AlertUtils.buildEventViewIntent(this, eventId, eventStart, eventEnd);
    103 
    104             TaskStackBuilder.create(this)
    105                     .addParentStack(EventInfoActivity.class).addNextIntent(i).startActivities();
    106         }
    107     }
    108 
    109     private String buildMultipleEventsQuery(long[] eventIds) {
    110         StringBuilder selection = new StringBuilder();
    111         selection.append(CalendarAlerts.STATE);
    112         selection.append("=");
    113         selection.append(CalendarAlerts.STATE_FIRED);
    114         if (eventIds.length > 0) {
    115             selection.append(" AND (");
    116             selection.append(CalendarAlerts.EVENT_ID);
    117             selection.append("=");
    118             selection.append(eventIds[0]);
    119             for (int i = 1; i < eventIds.length; i++) {
    120                 selection.append(" OR ");
    121                 selection.append(CalendarAlerts.EVENT_ID);
    122                 selection.append("=");
    123                 selection.append(eventIds[i]);
    124             }
    125             selection.append(")");
    126         }
    127         return selection.toString();
    128     }
    129 }
    130