mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 15:46:09 +00:00
Stop UI updates while the app is in the background
Home and Monitor polled the query log every second and Home queried ConnectivityManager every 3 s, and kept doing so while the activity was stopped. The log now signals changes (still at most once a second), Private DNS comes from a network callback, and all flows are collected with collectAsStateWithLifecycle.
This commit is contained in:
@@ -54,6 +54,8 @@ dependencies {
|
||||
implementation("androidx.activity:activity-compose:1.10.1")
|
||||
implementation("androidx.compose.ui:ui")
|
||||
implementation("androidx.compose.material3:material3")
|
||||
// Already pulled in by Compose at this version; declared because the app uses it directly
|
||||
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.9.0")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
|
||||
implementation("com.squareup.okhttp3:okhttp:4.12.0")
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TriStateCheckbox
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
@@ -59,6 +58,7 @@ import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.state.ToggleableState
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@@ -84,8 +84,8 @@ private val IconCache = LruCache<String, ImageBitmap>(256)
|
||||
fun AppsScreen(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val prefs = remember { Prefs(context) }
|
||||
val active by DohVpnService.active.collectAsState()
|
||||
val activeExcluded by DohVpnService.activeExcluded.collectAsState()
|
||||
val active by DohVpnService.active.collectAsStateWithLifecycle()
|
||||
val activeExcluded by DohVpnService.activeExcluded.collectAsStateWithLifecycle()
|
||||
|
||||
var apps by remember { mutableStateOf<List<AppItem>?>(null) }
|
||||
var excluded by remember { mutableStateOf(prefs.excludedApps) }
|
||||
|
||||
@@ -32,7 +32,6 @@ import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -49,6 +48,7 @@ import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -69,17 +69,17 @@ fun BlocklistScreen(modifier: Modifier = Modifier) {
|
||||
val prefs = remember { Prefs(context) }
|
||||
var showNote by remember { mutableStateOf(!prefs.isHintDismissed(HINT_BLOCKLIST)) }
|
||||
|
||||
val enabled by Blocklist.enabled.collectAsState()
|
||||
val total by Blocklist.total.collectAsState()
|
||||
val custom by Blocklist.custom.collectAsState()
|
||||
val lists by Blocklist.lists.collectAsState()
|
||||
val enabled by Blocklist.enabled.collectAsStateWithLifecycle()
|
||||
val total by Blocklist.total.collectAsStateWithLifecycle()
|
||||
val custom by Blocklist.custom.collectAsStateWithLifecycle()
|
||||
val lists by Blocklist.lists.collectAsStateWithLifecycle()
|
||||
|
||||
var domainInput by rememberSaveable { mutableStateOf("") }
|
||||
var domainError by remember { mutableStateOf(false) }
|
||||
var urlInput by rememberSaveable { mutableStateOf("") }
|
||||
// Downloads live in Blocklist, so they keep running and stay visible across tab switches
|
||||
val busy by Blocklist.busy.collectAsState()
|
||||
val error by Blocklist.error.collectAsState()
|
||||
val busy by Blocklist.busy.collectAsStateWithLifecycle()
|
||||
val error by Blocklist.error.collectAsStateWithLifecycle()
|
||||
val lastError = remember { arrayOf("") } // kept while the card animates out
|
||||
error?.let { lastError[0] = it }
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.LinkProperties
|
||||
import android.net.Network
|
||||
import android.net.Uri
|
||||
import android.net.VpnService
|
||||
import android.os.Build
|
||||
@@ -66,10 +68,8 @@ import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
@@ -87,8 +87,13 @@ import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
@@ -118,13 +123,13 @@ private class TestResult(val ok: Boolean, val text: String)
|
||||
fun HomeScreen(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val prefs = remember { Prefs(context) }
|
||||
val active by DohVpnService.active.collectAsState()
|
||||
val active by DohVpnService.active.collectAsStateWithLifecycle()
|
||||
val running = active != null
|
||||
val scope = rememberCoroutineScope()
|
||||
val stats = rememberLiveStats()
|
||||
|
||||
var selected by remember { mutableStateOf(prefs.providerId) }
|
||||
val servers by ServerList.servers.collectAsState()
|
||||
val servers by ServerList.servers.collectAsStateWithLifecycle()
|
||||
var syncing by remember { mutableStateOf(false) }
|
||||
var syncResult by remember { mutableStateOf<TestResult?>(null) }
|
||||
var shownSync by remember { mutableStateOf<TestResult?>(null) }
|
||||
@@ -135,13 +140,8 @@ fun HomeScreen(modifier: Modifier = Modifier) {
|
||||
var shownResult by remember { mutableStateOf<TestResult?>(null) } // kept while the card animates out
|
||||
var testing by remember { mutableStateOf(false) }
|
||||
val crashReport = remember { CrashLog.read(context) }
|
||||
// Strict Private DNS makes Android ignore the VPN's DNS server; re-checked while Home is open
|
||||
val privateDnsHost by produceState<String?>(null) {
|
||||
while (true) {
|
||||
value = readPrivateDnsHost(context)
|
||||
delay(3000)
|
||||
}
|
||||
}
|
||||
// Strict Private DNS makes Android ignore the VPN's DNS server; followed while Home is visible
|
||||
val privateDnsHost by remember { privateDnsHosts(context) }.collectAsStateWithLifecycle(initialValue = null)
|
||||
var crashVisible by remember { mutableStateOf(crashReport != null) }
|
||||
|
||||
val isCustom = selected == Providers.CUSTOM_ID
|
||||
@@ -631,11 +631,24 @@ private fun RowDivider() {
|
||||
)
|
||||
}
|
||||
|
||||
// Hostname of strict Private DNS (DNS-over-TLS) on the current network, null when it is Automatic or Off
|
||||
private fun readPrivateDnsHost(context: Context): String? {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return null
|
||||
val cm = context.getSystemService(ConnectivityManager::class.java) ?: return null
|
||||
return runCatching { cm.getLinkProperties(cm.activeNetwork)?.privateDnsServerName }.getOrNull()
|
||||
// Hostname of strict Private DNS (DNS-over-TLS) on the current network, null when it is Automatic or Off.
|
||||
// Android reports the current state on registration and every change after it.
|
||||
private fun privateDnsHosts(context: Context): Flow<String?> {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return flowOf(null)
|
||||
val cm = context.applicationContext.getSystemService(ConnectivityManager::class.java) ?: return flowOf(null)
|
||||
return callbackFlow {
|
||||
val callback = object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
|
||||
trySend(linkProperties.privateDnsServerName)
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
trySend(null)
|
||||
}
|
||||
}
|
||||
val registered = runCatching { cm.registerDefaultNetworkCallback(callback) }.isSuccess
|
||||
awaitClose { if (registered) runCatching { cm.unregisterNetworkCallback(callback) } }
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
||||
private fun openNetworkSettings(context: Context) {
|
||||
|
||||
@@ -28,8 +28,6 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -45,8 +43,8 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.text.SimpleDateFormat
|
||||
@@ -61,19 +59,16 @@ private val TimeFormat = SimpleDateFormat("HH:mm:ss", Locale.ROOT)
|
||||
fun MonitorScreen(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
val custom by Blocklist.custom.collectAsState()
|
||||
val custom by Blocklist.custom.collectAsStateWithLifecycle()
|
||||
|
||||
var entries by remember { mutableStateOf(QueryLog.snapshot()) }
|
||||
var stats by remember { mutableStateOf(QueryLog.stats()) }
|
||||
var filter by rememberSaveable { mutableStateOf(MonitorFilter.ALL) }
|
||||
var search by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) {
|
||||
OnQueryLogChange {
|
||||
entries = QueryLog.snapshot()
|
||||
stats = QueryLog.stats()
|
||||
delay(1000)
|
||||
}
|
||||
}
|
||||
|
||||
val visible = remember(entries, filter, search) {
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package cc.skysparkle.sparkledns
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.transform
|
||||
|
||||
enum class QueryStatus { OK, CACHED, BLOCKED, FAILED }
|
||||
|
||||
class QueryEntry(
|
||||
@@ -31,6 +36,14 @@ object QueryLog {
|
||||
private var failed = 0L
|
||||
private var latencySum = 0L
|
||||
private var latencyCount = 0L
|
||||
private val version = MutableStateFlow(0L)
|
||||
|
||||
// Fires right away, then at most once a second while queries keep coming: a busy log refreshes
|
||||
// the screen no more often than the old one-second polling did, and an idle one not at all
|
||||
fun changes(): Flow<Long> = version.transform {
|
||||
emit(it)
|
||||
delay(1000)
|
||||
}
|
||||
|
||||
fun add(question: DnsMessage.Question?, status: QueryStatus, latencyMs: Int) {
|
||||
if (question == null) return
|
||||
@@ -46,6 +59,7 @@ object QueryLog {
|
||||
QueryStatus.BLOCKED -> blocked++
|
||||
QueryStatus.FAILED -> failed++
|
||||
}
|
||||
version.value++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,5 +81,6 @@ object QueryLog {
|
||||
fun clear() {
|
||||
entries.clear()
|
||||
total = 0; cached = 0; blocked = 0; failed = 0; latencySum = 0; latencyCount = 0
|
||||
version.value++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -42,7 +43,9 @@ import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.delay
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import java.text.NumberFormat
|
||||
|
||||
val CachedColor = Color(0xFF7FD1AE)
|
||||
@@ -191,16 +194,23 @@ fun StatDivider() {
|
||||
)
|
||||
}
|
||||
|
||||
// Polls the in-memory query log once a second while the screen is visible
|
||||
// Runs [onChange] when the query log changes, at most once a second and only while the screen
|
||||
// is started: nothing runs while the app sits in the background
|
||||
@Composable
|
||||
fun OnQueryLogChange(onChange: () -> Unit) {
|
||||
val lifecycle = LocalLifecycleOwner.current.lifecycle
|
||||
val current by rememberUpdatedState(onChange)
|
||||
LaunchedEffect(lifecycle) {
|
||||
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
QueryLog.changes().collect { current() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun rememberLiveStats(): QueryStats {
|
||||
var stats by remember { mutableStateOf(QueryLog.stats()) }
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) {
|
||||
stats = QueryLog.stats()
|
||||
delay(1000)
|
||||
}
|
||||
}
|
||||
OnQueryLogChange { stats = QueryLog.stats() }
|
||||
return stats
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user