package com.tutpro.baresip import android.app.role.RoleManager import android.content.Context import android.content.Context.POWER_SERVICE import android.content.Context.ROLE_SERVICE import android.os.Build import android.os.PowerManager import androidx.appcompat.app.AppCompatDelegate import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import kotlinx.coroutines.flow.MutableStateFlow import java.io.File class SettingsViewModel: ViewModel() { val autoStart = MutableStateFlow(false) val listenAddress = MutableStateFlow("") val addressFamily = MutableStateFlow("") val transportProtocols = MutableStateFlow("") val dnsServers = MutableStateFlow("") val tlsCertificateFile = MutableStateFlow(false) val verifyServer = MutableStateFlow(false) val caFile = MutableStateFlow(false) val userAgent = MutableStateFlow("") val uniqueContactUri = MutableStateFlow(true) val batteryOptimizations = MutableStateFlow(false) val darkTheme = MutableStateFlow(false) val dynamicColors = MutableStateFlow(false) val colorblind = MutableStateFlow(false) val proximitySensing = MutableStateFlow(false) val defaultDialer = MutableStateFlow(false) val mobileAccount = MutableStateFlow(true) val defaultMessaging = MutableStateFlow(false) val debug = MutableStateFlow(false) val sipTrace = MutableStateFlow(false) var restart by mutableStateOf(false) var save by mutableStateOf(false) private var isLoaded = false fun loadSettings(ctx: Context) { if (isLoaded || !Config.isInitialized()) return else isLoaded = true autoStart.value = Config.variable("auto_start") == "yes" listenAddress.value = Config.variable("sip_listen") addressFamily.value = Config.variable("net_af").lowercase() transportProtocols.value = Config.variable("sip_transports") dnsServers.value = Config.dnsServers() tlsCertificateFile.value = File(BaresipService.filesPath + "/cert.pem").exists() verifyServer.value = Config.variable("sip_verify_server") == "yes" caFile.value = File(BaresipService.filesPath + "/ca_certs.crt").exists() userAgent.value = Config.variable("user_agent") uniqueContactUri.value = Config.variable("sip_cuser_random") == "yes" val powerManager = ctx.getSystemService(POWER_SERVICE) as PowerManager batteryOptimizations.value = !powerManager.isIgnoringBatteryOptimizations(ctx.packageName) darkTheme.value = Preferences(ctx).displayTheme == AppCompatDelegate.MODE_NIGHT_YES dynamicColors.value = BaresipService.dynamicColors.value colorblind.value = Config.variable("colorblind") == "yes" proximitySensing.value = Config.variable("proximity_sensing") == "yes" if (Build.VERSION.SDK_INT >= 29) { val roleManager = ctx.getSystemService(ROLE_SERVICE) as RoleManager defaultDialer.value = roleManager.isRoleHeld(RoleManager.ROLE_DIALER) mobileAccount.value = BaresipService.mobileAccount defaultMessaging.value = roleManager.isRoleHeld(RoleManager.ROLE_SMS) } debug.value = Config.variable("log_level") == "0" sipTrace.value = BaresipService.sipTrace } }