SystemIntegration.android.kt
composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/services/system/SystemIntegration.android.kt
1package eu.friedcutlet.yokan.services.system23import eu.friedcutlet.yokan.AndroidAppContext4import android.content.Context5import android.net.ConnectivityManager6import android.net.Network7import android.net.NetworkCapabilities8import android.os.Build9import kotlinx.coroutines.flow.MutableStateFlow10import kotlinx.coroutines.flow.StateFlow11import kotlinx.coroutines.flow.asStateFlow1213actual fun openSystemBrowser(url: String): Boolean {14 val context = AndroidAppContext.appContext ?: return false15 if (url.isBlank()) return false16 return runCatching {17 val intent = android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url))18 .addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)19 context.startActivity(intent)20 true21 }.getOrDefault(false)22}2324private val systemDefaultLocale: java.util.Locale = java.util.Locale.getDefault()2526actual fun applyAppLanguage(languageCode: String?) {27 val locale = if (languageCode.isNullOrBlank()) {28 systemDefaultLocale29 } else {30 runCatching { java.util.Locale.forLanguageTag(languageCode) }.getOrDefault(systemDefaultLocale)31 }32 // setDefault drives Compose Multiplatform Resources' locale lookup on33 // recomposition; the caller is responsible for keying the UI on the34 // change so cached String resources get re-resolved.35 java.util.Locale.setDefault(locale)36 // On Android 33+ the per-app locale API is the recommended path, but it37 // requires the AppCompat/androidx.core-i18n compat layer and a config38 // change that recreates the activity. Locale.setDefault + a Compose39 // `key()` block triggers a recomposition without that round-trip,40 // which is good enough for in-app text + Compose Resources lookups.41}4243actual fun isOnUnmeteredNetwork(): Boolean {44 val context = AndroidAppContext.appContext ?: return true45 val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager46 ?: return true47 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {48 val active = manager.activeNetwork ?: return false49 val capabilities = manager.getNetworkCapabilities(active) ?: return false50 return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) &&51 capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)52 }53 @Suppress("DEPRECATION")54 val info = manager.activeNetworkInfo55 @Suppress("DEPRECATION")56 return info != null && info.type == ConnectivityManager.TYPE_WIFI57}5859/** Follows the default network; without a context or network-state access it reports unmetered, like [isOnUnmeteredNetwork]. */60actual fun createNetworkMonitor(): NetworkMonitor = runCatching {61 val manager = checkNotNull(AndroidAppContext.appContext?.getSystemService(ConnectivityManager::class.java))62 val state = MutableStateFlow(manager.getNetworkCapabilities(manager.activeNetwork)?.isUnmetered() == true)63 manager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() {64 override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) {65 state.value = capabilities.isUnmetered()66 }6768 override fun onLost(network: Network) {69 state.value = false70 }71 })72 object : NetworkMonitor {73 override val unmetered: StateFlow<Boolean> = state.asStateFlow()74 }75}.getOrElse { UnmeteredNetwork }7677private fun NetworkCapabilities.isUnmetered(): Boolean = hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)78