*/
public abstract void onNotificationRemoved(StatusBarNotification sbn);
+ /**
+ * Implement this method to learn about when the listener is enabled and connected to
+ * the notification manager. You are safe to call {@link #getActiveNotifications(String[])
+ * at this time.
+ *
+ * @param notificationKeys The notification keys for all currently posted notifications.
+ */
+ public void onListenerConnected(String[] notificationKeys) {
+ // optional
+ }
+
private final INotificationManager getNotificationInterface() {
if (mNoMan == null) {
mNoMan = INotificationManager.Stub.asInterface(
* {@see #cancelNotification(String, String, int)}
*/
public final void cancelAllNotifications() {
+ cancelNotifications(null /*all*/);
+ }
+
+ /**
+ * Inform the notification manager about dismissal of specific notifications.
+ * <p>
+ * Use this if your listener has a user interface that allows the user to dismiss
+ * multiple notifications at once.
+ *
+ * @param keys Notifications to dismiss, or {@code null} to dismiss all.
+ *
+ * {@see #cancelNotification(String, String, int)}
+ */
+ public final void cancelNotifications(String[] keys) {
if (!isBound()) return;
try {
- getNotificationInterface().cancelAllNotificationsFromListener(mWrapper);
+ getNotificationInterface().cancelNotificationsFromListener(mWrapper, keys);
} catch (android.os.RemoteException ex) {
Log.v(TAG, "Unable to contact notification manager", ex);
}
/**
* Request the list of outstanding notifications (that is, those that are visible to the
- * current user). Useful when starting up and you don't know what's already been posted.
+ * current user). Useful when you don't know what's already been posted.
*
* @return An array of active notifications.
*/
public StatusBarNotification[] getActiveNotifications() {
+ return getActiveNotifications(null /*all*/);
+ }
+
+ /**
+ * Request the list of outstanding notifications (that is, those that are visible to the
+ * current user). Useful when you don't know what's already been posted.
+ *
+ * @param keys A specific list of notification keys, or {@code null} for all.
+ * @return An array of active notifications.
+ */
+ public StatusBarNotification[] getActiveNotifications(String[] keys) {
+ if (!isBound()) return null;
+ try {
+ return getNotificationInterface().getActiveNotificationsFromListener(mWrapper, keys);
+ } catch (android.os.RemoteException ex) {
+ Log.v(TAG, "Unable to contact notification manager", ex);
+ }
+ return null;
+ }
+
+ /**
+ * Request the list of outstanding notification keys(that is, those that are visible to the
+ * current user). You can use the notification keys for subsequent retrieval via
+ * {@link #getActiveNotifications(String[]) or dismissal via
+ * {@link #cancelNotifications(String[]).
+ *
+ * @return An array of active notification keys.
+ */
+ public String[] getActiveNotificationKeys() {
if (!isBound()) return null;
try {
- return getNotificationInterface().getActiveNotificationsFromListener(mWrapper);
+ return getNotificationInterface().getActiveNotificationKeysFromListener(mWrapper);
} catch (android.os.RemoteException ex) {
Log.v(TAG, "Unable to contact notification manager", ex);
}
Log.w(TAG, "Error running onNotificationRemoved", t);
}
}
+ @Override
+ public void onListenerConnected(String[] notificationKeys) {
+ try {
+ NotificationListenerService.this.onListenerConnected(notificationKeys);
+ } catch (Throwable t) {
+ Log.w(TAG, "Error running onListenerConnected", t);
+ }
+ }
}
}
import android.service.notification.StatusBarNotification;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
+import android.util.ArrayMap;
import android.util.AtomicFile;
-import android.util.EventLog;
import android.util.Log;
import android.util.Slog;
import android.util.SparseArray;
import android.widget.Toast;
import com.android.internal.R;
-
import com.android.internal.notification.NotificationScorer;
import com.android.server.EventLogTags;
import com.android.server.statusbar.StatusBarManagerInternal;
// used as a mutex for access to all active notifications & listeners
final ArrayList<NotificationRecord> mNotificationList =
new ArrayList<NotificationRecord>();
-
+ final ArrayMap<String, NotificationRecord> mNotificationsByKey =
+ new ArrayMap<String, NotificationRecord>();
final ArrayList<ToastRecord> mToastQueue = new ArrayList<ToastRecord>();
ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
+ boolean added = false;
synchronized (mNotificationList) {
mServicesBinding.remove(servicesBindingTag);
try {
= new NotificationListenerInfo(
mListener, name, userid, this);
service.linkToDeath(info, 0);
- mListeners.add(info);
+ added = mListeners.add(info);
} catch (RemoteException e) {
// already dead
}
}
+ if (added) {
+ final String[] keys =
+ getActiveNotificationKeysFromListener(mListener);
+ try {
+ mListener.onListenerConnected(keys);
+ } catch (RemoteException e) {
+ // we tried
+ }
+ }
}
@Override
pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
+ " / " + idDebugString(baseContext, sbn.getPackageName(), notification.icon));
pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.getScore());
+ pw.println(prefix + " key=" + sbn.getKey());
pw.println(prefix + " contentIntent=" + notification.contentIntent);
pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
pw.println(prefix + " tickerText=" + notification.tickerText);
@Override
public final String toString() {
return String.format(
- "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
+ "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d key=%s: %s)",
System.identityHashCode(this),
this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(),
- this.sbn.getTag(), this.sbn.getScore(), this.sbn.getNotification());
+ this.sbn.getTag(), this.sbn.getScore(), this.sbn.getKey(),
+ this.sbn.getNotification());
}
}
* @param token The binder for the listener, to check that the caller is allowed
*/
@Override
- public void cancelAllNotificationsFromListener(INotificationListener token) {
+ public void cancelNotificationsFromListener(INotificationListener token, String[] keys) {
final int callingUid = Binder.getCallingUid();
final int callingPid = Binder.getCallingPid();
long identity = Binder.clearCallingIdentity();
try {
synchronized (mNotificationList) {
- NotificationListenerInfo info = checkListenerTokenLocked(token);
- cancelAllLocked(callingUid, callingPid, info.userid,
- REASON_LISTENER_CANCEL_ALL, info);
+ final NotificationListenerInfo info = checkListenerTokenLocked(token);
+ if (keys != null) {
+ final int N = keys.length;
+ for (int i = 0; i < N; i++) {
+ NotificationRecord r = mNotificationsByKey.get(keys[i]);
+ if (r != null) {
+ cancelNotificationFromListenerLocked(info, callingUid, callingPid,
+ r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId());
+ }
+ }
+ } else {
+ cancelAllLocked(callingUid, callingPid, info.userid,
+ REASON_LISTENER_CANCEL_ALL, info);
+ }
}
} finally {
Binder.restoreCallingIdentity(identity);
}
}
+ private void cancelNotificationFromListenerLocked(NotificationListenerInfo info,
+ int callingUid, int callingPid, String pkg, String tag, int id) {
+ cancelNotification(callingUid, callingPid, pkg, tag, id, 0,
+ Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
+ true,
+ info.userid, REASON_LISTENER_CANCEL, info);
+ }
+
/**
* Allow an INotificationListener to simulate clearing (dismissing) a single notification.
*
long identity = Binder.clearCallingIdentity();
try {
synchronized (mNotificationList) {
- NotificationListenerInfo info = checkListenerTokenLocked(token);
- cancelNotification(callingUid, callingPid, pkg, tag, id, 0,
- Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
- true,
- info.userid, REASON_LISTENER_CANCEL, info);
+ final NotificationListenerInfo info = checkListenerTokenLocked(token);
+ cancelNotificationFromListenerLocked(info, callingUid, callingPid,
+ pkg, tag, id);
}
} finally {
Binder.restoreCallingIdentity(identity);
*/
@Override
public StatusBarNotification[] getActiveNotificationsFromListener(
- INotificationListener token) {
+ INotificationListener token, String[] keys) {
synchronized (mNotificationList) {
- NotificationListenerInfo info = checkListenerTokenLocked(token);
- ArrayList<StatusBarNotification> list = new ArrayList<StatusBarNotification>();
- final int N = mNotificationList.size();
- for (int i=0; i<N; i++) {
- StatusBarNotification sbn = mNotificationList.get(i).sbn;
- if (info.enabledAndUserMatches(sbn)) {
- list.add(sbn);
+ final NotificationListenerInfo info = checkListenerTokenLocked(token);
+ final ArrayList<StatusBarNotification> list
+ = new ArrayList<StatusBarNotification>();
+ if (keys == null) {
+ final int N = mNotificationList.size();
+ for (int i=0; i<N; i++) {
+ StatusBarNotification sbn = mNotificationList.get(i).sbn;
+ if (info.enabledAndUserMatches(sbn)) {
+ list.add(sbn);
+ }
+ }
+ } else {
+ final int N = keys.length;
+ for (int i=0; i<N; i++) {
+ NotificationRecord r = mNotificationsByKey.get(keys[i]);
+ if (r != null && info.enabledAndUserMatches(r.sbn)) {
+ list.add(r.sbn);
+ }
}
}
return list.toArray(new StatusBarNotification[list.size()]);
}
@Override
+ public String[] getActiveNotificationKeysFromListener(INotificationListener token) {
+ return NotificationManagerService.this.getActiveNotificationKeysFromListener(token);
+ }
+
+ @Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
!= PackageManager.PERMISSION_GRANTED) {
}
};
+ private String[] getActiveNotificationKeysFromListener(INotificationListener token) {
+ synchronized (mNotificationList) {
+ final NotificationListenerInfo info = checkListenerTokenLocked(token);
+ final ArrayList<String> keys = new ArrayList<String>();
+ final int N = mNotificationList.size();
+ for (int i=0; i<N; i++) {
+ final StatusBarNotification sbn = mNotificationList.get(i).sbn;
+ if (info.enabledAndUserMatches(sbn)) {
+ keys.add(sbn.getKey());
+ }
+ }
+ return keys.toArray(new String[keys.size()]);
+ }
+ }
+
void dumpImpl(PrintWriter pw) {
pw.println("Current Notification Manager state:");
old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE;
}
}
+ if (old != null) {
+ mNotificationsByKey.remove(old.sbn.getKey());
+ }
+ mNotificationsByKey.put(n.getKey(), r);
// Ensure if this is a foreground service that the proper additional
// flags are set.
}
mNotificationList.remove(index);
+ mNotificationsByKey.remove(r.sbn.getKey());
cancelNotificationLocked(r, sendDelete);
updateLightsLocked();
return true;
}
mNotificationList.remove(i);
+ mNotificationsByKey.remove(r.sbn.getKey());
cancelNotificationLocked(r, false);
}
if (canceledSomething) {
final int N = mNotificationList.size();
for (int i=N-1; i>=0; i--) {
NotificationRecord r = mNotificationList.get(i);
-
if (!notificationMatchesUserIdOrRelated(r, userId)) {
continue;
}
if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_NO_CLEAR)) == 0) {
mNotificationList.remove(i);
+ mNotificationsByKey.remove(r.sbn.getKey());
cancelNotificationLocked(r, true);
}
}