Carousel clock color updates

Clock color needs to conform with the lock screen

Test: See bug. Manually tested the color conforms with the LS
Bug: 277832214
Change-Id: I3673f8e1175fbc20b108d5f39b7a0f0c32b98b6e
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index 66814c5..6ab2a53 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -16,6 +16,7 @@
 package com.android.customization.module
 
 import android.app.UiModeManager
+import android.app.WallpaperManager
 import android.content.Context
 import android.content.Intent
 import android.net.Uri
@@ -380,7 +381,11 @@
                     activity.applicationContext,
                     ScreenSizeCalculator.getInstance()
                         .getScreenSize(activity.windowManager.defaultDisplay),
-                    getClockRegistry(activity.applicationContext, activity),
+                    WallpaperManager.getInstance(activity.applicationContext),
+                    getClockRegistry(
+                        context = activity.applicationContext,
+                        lifecycleOwner = activity,
+                    ),
                 )
                 .also {
                     clockViewFactories[activityHashCode] = it
diff --git a/src/com/android/customization/picker/clock/ui/binder/ClockCarouselViewBinder.kt b/src/com/android/customization/picker/clock/ui/binder/ClockCarouselViewBinder.kt
index 98c84f2..0c1617b 100644
--- a/src/com/android/customization/picker/clock/ui/binder/ClockCarouselViewBinder.kt
+++ b/src/com/android/customization/picker/clock/ui/binder/ClockCarouselViewBinder.kt
@@ -42,6 +42,7 @@
         hideSmartspace: (Boolean) -> Unit,
     ) {
         carouselView.setClockViewFactory(clockViewFactory)
+        clockViewFactory.updateRegionDarkness()
         val singleClockHostView =
             singleClockView.requireViewById<FrameLayout>(R.id.single_clock_host_view)
         lifecycleOwner.lifecycleScope.launch {
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 1aa6206..3d490d4 100644
--- a/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt
+++ b/src/com/android/customization/picker/clock/ui/view/ClockViewFactory.kt
@@ -15,11 +15,12 @@
  */
 package com.android.customization.picker.clock.ui.view
 
+import android.app.WallpaperColors
+import android.app.WallpaperManager
 import android.content.Context
 import android.content.res.Resources
 import android.graphics.Point
 import android.graphics.Rect
-import android.util.TypedValue
 import android.view.View
 import android.widget.FrameLayout
 import androidx.annotation.ColorInt
@@ -39,6 +40,7 @@
 class ClockViewFactory(
     private val appContext: Context,
     val screenSize: Point,
+    private val wallpaperManager: WallpaperManager,
     private val registry: ClockRegistry,
 ) {
     private val resources = appContext.resources
@@ -89,9 +91,21 @@
     }
 
     fun updateColor(clockId: String, @ColorInt seedColor: Int?) {
-        return (clockControllers[clockId] ?: initClockController(clockId))
-            .events
-            .onSeedColorChanged(seedColor)
+        clockControllers[clockId]?.events?.onSeedColorChanged(seedColor)
+    }
+
+    fun updateRegionDarkness() {
+        val isRegionDark = isLockscreenWallpaperDark()
+        clockControllers.values.forEach {
+            it.largeClock.events.onRegionDarknessChanged(isRegionDark)
+            it.smallClock.events.onRegionDarknessChanged(isRegionDark)
+        }
+    }
+
+    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
     }
 
     fun updateTimeFormat(clockId: String) {
@@ -136,18 +150,16 @@
             registry.createExampleClock(clockId).also { it?.initialize(resources, 0f, 0f) }
         checkNotNull(controller)
 
-        // Configure light/dark theme
-        val isLightTheme = TypedValue()
-        appContext.theme.resolveAttribute(android.R.attr.isLightTheme, isLightTheme, true)
-        val isRegionDark = isLightTheme.data == 0
-        controller.largeClock.events.onRegionDarknessChanged(isRegionDark)
-        // Configure font size
+        val isWallpaperDark = isLockscreenWallpaperDark()
+        // Initialize large clock
+        controller.largeClock.events.onRegionDarknessChanged(isWallpaperDark)
         controller.largeClock.events.onFontSettingChanged(
             resources.getDimensionPixelSize(R.dimen.large_clock_text_size).toFloat()
         )
         controller.largeClock.events.onTargetRegionChanged(getLargeClockRegion())
 
-        controller.smallClock.events.onRegionDarknessChanged(isRegionDark)
+        // Initialize small clock
+        controller.smallClock.events.onRegionDarknessChanged(isWallpaperDark)
         controller.smallClock.events.onFontSettingChanged(
             resources.getDimensionPixelSize(R.dimen.small_clock_text_size).toFloat()
         )