Update gomobile usage

This commit is contained in:
世界
2026-01-28 16:50:25 +08:00
parent 41d919bfe3
commit d036683923
5 changed files with 48 additions and 10 deletions

View File

@@ -36,9 +36,9 @@ class Application : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
AppLifecycleObserver.register() AppLifecycleObserver.register(this)
Seq.setContext(this) // Seq.setContext(this)
Libbox.setLocale(Locale.getDefault().toLanguageTag().replace("-", "_")) Libbox.setLocale(Locale.getDefault().toLanguageTag().replace("-", "_"))
HookStatusClient.register(this) HookStatusClient.register(this)
PrivilegeSettingsClient.register(this) PrivilegeSettingsClient.register(this)

View File

@@ -292,7 +292,7 @@ class BoxService(private val service: Service, private val platformInterface: Pl
closeService() closeService()
commandServer.apply { commandServer.apply {
close() close()
Seq.destroyRef(refnum) // Seq.destroyRef(refnum)
} }
Settings.startedByUser = false Settings.startedByUser = false
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {

View File

@@ -59,16 +59,25 @@ class ConnectionsViewModel :
override fun createInitialState() = ConnectionsUiState() override fun createInitialState() = ConnectionsUiState()
private data class ConnectionState(
val foreground: Boolean,
val screenOn: Boolean,
val visibleCount: Int,
val status: Status,
)
init { init {
viewModelScope.launch { viewModelScope.launch {
combine( combine(
AppLifecycleObserver.isForeground, AppLifecycleObserver.isForeground,
AppLifecycleObserver.isScreenOn,
_visibleCount, _visibleCount,
_serviceStatus, _serviceStatus,
) { foreground, visibleCount, status -> ) { foreground, screenOn, visibleCount, status ->
Triple(foreground, visibleCount, status) ConnectionState(foreground, screenOn, visibleCount, status)
}.collect { (foreground, visibleCount, status) -> }.collect { state ->
val shouldConnect = foreground && visibleCount > 0 && status == Status.Started val shouldConnect = state.foreground && state.screenOn &&
state.visibleCount > 0 && state.status == Status.Started
if (shouldConnect) { if (shouldConnect) {
updateState { copy(isLoading = true) } updateState { copy(isLoading = true) }
commandClient.connect() commandClient.connect()
@@ -190,7 +199,7 @@ class ConnectionsViewModel :
val generation = connectionsGeneration.get() val generation = connectionsGeneration.get()
val snapshot = connectionsMutex.withLock { val snapshot = connectionsMutex.withLock {
if (connectionsStore == null) { if (connectionsStore == null) {
connectionsStore = Libbox.newConnections() connectionsStore = Connections()
} }
val store = connectionsStore ?: return@withLock null val store = connectionsStore ?: return@withLock null
store.applyEvents(events) store.applyEvents(events)

View File

@@ -1,5 +1,11 @@
package io.nekohasekai.sfa.utils 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.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner
@@ -11,8 +17,31 @@ object AppLifecycleObserver : DefaultLifecycleObserver {
private val _isForeground = MutableStateFlow(true) private val _isForeground = MutableStateFlow(true)
val isForeground: StateFlow<Boolean> = _isForeground.asStateFlow() val isForeground: StateFlow<Boolean> = _isForeground.asStateFlow()
fun register() { private val _isScreenOn = MutableStateFlow(true)
val isScreenOn: StateFlow<Boolean> = _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) ProcessLifecycleOwner.get().lifecycle.addObserver(this)
val powerManager = context.getSystemService<PowerManager>()!!
_isScreenOn.value = powerManager.isInteractive
context.registerReceiver(
screenReceiver,
IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_SCREEN_OFF)
},
)
} }
override fun onStart(owner: LifecycleOwner) { override fun onStart(owner: LifecycleOwner) {

View File

@@ -109,7 +109,7 @@ open class CommandClient(
runCatching { runCatching {
disconnect() disconnect()
} }
Seq.destroyRef(refnum) // Seq.destroyRef(refnum)
} }
commandClient = null commandClient = null
} }