@@ -6,16 +6,21 @@ import kotlinx.coroutines.flow.MutableStateFlow
6
6
import kotlinx.coroutines.flow.SharingStarted
7
7
import kotlinx.coroutines.flow.combine
8
8
import kotlinx.coroutines.flow.debounce
9
+ import kotlinx.coroutines.flow.map
9
10
import kotlinx.coroutines.flow.stateIn
10
11
import kotlinx.coroutines.launch
11
12
import li.songe.gkd.META
13
+ import li.songe.gkd.a11y.useA11yServiceEnabledFlow
12
14
import li.songe.gkd.app
13
15
import li.songe.gkd.notif.abNotif
14
16
import li.songe.gkd.permission.foregroundServiceSpecialUseState
15
17
import li.songe.gkd.permission.notificationState
18
+ import li.songe.gkd.permission.shizukuOkState
19
+ import li.songe.gkd.permission.writeSecureSettingsState
16
20
import li.songe.gkd.store.actionCountFlow
17
21
import li.songe.gkd.store.storeFlow
18
22
import li.songe.gkd.util.OnSimpleLife
23
+ import li.songe.gkd.util.RuleSummary
19
24
import li.songe.gkd.util.getSubsStatus
20
25
import li.songe.gkd.util.ruleSummaryFlow
21
26
import li.songe.gkd.util.startForegroundServiceByClass
@@ -28,6 +33,50 @@ class StatusService : Service(), OnSimpleLife {
28
33
29
34
val scope = useScope()
30
35
36
+ val shizukuWarnFlow = combine(
37
+ shizukuOkState.stateFlow,
38
+ storeFlow.map { it.enableShizuku },
39
+ ) { a, b ->
40
+ ! a && b
41
+ }.stateIn(scope, SharingStarted .Eagerly , false )
42
+
43
+ val a11yServiceEnabledFlow = useA11yServiceEnabledFlow()
44
+
45
+ fun statusTriple (): Triple <String , String , String ?> {
46
+ val abRunning = A11yService .isRunning.value
47
+ val store = storeFlow.value
48
+ val ruleSummary = ruleSummaryFlow.value
49
+ val count = actionCountFlow.value
50
+ val shizukuWarn = shizukuWarnFlow.value
51
+ val title = if (store.useCustomNotifText) {
52
+ store.customNotifTitle.replaceTemplate(ruleSummary, count)
53
+ } else {
54
+ META .appName
55
+ }
56
+ return if (! abRunning) {
57
+ val text = if (a11yServiceEnabledFlow.value) {
58
+ " 无障碍服务发生故障"
59
+ } else if (writeSecureSettingsState.updateAndGet()) {
60
+ " 无障碍服务已关闭"
61
+ } else {
62
+ " 无障碍服务未授权"
63
+ }
64
+ Triple (title, text, abNotif.uri)
65
+ } else if (! store.enableMatch) {
66
+ Triple (title, " 暂停规则匹配" , " gkd://page?tab=1" )
67
+ } else if (shizukuWarn) {
68
+ Triple (title, " Shizuku 未连接,请授权或关闭优化" , " gkd://page/1" )
69
+ } else if (store.useCustomNotifText) {
70
+ Triple (
71
+ title,
72
+ store.customNotifText.replaceTemplate(ruleSummary, count),
73
+ abNotif.uri
74
+ )
75
+ } else {
76
+ Triple (title, getSubsStatus(ruleSummary, count), abNotif.uri)
77
+ }
78
+ }
79
+
31
80
init {
32
81
useAliveFlow(isRunning)
33
82
useAliveToast(
@@ -42,30 +91,23 @@ class StatusService : Service(), OnSimpleLife {
42
91
A11yService .isRunning,
43
92
storeFlow,
44
93
ruleSummaryFlow,
45
- actionCountFlow,
46
- ) { abRunning, store, ruleSummary, count ->
47
- if (! abRunning) {
48
- META .appName to " 无障碍未授权"
49
- } else if (! store.enableMatch) {
50
- META .appName to " 暂停规则匹配"
51
- } else if (store.useCustomNotifText) {
52
- listOf (store.customNotifTitle, store.customNotifText).map {
53
- it.replace(" \$ {i}" , ruleSummary.globalGroups.size.toString())
54
- .replace(" \$ {k}" , ruleSummary.appSize.toString())
55
- .replace(" \$ {u}" , ruleSummary.appGroupSize.toString())
56
- .replace(" \$ {n}" , count.toString())
57
- }.run {
58
- first() to last()
59
- }
60
- } else {
61
- META .appName to getSubsStatus(ruleSummary, count)
62
- }
63
- }.debounce(1000L )
64
- .stateIn(scope, SharingStarted .Eagerly , " " to " " )
65
- .collect { (title, text) ->
94
+ shizukuWarnFlow,
95
+ a11yServiceEnabledFlow,
96
+ writeSecureSettingsState.stateFlow,
97
+ actionCountFlow.debounce(1000L ),
98
+ ) {
99
+ statusTriple()
100
+ }
101
+ .stateIn(
102
+ scope,
103
+ SharingStarted .Eagerly ,
104
+ Triple (abNotif.title, abNotif.text, abNotif.uri)
105
+ )
106
+ .collect {
66
107
abNotif.copy(
67
- title = title,
68
- text = text
108
+ title = it.first,
109
+ text = it.second,
110
+ uri = it.third,
69
111
).notifyService()
70
112
}
71
113
}
@@ -93,3 +135,10 @@ class StatusService : Service(), OnSimpleLife {
93
135
}
94
136
}
95
137
}
138
+
139
+ private fun String.replaceTemplate (ruleSummary : RuleSummary , count : Long ): String {
140
+ return replace(" \$ {i}" , ruleSummary.globalGroups.size.toString())
141
+ .replace(" \$ {k}" , ruleSummary.appSize.toString())
142
+ .replace(" \$ {u}" , ruleSummary.appGroupSize.toString())
143
+ .replace(" \$ {n}" , count.toString())
144
+ }
0 commit comments