Merge "Clock text adjustment for large text (2/2)" into udc-dev
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 11e51c6..72b9f3a 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -1,6 +1,14 @@
 {
   "presubmit": [
     {
+      "name": "WallpaperPicker2Tests",
+      "options": [
+        {
+          "exclude-annotation": "org.junit.Ignore"
+        }
+      ]
+    },
+    {
       "name": "ThemePickerTests",
       "options": [
         {
diff --git a/src/com/android/customization/module/CustomizationInjector.kt b/src/com/android/customization/module/CustomizationInjector.kt
index b3400bc..8b0d90f 100644
--- a/src/com/android/customization/module/CustomizationInjector.kt
+++ b/src/com/android/customization/module/CustomizationInjector.kt
@@ -18,7 +18,6 @@
 import android.content.Context
 import androidx.activity.ComponentActivity
 import androidx.fragment.app.FragmentActivity
-import androidx.lifecycle.LifecycleOwner
 import com.android.customization.model.theme.OverlayManagerCompat
 import com.android.customization.model.theme.ThemeBundleProvider
 import com.android.customization.model.theme.ThemeManager
@@ -48,13 +47,12 @@
         context: Context,
     ): KeyguardQuickAffordancePickerInteractor
 
-    fun getClockRegistry(context: Context, lifecycleOwner: LifecycleOwner): ClockRegistry
+    fun getClockRegistry(context: Context): ClockRegistry?
 
     fun getClockPickerInteractor(context: Context): ClockPickerInteractor
 
     fun getClockSectionViewModel(
         context: Context,
-        lifecycleOwner: LifecycleOwner
     ): ClockSectionViewModel
 
     fun getColorPickerInteractor(
@@ -77,6 +75,5 @@
         context: Context,
         wallpaperColorsViewModel: WallpaperColorsViewModel,
         clockViewFactory: ClockViewFactory,
-        lifecycleOwner: LifecycleOwner,
     ): ClockSettingsViewModel.Factory
 }
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index 6c747d7..a9d2874 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -196,23 +196,22 @@
 
     override fun getSnapshotRestorers(
         context: Context,
-        lifecycleOwner: LifecycleOwner
     ): Map<Int, SnapshotRestorer> {
-        return super<WallpaperPicker2Injector>.getSnapshotRestorers(context, lifecycleOwner)
-            .toMutableMap()
-            .apply {
-                this[KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER] =
-                    getKeyguardQuickAffordanceSnapshotRestorer(context)
+        return super<WallpaperPicker2Injector>.getSnapshotRestorers(context).toMutableMap().apply {
+            this[KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER] =
+                getKeyguardQuickAffordanceSnapshotRestorer(context)
+            // TODO(b/285047815): Enable after adding wallpaper id for default static wallpaper
+            if (getFlags().isWallpaperRestorerEnabled()) {
                 this[KEY_WALLPAPER_SNAPSHOT_RESTORER] = getWallpaperSnapshotRestorer(context)
-                this[KEY_NOTIFICATIONS_SNAPSHOT_RESTORER] =
-                    getNotificationsSnapshotRestorer(context)
-                this[KEY_DARK_MODE_SNAPSHOT_RESTORER] = getDarkModeSnapshotRestorer(context)
-                this[KEY_THEMED_ICON_SNAPSHOT_RESTORER] = getThemedIconSnapshotRestorer(context)
-                this[KEY_APP_GRID_SNAPSHOT_RESTORER] = getGridSnapshotRestorer(context)
-                this[KEY_COLOR_PICKER_SNAPSHOT_RESTORER] =
-                    getColorPickerSnapshotRestorer(context, getWallpaperColorsViewModel())
-                this[KEY_CLOCKS_SNAPSHOT_RESTORER] = getClockPickerSnapshotRestorer(context)
             }
+            this[KEY_NOTIFICATIONS_SNAPSHOT_RESTORER] = getNotificationsSnapshotRestorer(context)
+            this[KEY_DARK_MODE_SNAPSHOT_RESTORER] = getDarkModeSnapshotRestorer(context)
+            this[KEY_THEMED_ICON_SNAPSHOT_RESTORER] = getThemedIconSnapshotRestorer(context)
+            this[KEY_APP_GRID_SNAPSHOT_RESTORER] = getGridSnapshotRestorer(context)
+            this[KEY_COLOR_PICKER_SNAPSHOT_RESTORER] =
+                getColorPickerSnapshotRestorer(context, getWallpaperColorsViewModel())
+            this[KEY_CLOCKS_SNAPSHOT_RESTORER] = getClockPickerSnapshotRestorer(context)
+        }
     }
 
     override fun getCustomizationPreferences(context: Context): CustomizationPreferences {
@@ -347,7 +346,7 @@
                 .also { notificationsSnapshotRestorer = it }
     }
 
-    override fun getClockRegistry(context: Context, lifecycleOwner: LifecycleOwner): ClockRegistry {
+    override fun getClockRegistry(context: Context): ClockRegistry {
         return (clockRegistryProvider
                 ?: ClockRegistryProvider(
                         context = context.applicationContext,
@@ -356,7 +355,7 @@
                         backgroundDispatcher = Dispatchers.IO,
                     )
                     .also { clockRegistryProvider = it })
-            .getForOwner(lifecycleOwner)
+            .get()
     }
 
     override fun getClockPickerInteractor(
@@ -367,9 +366,8 @@
             ?: ClockPickerInteractor(
                     repository =
                         ClockPickerRepositoryImpl(
-                            secureSettingsRepository = getSecureSettingsRepository(context),
-                            // TODO (b/285978251): remove second argument once b/285348630 is fixed
-                            registry = getClockRegistry(context, context as LifecycleOwner),
+                            secureSettingsRepository = getSecureSettingsRepository(appContext),
+                            registry = getClockRegistry(appContext),
                             scope = getApplicationCoroutineScope(),
                             mainDispatcher = Dispatchers.Main,
                         ),
@@ -380,10 +378,12 @@
 
     override fun getClockSectionViewModel(
         context: Context,
-        lifecycleOwner: LifecycleOwner
     ): ClockSectionViewModel {
         return clockSectionViewModel
-            ?: ClockSectionViewModel(context.applicationContext, getClockPickerInteractor(context))
+            ?: ClockSectionViewModel(
+                    context.applicationContext,
+                    getClockPickerInteractor(context.applicationContext)
+                )
                 .also { clockSectionViewModel = it }
     }
 
@@ -404,10 +404,7 @@
                     ScreenSizeCalculator.getInstance()
                         .getScreenSize(activity.windowManager.defaultDisplay),
                     WallpaperManager.getInstance(activity.applicationContext),
-                    getClockRegistry(
-                        context = activity.applicationContext,
-                        lifecycleOwner = activity,
-                    ),
+                    getClockRegistry(activity.applicationContext),
                 )
                 .also {
                     clockViewFactories[activityHashCode] = it
@@ -521,7 +518,6 @@
         context: Context,
         wallpaperColorsViewModel: WallpaperColorsViewModel,
         clockViewFactory: ClockViewFactory,
-        lifecycleOwner: LifecycleOwner,
     ): ClockSettingsViewModel.Factory {
         return clockSettingsViewModelFactory
             ?: ClockSettingsViewModel.Factory(
diff --git a/src/com/android/customization/picker/clock/data/repository/ClockRegistryProvider.kt b/src/com/android/customization/picker/clock/data/repository/ClockRegistryProvider.kt
index 52c3c4e..652ffdd 100644
--- a/src/com/android/customization/picker/clock/data/repository/ClockRegistryProvider.kt
+++ b/src/com/android/customization/picker/clock/data/repository/ClockRegistryProvider.kt
@@ -19,8 +19,6 @@
 import android.content.ComponentName
 import android.content.Context
 import android.view.LayoutInflater
-import androidx.lifecycle.DefaultLifecycleObserver
-import androidx.lifecycle.LifecycleOwner
 import com.android.systemui.plugins.Plugin
 import com.android.systemui.plugins.PluginManager
 import com.android.systemui.shared.clocks.ClockRegistry
@@ -45,50 +43,28 @@
     private val mainDispatcher: CoroutineDispatcher,
     private val backgroundDispatcher: CoroutineDispatcher,
 ) {
-    private val lifecycleOwners = mutableSetOf<Int>()
-    private val pluginManager: PluginManager by lazy { createPluginManager(context) }
     private val clockRegistry: ClockRegistry by lazy {
         ClockRegistry(
-                context,
-                pluginManager,
-                coroutineScope,
-                mainDispatcher,
-                backgroundDispatcher,
-                isEnabled = true,
-                handleAllUsers = false,
-                DefaultClockProvider(context, LayoutInflater.from(context), context.resources),
-                keepAllLoaded = true,
-                subTag = "Picker",
-            )
-            .apply { registerListeners() }
-    }
-
-    fun getForOwner(lifecycleOwner: LifecycleOwner): ClockRegistry {
-        registerLifecycleOwner(lifecycleOwner)
-        return clockRegistry
-    }
-
-    private fun registerLifecycleOwner(lifecycleOwner: LifecycleOwner) {
-        lifecycleOwners.add(lifecycleOwner.hashCode())
-
-        lifecycleOwner.lifecycle.addObserver(
-            object : DefaultLifecycleObserver {
-                override fun onDestroy(owner: LifecycleOwner) {
-                    super.onDestroy(owner)
-                    unregisterLifecycleOwner(owner)
-                }
-            }
+            context,
+            createPluginManager(context),
+            coroutineScope,
+            mainDispatcher,
+            backgroundDispatcher,
+            isEnabled = true,
+            handleAllUsers = false,
+            DefaultClockProvider(context, LayoutInflater.from(context), context.resources),
+            keepAllLoaded = true,
+            subTag = "Picker",
         )
     }
 
-    private fun unregisterLifecycleOwner(lifecycleOwner: LifecycleOwner) {
-        lifecycleOwners.remove(lifecycleOwner.hashCode())
-
-        if (lifecycleOwners.isEmpty()) {
-            clockRegistry.unregisterListeners()
-        }
+    init {
+        // Listeners in ClockRegistry get cleaned up when app ended
+        clockRegistry.registerListeners()
     }
 
+    fun get() = clockRegistry
+
     private fun createPluginManager(context: Context): PluginManager {
         val privilegedPlugins = listOf<String>()
         val isDebugDevice = true
diff --git a/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt b/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt
index 053ac55..4f4bd1b 100644
--- a/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt
+++ b/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt
@@ -176,23 +176,23 @@
                             ::Pair,
                         )
                         .collect { (clockId, size) ->
-                            val clockView =
-                                if (size == ClockSize.DYNAMIC) {
-                                    clockViewFactory.getLargeView(clockId)
-                                } else {
-                                    clockViewFactory.getSmallView(clockId)
-                                }
-                            (clockView.parent as? ViewGroup)?.removeView(clockView)
                             clockHostView.removeAllViews()
+                            val clockView =
+                                when (size) {
+                                    ClockSize.DYNAMIC -> clockViewFactory.getLargeView(clockId)
+                                    ClockSize.SMALL -> clockViewFactory.getSmallView(clockId)
+                                }
+                            // The clock view might still be attached to an existing parent. Detach
+                            // before adding to another parent.
+                            (clockView.parent as? ViewGroup)?.removeView(clockView)
                             clockHostView.addView(clockView)
-
                             when (size) {
                                 ClockSize.DYNAMIC -> {
                                     sizeOptions.radioButtonDynamic.isChecked = true
                                     sizeOptions.radioButtonSmall.isChecked = false
                                     clockHostView.doOnPreDraw {
-                                        it.pivotX = (it.width / 2).toFloat()
-                                        it.pivotY = (it.height / 2).toFloat()
+                                        it.pivotX = it.width / 2F
+                                        it.pivotY = it.height / 2F
                                     }
                                 }
                                 ClockSize.SMALL -> {
diff --git a/src/com/android/customization/picker/clock/ui/fragment/ClockCustomDemoFragment.kt b/src/com/android/customization/picker/clock/ui/fragment/ClockCustomDemoFragment.kt
index 6203e24..f138d6a 100644
--- a/src/com/android/customization/picker/clock/ui/fragment/ClockCustomDemoFragment.kt
+++ b/src/com/android/customization/picker/clock/ui/fragment/ClockCustomDemoFragment.kt
@@ -37,7 +37,6 @@
         clockRegistry =
             (InjectorProvider.getInjector() as ThemePickerInjector).getClockRegistry(
                 requireContext(),
-                this
             )
         val listInUse = clockRegistry.getClocks().filter { "NOT_IN_USE" !in it.clockId }
 
diff --git a/src/com/android/customization/picker/clock/ui/fragment/ClockSettingsFragment.kt b/src/com/android/customization/picker/clock/ui/fragment/ClockSettingsFragment.kt
index d19b33c..e476d6a 100644
--- a/src/com/android/customization/picker/clock/ui/fragment/ClockSettingsFragment.kt
+++ b/src/com/android/customization/picker/clock/ui/fragment/ClockSettingsFragment.kt
@@ -123,7 +123,6 @@
                         context,
                         injector.getWallpaperColorsViewModel(),
                         injector.getClockViewFactory(activity),
-                        activity,
                     ),
                 )
                 .get(),
diff --git a/src/com/android/customization/picker/clock/ui/view/ClockCarouselView.kt b/src/com/android/customization/picker/clock/ui/view/ClockCarouselView.kt
index c856112..56d4dea 100644
--- a/src/com/android/customization/picker/clock/ui/view/ClockCarouselView.kt
+++ b/src/com/android/customization/picker/clock/ui/view/ClockCarouselView.kt
@@ -48,8 +48,8 @@
     private lateinit var clockViewFactory: ClockViewFactory
     private var toCenterClockController: ClockController? = null
     private var offCenterClockController: ClockController? = null
-    private var toCenterClockView: View? = null
-    private var offCenterClockView: View? = null
+    private var toCenterClockScaleView: View? = null
+    private var offCenterClockScaleView: View? = null
     private var toCenterClockHostView: ClockHostView? = null
     private var offCenterClockHostView: ClockHostView? = null
     private var toCenterCardView: View? = null
@@ -125,8 +125,8 @@
                     val scalingUpClockId = adapter.clockIds[scalingUpIdx]
                     offCenterClockController = clockViewFactory.getController(scalingDownClockId)
                     toCenterClockController = clockViewFactory.getController(scalingUpClockId)
-                    offCenterClockView = motionLayout.findViewById(R.id.clock_scale_view_2)
-                    toCenterClockView =
+                    offCenterClockScaleView = motionLayout.findViewById(R.id.clock_scale_view_2)
+                    toCenterClockScaleView =
                         motionLayout.findViewById(
                             if (endId == R.id.next) R.id.clock_scale_view_3
                             else R.id.clock_scale_view_1
@@ -166,10 +166,10 @@
                         ?.onPickerCarouselSwiping(progress)
                     val scalingDownScale = getScalingDownScale(progress)
                     val scalingUpScale = getScalingUpScale(progress)
-                    offCenterClockView?.scaleX = scalingDownScale
-                    offCenterClockView?.scaleY = scalingDownScale
-                    toCenterClockView?.scaleX = scalingUpScale
-                    toCenterClockView?.scaleY = scalingUpScale
+                    offCenterClockScaleView?.scaleX = scalingDownScale
+                    offCenterClockScaleView?.scaleY = scalingDownScale
+                    toCenterClockScaleView?.scaleX = scalingUpScale
+                    toCenterClockScaleView?.scaleY = scalingUpScale
                 }
 
                 private fun onSmallClockViewTransition(progress: Float) {
@@ -339,6 +339,7 @@
                         isMiddleView,
                         clockScaleView,
                         clockId,
+                        clockHostView,
                     )
                 ClockSize.SMALL ->
                     initializeSmallClockView(
@@ -354,7 +355,12 @@
             isMiddleView: Boolean,
             clockScaleView: View,
             clockId: String,
+            clockHostView: ClockHostView,
         ) {
+            clockHostView.doOnPreDraw {
+                it.pivotX = it.width / 2F
+                it.pivotY = it.height / 2F
+            }
             if (isMiddleView) {
                 clockScaleView.scaleX = 1f
                 clockScaleView.scaleY = 1f
diff --git a/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt b/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt
index 3d490d4..e733866 100644
--- a/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt
+++ b/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt
@@ -53,16 +53,31 @@
             ?: initClockController(clockId).also { clockControllers[clockId] = it }
     }
 
+    /**
+     * Reset the large view to its initial state when getting the view. This is because some view
+     * configs, e.g. animation state, might change during the reuse of the clock view in the app.
+     */
     fun getLargeView(clockId: String): View {
-        return getController(clockId).largeClock.view
+        return getController(clockId).largeClock.let {
+            it.animations.onPickerCarouselSwiping(1F)
+            it.view
+        }
     }
 
+    /**
+     * Reset the small view to its initial state when getting the view. This is because some view
+     * configs, e.g. translation X, might change during the reuse of the clock view in the app.
+     */
     fun getSmallView(clockId: String): View {
-        return smallClockFrames[clockId]
-            ?: createSmallClockFrame().also {
-                it.addView(getController(clockId).smallClock.view)
-                smallClockFrames[clockId] = it
-            }
+        val smallClockFrame =
+            smallClockFrames[clockId]
+                ?: createSmallClockFrame().also {
+                    it.addView(getController(clockId).smallClock.view)
+                    smallClockFrames[clockId] = it
+                }
+        smallClockFrame.translationX = 0F
+        smallClockFrame.translationY = 0F
+        return smallClockFrame
     }
 
     private fun createSmallClockFrame(): FrameLayout {
@@ -104,8 +119,7 @@
 
     private fun isLockscreenWallpaperDark(): Boolean {
         val colors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_LOCK)
-        return (colors?.colorHints?.and(WallpaperColors.HINT_SUPPORTS_DARK_TEXT)) !=
-            WallpaperColors.HINT_SUPPORTS_DARK_TEXT
+        return (colors?.colorHints?.and(WallpaperColors.HINT_SUPPORTS_DARK_TEXT)) == 0
     }
 
     fun updateTimeFormat(clockId: String) {