Include bubble changes in ranking & move flagging to BubbleExtractor

Previously, only changes to the "allowBubbles" on the channel or
package would trigger a ranking change. This bit only indicates that
the notification is allowed to bubble -- it doesn't indicate that
the notification *is* a bubble. To allow active notifications to
become bubbles if the user changes the setting, we need to
flag them in response to ranking changes.

This CL moves the bubble flagging code into BubbleExtractor that
way the flag is always updated during ranking changes.

BubbleController listens to ranking changes and adds / removes bubbles
based on the ranking. The ranking needs to have an isBubble bit on
it because ranking changes won't pipe flag updates through. SysUI
uses this bit to flag the entry on SysUI's side.

Moves the shortcut getting / validating code into a helper class.

Also removes the inline reply requirement.

Test: NotificationManagerTest NotificationManagerServiceTest BubbleExtractorTest ShortcutHelperTest BubbleCheckerTest
Bug: 149736441
Change-Id: Ib5b62923c123187ae5f7073ec7ca50d7e20c04b1
Merged-In: Ib5b62923c123187ae5f7073ec7ca50d7e20c04b1
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
index 22c2c7e..1138b02 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
@@ -52,6 +52,7 @@
 import android.graphics.Rect;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.service.notification.NotificationListenerService;
 import android.service.notification.NotificationListenerService.RankingMap;
 import android.service.notification.ZenModeConfig;
 import android.util.ArraySet;
@@ -146,13 +147,15 @@
     private BubbleData mBubbleData;
     @Nullable private BubbleStackView mStackView;
     private BubbleIconFactory mBubbleIconFactory;
-    private int mMaxBubbles;
 
     // Tracks the id of the current (foreground) user.
     private int mCurrentUserId;
     // Saves notification keys of active bubbles when users are switched.
     private final SparseSetArray<String> mSavedBubbleKeysPerUser;
 
+    // Used when ranking updates occur and we check if things should bubble / unbubble
+    private NotificationListenerService.Ranking mTmpRanking;
+
     // Saves notification keys of user created "fake" bubbles so that we can allow notifications
     // like these to bubble by default. Doesn't persist across reboots, not a long-term solution.
     private final HashSet<String> mUserCreatedBubbles;
@@ -338,7 +341,6 @@
 
         configurationController.addCallback(this /* configurationListener */);
 
-        mMaxBubbles = mContext.getResources().getInteger(R.integer.bubbles_max_rendered);
         mBubbleData = data;
         mBubbleData.setListener(mBubbleDataListener);
         mBubbleData.setSuppressionChangedListener(new NotificationSuppressionChangedListener() {
@@ -939,9 +941,29 @@
         }
     }
 
+    /**
+     * Called when NotificationListener has received adjusted notification rank and reapplied
+     * filtering and sorting. This is used to dismiss or create bubbles based on changes in
+     * permissions on the notification channel or the global setting.
+     *
+     * @param rankingMap the updated ranking map from NotificationListenerService
+     */
     private void onRankingUpdated(RankingMap rankingMap) {
-        // Forward to BubbleData to block any bubbles which should no longer be shown
-        mBubbleData.notificationRankingUpdated(rankingMap);
+        if (mTmpRanking == null) {
+            mTmpRanking = new NotificationListenerService.Ranking();
+        }
+        String[] orderedKeys = rankingMap.getOrderedKeys();
+        for (int i = 0; i < orderedKeys.length; i++) {
+            String key = orderedKeys[i];
+            NotificationEntry entry = mNotificationEntryManager.getPendingOrActiveNotif(key);
+            rankingMap.getRanking(key, mTmpRanking);
+            if (mBubbleData.hasBubbleWithKey(key) && !mTmpRanking.canBubble()) {
+                mBubbleData.notificationEntryRemoved(entry, BubbleController.DISMISS_BLOCKED);
+            } else if (entry != null && mTmpRanking.isBubble()) {
+                entry.setFlagBubble(true);
+                onEntryUpdated(entry);
+            }
+        }
     }
 
     @SuppressWarnings("FieldCanBeLocal")