1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.customization.model.color 18 19 import android.R 20 import android.app.WallpaperColors 21 import android.content.Context 22 import android.provider.Settings 23 import android.util.Log 24 import com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_THEME_STYLE 25 import com.android.systemui.monet.ColorScheme 26 import com.android.systemui.monet.Style 27 import com.android.systemui.shared.settings.data.repository.SecureSettingsRepository 28 import kotlinx.coroutines.Dispatchers 29 import kotlinx.coroutines.withContext 30 import org.json.JSONException 31 import org.json.JSONObject 32 33 class ThemedWallpaperColorResources( 34 private val wallpaperColors: WallpaperColors, 35 private val secureSettingsRepository: SecureSettingsRepository, 36 ) : WallpaperColorResources() { 37 applynull38 override suspend fun apply(context: Context, callback: () -> Unit) { 39 withContext(Dispatchers.IO) { 40 val wallpaperColorScheme = 41 ColorScheme( 42 wallpaperColors, 43 false, 44 fetchThemeStyleFromSetting(), 45 ) 46 with<ColorScheme, Unit>(wallpaperColorScheme) { 47 addOverlayColor(neutral1, R.color.system_neutral1_10) 48 addOverlayColor(neutral2, R.color.system_neutral2_10) 49 addOverlayColor(accent1, R.color.system_accent1_10) 50 addOverlayColor(accent2, R.color.system_accent2_10) 51 addOverlayColor(accent3, R.color.system_accent3_10) 52 } 53 applyToContext(context) 54 callback.invoke() 55 } 56 } 57 fetchThemeStyleFromSettingnull58 private suspend fun fetchThemeStyleFromSetting(): Style { 59 val overlayPackageJson = 60 secureSettingsRepository.getString(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES) 61 return if (!overlayPackageJson.isNullOrEmpty()) { 62 try { 63 val jsonObject = JSONObject(overlayPackageJson) 64 Style.valueOf(jsonObject.getString(OVERLAY_CATEGORY_THEME_STYLE)) 65 } catch (e: (JSONException)) { 66 Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e) 67 Style.TONAL_SPOT 68 } catch (e: IllegalArgumentException) { 69 Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e) 70 Style.TONAL_SPOT 71 } 72 } else { 73 Style.TONAL_SPOT 74 } 75 } 76 77 companion object { 78 private const val TAG = "ThemedWallpaperColorResources" 79 } 80 } 81