summaryrefslogtreecommitdiffstats
path: root/src/com/android/calendar/alerts
diff options
context:
space:
mode:
authorMichael Chan <chanm@google.com>2012-05-15 22:44:25 -0700
committerMichael Chan <chanm@google.com>2012-05-17 01:05:19 -0700
commit25b09db869f377b131052447f62ad02e2505cd87 (patch)
tree2e1a675dd4e852e71f331566d21daa38d4cfc500 /src/com/android/calendar/alerts
parente8701d3f3126d378bcee634418037ef2bcf8a679 (diff)
downloadandroid_packages_apps_Calendar-25b09db869f377b131052447f62ad02e2505cd87.tar.gz
android_packages_apps_Calendar-25b09db869f377b131052447f62ad02e2505cd87.tar.bz2
android_packages_apps_Calendar-25b09db869f377b131052447f62ad02e2505cd87.zip
Add tests for notification
Bug: 6282451 Change-Id: I53816be140f9f331cf84cd4ebb4c5d0d535df645
Diffstat (limited to 'src/com/android/calendar/alerts')
-rw-r--r--src/com/android/calendar/alerts/AlertReceiver.java43
-rw-r--r--src/com/android/calendar/alerts/AlertService.java104
-rw-r--r--src/com/android/calendar/alerts/NotificationMgr.java11
3 files changed, 125 insertions, 33 deletions
diff --git a/src/com/android/calendar/alerts/AlertReceiver.java b/src/com/android/calendar/alerts/AlertReceiver.java
index 59cea57f..97e48c2e 100644
--- a/src/com/android/calendar/alerts/AlertReceiver.java
+++ b/src/com/android/calendar/alerts/AlertReceiver.java
@@ -16,14 +16,10 @@
package com.android.calendar.alerts;
-import com.android.calendar.R;
-import com.android.calendar.Utils;
-
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
-import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
@@ -42,9 +38,12 @@ import android.text.style.RelativeSizeSpan;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
+import com.android.calendar.R;
+import com.android.calendar.Utils;
+import com.android.calendar.alerts.AlertService.NotificationWrapper;
+
import java.util.ArrayList;
import java.util.List;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@@ -216,11 +215,14 @@ public class AlertReceiver extends BroadcastReceiver {
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
- public static Notification makeBasicNotification(Context context, String title,
+ public static NotificationWrapper makeBasicNotification(Context context, String title,
String summaryText, long startMillis, long endMillis, long eventId,
int notificationId, boolean doPopup) {
- return makeBasicNotificationBuilder(context, title, summaryText, startMillis, endMillis,
- eventId, notificationId, doPopup, false, false).build();
+
+ Notification n = makeBasicNotificationBuilder(context, title, summaryText, startMillis,
+ endMillis, eventId, notificationId, doPopup, false, false).build();
+
+ return new NotificationWrapper(n, notificationId, eventId, startMillis, endMillis, doPopup);
}
private static Notification.Builder makeBasicNotificationBuilder(Context context, String title,
@@ -282,7 +284,7 @@ public class AlertReceiver extends BroadcastReceiver {
* Creates an expanding notification. The initial expanded state is decided by
* the notification manager based on the priority.
*/
- public static Notification makeExpandingNotification(Context context, String title,
+ public static NotificationWrapper makeExpandingNotification(Context context, String title,
String summaryText, String description, long startMillis, long endMillis, long eventId,
int notificationId, boolean doPopup, boolean highPriority) {
Notification.Builder basicBuilder = makeBasicNotificationBuilder(context, title,
@@ -309,13 +311,15 @@ public class AlertReceiver extends BroadcastReceiver {
text = stringBuilder;
}
expandedBuilder.bigText(text);
- return expandedBuilder.build();
+
+ return new NotificationWrapper(expandedBuilder.build(), notificationId, eventId,
+ startMillis, endMillis, doPopup);
}
/**
* Creates an expanding digest notification for expired events.
*/
- public static Notification makeDigestNotification(Context context,
+ public static NotificationWrapper makeDigestNotification(Context context,
ArrayList<AlertService.NotificationInfo> notificationInfos, String digestTitle,
boolean expandable) {
if (notificationInfos == null || notificationInfos.size() < 1) {
@@ -360,6 +364,8 @@ public class AlertReceiver extends BroadcastReceiver {
// Set to min priority to encourage the notification manager to collapse it.
notificationBuilder.setPriority(Notification.PRIORITY_MIN);
+ Notification n;
+
if (expandable) {
// Multiple reminders. Combine into an expanded digest notification.
Notification.InboxStyle expandedBuilder = new Notification.InboxStyle(
@@ -409,10 +415,19 @@ public class AlertReceiver extends BroadcastReceiver {
// Remove the title in the expanded form (redundant with the listed items).
expandedBuilder.setBigContentTitle("");
- return expandedBuilder.build();
+ n = expandedBuilder.build();
} else {
- return notificationBuilder.build();
+ n = notificationBuilder.build();
+ }
+
+ NotificationWrapper nw = new NotificationWrapper(n);
+ if (AlertService.DEBUG) {
+ for (AlertService.NotificationInfo info : notificationInfos) {
+ nw.add(new NotificationWrapper(null, 0, info.eventId, info.startMillis,
+ info.endMillis, false));
+ }
}
+ return nw;
}
private static final String[] ATTENDEES_PROJECTION = new String[] {
@@ -499,8 +514,6 @@ public class AlertReceiver extends BroadcastReceiver {
* are no emailable attendees.
*/
private static Intent createEmailIntent(Context context, long eventId) {
- ContentResolver resolver = context.getContentResolver();
-
// TODO: Refactor to move query part into Utils.createEmailAttendeeIntent, to
// be shared with EventInfoFragment.
diff --git a/src/com/android/calendar/alerts/AlertService.java b/src/com/android/calendar/alerts/AlertService.java
index fc4b9880..c6c74075 100644
--- a/src/com/android/calendar/alerts/AlertService.java
+++ b/src/com/android/calendar/alerts/AlertService.java
@@ -16,10 +16,6 @@
package com.android.calendar.alerts;
-import com.android.calendar.GeneralPreferences;
-import com.android.calendar.R;
-import com.android.calendar.Utils;
-
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
@@ -48,6 +44,10 @@ import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;
+import com.android.calendar.GeneralPreferences;
+import com.android.calendar.R;
+import com.android.calendar.Utils;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -62,7 +62,7 @@ public class AlertService extends Service {
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
- private static final String[] ALERT_PROJECTION = new String[] {
+ static final String[] ALERT_PROJECTION = new String[] {
CalendarAlerts._ID, // 0
CalendarAlerts.EVENT_ID, // 1
CalendarAlerts.STATE, // 2
@@ -109,7 +109,72 @@ public class AlertService extends Service {
private static final int MIN_DEPRIORITIZE_GRACE_PERIOD_MS = 15 * MINUTE_MS;
// Hard limit to the number of notifications displayed.
- private static final int MAX_NOTIFICATIONS = 20;
+ public static final int MAX_NOTIFICATIONS = 20;
+
+ // Added wrapper for testing
+ public static class NotificationWrapper {
+ Notification mNotification;
+ long mEventId;
+ long mBegin;
+ long mEnd;
+ ArrayList<NotificationWrapper> mNw;
+
+ public NotificationWrapper(Notification n, int notificationId, long eventId,
+ long startMillis, long endMillis, boolean doPopup) {
+ mNotification = n;
+ mEventId = eventId;
+ mBegin = startMillis;
+ mEnd = endMillis;
+
+ // popup?
+ // notification id?
+ }
+
+ public NotificationWrapper(Notification n) {
+ mNotification = n;
+ }
+
+ public void add(NotificationWrapper nw) {
+ if (mNw == null) {
+ mNw = new ArrayList<NotificationWrapper>();
+ }
+ mNw.add(nw);
+ }
+ }
+
+ // Added wrapper for testing
+ public static class NotificationMgrWrapper implements NotificationMgr {
+ NotificationManager mNm;
+
+ public NotificationMgrWrapper(NotificationManager nm) {
+ mNm = nm;
+ }
+
+ @Override
+ public void cancel(int id) {
+ mNm.cancel(id);
+ }
+
+ @Override
+ public void cancel(String tag, int id) {
+ mNm.cancel(tag, id);
+ }
+
+ @Override
+ public void cancelAll() {
+ mNm.cancelAll();
+ }
+
+ @Override
+ public void notify(int id, NotificationWrapper nw) {
+ mNm.notify(id, nw.mNotification);
+ }
+
+ @Override
+ public void notify(String tag, int id, NotificationWrapper nw) {
+ mNm.notify(tag, id, nw.mNotification);
+ }
+ }
void processMessage(Message msg) {
Bundle bundle = (Bundle) msg.obj;
@@ -148,8 +213,8 @@ public class AlertService extends Service {
static boolean updateAlertNotification(Context context) {
ContentResolver cr = context.getContentResolver();
- NotificationManager nm =
- (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ NotificationMgr nm = new NotificationMgrWrapper(
+ (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
final long currentTime = System.currentTimeMillis();
SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
@@ -182,6 +247,11 @@ public class AlertService extends Service {
return false;
}
+ return generateAlerts(context, nm, prefs, alertCursor, currentTime);
+ }
+
+ public static boolean generateAlerts(Context context, NotificationMgr nm,
+ SharedPreferences prefs, Cursor alertCursor, final long currentTime) {
if (DEBUG) {
Log.d(TAG, "alertCursor count:" + alertCursor.getCount());
}
@@ -246,7 +316,7 @@ public class AlertService extends Service {
int numLowPriority = lowPriorityEvents.size();
if (numLowPriority > 0) {
String expiredDigestTitle = getDigestTitle(lowPriorityEvents);
- Notification notification;
+ NotificationWrapper notification;
if (numLowPriority == 1) {
// If only 1 expired event, display an "old-style" basic alert.
NotificationInfo info = lowPriorityEvents.get(0);
@@ -415,7 +485,6 @@ public class AlertService extends Service {
ContentResolver cr = context.getContentResolver();
HashMap<Long, NotificationInfo> eventIds = new HashMap<Long, NotificationInfo>();
int numFired = 0;
- int notificationId = 1;
try {
while (alertCursor.moveToNext()) {
final long alertId = alertCursor.getLong(ALERT_INDEX_ID);
@@ -487,8 +556,7 @@ public class AlertService extends Service {
continue;
}
- // Pick an Event title for the notification panel by the latest
- // alertTime and give prefer accepted events in case of ties.
+ // TODO: Prefer accepted events in case of ties.
int newStatus;
switch (status) {
case Attendees.ATTENDEE_STATUS_ACCEPTED:
@@ -596,9 +664,9 @@ public class AlertService extends Service {
private static void postNotification(NotificationInfo info, String summaryText,
Context context, boolean highPriority, NotificationPrefs prefs,
- NotificationManager notificationMgr, int notificationId) {
+ NotificationMgr notificationMgr, int notificationId) {
String tickerText = getTickerText(info.eventName, info.location);
- Notification notification = AlertReceiver.makeExpandingNotification(context,
+ NotificationWrapper notification = AlertReceiver.makeExpandingNotification(context,
info.eventName, summaryText, info.description, info.startMillis,
info.endMillis, info.eventId, notificationId, prefs.getDoPopup(),
highPriority);
@@ -657,8 +725,9 @@ public class AlertService extends Service {
}
}
- private static void addNotificationOptions(Notification notification, boolean quietUpdate,
+ private static void addNotificationOptions(NotificationWrapper nw, boolean quietUpdate,
String tickerText, boolean defaultVibrate, String reminderRingtone) {
+ Notification notification = nw.mNotification;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Quietly update notification bar. Nothing new. Maybe something just got deleted.
@@ -683,7 +752,7 @@ public class AlertService extends Service {
}
}
- private static class NotificationPrefs {
+ /* package */ static class NotificationPrefs {
boolean quietUpdate;
private Context context;
private SharedPreferences prefs;
@@ -695,8 +764,7 @@ public class AlertService extends Service {
private static final String EMPTY_RINGTONE = "";
- NotificationPrefs(Context context, SharedPreferences prefs,
- boolean quietUpdate) {
+ NotificationPrefs(Context context, SharedPreferences prefs, boolean quietUpdate) {
this.context = context;
this.prefs = prefs;
this.quietUpdate = quietUpdate;
diff --git a/src/com/android/calendar/alerts/NotificationMgr.java b/src/com/android/calendar/alerts/NotificationMgr.java
new file mode 100644
index 00000000..e0a5baff
--- /dev/null
+++ b/src/com/android/calendar/alerts/NotificationMgr.java
@@ -0,0 +1,11 @@
+package com.android.calendar.alerts;
+
+import com.android.calendar.alerts.AlertService.NotificationWrapper;
+
+public interface NotificationMgr {
+ public void cancel(int id);
+ public void cancel(String tag, int id);
+ public void cancelAll();
+ public void notify(int id, NotificationWrapper notification);
+ public void notify(String tag, int id, NotificationWrapper notification);
+}