From d036683923f41871196125a640bc1211b6878d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 28 Jan 2026 16:50:25 +0800 Subject: [PATCH] Update gomobile usage --- .../java/io/nekohasekai/sfa/Application.kt | 4 +-- .../java/io/nekohasekai/sfa/bg/BoxService.kt | 2 +- .../connections/ConnectionsViewModel.kt | 19 +++++++++--- .../sfa/utils/AppLifecycleObserver.kt | 31 ++++++++++++++++++- .../io/nekohasekai/sfa/utils/CommandClient.kt | 2 +- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sfa/Application.kt b/app/src/main/java/io/nekohasekai/sfa/Application.kt index bb5ba70..02b2467 100644 --- a/app/src/main/java/io/nekohasekai/sfa/Application.kt +++ b/app/src/main/java/io/nekohasekai/sfa/Application.kt @@ -36,9 +36,9 @@ class Application : Application() { override fun onCreate() { super.onCreate() - AppLifecycleObserver.register() + AppLifecycleObserver.register(this) - Seq.setContext(this) +// Seq.setContext(this) Libbox.setLocale(Locale.getDefault().toLanguageTag().replace("-", "_")) HookStatusClient.register(this) PrivilegeSettingsClient.register(this) diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt b/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt index 2a5af61..c3e9e16 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt @@ -292,7 +292,7 @@ class BoxService(private val service: Service, private val platformInterface: Pl closeService() commandServer.apply { close() - Seq.destroyRef(refnum) +// Seq.destroyRef(refnum) } Settings.startedByUser = false withContext(Dispatchers.Main) { diff --git a/app/src/main/java/io/nekohasekai/sfa/compose/screen/connections/ConnectionsViewModel.kt b/app/src/main/java/io/nekohasekai/sfa/compose/screen/connections/ConnectionsViewModel.kt index 4d7796c..c54087c 100644 --- a/app/src/main/java/io/nekohasekai/sfa/compose/screen/connections/ConnectionsViewModel.kt +++ b/app/src/main/java/io/nekohasekai/sfa/compose/screen/connections/ConnectionsViewModel.kt @@ -59,16 +59,25 @@ class ConnectionsViewModel : override fun createInitialState() = ConnectionsUiState() + private data class ConnectionState( + val foreground: Boolean, + val screenOn: Boolean, + val visibleCount: Int, + val status: Status, + ) + init { viewModelScope.launch { combine( AppLifecycleObserver.isForeground, + AppLifecycleObserver.isScreenOn, _visibleCount, _serviceStatus, - ) { foreground, visibleCount, status -> - Triple(foreground, visibleCount, status) - }.collect { (foreground, visibleCount, status) -> - val shouldConnect = foreground && visibleCount > 0 && status == Status.Started + ) { foreground, screenOn, visibleCount, status -> + ConnectionState(foreground, screenOn, visibleCount, status) + }.collect { state -> + val shouldConnect = state.foreground && state.screenOn && + state.visibleCount > 0 && state.status == Status.Started if (shouldConnect) { updateState { copy(isLoading = true) } commandClient.connect() @@ -190,7 +199,7 @@ class ConnectionsViewModel : val generation = connectionsGeneration.get() val snapshot = connectionsMutex.withLock { if (connectionsStore == null) { - connectionsStore = Libbox.newConnections() + connectionsStore = Connections() } val store = connectionsStore ?: return@withLock null store.applyEvents(events) diff --git a/app/src/main/java/io/nekohasekai/sfa/utils/AppLifecycleObserver.kt b/app/src/main/java/io/nekohasekai/sfa/utils/AppLifecycleObserver.kt index 123bb12..9a2969f 100644 --- a/app/src/main/java/io/nekohasekai/sfa/utils/AppLifecycleObserver.kt +++ b/app/src/main/java/io/nekohasekai/sfa/utils/AppLifecycleObserver.kt @@ -1,5 +1,11 @@ package io.nekohasekai.sfa.utils +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.os.PowerManager +import androidx.core.content.getSystemService import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner @@ -11,8 +17,31 @@ object AppLifecycleObserver : DefaultLifecycleObserver { private val _isForeground = MutableStateFlow(true) val isForeground: StateFlow = _isForeground.asStateFlow() - fun register() { + private val _isScreenOn = MutableStateFlow(true) + val isScreenOn: StateFlow = _isScreenOn.asStateFlow() + + private val screenReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + when (intent.action) { + Intent.ACTION_SCREEN_ON -> _isScreenOn.value = true + Intent.ACTION_SCREEN_OFF -> _isScreenOn.value = false + } + } + } + + fun register(context: Context) { ProcessLifecycleOwner.get().lifecycle.addObserver(this) + + val powerManager = context.getSystemService()!! + _isScreenOn.value = powerManager.isInteractive + + context.registerReceiver( + screenReceiver, + IntentFilter().apply { + addAction(Intent.ACTION_SCREEN_ON) + addAction(Intent.ACTION_SCREEN_OFF) + }, + ) } override fun onStart(owner: LifecycleOwner) { diff --git a/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt b/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt index 637692a..c5b7681 100644 --- a/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt +++ b/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt @@ -109,7 +109,7 @@ open class CommandClient( runCatching { disconnect() } - Seq.destroyRef(refnum) +// Seq.destroyRef(refnum) } commandClient = null }