Update scrim controller to bubble state when bubbles are expanded

There's one issue with this where if you expand status bar
on top of the expanded bubble there is no scrim in between.

Test: manual / existing tests pass (atest SystemUITests)
Bug: 111236845
Change-Id: I2510758366999131ac7ffbb7505026727f4e40e1
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
index ddb8166..e868f96 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java
@@ -60,6 +60,7 @@
     private Context mContext;
     private BubbleDismissListener mDismissListener;
     private BubbleStateChangeListener mStateChangeListener;
+    private BubbleExpandListener mExpandListener;
 
     private Map<String, BubbleView> mBubbles = new HashMap<>();
     private BubbleStackView mStackView;
@@ -96,6 +97,19 @@
         void onHasBubblesChanged(boolean hasBubbles);
     }
 
+    /**
+     * Listener to find out about stack expansion / collapse events.
+     */
+    public interface BubbleExpandListener {
+        /**
+         * Called when the expansion state of the bubble stack changes.
+         *
+         * @param isExpanding whether it's expanding or collapsing
+         * @param amount fraction of how expanded or collapsed it is, 1 being fully, 0 at the start
+         */
+        void onBubbleExpandChanged(boolean isExpanding, float amount);
+    }
+
     public BubbleController(Context context) {
         mContext = context;
         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
@@ -119,6 +133,16 @@
     }
 
     /**
+     * Set a listener to be notified of bubble expand events.
+     */
+    public void setExpandListener(BubbleExpandListener listener) {
+        mExpandListener = listener;
+        if (mStackView != null) {
+            mStackView.setExpandListener(mExpandListener);
+        }
+    }
+
+    /**
      * Whether or not there are bubbles present, regardless of them being visible on the
      * screen (e.g. if on AOD).
      */
@@ -185,6 +209,9 @@
                 int bubblePosition = sbv.indexOfChild(sbv.findViewById(R.id.scrim_behind)) + 1;
                 sbv.addView(mStackView, bubblePosition,
                         new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
+                if (mExpandListener != null) {
+                    mStackView.setExpandListener(mExpandListener);
+                }
             }
             mStackView.addBubble(bubble);
             if (setPosition) {
diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java
index b6a8a08..365dbda 100644
--- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java
@@ -61,6 +61,7 @@
     private BubbleView mExpandedBubble;
     private Point mCollapsedPosition;
     private BubbleTouchHandler mTouchHandler;
+    private BubbleController.BubbleExpandListener mExpandListener;
 
     private boolean mViewUpdatedRequested = false;
     private boolean mIsAnimating = false;
@@ -176,6 +177,13 @@
     }
 
     /**
+     * Sets the listener to notify when the bubble stack is expanded.
+     */
+    public void setExpandListener(BubbleController.BubbleExpandListener listener) {
+        mExpandListener = listener;
+    }
+
+    /**
      * Whether the stack of bubbles is expanded or not.
      */
     public boolean isExpanded() {
@@ -225,6 +233,9 @@
         }
         mIsExpanded = wasExpanded && mBubbleContainer.getChildCount() > 0;
         requestUpdate();
+        if (wasExpanded && !mIsExpanded && mExpandListener != null) {
+            mExpandListener.onBubbleExpandChanged(mIsExpanded, 1 /* amount */);
+        }
     }
 
     /**
@@ -275,6 +286,9 @@
             mExpandedBubble = shouldExpand ? getTopBubble() : null;
             updateExpandedBubble();
 
+            if (mExpandListener != null) {
+                mExpandListener.onBubbleExpandChanged(mIsExpanded, 1 /* amount */);
+            }
             if (shouldExpand) {
                 // Save current position so that we might return there
                 savePosition();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index a6a9d74..c28a90f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -141,6 +141,7 @@
 import com.android.systemui.UiOffloadThread;
 import com.android.systemui.appops.AppOpsController;
 import com.android.systemui.assist.AssistManager;
+import com.android.systemui.bubbles.BubbleController;
 import com.android.systemui.charging.WirelessChargingAnimation;
 import com.android.systemui.classifier.FalsingLog;
 import com.android.systemui.classifier.FalsingManager;
@@ -456,6 +457,13 @@
     private NotificationMediaManager mMediaManager;
     protected NotificationLockscreenUserManager mLockscreenUserManager;
     protected NotificationRemoteInputManager mRemoteInputManager;
+    protected BubbleController mBubbleController;
+    private final BubbleController.BubbleExpandListener mBubbleExpandListener =
+            (isExpanding, amount) -> {
+                if (amount == 1) {
+                    updateScrimController();
+                }
+            };
 
     private final BroadcastReceiver mWallpaperChangedReceiver = new BroadcastReceiver() {
         @Override
@@ -613,6 +621,8 @@
         mColorExtractor = Dependency.get(SysuiColorExtractor.class);
         mDeviceProvisionedController = Dependency.get(DeviceProvisionedController.class);
         mNavigationBarController = Dependency.get(DisplayNavigationBarController.class);
+        mBubbleController = Dependency.get(BubbleController.class);
+        mBubbleController.setExpandListener(mBubbleExpandListener);
 
         mColorExtractor.addOnColorsChangedListener(this);
         mStatusBarStateController.addListener(this, StatusBarStateController.RANK_STATUS_BAR);
@@ -3845,6 +3855,8 @@
         } else if (mIsKeyguard && !wakeAndUnlocking) {
             mScrimController.transitionTo(mNotificationPanel.isSemiAwake()
                     ? ScrimState.DARK_KEYGUARD : ScrimState.KEYGUARD);
+        } else if (mBubbleController.isStackExpanded()) {
+            mScrimController.transitionTo(ScrimState.BUBBLE_EXPANDED);
         } else {
             mScrimController.transitionTo(ScrimState.UNLOCKED, mUnlockScrimCallback);
         }