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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/* -*- 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 android.app.Notification;
import android.app.PendingIntent;
import android.text.TextUtils;
import android.util.Log;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
/**
* Client for posting notifications through a NotificationHandler.
*/
public abstract class NotificationClient {
private static final String LOGTAG = "GeckoNotificationClient";
private volatile NotificationHandler mHandler;
private boolean mReady;
private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>();
private final ConcurrentHashMap<Integer, UpdateRunnable> mUpdatesMap =
new ConcurrentHashMap<Integer, UpdateRunnable>();
/**
* Runnable that is reused between update notifications.
*
* Updates happen frequently, so reusing Runnables prevents frequent dynamic allocation.
*/
private class UpdateRunnable implements Runnable {
private long mProgress;
private long mProgressMax;
private String mAlertText;
final private int mNotificationID;
public UpdateRunnable(int notificationID) {
mNotificationID = notificationID;
}
public synchronized boolean updateProgress(long progress, long progressMax, String alertText) {
if (progress == mProgress
&& mProgressMax == progressMax
&& TextUtils.equals(mAlertText, alertText)) {
return false;
}
mProgress = progress;
mProgressMax = progressMax;
mAlertText = alertText;
return true;
}
@Override
public void run() {
long progress;
long progressMax;
String alertText;
synchronized (this) {
progress = mProgress;
progressMax = mProgressMax;
alertText = mAlertText;
}
mHandler.update(mNotificationID, progress, progressMax, alertText);
}
};
/**
* Adds a notification.
*
* @see NotificationHandler#add(int, String, String, String, PendingIntent, PendingIntent)
*/
public synchronized void add(final int notificationID, final String aImageUrl,
final String aAlertTitle, final String aAlertText, final PendingIntent contentIntent) {
mTaskQueue.add(new Runnable() {
@Override
public void run() {
mHandler.add(notificationID, aImageUrl, aAlertTitle, aAlertText, contentIntent);
}
});
notify();
if (!mReady) {
bind();
}
}
/**
* Adds a notification.
*
* @see NotificationHandler#add(int, Notification)
*/
public synchronized void add(final int notificationID, final Notification notification) {
mTaskQueue.add(new Runnable() {
@Override
public void run() {
mHandler.add(notificationID, notification);
}
});
notify();
if (!mReady) {
bind();
}
}
/**
* Updates a notification.
*
* @see NotificationHandler#update(int, long, long, String)
*/
public void update(final int notificationID, final long aProgress, final long aProgressMax,
final String aAlertText) {
UpdateRunnable runnable = mUpdatesMap.get(notificationID);
if (runnable == null) {
runnable = new UpdateRunnable(notificationID);
mUpdatesMap.put(notificationID, runnable);
}
// If we've already posted an update with these values, there's no
// need to do it again.
if (!runnable.updateProgress(aProgress, aProgressMax, aAlertText)) {
return;
}
synchronized (this) {
if (mReady) {
mTaskQueue.add(runnable);
notify();
}
}
}
/**
* Removes a notification.
*
* @see NotificationHandler#remove(int)
*/
public synchronized void remove(final int notificationID) {
if (!mReady) {
return;
}
mTaskQueue.add(new Runnable() {
@Override
public void run() {
mHandler.remove(notificationID);
mUpdatesMap.remove(notificationID);
}
});
notify();
}
/**
* Determines whether a notification is showing progress.
*
* @see NotificationHandler#isProgressStyle(int)
*/
public boolean isOngoing(int notificationID) {
final NotificationHandler handler = mHandler;
return handler != null && handler.isOngoing(notificationID);
}
protected void bind() {
mReady = true;
}
protected void unbind() {
mReady = false;
mUpdatesMap.clear();
}
protected void connectHandler(NotificationHandler handler) {
mHandler = handler;
new Thread(new NotificationRunnable()).start();
}
private class NotificationRunnable implements Runnable {
@Override
public void run() {
Runnable r;
try {
while (true) {
// Synchronize polls to prevent tasks from being added to the queue
// during the isDone check.
synchronized (NotificationClient.this) {
r = mTaskQueue.poll();
while (r == null) {
if (mHandler.isDone()) {
unbind();
return;
}
NotificationClient.this.wait();
r = mTaskQueue.poll();
}
}
r.run();
}
} catch (InterruptedException e) {
Log.e(LOGTAG, "Notification task queue processing interrupted", e);
}
}
}
}
|