Skip to content

Commit eec8392

Browse files
committed
perf: a11y status
1 parent 44a56e1 commit eec8392

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

app/src/main/kotlin/li/songe/gkd/MainActivity.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,23 @@ val accessRestrictedSettingsShowFlow = MutableStateFlow(false)
254254

255255
@Composable
256256
fun AccessRestrictedSettingsDlg() {
257+
val a11yRunning by A11yService.isRunning.collectAsState()
258+
LaunchedEffect(a11yRunning) {
259+
if (a11yRunning) {
260+
accessRestrictedSettingsShowFlow.value = false
261+
}
262+
}
257263
val accessRestrictedSettingsShow by accessRestrictedSettingsShowFlow.collectAsState()
258264
val navController = LocalNavController.current
259265
val currentDestination by navController.currentDestinationAsState()
260266
val isA11yPage = currentDestination?.route == AuthA11YPageDestination.route
261267
LaunchedEffect(isA11yPage, accessRestrictedSettingsShow) {
262-
if (isA11yPage && accessRestrictedSettingsShow) {
268+
if (isA11yPage && accessRestrictedSettingsShow && !a11yRunning) {
263269
toast("请重新授权以解除限制")
264270
accessRestrictedSettingsShowFlow.value = false
265271
}
266272
}
267-
if (accessRestrictedSettingsShow && !isA11yPage) {
273+
if (accessRestrictedSettingsShow && !isA11yPage && !a11yRunning) {
268274
AlertDialog(
269275
title = {
270276
Text(text = "访问受限")

app/src/main/kotlin/li/songe/gkd/service/GkdTileService.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,16 @@ private val modifyA11yMutex by lazy { Mutex() }
111111

112112
fun switchA11yService() = appScope.launchTry(Dispatchers.IO) {
113113
modifyA11yMutex.withLock {
114-
if (!writeSecureSettingsState.updateAndGet()) {
115-
toast("请先授予「写入安全设置权限」")
116-
return@launchTry
117-
}
118-
val names = getServiceNames()
119-
storeFlow.update { it.copy(enableService = !A11yService.isRunning.value) }
114+
val newEnableService = !A11yService.isRunning.value
120115
if (A11yService.isRunning.value) {
121-
names.remove(a11yClsName)
122-
updateServiceNames(names)
123-
delay(500)
124-
// https://github.com/orgs/gkd-kit/discussions/799
125-
if (A11yService.isRunning.value) {
126-
toast("关闭无障碍失败")
127-
accessRestrictedSettingsShowFlow.value = true
128-
return@launchTry
129-
}
116+
A11yService.instance?.disableSelf()
130117
toast("关闭无障碍")
131118
} else {
119+
if (!writeSecureSettingsState.updateAndGet()) {
120+
toast("请先授予「写入安全设置权限」")
121+
return@launchTry
122+
}
123+
val names = getServiceNames()
132124
enableA11yService()
133125
if (names.contains(a11yClsName)) { // 当前无障碍异常, 重启服务
134126
names.remove(a11yClsName)
@@ -138,13 +130,15 @@ fun switchA11yService() = appScope.launchTry(Dispatchers.IO) {
138130
names.add(a11yClsName)
139131
updateServiceNames(names)
140132
delay(500)
133+
// https://github.com/orgs/gkd-kit/discussions/799
141134
if (!A11yService.isRunning.value) {
142135
toast("开启无障碍失败")
143136
accessRestrictedSettingsShowFlow.value = true
144137
return@launchTry
145138
}
146139
toast("开启无障碍")
147140
}
141+
storeFlow.update { it.copy(enableService = newEnableService) }
148142
}
149143
}
150144

app/src/main/kotlin/li/songe/gkd/ui/AuthA11yPage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fun AuthA11yPage() {
172172
Text(
173173
modifier = Modifier.padding(cardHorizontalPadding, 0.dp),
174174
style = MaterialTheme.typography.bodyMedium,
175-
text = "1. 授予「写入安全设置权限」\n2. 授权永久有效, 包含「无障碍权限」\n3. 应用重启后可自动打开无障碍服务\n4. 在通知栏快捷开关可快捷重启, 无感保活"
175+
text = "1. 授予「写入安全设置权限」\n2. 授权永久有效, 包含「无障碍权限」\n3. 应用可自行控制开关无障碍服务\n4. 在通知栏快捷开关可快捷重启, 无感保活"
176176
)
177177
if (!writeSecureSettings) {
178178
AuthButtonGroup()

app/src/main/kotlin/li/songe/gkd/ui/home/ControlPage.kt

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import androidx.compose.material3.Card
2828
import androidx.compose.material3.Icon
2929
import androidx.compose.material3.IconButton
3030
import androidx.compose.material3.MaterialTheme
31-
import androidx.compose.material3.OutlinedButton
3231
import androidx.compose.material3.Switch
3332
import androidx.compose.material3.Text
3433
import androidx.compose.material3.TopAppBar
@@ -107,43 +106,31 @@ fun useControlPage(): ScaffoldExt {
107106
val a11yServiceEnabled by a11yServiceEnabledFlow.collectAsState()
108107

109108
// 无障碍故障: 设置中无障碍开启, 但是实际 service 没有运行
110-
val a11yBroken = !writeSecureSettings && !a11yRunning && a11yServiceEnabled
109+
val a11yBroken = !a11yRunning && a11yServiceEnabled
111110

112111
Column(
113112
modifier = Modifier
114113
.verticalScroll(scrollState)
115114
.padding(contentPadding)
116115
) {
117-
if (writeSecureSettings) {
118-
PageItemCard(
119-
imageVector = Icons.Default.Memory,
120-
title = "服务状态",
121-
subtitle = if (a11yRunning) "无障碍服务正在运行" else "无障碍服务已关闭",
122-
rightContent = {
123-
Switch(
124-
checked = a11yRunning,
125-
onCheckedChange = throttle<Boolean> {
116+
PageItemCard(
117+
imageVector = Icons.Default.Memory,
118+
title = "服务状态",
119+
subtitle = if (a11yRunning) "无障碍服务正在运行" else if (a11yBroken) "无障碍服务发生故障" else if (writeSecureSettings) "无障碍服务已关闭" else "无障碍服务未授权",
120+
rightContent = {
121+
Switch(
122+
checked = a11yRunning,
123+
onCheckedChange = throttle(vm.viewModelScope.launchAsFn<Boolean> { newEnabled ->
124+
if (writeSecureSettings || !newEnabled) {
126125
switchA11yService()
127-
},
128-
)
129-
}
130-
)
131-
}
132-
if (!writeSecureSettings && !a11yRunning) {
133-
PageItemCard(
134-
imageVector = Icons.Default.Memory,
135-
title = "无障碍授权",
136-
subtitle = if (a11yBroken) "服务故障,请重新授权" else "授权使无障碍服务运行",
137-
rightContent = {
138-
OutlinedButton(onClick = throttle {
139-
navController.toDestinationsNavigator()
140-
.navigate(AuthA11YPageDestination)
141-
}) {
142-
Text(text = "授权")
143-
}
144-
}
145-
)
146-
}
126+
} else {
127+
navController.toDestinationsNavigator()
128+
.navigate(AuthA11YPageDestination)
129+
}
130+
}),
131+
)
132+
}
133+
)
147134

148135
PageItemCard(
149136
imageVector = Icons.Outlined.Notifications,

0 commit comments

Comments
 (0)