Fix color in the preview fragment (2/2)

Colors were inconsistent between the preview fragment and the actual
wallpaper colors set, because the style was not accounted for.
Adjust to account for style when changing the colors of the preview
fragment UI. Style-related changes are included in ThemePicker while
PreviewFragment changes are in WallpaperPicker2.

Flag: NONE
Bug: 308947090
Bug: 308946897
Test: manually verified with different wallpapers
Change-Id: I1897ae1e18113fdbfa573676111ccf3b1daee82f
diff --git a/src/com/android/customization/model/color/ThemedWallpaperColorResources.kt b/src/com/android/customization/model/color/ThemedWallpaperColorResources.kt
new file mode 100644
index 0000000..906d902
--- /dev/null
+++ b/src/com/android/customization/model/color/ThemedWallpaperColorResources.kt
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.customization.model.color
+
+import android.R
+import android.app.WallpaperColors
+import android.content.Context
+import android.provider.Settings
+import android.util.Log
+import com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_THEME_STYLE
+import com.android.systemui.monet.ColorScheme
+import com.android.systemui.monet.Style
+import org.json.JSONException
+import org.json.JSONObject
+
+class ThemedWallpaperColorResources(wallpaperColors: WallpaperColors, context: Context) :
+    WallpaperColorResources(wallpaperColors) {
+
+    init {
+        val wallpaperColorScheme =
+            ColorScheme(
+                wallpaperColors = wallpaperColors,
+                darkTheme = false,
+                style = fetchThemeStyleFromSetting(context)
+            )
+        addOverlayColor(wallpaperColorScheme.neutral1, R.color.system_neutral1_10)
+        addOverlayColor(wallpaperColorScheme.neutral2, R.color.system_neutral2_10)
+        addOverlayColor(wallpaperColorScheme.accent1, R.color.system_accent1_10)
+        addOverlayColor(wallpaperColorScheme.accent2, R.color.system_accent2_10)
+        addOverlayColor(wallpaperColorScheme.accent3, R.color.system_accent3_10)
+    }
+
+    private fun fetchThemeStyleFromSetting(context: Context): Style {
+        val overlayPackageJson =
+            Settings.Secure.getString(
+                context.contentResolver,
+                Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES,
+            )
+        return if (!overlayPackageJson.isNullOrEmpty()) {
+            try {
+                val jsonObject = JSONObject(overlayPackageJson)
+                Style.valueOf(jsonObject.getString(OVERLAY_CATEGORY_THEME_STYLE))
+            } catch (e: (JSONException)) {
+                Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e)
+                Style.TONAL_SPOT
+            } catch (e: IllegalArgumentException) {
+                Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e)
+                Style.TONAL_SPOT
+            }
+        } else {
+            Style.TONAL_SPOT
+        }
+    }
+
+    companion object {
+        private const val TAG = "ThemedWallpaperColorResources"
+    }
+}
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index e01fa44..0ae9600 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -17,6 +17,7 @@
 
 import android.app.Activity
 import android.app.UiModeManager
+import android.app.WallpaperColors
 import android.app.WallpaperManager
 import android.content.Context
 import android.content.Intent
@@ -29,6 +30,8 @@
 import androidx.lifecycle.ViewModelProvider
 import com.android.customization.model.color.ColorCustomizationManager
 import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_PRESET
+import com.android.customization.model.color.ThemedWallpaperColorResources
+import com.android.customization.model.color.WallpaperColorResources
 import com.android.customization.model.grid.GridOptionsManager
 import com.android.customization.model.mode.DarkModeSnapshotRestorer
 import com.android.customization.model.theme.OverlayManagerCompat
@@ -399,6 +402,13 @@
             }
     }
 
+    override fun getWallpaperColorResources(
+        wallpaperColors: WallpaperColors,
+        context: Context
+    ): WallpaperColorResources {
+        return ThemedWallpaperColorResources(wallpaperColors, context)
+    }
+
     override fun getColorPickerInteractor(
         context: Context,
         wallpaperColorsRepository: WallpaperColorsRepository,
diff --git a/tests/common/src/com/android/customization/testing/TestCustomizationInjector.kt b/tests/common/src/com/android/customization/testing/TestCustomizationInjector.kt
index 22a8336..b0ffcb6 100644
--- a/tests/common/src/com/android/customization/testing/TestCustomizationInjector.kt
+++ b/tests/common/src/com/android/customization/testing/TestCustomizationInjector.kt
@@ -1,8 +1,11 @@
 package com.android.customization.testing
 
+import android.app.WallpaperColors
 import android.content.Context
 import android.content.res.Resources
 import androidx.activity.ComponentActivity
+import com.android.customization.model.color.ThemedWallpaperColorResources
+import com.android.customization.model.color.WallpaperColorResources
 import com.android.customization.module.CustomizationInjector
 import com.android.customization.module.CustomizationPreferences
 import com.android.customization.module.logging.ThemesUserEventLogger
@@ -49,6 +52,13 @@
         throw UnsupportedOperationException("not implemented")
     }
 
+    override fun getWallpaperColorResources(
+        wallpaperColors: WallpaperColors,
+        context: Context
+    ): WallpaperColorResources {
+        return ThemedWallpaperColorResources(wallpaperColors, context)
+    }
+
     override fun getColorPickerInteractor(
         context: Context,
         wallpaperColorsRepository: WallpaperColorsRepository,