forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NotificationHandler.java
157 lines (135 loc) · 5.84 KB
/
NotificationHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
import android.app.PendingIntent;
import android.content.Context;
import android.net.Uri;
public class NotificationHandler {
private final ConcurrentHashMap<Integer, AlertNotification>
mAlertNotifications = new ConcurrentHashMap<Integer, AlertNotification>();
private final Context mContext;
/**
* Notification associated with this service's foreground state.
*
* {@link android.app.Service#startForeground(int, android.app.Notification)}
* associates the foreground with exactly one notification from the service.
* To keep Fennec alive during downloads (and to make sure it can be killed
* once downloads are complete), we make sure that the foreground is always
* associated with an active progress notification if and only if at least
* one download is in progress.
*/
private AlertNotification mForegroundNotification;
public NotificationHandler(Context context) {
mContext = context;
}
/**
* Adds a notification.
*
* @param notificationID the unique ID of the notification
* @param aImageUrl URL of the image to use
* @param aAlertTitle title of the notification
* @param aAlertText text of the notification
* @param contentIntent Intent used when the notification is clicked
* @param clearIntent Intent used when the notification is removed
*/
public void add(int notificationID, String aImageUrl, String aAlertTitle,
String aAlertText, PendingIntent contentIntent) {
// Remove the old notification with the same ID, if any
remove(notificationID);
int icon = R.drawable.ic_status_logo;
Uri imageUri = Uri.parse(aImageUrl);
final String scheme = imageUri.getScheme();
if ("drawable".equals(scheme)) {
String resource = imageUri.getSchemeSpecificPart();
resource = resource.substring(resource.lastIndexOf('/') + 1);
try {
final Class<R.drawable> drawableClass = R.drawable.class;
final Field f = drawableClass.getField(resource);
icon = f.getInt(null);
} catch (final Exception e) {} // just means the resource doesn't exist
imageUri = null;
}
final AlertNotification notification = new AlertNotification(mContext, notificationID,
icon, aAlertTitle, aAlertText, System.currentTimeMillis(), imageUri);
notification.setLatestEventInfo(mContext, aAlertTitle, aAlertText, contentIntent);
notification.show();
mAlertNotifications.put(notification.getId(), notification);
}
/**
* Updates a notification.
*
* @param notificationID ID of existing notification
* @param aProgress progress of item being updated
* @param aProgressMax max progress of item being updated
* @param aAlertText text of the notification
*/
public void update(int notificationID, long aProgress, long aProgressMax, String aAlertText) {
final AlertNotification notification = mAlertNotifications.get(notificationID);
if (notification == null) {
return;
}
notification.updateProgress(aAlertText, aProgress, aProgressMax);
if (mForegroundNotification == null && notification.isProgressStyle()) {
setForegroundNotification(notification);
}
// Hide the notification at 100%
if (aProgress == aProgressMax) {
remove(notificationID);
}
}
/**
* Removes a notification.
*
* @param notificationID ID of existing notification
*/
public void remove(int notificationID) {
final AlertNotification notification = mAlertNotifications.remove(notificationID);
if (notification != null) {
updateForegroundNotification(notification);
notification.cancel();
}
}
/**
* Determines whether the service is done.
*
* The service is considered finished when all notifications have been
* removed.
*
* @return whether all notifications have been removed
*/
public boolean isDone() {
return mAlertNotifications.isEmpty();
}
/**
* Determines whether a notification is showing progress.
*
* @param notificationID the notification to check
* @return whether the notification is progress style
*/
public boolean isProgressStyle(int notificationID) {
final AlertNotification notification = mAlertNotifications.get(notificationID);
return notification != null && notification.isProgressStyle();
}
protected void setForegroundNotification(AlertNotification notification) {
mForegroundNotification = notification;
}
private void updateForegroundNotification(AlertNotification oldNotification) {
if (mForegroundNotification == oldNotification) {
// If we're removing the notification associated with the
// foreground, we need to pick another active notification to act
// as the foreground notification.
AlertNotification foregroundNotification = null;
for (final AlertNotification notification : mAlertNotifications.values()) {
if (notification.isProgressStyle()) {
foregroundNotification = notification;
break;
}
}
setForegroundNotification(foregroundNotification);
}
}
}