• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.Notification;
20 import android.app.PendingIntent;
21 import android.app.Service;
22 import android.content.BroadcastReceiver;
23 import android.content.ContentUris;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.res.Resources;
27 import android.database.Cursor;
28 import android.net.Uri;
29 import android.os.Handler;
30 import android.os.HandlerThread;
31 import android.os.PowerManager;
32 import android.provider.CalendarContract.Attendees;
33 import android.provider.CalendarContract.Calendars;
34 import android.provider.CalendarContract.Events;
35 import android.telephony.TelephonyManager;
36 import android.text.Spannable;
37 import android.text.SpannableStringBuilder;
38 import android.text.TextUtils;
39 import android.text.style.RelativeSizeSpan;
40 import android.text.style.TextAppearanceSpan;
41 import android.text.style.URLSpan;
42 import android.util.Log;
43 import android.view.View;
44 import android.widget.RemoteViews;
45 
46 import com.android.calendar.R;
47 import com.android.calendar.Utils;
48 import com.android.calendar.alerts.AlertService.NotificationWrapper;
49 
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.regex.Pattern;
53 
54 /**
55  * Receives android.intent.action.EVENT_REMINDER intents and handles
56  * event reminders.  The intent URI specifies an alert id in the
57  * CalendarAlerts database table.  This class also receives the
58  * BOOT_COMPLETED intent so that it can add a status bar notification
59  * if there are Calendar event alarms that have not been dismissed.
60  * It also receives the TIME_CHANGED action so that it can fire off
61  * snoozed alarms that have become ready.  The real work is done in
62  * the AlertService class.
63  *
64  * To trigger this code after pushing the apk to device:
65  * adb shell am broadcast -a "android.intent.action.EVENT_REMINDER"
66  *    -n "com.android.calendar/.alerts.AlertReceiver"
67  */
68 public class AlertReceiver extends BroadcastReceiver {
69     private static final String TAG = "AlertReceiver";
70 
71     // The broadcast for notification refreshes scheduled by the app. This is to
72     // distinguish the EVENT_REMINDER broadcast sent by the provider.
73     public static final String EVENT_REMINDER_APP_ACTION =
74             "com.android.calendar.EVENT_REMINDER_APP";
75 
76     public static final String ACTION_DISMISS_OLD_REMINDERS = "removeOldReminders";
77 
78     @Override
onReceive(final Context context, final Intent intent)79     public void onReceive(final Context context, final Intent intent) {
80         if (AlertService.DEBUG) {
81             Log.d(TAG, "onReceive: a=" + intent.getAction() + " " + intent.toString());
82         }
83         closeNotificationShade(context);
84     }
85 
makeBasicNotification(Context context, String title, String summaryText, long startMillis, long endMillis, long eventId, int notificationId, boolean doPopup, int priority)86     public static NotificationWrapper makeBasicNotification(Context context, String title,
87             String summaryText, long startMillis, long endMillis, long eventId,
88             int notificationId, boolean doPopup, int priority) {
89         Notification n = buildBasicNotification(new Notification.Builder(context),
90                 context, title, summaryText, startMillis, endMillis, eventId, notificationId,
91                 doPopup, priority, false);
92         return new NotificationWrapper(n, notificationId, eventId, startMillis, endMillis, doPopup);
93     }
94 
buildBasicNotification(Notification.Builder notificationBuilder, Context context, String title, String summaryText, long startMillis, long endMillis, long eventId, int notificationId, boolean doPopup, int priority, boolean addActionButtons)95     private static Notification buildBasicNotification(Notification.Builder notificationBuilder,
96             Context context, String title, String summaryText, long startMillis, long endMillis,
97             long eventId, int notificationId, boolean doPopup, int priority,
98             boolean addActionButtons) {
99         Resources resources = context.getResources();
100         if (title == null || title.length() == 0) {
101             title = resources.getString(R.string.no_title_label);
102         }
103 
104         // Create the base notification.
105         notificationBuilder.setContentTitle(title);
106         notificationBuilder.setContentText(summaryText);
107         notificationBuilder.setSmallIcon(R.drawable.stat_notify_calendar);
108         if (Utils.isJellybeanOrLater()) {
109             // Turn off timestamp.
110             notificationBuilder.setWhen(0);
111 
112             // Should be one of the values in Notification (ie. Notification.PRIORITY_HIGH, etc).
113             // A higher priority will encourage notification manager to expand it.
114             notificationBuilder.setPriority(priority);
115         }
116         return notificationBuilder.getNotification();
117     }
118 
closeNotificationShade(Context context)119     private void closeNotificationShade(Context context) {
120         Intent closeNotificationShadeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
121         context.sendBroadcast(closeNotificationShadeIntent);
122     }
123 }
124