Added Dynamic Colors setting

This commit is contained in:
Juha Heinanen
2025-10-15 11:31:06 +03:00
parent 0fbbf1dde6
commit dd0d9fba87
5 changed files with 85 additions and 17 deletions
@@ -1,7 +1,7 @@
package com.tutpro.baresip package com.tutpro.baresip
import android.app.Activity import android.app.Activity
import android.os.Build import android.os.Build.VERSION
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme import androidx.compose.material3.darkColorScheme
@@ -23,31 +23,40 @@ import androidx.core.view.WindowCompat
fun AppTheme( fun AppTheme(
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
val context = LocalContext.current
val useDarkTheme by remember { BaresipService.darkTheme } val useDarkTheme by remember { BaresipService.darkTheme }
val useDynamicColors by remember { BaresipService.dynamicColors }
val useActualDynamicColors = useDynamicColors && VERSION.SDK_INT >= 31
Log.d(TAG, "Use actual dynamic colors: $useActualDynamicColors")
val colorScheme = when { val colorScheme = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { useActualDynamicColors -> {
if (useDarkTheme) if (useDarkTheme || isSystemInDarkTheme())
dynamicDarkColorScheme(context = LocalContext.current) dynamicDarkColorScheme(context)
else else
if (isSystemInDarkTheme()) dynamicLightColorScheme(context)
dynamicDarkColorScheme(context = LocalContext.current)
else
dynamicLightColorScheme(context = LocalContext.current)
} }
useDarkTheme -> darkColorScheme() useDarkTheme || isSystemInDarkTheme() -> darkColorScheme()
else -> else -> lightColorScheme()
if (isSystemInDarkTheme())
darkColorScheme()
else
lightColorScheme()
} }
val customColorsPalette = val basePalette = if (useDarkTheme) DarkCustomColors else LightCustomColors
(if (useDarkTheme) DarkCustomColors else LightCustomColors).copy(
val customColorsPalette = basePalette.copy(
background = colorScheme.background, background = colorScheme.background,
cardBackground = colorScheme.surfaceVariant, cardBackground = colorScheme.surfaceVariant,
textFieldBackground = colorScheme.surfaceVariant textFieldBackground = colorScheme.surfaceVariant,
primary = if (useActualDynamicColors) {
colorScheme.primary
} else {
basePalette.primary
},
onPrimary = if (useActualDynamicColors) {
colorScheme.onPrimary
} else {
basePalette.onPrimary
}
) )
val view = LocalView.current val view = LocalView.current
@@ -1750,6 +1750,7 @@ class BaresipService: Service() {
val androidContacts = mutableStateOf(emptyList<Contact.AndroidContact>()) val androidContacts = mutableStateOf(emptyList<Contact.AndroidContact>())
val contactNames = mutableStateOf(emptyList<String>()) val contactNames = mutableStateOf(emptyList<String>())
val darkTheme = mutableStateOf(false) val darkTheme = mutableStateOf(false)
val dynamicColors = mutableStateOf(false)
var messages by mutableStateOf(emptyList<Message>()) var messages by mutableStateOf(emptyList<Message>())
val messageUpdate = MutableLiveData<Long>() val messageUpdate = MutableLiveData<Long>()
val registrationUpdate = MutableLiveData<Long>() val registrationUpdate = MutableLiveData<Long>()
@@ -119,6 +119,15 @@ object Config {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} }
val dynamicColors = previousVariable("dynamic_colors")
BaresipService.dynamicColors.value = if (dynamicColors == "yes") {
config = "${config}dynamic_colors yes\n"
true
} else {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
false
}
val colorblind = previousVariable("colorblind") val colorblind = previousVariable("colorblind")
config = if (colorblind != "") config = if (colorblind != "")
"${config}colorblind $colorblind\n" "${config}colorblind $colorblind\n"
@@ -90,6 +90,8 @@ private var save = false
private var oldBatteryOptimizations = false private var oldBatteryOptimizations = false
private var oldDarkTheme = false private var oldDarkTheme = false
private var oldDynamicColors = false
private var oldDefaultDialer = false private var oldDefaultDialer = false
fun NavGraphBuilder.settingsScreenRoute( fun NavGraphBuilder.settingsScreenRoute(
@@ -104,6 +106,8 @@ fun NavGraphBuilder.settingsScreenRoute(
oldDarkTheme = Preferences(ctx).displayTheme == AppCompatDelegate.MODE_NIGHT_YES oldDarkTheme = Preferences(ctx).displayTheme == AppCompatDelegate.MODE_NIGHT_YES
oldDynamicColors = BaresipService.dynamicColors.value
if (VERSION.SDK_INT >= 29) { if (VERSION.SDK_INT >= 29) {
val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager
oldDefaultDialer = roleManager.isRoleHeld(RoleManager.ROLE_DIALER) oldDefaultDialer = roleManager.isRoleHeld(RoleManager.ROLE_DIALER)
@@ -246,6 +250,8 @@ private var newBatteryOptimizations = false
private var newDarkTheme = false private var newDarkTheme = false
private var newDynamicColors = false
private var newColorblind = false private var newColorblind = false
private var newProximitySensing = true private var newProximitySensing = true
@@ -316,6 +322,8 @@ private fun SettingsContent(
Ringtone(ctx) Ringtone(ctx)
BatteryOptimizations() BatteryOptimizations()
DarkTheme() DarkTheme()
if (VERSION.SDK_INT >= 31)
DynamicColors()
ColorBlind() ColorBlind()
ProximitySensing() ProximitySensing()
if (VERSION.SDK_INT >= 29) if (VERSION.SDK_INT >= 29)
@@ -1099,6 +1107,38 @@ private fun DarkTheme() {
} }
} }
@Composable
private fun DynamicColors() {
Row(
Modifier
.fillMaxWidth()
.padding(end = 10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
) {
val ctx = LocalContext.current
Text(text = stringResource(R.string.dynamic_colors),
modifier = Modifier
.weight(1f)
.clickable {
alertTitle.value = ctx.getString(R.string.dynamic_colors)
alertMessage.value = ctx.getString(R.string.dynamic_colors_help)
showAlert.value = true
},
color = LocalCustomColors.current.itemText,
fontSize = 18.sp)
var dynamicColors by remember { mutableStateOf(oldDynamicColors) }
newDynamicColors = dynamicColors
Switch(
checked = dynamicColors,
onCheckedChange = {
dynamicColors = it
newDynamicColors = dynamicColors
}
)
}
}
@Composable @Composable
private fun ColorBlind() { private fun ColorBlind() {
Row( Row(
@@ -1447,6 +1487,13 @@ private fun checkOnClick(ctx: Context) {
save = true save = true
} }
if (oldDynamicColors != newDynamicColors) {
BaresipService.dynamicColors.value = newDynamicColors
Config.replaceVariable("dynamic_colors",
if (newDynamicColors) "yes" else "no")
save = true
}
if ((Config.variable("colorblind") == "yes") != newColorblind) { if ((Config.variable("colorblind") == "yes") != newColorblind) {
Config.replaceVariable( Config.replaceVariable(
"colorblind", "colorblind",
+2
View File
@@ -396,6 +396,8 @@
<string name="tone_country_help">Country of call ringing, waiting, and callee busy tones</string> <string name="tone_country_help">Country of call ringing, waiting, and callee busy tones</string>
<string name="dark_theme">Dark Theme</string> <string name="dark_theme">Dark Theme</string>
<string name="dark_theme_help">Force dark display theme</string> <string name="dark_theme_help">Force dark display theme</string>
<string name="dynamic_colors">Dynamic Colors</string>
<string name="dynamic_colors_help">Use dynamic colors if enabled in Android Settings</string>
<string name="colorblind">Colorblind</string> <string name="colorblind">Colorblind</string>
<string name="colorblind_help">Use colorblind friendly registration status icons</string> <string name="colorblind_help">Use colorblind friendly registration status icons</string>
<string name="proximity_sensing">Proximity Sensing</string>"> <string name="proximity_sensing">Proximity Sensing</string>">