diff options
| author | 2024-09-20 02:59:14 +0000 | |
|---|---|---|
| committer | 2024-09-20 02:59:14 +0000 | |
| commit | 6a2564663bcad02b758cff76593891378506812c (patch) | |
| tree | 5c0b65eec58f7436746679abe5843ca558d632bd | |
| parent | 5715cb7417dedfc9eea298cfadd335913eb5c714 (diff) | |
[Expressive design] create helper class on the device.
Helper function to check if expressive theme is enabled on the device.
Bug: 366336385
Test: manual
Flag: EXEMPT resource only update
Change-Id: Ib18e1073ae137ab44eb44a042ff939a00e9f71a6
| -rw-r--r-- | packages/SettingsLib/SettingsTheme/Android.bp | 4 | ||||
| -rw-r--r-- | packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsThemeHelper.kt | 78 |
2 files changed, 82 insertions, 0 deletions
diff --git a/packages/SettingsLib/SettingsTheme/Android.bp b/packages/SettingsLib/SettingsTheme/Android.bp index 996477cf7960..baeff7e6c006 100644 --- a/packages/SettingsLib/SettingsTheme/Android.bp +++ b/packages/SettingsLib/SettingsTheme/Android.bp @@ -10,6 +10,10 @@ package { android_library { name: "SettingsLibSettingsTheme", use_resource_processor: true, + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], resource_dirs: ["res"], static_libs: ["androidx.preference_preference"], sdk_version: "system_current", diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsThemeHelper.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsThemeHelper.kt new file mode 100644 index 000000000000..10e5267c02d3 --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/SettingsThemeHelper.kt @@ -0,0 +1,78 @@ +/* +* Copyright (C) 2024 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.settingslib.widget + +import android.content.Context +import android.os.Build +import android.os.SystemProperties + +object SettingsThemeHelper { + private const val IS_EXPRESSIVE_DESIGN_ENABLED = "is_expressive_design_enabled" + private var expressiveThemeState: ExpressiveThemeState = ExpressiveThemeState.UNKNOWN + + enum class ExpressiveThemeState { + UNKNOWN, + ENABLED, + DISABLED, + } + + @JvmStatic + fun isExpressiveTheme(context: Context): Boolean { + tryInit(context) + if (expressiveThemeState == ExpressiveThemeState.UNKNOWN) { + throw Exception( + "need to call com.android.settingslib.widget.SettingsThemeHelper.init(Context) first." + ) + } + + return expressiveThemeState == ExpressiveThemeState.ENABLED + } + + private fun tryInit(context: Context) { + if (expressiveThemeState != ExpressiveThemeState.UNKNOWN) { + return + } + + expressiveThemeState = + if ( + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) && + (SystemProperties.getBoolean(IS_EXPRESSIVE_DESIGN_ENABLED, false) || + getPropBoolean(context, IS_EXPRESSIVE_DESIGN_ENABLED, false)) + ) { + ExpressiveThemeState.ENABLED + } else { + ExpressiveThemeState.DISABLED + } + } + + private fun getPropBoolean(context: Context, property: String, def: Boolean): Boolean { + return try { + val systemProperties = context.classLoader.loadClass("android.os.SystemProperties") + + val paramTypes = + arrayOf<Class<*>?>(String::class.java, Boolean::class.javaPrimitiveType) + val getBoolean = systemProperties.getMethod("getBoolean", *paramTypes) + + val params = arrayOf<Any>(property, def) + getBoolean.invoke(systemProperties, *params) as Boolean + } catch (iae: IllegalArgumentException) { + throw iae + } catch (exception: Exception) { + def + } + } +} |