Fix test Dagger initialization deadlock in RecentsDisplayModel

WallpaperColorHints is a MainThreadInitializedObject that was causing a deadlock in RecentsDisplayModel initialization. Converting WallpaperColorHints to a dagger singleton.

Flag: com.android.launcher3.enable_fallback_overview_in_window
Bug: 377678992
Test: pre/post submit
Change-Id: Ib5b4eab27b4e28baf1208915fa7cf6dbd36bc1f5
diff --git a/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt b/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt
index e7e9f51..31a1be8 100644
--- a/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt
+++ b/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt
@@ -25,6 +25,7 @@
 import com.android.launcher3.util.DaggerSingletonObject
 import com.android.launcher3.util.DaggerSingletonTracker
 import com.android.launcher3.util.Executors
+import com.android.launcher3.util.WallpaperColorHints
 import com.android.quickstep.DisplayModel
 import com.android.quickstep.FallbackWindowInterface
 import com.android.quickstep.dagger.QuickstepBaseAppComponent
@@ -34,8 +35,11 @@
 @LauncherAppSingleton
 class RecentsDisplayModel
 @Inject
-constructor(@ApplicationContext context: Context, tracker: DaggerSingletonTracker) :
-    DisplayModel<RecentsDisplayResource>(context) {
+constructor(
+    @ApplicationContext context: Context,
+    private val wallpaperColorHints: WallpaperColorHints,
+    tracker: DaggerSingletonTracker,
+) : DisplayModel<RecentsDisplayResource>(context) {
 
     companion object {
         private const val TAG = "RecentsDisplayModel"
@@ -81,7 +85,11 @@
 
     private fun storeRecentsDisplayResource(displayId: Int, display: Display) {
         displayResourceArray[displayId] =
-            RecentsDisplayResource(displayId, context.createDisplayContext(display))
+            RecentsDisplayResource(
+                displayId,
+                context.createDisplayContext(display),
+                wallpaperColorHints.hints,
+            )
     }
 
     fun getRecentsWindowManager(displayId: Int): RecentsWindowManager? {
@@ -92,9 +100,12 @@
         return getDisplayResource(displayId)?.fallbackWindowInterface
     }
 
-    data class RecentsDisplayResource(var displayId: Int, var displayContext: Context) :
-        DisplayResource() {
-        val recentsWindowManager = RecentsWindowManager(displayContext)
+    data class RecentsDisplayResource(
+        var displayId: Int,
+        var displayContext: Context,
+        val wallpaperColorHints: Int,
+    ) : DisplayResource() {
+        val recentsWindowManager = RecentsWindowManager(displayContext, wallpaperColorHints)
         val fallbackWindowInterface: FallbackWindowInterface =
             FallbackWindowInterface(recentsWindowManager)
 
diff --git a/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowContext.kt b/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowContext.kt
index 52a7682..047658c 100644
--- a/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowContext.kt
+++ b/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowContext.kt
@@ -32,11 +32,16 @@
 
 /**
  * Window context for the Overview overlays.
+ *
  * <p>
  * Overlays have their own window and need a window context.
  */
-open class RecentsWindowContext(windowContext: Context) :
-    ContextThemeWrapper(windowContext, Themes.getActivityThemeRes(windowContext)), ActivityContext {
+open class RecentsWindowContext(windowContext: Context, wallpaperColorHints: Int) :
+    ContextThemeWrapper(
+        windowContext,
+        Themes.getActivityThemeRes(windowContext, wallpaperColorHints),
+    ),
+    ActivityContext {
 
     private var deviceProfile: DeviceProfile? = null
     private var dragLayer: RecentsDragLayer<RecentsWindowManager> = RecentsDragLayer(this, null)
@@ -48,7 +53,9 @@
 
     protected var windowLayoutParams: WindowManager.LayoutParams? =
         createDefaultWindowLayoutParams(
-            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, windowTitle)
+            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
+            windowTitle,
+        )
 
     override fun getDragLayer(): BaseDragLayer<RecentsWindowManager> {
         return dragLayer
@@ -56,8 +63,7 @@
 
     override fun getDeviceProfile(): DeviceProfile {
         if (deviceProfile == null) {
-            deviceProfile = InvariantDeviceProfile.INSTANCE[this].getDeviceProfile(this)
-                .copy(this)
+            deviceProfile = InvariantDeviceProfile.INSTANCE[this].getDeviceProfile(this).copy(this)
         }
         return deviceProfile!!
     }
diff --git a/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt b/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt
index 5d99aec..cda6c1b 100644
--- a/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt
+++ b/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt
@@ -88,8 +88,10 @@
  * To add new protologs, see [RecentsWindowProtoLogProxy]. To enable logging to logcat, see
  * [QuickstepProtoLogGroup.Constants.DEBUG_RECENTS_WINDOW]
  */
-class RecentsWindowManager(context: Context) :
-    RecentsWindowContext(context), RecentsViewContainer, StatefulContainer<RecentsState> {
+class RecentsWindowManager(context: Context, wallpaperColorHints: Int) :
+    RecentsWindowContext(context, wallpaperColorHints),
+    RecentsViewContainer,
+    StatefulContainer<RecentsState> {
 
     companion object {
         private const val HOME_APPEAR_DURATION: Long = 250
diff --git a/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java b/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java
index 5883a88..c3e3992 100644
--- a/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java
+++ b/src/com/android/launcher3/dagger/LauncherBaseAppComponent.java
@@ -33,6 +33,7 @@
 import com.android.launcher3.util.ScreenOnTracker;
 import com.android.launcher3.util.SettingsCache;
 import com.android.launcher3.util.VibratorWrapper;
+import com.android.launcher3.util.WallpaperColorHints;
 import com.android.launcher3.util.window.RefreshRateTracker;
 import com.android.launcher3.util.window.WindowManagerProxy;
 import com.android.launcher3.widget.custom.CustomWidgetManager;
@@ -66,6 +67,7 @@
     LauncherPrefs getLauncherPrefs();
     ThemeManager getThemeManager();
     DisplayController getDisplayController();
+    WallpaperColorHints getWallpaperColorHints();
 
     /** Builder for LauncherBaseAppComponent. */
     interface Builder {
diff --git a/src/com/android/launcher3/util/WallpaperColorHints.kt b/src/com/android/launcher3/util/WallpaperColorHints.kt
index 11d4c25..29fe31a 100644
--- a/src/com/android/launcher3/util/WallpaperColorHints.kt
+++ b/src/com/android/launcher3/util/WallpaperColorHints.kt
@@ -23,14 +23,21 @@
 import android.content.Context
 import androidx.annotation.MainThread
 import androidx.annotation.VisibleForTesting
+import com.android.launcher3.dagger.ApplicationContext
+import com.android.launcher3.dagger.LauncherAppComponent
+import com.android.launcher3.dagger.LauncherAppSingleton
 import com.android.launcher3.util.Executors.MAIN_EXECUTOR
 import com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR
+import javax.inject.Inject
 
 /**
  * This class caches the system's wallpaper color hints for use by other classes as a performance
  * enhancer. It also centralizes all the WallpaperManager color hint code in one location.
  */
-class WallpaperColorHints(private val context: Context) : SafeCloseable {
+@LauncherAppSingleton
+class WallpaperColorHints
+@Inject
+constructor(@ApplicationContext private val context: Context, tracker: DaggerSingletonTracker) {
     var hints: Int = 0
         private set
 
@@ -38,7 +45,6 @@
         get() = context.getSystemService(WallpaperManager::class.java)!!
 
     private val onColorHintsChangedListeners = mutableListOf<OnColorHintListener>()
-    private val onClose: SafeCloseable
 
     init {
         hints = wallpaperManager.getWallpaperColors(FLAG_SYSTEM)?.colorHints ?: 0
@@ -51,7 +57,7 @@
                 MAIN_EXECUTOR.handler,
             )
         }
-        onClose = SafeCloseable {
+        tracker.addCloseable {
             UI_HELPER_EXECUTOR.execute {
                 wallpaperManager.removeOnColorsChangedListener(onColorsChangedListener)
             }
@@ -69,8 +75,6 @@
         }
     }
 
-    override fun close() = onClose.close()
-
     fun registerOnColorHintsChangedListener(listener: OnColorHintListener) {
         onColorHintsChangedListeners.add(listener)
     }
@@ -82,7 +86,7 @@
     companion object {
         @VisibleForTesting
         @JvmField
-        val INSTANCE = MainThreadInitializedObject { WallpaperColorHints(it) }
+        val INSTANCE = DaggerSingletonObject(LauncherAppComponent::getWallpaperColorHints)
 
         @JvmStatic fun get(context: Context): WallpaperColorHints = INSTANCE.get(context)
     }