• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(wallpaperColors, false, fetchThemeStyleFromSetting())
42             with<ColorScheme, Unit>(wallpaperColorScheme) {
43                 addOverlayColor(neutral1, R.color.system_neutral1_10)
44                 addOverlayColor(neutral2, R.color.system_neutral2_10)
45                 addOverlayColor(accent1, R.color.system_accent1_10)
46                 addOverlayColor(accent2, R.color.system_accent2_10)
47                 addOverlayColor(accent3, R.color.system_accent3_10)
48             }
49             applyToContext(context)
50             callback.invoke()
51         }
52     }
53 
54     @Style.Type
fetchThemeStyleFromSettingnull55     private suspend fun fetchThemeStyleFromSetting(): Int {
56         val overlayPackageJson =
57             secureSettingsRepository.getString(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES)
58         return if (!overlayPackageJson.isNullOrEmpty()) {
59             try {
60                 val jsonObject = JSONObject(overlayPackageJson)
61                 Style.valueOf(jsonObject.getString(OVERLAY_CATEGORY_THEME_STYLE))
62             } catch (e: (JSONException)) {
63                 Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e)
64                 Style.TONAL_SPOT
65             } catch (e: IllegalArgumentException) {
66                 Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e)
67                 Style.TONAL_SPOT
68             }
69         } else {
70             Style.TONAL_SPOT
71         }
72     }
73 
74     companion object {
75         private const val TAG = "ThemedWallpaperColorResources"
76     }
77 }
78