package eu.friedcutlet.yokan.services.system import eu.friedcutlet.yokan.AndroidAppContext import android.content.Context import android.net.ConnectivityManager import android.net.Network import android.net.NetworkCapabilities import android.os.Build import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow actual fun openSystemBrowser(url: String): Boolean { val context = AndroidAppContext.appContext ?: return false if (url.isBlank()) return false return runCatching { val intent = android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url)) .addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(intent) true }.getOrDefault(false) } private val systemDefaultLocale: java.util.Locale = java.util.Locale.getDefault() actual fun applyAppLanguage(languageCode: String?) { val locale = if (languageCode.isNullOrBlank()) { systemDefaultLocale } else { runCatching { java.util.Locale.forLanguageTag(languageCode) }.getOrDefault(systemDefaultLocale) } // setDefault drives Compose Multiplatform Resources' locale lookup on // recomposition; the caller is responsible for keying the UI on the // change so cached String resources get re-resolved. java.util.Locale.setDefault(locale) // On Android 33+ the per-app locale API is the recommended path, but it // requires the AppCompat/androidx.core-i18n compat layer and a config // change that recreates the activity. Locale.setDefault + a Compose // `key()` block triggers a recomposition without that round-trip, // which is good enough for in-app text + Compose Resources lookups. } actual fun isOnUnmeteredNetwork(): Boolean { val context = AndroidAppContext.appContext ?: return true val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager ?: return true if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { val active = manager.activeNetwork ?: return false val capabilities = manager.getNetworkCapabilities(active) ?: return false return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) } @Suppress("DEPRECATION") val info = manager.activeNetworkInfo @Suppress("DEPRECATION") return info != null && info.type == ConnectivityManager.TYPE_WIFI } /** Follows the default network; without a context or network-state access it reports unmetered, like [isOnUnmeteredNetwork]. */ actual fun createNetworkMonitor(): NetworkMonitor = runCatching { val manager = checkNotNull(AndroidAppContext.appContext?.getSystemService(ConnectivityManager::class.java)) val state = MutableStateFlow(manager.getNetworkCapabilities(manager.activeNetwork)?.isUnmetered() == true) manager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() { override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) { state.value = capabilities.isUnmetered() } override fun onLost(network: Network) { state.value = false } }) object : NetworkMonitor { override val unmetered: StateFlow = state.asStateFlow() } }.getOrElse { UnmeteredNetwork } private fun NetworkCapabilities.isUnmetered(): Boolean = hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)