fix: sync enable state
Some checks are pending
Build-Apk / build (push) Waiting to run

This commit is contained in:
lisonge 2024-10-20 20:08:08 +08:00
parent 30d6169b64
commit dc54864751
3 changed files with 63 additions and 45 deletions

View File

@ -485,11 +485,18 @@ private fun A11yService.useRuleChangedLog() {
} }
private fun A11yService.useRunningState() { private fun A11yService.useRunningState() {
onCreated {
A11yService.weakInstance = WeakReference(this) A11yService.weakInstance = WeakReference(this)
A11yService.isRunning.value = true A11yService.isRunning.value = true
ManageService.autoStart() if (!storeFlow.value.enableService) {
// https://github.com/gkd-kit/gkd/issues/754
storeFlow.update { it.copy(enableService = true) }
} }
onDestroyed {
if (storeFlow.value.enableService) {
storeFlow.update { it.copy(enableService = false) }
}
}
ManageService.autoStart()
onDestroyed { onDestroyed {
A11yService.weakInstance = WeakReference(null) A11yService.weakInstance = WeakReference(null)
A11yService.isRunning.value = false A11yService.isRunning.value = false

View File

@ -1,59 +1,71 @@
package li.songe.gkd.service package li.songe.gkd.service
import android.os.Handler
import android.os.Looper
import android.provider.Settings import android.provider.Settings
import android.service.quicksettings.Tile import android.service.quicksettings.Tile
import android.service.quicksettings.TileService import android.service.quicksettings.TileService
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import li.songe.gkd.app import li.songe.gkd.app
import li.songe.gkd.permission.writeSecureSettingsState import li.songe.gkd.permission.writeSecureSettingsState
import li.songe.gkd.util.OnChangeListen
import li.songe.gkd.util.OnDestroy
import li.songe.gkd.util.OnTileClick
import li.songe.gkd.util.componentName import li.songe.gkd.util.componentName
import li.songe.gkd.util.lastRestartA11yServiceTimeFlow import li.songe.gkd.util.lastRestartA11yServiceTimeFlow
import li.songe.gkd.util.storeFlow import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.toast import li.songe.gkd.util.toast
class GkdTileService : TileService() { class GkdTileService : TileService(), OnDestroy, OnChangeListen, OnTileClick {
private fun updateTile(): Boolean {
val oldState = qsTile.state
val newState = if (A11yService.isRunning.value) {
Tile.STATE_ACTIVE
} else {
Tile.STATE_INACTIVE
}
if (oldState != newState) {
qsTile.state = newState
qsTile.updateTile()
return true
}
return false
}
private fun autoUpdateTile() {
Handler(Looper.getMainLooper()).postDelayed({
if (!updateTile()) {
Handler(Looper.getMainLooper()).postDelayed(::updateTile, 250)
}
}, 250)
}
override fun onTileAdded() {
super.onTileAdded()
updateTile()
}
override fun onStartListening() { override fun onStartListening() {
super.onStartListening() super.onStartListening()
updateTile() onStartListened()
if (fixRestartService()) {
autoUpdateTile()
}
} }
override fun onClick() { override fun onClick() {
super.onClick() super.onClick()
if (switchA11yService()) { onTileClicked()
autoUpdateTile() }
override fun onStopListening() {
super.onStopListening()
onStopListened()
}
override fun onDestroy() {
super.onDestroy()
onDestroyed()
}
val scope = MainScope().also { scope ->
onDestroyed { scope.cancel() }
}
private val listeningFlow = MutableStateFlow(false).also { listeningFlow ->
onStartListened { listeningFlow.value = true }
onStopListened { listeningFlow.value = false }
}
init {
scope.launch {
combine(
A11yService.isRunning,
listeningFlow
) { v1, v2 -> v1 to v2 }.collect { (running, listening) ->
if (listening) {
qsTile.state = if (running) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
qsTile.updateTile()
}
}
}
onStartListened {
fixRestartService()
}
onTileClicked {
switchA11yService()
} }
} }
} }
@ -93,10 +105,10 @@ fun switchA11yService(): Boolean {
return false return false
} }
val names = getServiceNames() val names = getServiceNames()
storeFlow.update { it.copy(enableService = !A11yService.isRunning.value) }
if (A11yService.isRunning.value) { if (A11yService.isRunning.value) {
names.remove(a11yClsName) names.remove(a11yClsName)
updateServiceNames(names) updateServiceNames(names)
storeFlow.update { it.copy(enableService = false) }
toast("关闭无障碍") toast("关闭无障碍")
} else { } else {
enableA11yService() enableA11yService()
@ -106,7 +118,6 @@ fun switchA11yService(): Boolean {
} }
names.add(a11yClsName) names.add(a11yClsName)
updateServiceNames(names) updateServiceNames(names)
storeFlow.update { it.copy(enableService = true) }
toast("开启无障碍") toast("开启无障碍")
} }
return true return true

View File

@ -108,8 +108,8 @@ fun useControlPage(): ScaffoldExt {
if (writeSecureSettings) { if (writeSecureSettings) {
TextSwitch( TextSwitch(
title = "服务状态", title = "服务状态",
subtitle = if (store.enableService) "无障碍服务正在运行" else "无障碍服务已关闭", subtitle = if (a11yRunning) "无障碍服务正在运行" else "无障碍服务已关闭",
checked = store.enableService, checked = a11yRunning,
onCheckedChange = { onCheckedChange = {
switchA11yService() switchA11yService()
}) })