Merge "Fix test Dagger initialization deadlock in RecentsDisplayModel" into main
diff --git a/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt b/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt
index a9259d9..505f2cb 100644
--- a/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt
+++ b/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt
@@ -24,6 +24,8 @@
 import com.android.launcher3.dagger.ApplicationContext
 import com.android.launcher3.dagger.LauncherAppSingleton
 import com.android.launcher3.util.DaggerSingletonObject
+import com.android.launcher3.util.DaggerSingletonTracker
+import com.android.launcher3.util.Executors.MAIN_EXECUTOR
 import com.android.quickstep.DisplayModel
 import com.android.quickstep.FallbackWindowInterface
 import com.android.quickstep.dagger.QuickstepBaseAppComponent
@@ -31,7 +33,9 @@
 import javax.inject.Inject
 
 @LauncherAppSingleton
-class RecentsDisplayModel @Inject constructor(@ApplicationContext context: Context) :
+class RecentsDisplayModel
+@Inject
+constructor(@ApplicationContext context: Context, tracker: DaggerSingletonTracker) :
     DisplayModel<RecentsDisplayResource>(context) {
 
     companion object {
@@ -47,17 +51,38 @@
 
     init {
         if (Flags.enableFallbackOverviewInWindow() || Flags.enableLauncherOverviewInWindow()) {
-            displayManager.registerDisplayListener(displayListener, Handler.getMain())
-            createDisplayResource(Display.DEFAULT_DISPLAY)
+            MAIN_EXECUTOR.execute {
+                displayManager.registerDisplayListener(displayListener, Handler.getMain())
+                // In the scenario where displays were added before this display listener was
+                // registered, we should store the RecentsDisplayResources for those displays
+                // directly.
+                displayManager.displays
+                    .filter { getDisplayResource(it.displayId) == null }
+                    .forEach { storeRecentsDisplayResource(it.displayId, it) }
+            }
+            tracker.addCloseable { destroy() }
         }
     }
 
     override fun createDisplayResource(displayId: Int) {
-        if (DEBUG) Log.d(TAG, "create: displayId=$displayId")
+        if (DEBUG) Log.d(TAG, "createDisplayResource: displayId=$displayId")
         getDisplayResource(displayId)?.let {
             return
         }
         val display = displayManager.getDisplay(displayId)
+        if (display == null) {
+            if (DEBUG)
+                Log.w(
+                    TAG,
+                    "createDisplayResource: could not create display for displayId=$displayId",
+                    Exception(),
+                )
+            return
+        }
+        storeRecentsDisplayResource(displayId, display)
+    }
+
+    private fun storeRecentsDisplayResource(displayId: Int, display: Display) {
         displayResourceArray[displayId] =
             RecentsDisplayResource(displayId, context.createDisplayContext(display))
     }
diff --git a/quickstep/tests/multivalentTests/src/com/android/quickstep/LauncherSwipeHandlerV2Test.kt b/quickstep/tests/multivalentTests/src/com/android/quickstep/LauncherSwipeHandlerV2Test.kt
index ffb1f23..0738336 100644
--- a/quickstep/tests/multivalentTests/src/com/android/quickstep/LauncherSwipeHandlerV2Test.kt
+++ b/quickstep/tests/multivalentTests/src/com/android/quickstep/LauncherSwipeHandlerV2Test.kt
@@ -25,7 +25,6 @@
 import com.android.launcher3.dagger.LauncherAppSingleton
 import com.android.launcher3.util.LauncherModelHelper
 import com.android.launcher3.util.MSDLPlayerWrapper
-import com.android.quickstep.fallback.window.RecentsDisplayModel
 import com.android.systemui.contextualeducation.GestureType
 import com.android.systemui.shared.system.InputConsumerController
 import dagger.BindsInstance
@@ -67,9 +66,7 @@
     @Before
     fun setup() {
         sandboxContext.initDaggerComponent(
-            DaggerTestComponent.builder()
-                .bindSystemUiProxy(systemUiProxy)
-                .bindRecentsDisplayModel(RecentsDisplayModel(sandboxContext))
+            DaggerTestComponent.builder().bindSystemUiProxy(systemUiProxy)
         )
         sandboxContext.putObject(
             RotationTouchHelper.INSTANCE,
@@ -122,8 +119,6 @@
     interface Builder : LauncherAppComponent.Builder {
         @BindsInstance fun bindSystemUiProxy(proxy: SystemUiProxy): Builder
 
-        @BindsInstance fun bindRecentsDisplayModel(model: RecentsDisplayModel): Builder
-
         override fun build(): TestComponent
     }
 }
diff --git a/quickstep/tests/multivalentTests/src/com/android/quickstep/recents/window/RecentsDisplayModelTest.kt b/quickstep/tests/multivalentTests/src/com/android/quickstep/recents/window/RecentsDisplayModelTest.kt
index d2aa6ac..44ea73e 100644
--- a/quickstep/tests/multivalentTests/src/com/android/quickstep/recents/window/RecentsDisplayModelTest.kt
+++ b/quickstep/tests/multivalentTests/src/com/android/quickstep/recents/window/RecentsDisplayModelTest.kt
@@ -28,14 +28,9 @@
 import androidx.test.platform.app.InstrumentationRegistry
 import com.android.launcher3.Flags.FLAG_ENABLE_FALLBACK_OVERVIEW_IN_WINDOW
 import com.android.launcher3.Flags.FLAG_ENABLE_LAUNCHER_OVERVIEW_IN_WINDOW
-import com.android.launcher3.dagger.LauncherAppComponent
-import com.android.launcher3.dagger.LauncherAppModule
-import com.android.launcher3.dagger.LauncherAppSingleton
 import com.android.launcher3.util.LauncherModelHelper
 import com.android.launcher3.util.window.CachedDisplayInfo
 import com.android.quickstep.fallback.window.RecentsDisplayModel
-import dagger.BindsInstance
-import dagger.Component
 import org.junit.Assert
 import org.junit.Before
 import org.junit.Rule
@@ -75,10 +70,6 @@
         whenever(displayManager.getDisplay(anyInt())).thenReturn(display)
 
         runOnMainSync { recentsDisplayModel = RecentsDisplayModel.INSTANCE.get(context) }
-        context.initDaggerComponent(
-            DaggerRecentsDisplayModelComponent.builder()
-                .bindRecentsDisplayModel(recentsDisplayModel)
-        )
     }
 
     @Test
@@ -125,14 +116,3 @@
         InstrumentationRegistry.getInstrumentation().runOnMainSync { f.run() }
     }
 }
-
-@LauncherAppSingleton
-@Component(modules = [LauncherAppModule::class])
-interface RecentsDisplayModelComponent : LauncherAppComponent {
-    @Component.Builder
-    interface Builder : LauncherAppComponent.Builder {
-        @BindsInstance fun bindRecentsDisplayModel(model: RecentsDisplayModel): Builder
-
-        override fun build(): RecentsDisplayModelComponent
-    }
-}