Log color snapshot (1/2)

Test: Manually tested the log is correct
Bug: 290848448
Flag: None
Change-Id: Ic7ea9c4ea383f733675eca84c8d405794c6e9bfe
diff --git a/src/com/android/customization/model/color/ColorCustomizationManager.java b/src/com/android/customization/model/color/ColorCustomizationManager.java
index 0f87a7b..a09efd2 100644
--- a/src/com/android/customization/model/color/ColorCustomizationManager.java
+++ b/src/com/android/customization/model/color/ColorCustomizationManager.java
@@ -15,6 +15,11 @@
  */
 package com.android.customization.model.color;
 
+import static android.stats.style.StyleEnums.COLOR_SOURCE_HOME_SCREEN_WALLPAPER;
+import static android.stats.style.StyleEnums.COLOR_SOURCE_LOCK_SCREEN_WALLPAPER;
+import static android.stats.style.StyleEnums.COLOR_SOURCE_PRESET_COLOR;
+import static android.stats.style.StyleEnums.COLOR_SOURCE_UNSPECIFIED;
+
 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_COLOR;
 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_SYSTEM_PALETTE;
 import static com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_PRESET;
@@ -27,6 +32,7 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.ContentObserver;
+import android.graphics.Color;
 import android.net.Uri;
 import android.os.Handler;
 import android.os.Looper;
@@ -41,6 +47,7 @@
 import com.android.customization.model.ResourceConstants;
 import com.android.customization.model.color.ColorOptionsProvider.ColorSource;
 import com.android.customization.model.theme.OverlayManagerCompat;
+import com.android.customization.module.logging.ThemesUserEventLogger;
 import com.android.wallpaper.R;
 
 import org.json.JSONArray;
@@ -205,6 +212,38 @@
         return mCurrentOverlays;
     }
 
+    /** */
+    public int getCurrentColorSourceForLogging() {
+        String colorSource = getCurrentColorSource();
+        if (colorSource == null) {
+            return COLOR_SOURCE_UNSPECIFIED;
+        }
+        return switch (colorSource) {
+            case ColorOptionsProvider.COLOR_SOURCE_PRESET -> COLOR_SOURCE_PRESET_COLOR;
+            case ColorOptionsProvider.COLOR_SOURCE_HOME -> COLOR_SOURCE_HOME_SCREEN_WALLPAPER;
+            case ColorOptionsProvider.COLOR_SOURCE_LOCK -> COLOR_SOURCE_LOCK_SCREEN_WALLPAPER;
+            default -> COLOR_SOURCE_UNSPECIFIED;
+        };
+    }
+
+    /** */
+    public int getCurrentStyleForLogging() {
+        String style = getCurrentStyle();
+        return style != null ? style.hashCode() : 0;
+    }
+
+    /** */
+    public int getCurrentSeedColorForLogging() {
+        String seedColor = getCurrentOverlays().get(OVERLAY_CATEGORY_SYSTEM_PALETTE);
+        if (seedColor == null || seedColor.isEmpty()) {
+            return ThemesUserEventLogger.NULL_SEED_COLOR;
+        }
+        if (!seedColor.startsWith("#")) {
+            seedColor = "#" + seedColor;
+        }
+        return Color.parseColor(seedColor);
+    }
+
     /**
      * @return The source of the currently applied color. One of
      * {@link ColorOptionsProvider#COLOR_SOURCE_HOME},{@link ColorOptionsProvider#COLOR_SOURCE_LOCK}
diff --git a/src/com/android/customization/model/color/ColorOption.java b/src/com/android/customization/model/color/ColorOption.java
index 77fe404..f57aa86 100644
--- a/src/com/android/customization/model/color/ColorOption.java
+++ b/src/com/android/customization/model/color/ColorOption.java
@@ -240,6 +240,11 @@
     }
 
     /**
+     * @return the style of this color option for logging
+     */
+    public abstract int getStyleForLogging();
+
+    /**
      * @return the index of this color option
      */
     public int getIndex() {
diff --git a/src/com/android/customization/model/color/ColorOptionImpl.kt b/src/com/android/customization/model/color/ColorOptionImpl.kt
index 461d2a3..f090528 100644
--- a/src/com/android/customization/model/color/ColorOptionImpl.kt
+++ b/src/com/android/customization/model/color/ColorOptionImpl.kt
@@ -78,6 +78,8 @@
         }
     }
 
+    override fun getStyleForLogging(): Int = style.toString().hashCode()
+
     class Builder {
         var title: String? = null
 
diff --git a/src/com/android/customization/model/color/ColorProvider.kt b/src/com/android/customization/model/color/ColorProvider.kt
index 339e558..6fdfd2c 100644
--- a/src/com/android/customization/model/color/ColorProvider.kt
+++ b/src/com/android/customization/model/color/ColorProvider.kt
@@ -48,6 +48,7 @@
 
 /**
  * Default implementation of {@link ColorOptionsProvider} that reads preset colors from a stub APK.
+ * TODO (b/311212666): Make [ColorProvider] and [ColorCustomizationManager] injectable
  */
 class ColorProvider(private val context: Context, stubPackageName: String) :
     ResourcesApkProvider(context, stubPackageName), ColorOptionsProvider {
@@ -68,8 +69,6 @@
             arrayOf(Style.TONAL_SPOT, Style.SPRITZ, Style.VIBRANT, Style.EXPRESSIVE)
         else arrayOf(Style.TONAL_SPOT)
 
-    private val monochromeEnabled =
-        InjectorProvider.getInjector().getFlags().isMonochromaticThemeEnabled(mContext)
     private var monochromeBundleName: String? = null
 
     private val scope =
@@ -177,7 +176,7 @@
 
         // Insert monochrome in the second position if it is enabled and included in preset
         // colors
-        if (monochromeEnabled) {
+        if (InjectorProvider.getInjector().getFlags().isMonochromaticThemeEnabled(mContext)) {
             monochromeBundleName?.let {
                 bundles.add(1, buildPreset(it, -1, Style.MONOCHROMATIC, ColorType.WALLPAPER_COLOR))
             }
@@ -361,7 +360,11 @@
                         }
 
                     if (style == Style.MONOCHROMATIC) {
-                        if (!monochromeEnabled) {
+                        if (
+                            !InjectorProvider.getInjector()
+                                .getFlags()
+                                .isMonochromaticThemeEnabled(mContext)
+                        ) {
                             continue
                         }
                         hasMonochrome = true
diff --git a/src/com/android/customization/module/logging/ThemesUserEventLogger.kt b/src/com/android/customization/module/logging/ThemesUserEventLogger.kt
index 66dea5a..60fd062 100644
--- a/src/com/android/customization/module/logging/ThemesUserEventLogger.kt
+++ b/src/com/android/customization/module/logging/ThemesUserEventLogger.kt
@@ -23,7 +23,7 @@
 /** Extension of [UserEventLogger] that adds ThemePicker specific events. */
 interface ThemesUserEventLogger : UserEventLogger {
 
-    fun logThemeColorApplied(@ColorSource source: Int, variant: Int, seedColor: Int)
+    fun logThemeColorApplied(@ColorSource source: Int, style: Int, seedColor: Int)
 
     fun logGridApplied(grid: GridOption)
 
diff --git a/src/com/android/customization/module/logging/ThemesUserEventLoggerImpl.kt b/src/com/android/customization/module/logging/ThemesUserEventLoggerImpl.kt
index 1cbda98..1441c71 100644
--- a/src/com/android/customization/module/logging/ThemesUserEventLoggerImpl.kt
+++ b/src/com/android/customization/module/logging/ThemesUserEventLoggerImpl.kt
@@ -47,6 +47,7 @@
 import android.stats.style.StyleEnums.WALLPAPER_EFFECT_PROBE
 import android.stats.style.StyleEnums.WALLPAPER_EXPLORE
 import android.text.TextUtils
+import com.android.customization.model.color.ColorCustomizationManager
 import com.android.customization.model.grid.GridOption
 import com.android.customization.module.logging.ThemesUserEventLogger.ClockSize
 import com.android.customization.module.logging.ThemesUserEventLogger.ColorSource
@@ -64,6 +65,7 @@
 @Inject
 constructor(
     private val preferences: WallpaperPreferences,
+    private val colorManager: ColorCustomizationManager,
     private val appSessionId: AppSessionId,
 ) : ThemesUserEventLogger {
 
@@ -74,6 +76,9 @@
             .setLockWallpaperCategoryHash(preferences.getLockCategoryHash())
             .setLockWallpaperIdHash(preferences.getLockWallpaperIdHash())
             .setEffectIdHash(preferences.getHomeWallpaperEffectsIdHash())
+            .setColorSource(colorManager.currentColorSourceForLogging)
+            .setColorVariant(colorManager.currentStyleForLogging)
+            .setSeedColor(colorManager.currentSeedColorForLogging)
             .log()
     }
 
@@ -157,13 +162,13 @@
 
     override fun logThemeColorApplied(
         @ColorSource source: Int,
-        variant: Int,
+        style: Int,
         seedColor: Int,
     ) {
         SysUiStatsLogger(THEME_COLOR_APPLIED)
             .setAppSessionId(appSessionId.getId())
             .setColorSource(source)
-            .setColorVariant(variant)
+            .setColorVariant(style)
             .setSeedColor(seedColor)
             .log()
     }
diff --git a/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt b/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt
index 3c3d114..ed83136 100644
--- a/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt
+++ b/src/com/android/customization/picker/color/ui/viewmodel/ColorPickerViewModel.kt
@@ -147,8 +147,8 @@
                                                         logger.logThemeColorApplied(
                                                             colorOptionModel.colorOption
                                                                 .sourceForLogging,
-                                                            colorOptionModel.colorOption.style
-                                                                .ordinal + 1,
+                                                            colorOptionModel.colorOption
+                                                                .styleForLogging,
                                                             colorOptionModel.colorOption
                                                                 .seedColorForLogging,
                                                         )
diff --git a/src_override/com/android/wallpaper/module/AppModule.kt b/src_override/com/android/wallpaper/module/AppModule.kt
index f07c2b4..16e5a88 100644
--- a/src_override/com/android/wallpaper/module/AppModule.kt
+++ b/src_override/com/android/wallpaper/module/AppModule.kt
@@ -16,6 +16,8 @@
 package com.android.wallpaper.module
 
 import android.content.Context
+import com.android.customization.model.color.ColorCustomizationManager
+import com.android.customization.model.theme.OverlayManagerCompat
 import com.android.customization.module.CustomizationInjector
 import com.android.customization.module.DefaultCustomizationPreferences
 import com.android.customization.module.ThemePickerInjector
@@ -58,5 +60,13 @@
         fun provideDefaultWallpaperModelFactory(): DefaultWallpaperModelFactory {
             return DefaultWallpaperModelFactory()
         }
+
+        @Provides
+        @Singleton
+        fun provideColorCustomizationManager(
+            @ApplicationContext context: Context
+        ): ColorCustomizationManager {
+            return ColorCustomizationManager.getInstance(context, OverlayManagerCompat(context))
+        }
     }
 }
diff --git a/tests/common/src/com/android/customization/module/logging/TestThemesUserEventLogger.kt b/tests/common/src/com/android/customization/module/logging/TestThemesUserEventLogger.kt
index bb49ff0..8e9dacd 100644
--- a/tests/common/src/com/android/customization/module/logging/TestThemesUserEventLogger.kt
+++ b/tests/common/src/com/android/customization/module/logging/TestThemesUserEventLogger.kt
@@ -31,14 +31,14 @@
     @ColorSource
     var themeColorSource: Int = StyleEnums.COLOR_SOURCE_UNSPECIFIED
         private set
-    var themeColorVariant: Int = -1
+    var themeColorStyle: Int = -1
         private set
     var themeSeedColor: Int = -1
         private set
 
-    override fun logThemeColorApplied(@ColorSource source: Int, variant: Int, seedColor: Int) {
+    override fun logThemeColorApplied(@ColorSource source: Int, style: Int, seedColor: Int) {
         this.themeColorSource = source
-        this.themeColorVariant = variant
+        this.themeColorStyle = style
         this.themeSeedColor = seedColor
     }
 
diff --git a/tests/robotests/src/com/android/customization/model/picker/color/ui/viewmodel/ColorPickerViewModelTest.kt b/tests/robotests/src/com/android/customization/model/picker/color/ui/viewmodel/ColorPickerViewModelTest.kt
index 6617582..889720e 100644
--- a/tests/robotests/src/com/android/customization/model/picker/color/ui/viewmodel/ColorPickerViewModelTest.kt
+++ b/tests/robotests/src/com/android/customization/model/picker/color/ui/viewmodel/ColorPickerViewModelTest.kt
@@ -149,7 +149,7 @@
 
             assertThat(logger.themeColorSource)
                 .isEqualTo(StyleEnums.COLOR_SOURCE_LOCK_SCREEN_WALLPAPER)
-            assertThat(logger.themeColorVariant).isEqualTo(Style.EXPRESSIVE.ordinal + 1)
+            assertThat(logger.themeColorStyle).isEqualTo(Style.EXPRESSIVE.toString().hashCode())
             assertThat(logger.themeSeedColor).isEqualTo(Color.parseColor("#121212"))
         }
 
@@ -179,7 +179,7 @@
             advanceUntilIdle()
 
             assertThat(logger.themeColorSource).isEqualTo(StyleEnums.COLOR_SOURCE_PRESET_COLOR)
-            assertThat(logger.themeColorVariant).isEqualTo(Style.FRUIT_SALAD.ordinal + 1)
+            assertThat(logger.themeColorStyle).isEqualTo(Style.FRUIT_SALAD.toString().hashCode())
             assertThat(logger.themeSeedColor).isEqualTo(Color.parseColor("#ABCDEF"))
         }