Merge "Ensure that filtered notifications are removed from BadgeInfo." into ub-launcher3-master
diff --git a/src/com/android/launcher3/notification/NotificationListener.java b/src/com/android/launcher3/notification/NotificationListener.java
index 206bb31..5c16176 100644
--- a/src/com/android/launcher3/notification/NotificationListener.java
+++ b/src/com/android/launcher3/notification/NotificationListener.java
@@ -80,9 +80,9 @@
             switch (message.what) {
                 case MSG_NOTIFICATION_POSTED:
                     if (sNotificationsChangedListener != null) {
-                        Pair<PackageUserKey, String> pair
-                                = (Pair<PackageUserKey, String>) 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 @@
     @Override
     public void onNotificationPosted(final StatusBarNotification sbn) {
         super.onNotificationPosted(sbn);
-        if (!shouldBeFilteredOut(sbn.getNotification())) {
-            Pair<PackageUserKey, String> 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<PackageUserKey, String> packageUserKeyAndNotificationKey
-                    = new Pair<>(PackageUserKey.fromNotification(sbn), sbn.getKey());
-            mWorkerHandler.obtainMessage(MSG_NOTIFICATION_REMOVED, packageUserKeyAndNotificationKey)
-                    .sendToTarget();
-        }
+        Pair<PackageUserKey, String> 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 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<StatusBarNotification> activeNotifications);
     }
diff --git a/src/com/android/launcher3/popup/PopupDataProvider.java b/src/com/android/launcher3/popup/PopupDataProvider.java
index c754fda..e314b64 100644
--- a/src/com/android/launcher3/popup/PopupDataProvider.java
+++ b/src/com/android/launcher3/popup/PopupDataProvider.java
@@ -58,19 +58,26 @@
     }
 
     @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