Merge "Remove redundant instances of depth controller." into udc-dev
diff --git a/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java b/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java
index 3ec82ce..42e51cd 100644
--- a/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java
+++ b/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java
@@ -1065,6 +1065,9 @@
                     new SurfaceControl.Transaction().remove(dimLayer).apply()));
         }
 
+        backgroundRadiusAnim.addListener(
+                AnimatorListeners.forEndCallback(depthController::dispose));
+
         return backgroundRadiusAnim;
     }
 
diff --git a/quickstep/src/com/android/launcher3/statehandlers/DepthController.java b/quickstep/src/com/android/launcher3/statehandlers/DepthController.java
index d8458c9..7c62763 100644
--- a/quickstep/src/com/android/launcher3/statehandlers/DepthController.java
+++ b/quickstep/src/com/android/launcher3/statehandlers/DepthController.java
@@ -84,8 +84,7 @@
 
                 @Override
                 public void onViewDetachedFromWindow(View view) {
-                    CrossWindowBlurListeners.getInstance().removeListener(mCrossWindowBlurListener);
-                    mLauncher.getScrimView().removeOpaquenessListener(mOpaquenessListener);
+                    removeSecondaryListeners();
                 }
             };
             rootView.addOnAttachStateChangeListener(mOnAttachListener);
@@ -96,6 +95,27 @@
     }
 
     /**
+     * Cleans up after this controller so it can be garbage collected without leaving traces.
+     */
+    public void dispose() {
+        removeSecondaryListeners();
+
+        if (mLauncher.getRootView() != null && mOnAttachListener != null) {
+            mLauncher.getRootView().removeOnAttachStateChangeListener(mOnAttachListener);
+            mOnAttachListener = null;
+        }
+    }
+
+    private void removeSecondaryListeners() {
+        if (mCrossWindowBlurListener != null) {
+            CrossWindowBlurListeners.getInstance().removeListener(mCrossWindowBlurListener);
+        }
+        if (mOpaquenessListener != null) {
+            mLauncher.getScrimView().removeOpaquenessListener(mOpaquenessListener);
+        }
+    }
+
+    /**
      * Sets if the underlying activity is started or not
      */
     public void setActivityStarted(boolean isStarted) {