Fix Activity leaks
* Workspace and WallpaperSurfaceCallback's cleanUp was not being called,
leaking listeners and preventing surfaces to be properly disposed of
* Moved to use ApplicationContext in Injector where possible
* Fixed lambdas in Injector that were keeping a reference to the Context
Test: ran profiler, switched wallpapers via quick-switch, colors, manually
set live wallpapers, clocks, and changed affordances
Bug: 285978251
Change-Id: I8396761c09596c7da0603fb97596336dbd36e929
diff --git a/src/com/android/customization/module/CustomizationInjector.kt b/src/com/android/customization/module/CustomizationInjector.kt
index b943925..b3400bc 100644
--- a/src/com/android/customization/module/CustomizationInjector.kt
+++ b/src/com/android/customization/module/CustomizationInjector.kt
@@ -50,10 +50,7 @@
fun getClockRegistry(context: Context, lifecycleOwner: LifecycleOwner): ClockRegistry
- fun getClockPickerInteractor(
- context: Context,
- lifecycleOwner: LifecycleOwner
- ): ClockPickerInteractor
+ fun getClockPickerInteractor(context: Context): ClockPickerInteractor
fun getClockSectionViewModel(
context: Context,
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index b23d912..6c747d7 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -134,7 +134,7 @@
),
getFlags(),
getClockCarouselViewModelFactory(
- getClockPickerInteractor(activity.applicationContext, activity),
+ getClockPickerInteractor(activity.applicationContext),
),
getClockViewFactory(activity),
getDarkModeSnapshotRestorer(activity),
@@ -181,7 +181,7 @@
@Synchronized
override fun getUserEventLogger(context: Context): ThemesUserEventLogger {
return if (userEventLogger != null) userEventLogger as ThemesUserEventLogger
- else StatsLogUserEventLogger(context).also { userEventLogger = it }
+ else StatsLogUserEventLogger(context.applicationContext).also { userEventLogger = it }
}
@Synchronized
@@ -211,8 +211,7 @@
this[KEY_APP_GRID_SNAPSHOT_RESTORER] = getGridSnapshotRestorer(context)
this[KEY_COLOR_PICKER_SNAPSHOT_RESTORER] =
getColorPickerSnapshotRestorer(context, getWallpaperColorsViewModel())
- this[KEY_CLOCKS_SNAPSHOT_RESTORER] =
- getClockPickerSnapshotRestorer(context, lifecycleOwner)
+ this[KEY_CLOCKS_SNAPSHOT_RESTORER] = getClockPickerSnapshotRestorer(context)
}
}
@@ -230,6 +229,7 @@
}
override fun getWallpaperInteractor(context: Context): WallpaperInteractor {
+ val appContext = context.applicationContext
return wallpaperInteractor
?: WallpaperInteractor(
repository =
@@ -237,16 +237,16 @@
scope = getApplicationCoroutineScope(),
client =
WallpaperClientImpl(
- context = context,
+ context = appContext,
infoFactory = getCurrentWallpaperInfoFactory(context),
wallpaperManager = WallpaperManager.getInstance(context)
),
- wallpaperPreferences = getPreferences(context = context),
+ wallpaperPreferences = getPreferences(context = appContext),
backgroundDispatcher = Dispatchers.IO,
),
shouldHandleReload = {
TextUtils.equals(
- getColorCustomizationManager(context).currentColorSource,
+ getColorCustomizationManager(appContext).currentColorSource,
ColorOptionsProvider.COLOR_SOURCE_PRESET
)
}
@@ -268,7 +268,7 @@
): KeyguardQuickAffordancePickerViewModel.Factory {
return keyguardQuickAffordancePickerViewModelFactory
?: KeyguardQuickAffordancePickerViewModel.Factory(
- context,
+ context.applicationContext,
getKeyguardQuickAffordancePickerInteractor(context),
getWallpaperInteractor(context),
getCurrentWallpaperInfoFactory(context),
@@ -280,11 +280,12 @@
context: Context
): KeyguardQuickAffordancePickerInteractor {
val client = getKeyguardQuickAffordancePickerProviderClient(context)
+ val appContext = context.applicationContext
return KeyguardQuickAffordancePickerInteractor(
KeyguardQuickAffordancePickerRepository(client, Dispatchers.IO),
client
) {
- getKeyguardQuickAffordanceSnapshotRestorer(context)
+ getKeyguardQuickAffordanceSnapshotRestorer(appContext)
}
}
@@ -292,7 +293,7 @@
context: Context
): CustomizationProviderClient {
return customizationProviderClient
- ?: CustomizationProviderClientImpl(context, Dispatchers.IO).also {
+ ?: CustomizationProviderClientImpl(context.applicationContext, Dispatchers.IO).also {
customizationProviderClient = it
}
}
@@ -321,6 +322,7 @@
private fun getNotificationsInteractor(
context: Context,
): NotificationsInteractor {
+ val appContext = context.applicationContext
return notificationsInteractor
?: NotificationsInteractor(
repository =
@@ -329,7 +331,7 @@
backgroundDispatcher = Dispatchers.IO,
secureSettingsRepository = getSecureSettingsRepository(context),
),
- snapshotRestorer = { getNotificationsSnapshotRestorer(context) },
+ snapshotRestorer = { getNotificationsSnapshotRestorer(appContext) },
)
.also { notificationsInteractor = it }
}
@@ -348,7 +350,7 @@
override fun getClockRegistry(context: Context, lifecycleOwner: LifecycleOwner): ClockRegistry {
return (clockRegistryProvider
?: ClockRegistryProvider(
- context = context,
+ context = context.applicationContext,
coroutineScope = getApplicationCoroutineScope(),
mainDispatcher = Dispatchers.Main,
backgroundDispatcher = Dispatchers.IO,
@@ -359,18 +361,19 @@
override fun getClockPickerInteractor(
context: Context,
- lifecycleOwner: LifecycleOwner,
): ClockPickerInteractor {
+ val appContext = context.applicationContext
return clockPickerInteractor
?: ClockPickerInteractor(
repository =
ClockPickerRepositoryImpl(
secureSettingsRepository = getSecureSettingsRepository(context),
- registry = getClockRegistry(context, lifecycleOwner),
+ // TODO (b/285978251): remove second argument once b/285348630 is fixed
+ registry = getClockRegistry(context, context as LifecycleOwner),
scope = getApplicationCoroutineScope(),
mainDispatcher = Dispatchers.Main,
),
- snapshotRestorer = { getClockPickerSnapshotRestorer(context, lifecycleOwner) },
+ snapshotRestorer = { getClockPickerSnapshotRestorer(appContext) },
)
.also { clockPickerInteractor = it }
}
@@ -380,7 +383,7 @@
lifecycleOwner: LifecycleOwner
): ClockSectionViewModel {
return clockSectionViewModel
- ?: ClockSectionViewModel(context, getClockPickerInteractor(context, lifecycleOwner))
+ ?: ClockSectionViewModel(context.applicationContext, getClockPickerInteractor(context))
.also { clockSectionViewModel = it }
}
@@ -422,10 +425,9 @@
private fun getClockPickerSnapshotRestorer(
context: Context,
- lifecycleOwner: LifecycleOwner
): ClockPickerSnapshotRestorer {
return clockPickerSnapshotRestorer
- ?: ClockPickerSnapshotRestorer(getClockPickerInteractor(context, lifecycleOwner)).also {
+ ?: ClockPickerSnapshotRestorer(getClockPickerInteractor(context)).also {
clockPickerSnapshotRestorer = it
}
}
@@ -434,15 +436,16 @@
context: Context,
wallpaperColorsViewModel: WallpaperColorsViewModel,
): ColorPickerInteractor {
+ val appContext = context.applicationContext
return colorPickerInteractor
?: ColorPickerInteractor(
repository =
ColorPickerRepositoryImpl(
wallpaperColorsViewModel,
- getColorCustomizationManager(context)
+ getColorCustomizationManager(appContext)
),
snapshotRestorer = {
- getColorPickerSnapshotRestorer(context, wallpaperColorsViewModel)
+ getColorPickerSnapshotRestorer(appContext, wallpaperColorsViewModel)
}
)
.also { colorPickerInteractor = it }
@@ -454,7 +457,7 @@
): ColorPickerViewModel.Factory {
return colorPickerViewModelFactory
?: ColorPickerViewModel.Factory(
- context,
+ context.applicationContext,
getColorPickerInteractor(context, wallpaperColorsViewModel),
)
.also { colorPickerViewModelFactory = it }
@@ -481,10 +484,11 @@
fun getDarkModeSnapshotRestorer(
context: Context,
): DarkModeSnapshotRestorer {
+ val appContext = context.applicationContext
return darkModeSnapshotRestorer
?: DarkModeSnapshotRestorer(
- context = context,
- manager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager,
+ context = appContext,
+ manager = appContext.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager,
backgroundDispatcher = Dispatchers.IO,
)
.also { darkModeSnapshotRestorer = it }
@@ -521,8 +525,8 @@
): ClockSettingsViewModel.Factory {
return clockSettingsViewModelFactory
?: ClockSettingsViewModel.Factory(
- context,
- getClockPickerInteractor(context, lifecycleOwner),
+ context.applicationContext,
+ getClockPickerInteractor(context),
getColorPickerInteractor(
context,
wallpaperColorsViewModel,
@@ -548,6 +552,7 @@
private fun getGridInteractor(
context: Context,
): GridInteractor {
+ val appContext = context.applicationContext
return gridInteractor
?: GridInteractor(
applicationScope = getApplicationCoroutineScope(),
@@ -557,7 +562,7 @@
manager = GridOptionsManager.getInstance(context),
backgroundDispatcher = Dispatchers.IO,
),
- snapshotRestorer = { getGridSnapshotRestorer(context) },
+ snapshotRestorer = { getGridSnapshotRestorer(appContext) },
)
.also { gridInteractor = it }
}
diff --git a/src/com/android/customization/picker/clock/ui/viewmodel/ClockSectionViewModel.kt b/src/com/android/customization/picker/clock/ui/viewmodel/ClockSectionViewModel.kt
index 008a125..8a65522 100644
--- a/src/com/android/customization/picker/clock/ui/viewmodel/ClockSectionViewModel.kt
+++ b/src/com/android/customization/picker/clock/ui/viewmodel/ClockSectionViewModel.kt
@@ -35,7 +35,7 @@
(selectedColorId, selectedClockSize) ->
val colorText =
clockColorMap[selectedColorId]?.colorName
- ?: context.getString(R.string.default_theme_title)
+ ?: appContext.getString(R.string.default_theme_title)
val sizeText =
when (selectedClockSize) {
ClockSize.SMALL -> appContext.getString(R.string.clock_size_small)