1 /*
2 * Copyright (C) 2024 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.settingslib.widget
18 
19 import android.content.Context
20 import android.os.Build
21 
22 object SettingsThemeHelper {
23     private const val IS_EXPRESSIVE_DESIGN_ENABLED = "is_expressive_design_enabled"
24     private var expressiveThemeState: ExpressiveThemeState = ExpressiveThemeState.UNKNOWN
25 
26     enum class ExpressiveThemeState {
27         UNKNOWN,
28         ENABLED,
29         DISABLED,
30     }
31 
32     @JvmStatic
isExpressiveThemenull33     fun isExpressiveTheme(context: Context): Boolean {
34         tryInit(context)
35         if (expressiveThemeState == ExpressiveThemeState.UNKNOWN) {
36             throw Exception(
37                 "need to call com.android.settingslib.widget.SettingsThemeHelper.init(Context) first."
38             )
39         }
40 
41         return expressiveThemeState == ExpressiveThemeState.ENABLED
42     }
43 
tryInitnull44     private fun tryInit(context: Context) {
45         if (expressiveThemeState != ExpressiveThemeState.UNKNOWN) {
46             return
47         }
48 
49         expressiveThemeState =
50             if (
51                 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) &&
52                         getPropBoolean(context, IS_EXPRESSIVE_DESIGN_ENABLED, false)
53             ) {
54                 ExpressiveThemeState.ENABLED
55             } else {
56                 ExpressiveThemeState.DISABLED
57             }
58     }
59 
getPropBooleannull60     private fun getPropBoolean(context: Context, property: String, def: Boolean): Boolean {
61         return try {
62             val systemProperties = context.classLoader.loadClass("android.os.SystemProperties")
63 
64             val paramTypes =
65                 arrayOf<Class<*>?>(String::class.java, Boolean::class.javaPrimitiveType)
66             val getBoolean = systemProperties.getMethod("getBoolean", *paramTypes)
67 
68             val params = arrayOf<Any>(property, def)
69             getBoolean.invoke(systemProperties, *params) as Boolean
70         } catch (iae: IllegalArgumentException) {
71             throw iae
72         } catch (exception: Exception) {
73             def
74         }
75     }
76 }
77