Fix stale background blur drawable in DecorView

The BackgroundBlurDrawable is linked to a specific ViewRootImpl.
When the view is detached and re-attached, the ViewRootImpl changes, so
we need to reinitialize the background blur drawable inside DecorView.

This CL properly handles onDetachedFromWindow in DecorView.

Bug: 177523043
Test: m && atest BlurAggregatorTest
Change-Id: I2e25a2f85bdf95c151fd01dc794a6bc4b16c03b1
diff --git a/core/java/com/android/internal/policy/DecorView.java b/core/java/com/android/internal/policy/DecorView.java
index 9a91d20..424632fe 100644
--- a/core/java/com/android/internal/policy/DecorView.java
+++ b/core/java/com/android/internal/policy/DecorView.java
@@ -258,6 +258,7 @@
     private Drawable mLastOriginalBackgroundDrawable;
     private Drawable mResizingBackgroundDrawable;
     private BackgroundBlurDrawable mBackgroundBlurDrawable;
+    private BackgroundBlurDrawable mLastBackgroundBlurDrawable;
 
     /**
      * Temporary holder for a window background when it is set before {@link #mWindow} is
@@ -289,7 +290,6 @@
 
     private int mOriginalBackgroundBlurRadius = 0;
     private int mBackgroundBlurRadius = 0;
-    private int mLastBackgroundBlurRadius = 0;
     private boolean mCrossWindowBlurEnabled;
     private final ViewTreeObserver.OnPreDrawListener mBackgroundBlurOnPreDrawListener = () -> {
         updateBackgroundBlurCorners();
@@ -1278,13 +1278,13 @@
         }
 
         if (mBackgroundInsets.equals(mLastBackgroundInsets)
-                && mBackgroundBlurRadius == mLastBackgroundBlurRadius
+                && mBackgroundBlurDrawable == mLastBackgroundBlurDrawable
                 && mLastOriginalBackgroundDrawable == mOriginalBackgroundDrawable) {
             return;
         }
 
         Drawable destDrawable = mOriginalBackgroundDrawable;
-        if (mBackgroundBlurRadius > 0) {
+        if (mBackgroundBlurDrawable != null) {
             destDrawable = new LayerDrawable(new Drawable[] {mBackgroundBlurDrawable,
                                                              mOriginalBackgroundDrawable});
         }
@@ -1309,7 +1309,7 @@
         super.setBackgroundDrawable(destDrawable);
 
         mLastBackgroundInsets = mBackgroundInsets;
-        mLastBackgroundBlurRadius = mBackgroundBlurRadius;
+        mLastBackgroundBlurDrawable = mBackgroundBlurDrawable;
         mLastOriginalBackgroundDrawable = mOriginalBackgroundDrawable;
     }
 
@@ -1334,11 +1334,11 @@
                 ? mOriginalBackgroundBlurRadius : 0;
         if (mBackgroundBlurDrawable == null && mBackgroundBlurRadius > 0) {
             mBackgroundBlurDrawable = getViewRootImpl().createBackgroundBlurDrawable();
+            updateBackgroundDrawable();
         }
 
         if (mBackgroundBlurDrawable != null) {
             mBackgroundBlurDrawable.setBlurRadius(mBackgroundBlurRadius);
-            updateBackgroundDrawable();
         }
     }
 
@@ -1357,12 +1357,20 @@
                 updateBackgroundBlurRadius();
             }
         } else if (mCrossWindowBlurEnabledListener != null) {
-            mCrossWindowBlurEnabledListener = null;
+            updateBackgroundBlurRadius();
+            removeBackgroundBlurDrawable();
+        }
+    }
+
+    void removeBackgroundBlurDrawable() {
+        if (mCrossWindowBlurEnabledListener != null) {
             getContext().getSystemService(WindowManager.class)
                     .removeCrossWindowBlurEnabledListener(mCrossWindowBlurEnabledListener);
-            getViewTreeObserver().removeOnPreDrawListener(mBackgroundBlurOnPreDrawListener);
-            updateBackgroundBlurRadius();
+            mCrossWindowBlurEnabledListener = null;
         }
+        getViewTreeObserver().removeOnPreDrawListener(mBackgroundBlurOnPreDrawListener);
+        mBackgroundBlurDrawable = null;
+        updateBackgroundDrawable();
     }
 
     @Override
@@ -1847,6 +1855,8 @@
             mFloatingToolbar = null;
         }
 
+        removeBackgroundBlurDrawable();
+
         PhoneWindow.PanelFeatureState st = mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false);
         if (st != null && st.menu != null && mFeatureId < 0) {
             st.menu.close();