From 988f34b7f2bb7e57d99ec19fbe9b7024da1fe52f Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Fri, 10 Feb 2017 08:03:22 -0800 Subject: [PATCH] Ensure that filtered notifications are removed from BadgeInfo. There are cases where a BadgeInfo can contain a key that is later used for a notification that should be filtered out. So instead of simply not sending filtered notifications to PopupDataProvider, now we explicitly send them and remove the corresponding key from the BadgeInfo if it exists. Bug: 35239510 Change-Id: I9532f47b1f07b44234f8707657b15b0de519b347 --- .../notification/NotificationListener.java | 40 ++++++++++++++-------- .../android/launcher3/popup/PopupDataProvider.java | 23 ++++++++----- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java index 206bb31d4..5c16176f5 100644 --- a/src/com/android/launcher3/notification/NotificationListener.java +++ b/src/com/android/launcher3/notification/NotificationListener.java @@ -80,9 +80,9 @@ public class NotificationListener extends NotificationListenerService { switch (message.what) { case MSG_NOTIFICATION_POSTED: if (sNotificationsChangedListener != null) { - Pair pair - = (Pair) message.obj; - sNotificationsChangedListener.onNotificationPosted(pair.first, pair.second); + NotificationPostedMsg msg = (NotificationPostedMsg) message.obj; + sNotificationsChangedListener.onNotificationPosted(msg.packageUserKey, + msg.notificationKey, msg.shouldBeFilteredOut); } break; case MSG_NOTIFICATION_REMOVED: @@ -149,23 +149,32 @@ public class NotificationListener extends NotificationListenerService { @Override public void onNotificationPosted(final StatusBarNotification sbn) { super.onNotificationPosted(sbn); - if (!shouldBeFilteredOut(sbn.getNotification())) { - Pair packageUserKeyAndNotificationKey - = new Pair<>(PackageUserKey.fromNotification(sbn), sbn.getKey()); - mWorkerHandler.obtainMessage(MSG_NOTIFICATION_POSTED, packageUserKeyAndNotificationKey) - .sendToTarget(); + mWorkerHandler.obtainMessage(MSG_NOTIFICATION_POSTED, new NotificationPostedMsg(sbn)) + .sendToTarget(); + } + + /** + * An object containing data to send to MSG_NOTIFICATION_POSTED targets. + */ + private class NotificationPostedMsg { + PackageUserKey packageUserKey; + String notificationKey; + boolean shouldBeFilteredOut; + + NotificationPostedMsg(StatusBarNotification sbn) { + packageUserKey = PackageUserKey.fromNotification(sbn); + notificationKey = sbn.getKey(); + shouldBeFilteredOut = shouldBeFilteredOut(sbn.getNotification()); } } @Override public void onNotificationRemoved(final StatusBarNotification sbn) { super.onNotificationRemoved(sbn); - if (!shouldBeFilteredOut(sbn.getNotification())) { - Pair packageUserKeyAndNotificationKey - = new Pair<>(PackageUserKey.fromNotification(sbn), sbn.getKey()); - mWorkerHandler.obtainMessage(MSG_NOTIFICATION_REMOVED, packageUserKeyAndNotificationKey) - .sendToTarget(); - } + Pair packageUserKeyAndNotificationKey + = new Pair<>(PackageUserKey.fromNotification(sbn), sbn.getKey()); + mWorkerHandler.obtainMessage(MSG_NOTIFICATION_REMOVED, packageUserKeyAndNotificationKey) + .sendToTarget(); } /** This makes a potentially expensive binder call and should be run on a background thread. */ @@ -206,7 +215,8 @@ public class NotificationListener extends NotificationListenerService { } public interface NotificationsChangedListener { - void onNotificationPosted(PackageUserKey postedPackageUserKey, String notificationKey); + void onNotificationPosted(PackageUserKey postedPackageUserKey, String notificationKey, + boolean shouldBeFilteredOut); void onNotificationRemoved(PackageUserKey removedPackageUserKey, String notificationKey); void onNotificationFullRefresh(List activeNotifications); } diff --git a/src/com/android/launcher3/popup/PopupDataProvider.java b/src/com/android/launcher3/popup/PopupDataProvider.java index c754fda99..e314b646b 100644 --- a/src/com/android/launcher3/popup/PopupDataProvider.java +++ b/src/com/android/launcher3/popup/PopupDataProvider.java @@ -58,19 +58,26 @@ public class PopupDataProvider implements NotificationListener.NotificationsChan } @Override - public void onNotificationPosted(PackageUserKey postedPackageUserKey, String notificationKey) { + public void onNotificationPosted(PackageUserKey postedPackageUserKey, String notificationKey, + boolean shouldBeFilteredOut) { BadgeInfo badgeInfo = mPackageUserToBadgeInfos.get(postedPackageUserKey); - boolean notificationWasAdded; // As opposed to updated. + boolean notificationWasAddedOrRemoved; // As opposed to updated. if (badgeInfo == null) { - BadgeInfo newBadgeInfo = new BadgeInfo(postedPackageUserKey); - newBadgeInfo.addNotificationKeyIfNotExists(notificationKey); - mPackageUserToBadgeInfos.put(postedPackageUserKey, newBadgeInfo); - notificationWasAdded = true; + if (!shouldBeFilteredOut) { + BadgeInfo newBadgeInfo = new BadgeInfo(postedPackageUserKey); + newBadgeInfo.addNotificationKeyIfNotExists(notificationKey); + mPackageUserToBadgeInfos.put(postedPackageUserKey, newBadgeInfo); + notificationWasAddedOrRemoved = true; + } else { + notificationWasAddedOrRemoved = false; + } } else { - notificationWasAdded = badgeInfo.addNotificationKeyIfNotExists(notificationKey); + notificationWasAddedOrRemoved = shouldBeFilteredOut + ? badgeInfo.removeNotificationKey(notificationKey) + : badgeInfo.addNotificationKeyIfNotExists(notificationKey); } updateLauncherIconBadges(Utilities.singletonHashSet(postedPackageUserKey), - notificationWasAdded); + notificationWasAddedOrRemoved); } @Override -- 2.11.0