From f8fa42be843777c72026555c645455ef1879e310 Mon Sep 17 00:00:00 2001 From: Schneider Victor-Tulias Date: Tue, 14 Jan 2025 10:09:20 -0500 Subject: [PATCH] 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 --- .../fallback/window/RecentsDisplayModel.kt | 23 ++++++++++++++----- .../fallback/window/RecentsWindowContext.kt | 16 +++++++++---- .../fallback/window/RecentsWindowManager.kt | 6 +++-- .../dagger/LauncherBaseAppComponent.java | 2 ++ .../launcher3/util/WallpaperColorHints.kt | 16 ++++++++----- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt b/quickstep/src/com/android/quickstep/fallback/window/RecentsDisplayModel.kt index e7e9f51d32..31a1be8680 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.dagger.LauncherAppSingleton 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 @@ import javax.inject.Inject @LauncherAppSingleton class RecentsDisplayModel @Inject -constructor(@ApplicationContext context: Context, tracker: DaggerSingletonTracker) : - DisplayModel(context) { +constructor( + @ApplicationContext context: Context, + private val wallpaperColorHints: WallpaperColorHints, + tracker: DaggerSingletonTracker, +) : DisplayModel(context) { companion object { private const val TAG = "RecentsDisplayModel" @@ -81,7 +85,11 @@ constructor(@ApplicationContext context: Context, tracker: DaggerSingletonTracke 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 @@ constructor(@ApplicationContext context: Context, tracker: DaggerSingletonTracke 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 52a7682a62..047658c419 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 @@ import com.android.quickstep.fallback.RecentsDragLayer /** * Window context for the Overview overlays. + * *

* 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 = RecentsDragLayer(this, null) @@ -48,7 +53,9 @@ open class RecentsWindowContext(windowContext: Context) : protected var windowLayoutParams: WindowManager.LayoutParams? = createDefaultWindowLayoutParams( - WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, windowTitle) + WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, + windowTitle, + ) override fun getDragLayer(): BaseDragLayer { return dragLayer @@ -56,8 +63,7 @@ open class RecentsWindowContext(windowContext: Context) : 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 5d99aec543..cda6c1b360 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 @@ import java.util.function.Predicate * 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 { +class RecentsWindowManager(context: Context, wallpaperColorHints: Int) : + RecentsWindowContext(context, wallpaperColorHints), + RecentsViewContainer, + StatefulContainer { 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 5883a88a51..c3e3992cd0 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.PluginManagerWrapper; 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 @@ public interface LauncherBaseAppComponent { 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 11d4c25689..29fe31ae3d 100644 --- a/src/com/android/launcher3/util/WallpaperColorHints.kt +++ b/src/com/android/launcher3/util/WallpaperColorHints.kt @@ -23,14 +23,21 @@ import android.app.WallpaperManager.OnColorsChangedListener 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 @@ class WallpaperColorHints(private val context: Context) : SafeCloseable { get() = context.getSystemService(WallpaperManager::class.java)!! private val onColorHintsChangedListeners = mutableListOf() - private val onClose: SafeCloseable init { hints = wallpaperManager.getWallpaperColors(FLAG_SYSTEM)?.colorHints ?: 0 @@ -51,7 +57,7 @@ class WallpaperColorHints(private val context: Context) : SafeCloseable { MAIN_EXECUTOR.handler, ) } - onClose = SafeCloseable { + tracker.addCloseable { UI_HELPER_EXECUTOR.execute { wallpaperManager.removeOnColorsChangedListener(onColorsChangedListener) } @@ -69,8 +75,6 @@ class WallpaperColorHints(private val context: Context) : SafeCloseable { } } - override fun close() = onClose.close() - fun registerOnColorHintsChangedListener(listener: OnColorHintListener) { onColorHintsChangedListeners.add(listener) } @@ -82,7 +86,7 @@ class WallpaperColorHints(private val context: Context) : SafeCloseable { companion object { @VisibleForTesting @JvmField - val INSTANCE = MainThreadInitializedObject { WallpaperColorHints(it) } + val INSTANCE = DaggerSingletonObject(LauncherAppComponent::getWallpaperColorHints) @JvmStatic fun get(context: Context): WallpaperColorHints = INSTANCE.get(context) }