Skip to content

Commit bc9be7d

Browse files
committed
perf: activityOkFlow
1 parent 0d36c19 commit bc9be7d

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import li.songe.gkd.util.OnA11yLife
2020
import li.songe.gkd.util.componentName
2121
import li.songe.selector.MatchOption
2222
import li.songe.selector.Selector
23-
import java.lang.ref.WeakReference
2423

2524
abstract class A11yService : AccessibilityService(), OnA11yLife {
2625
override fun onCreate() = onCreated()
@@ -52,8 +51,8 @@ abstract class A11yService : AccessibilityService(), OnA11yLife {
5251
init {
5352
useLogLifecycle()
5453
useAliveFlow(isRunning)
55-
onCreated { a11yWeakRef = WeakReference(this) }
56-
onDestroyed { a11yWeakRef = null }
54+
onCreated { a11yRef = this }
55+
onDestroyed { a11yRef = null }
5756
A11yRuleEngine(this)
5857
onA11yFeatInit()
5958
}
@@ -63,9 +62,9 @@ abstract class A11yService : AccessibilityService(), OnA11yLife {
6362
val a11yClsName by lazy { a11yComponentName.flattenToShortString() }
6463

6564
val isRunning = MutableStateFlow(false)
66-
private var a11yWeakRef: WeakReference<A11yService>? = null
65+
private var a11yRef: A11yService? = null
6766
val instance: A11yService?
68-
get() = a11yWeakRef?.get()
67+
get() = a11yRef
6968

7069
fun execAction(gkdAction: GkdAction): ActionResult {
7170
val service = instance ?: throw RpcError("无障碍没有运行")

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import li.songe.gkd.a11y.topActivityFlow
2626
import li.songe.gkd.notif.StopServiceReceiver
2727
import li.songe.gkd.notif.recordNotif
2828
import li.songe.gkd.permission.canDrawOverlaysState
29+
import li.songe.gkd.shizuku.SafeTaskListener
30+
import li.songe.gkd.shizuku.shizukuContextFlow
2931
import li.songe.gkd.ui.component.AppNameText
3032
import li.songe.gkd.ui.theme.AppTheme
3133
import li.songe.gkd.util.appInfoCacheFlow
@@ -34,13 +36,19 @@ import li.songe.gkd.util.stopServiceByClass
3436

3537

3638
class RecordService : OverlayWindowService() {
39+
override val positionStoreKey = "overlay_xy_record"
40+
3741
val topAppInfoFlow by lazy {
3842
appInfoCacheFlow.combine(topActivityFlow) { map, topActivity ->
3943
map[topActivity.appId]
4044
}.stateIn(lifecycleScope, SharingStarted.Eagerly, null)
4145
}
4246

43-
override val positionStoreKey = "overlay_xy_record"
47+
val activityOkFlow by lazy {
48+
combine(A11yService.isRunning, shizukuContextFlow) { a, b ->
49+
a || (b.activityTaskManager != null && SafeTaskListener.isAvailable)
50+
}.stateIn(scope = lifecycleScope, started = SharingStarted.Eagerly, initialValue = false)
51+
}
4452

4553
@Composable
4654
override fun ComposeContent() = AppTheme(invertedTheme = true) {
@@ -52,7 +60,7 @@ class RecordService : OverlayWindowService() {
5260
.padding(horizontal = 4.dp, vertical = 2.dp)
5361
) {
5462
CompositionLocalProvider(LocalContentColor provides contentColorFor(bgColor)) {
55-
if (A11yService.isRunning.collectAsState().value) {
63+
if (activityOkFlow.collectAsState().value) {
5664
val topActivity = topActivityFlow.collectAsState().value
5765
Text(
5866
text = topActivity.number.toString(),
@@ -73,7 +81,7 @@ class RecordService : OverlayWindowService() {
7381
} else {
7482
Column {
7583
Text(text = "记录服务")
76-
Text(text = "无障碍服务未运行")
84+
Text(text = "无权限检测界面切换")
7785
}
7886
}
7987
}

app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import kotlinx.coroutines.flow.combine
1313
import kotlinx.coroutines.flow.debounce
1414
import kotlinx.coroutines.flow.map
1515
import kotlinx.coroutines.flow.stateIn
16-
import li.songe.gkd.META
1716
import li.songe.gkd.app
1817
import li.songe.gkd.appScope
1918
import li.songe.gkd.data.AppInfo
@@ -145,8 +144,10 @@ private fun updateShizukuBinder() = appScope.launchTry(Dispatchers.IO) {
145144
serviceWrapper?.destroy()
146145
activityTaskManager?.unregisterDefault()
147146
}
148-
val prefix = if (isActivityVisible()) "" else "${META.appName}: "
149-
toast("${prefix}Shizuku 服务已断开")
147+
shizukuContextFlow.value = defaultShizukuContext
148+
if (isActivityVisible()) {
149+
toast("Shizuku 服务已断开")
150+
}
150151
}
151152
}
152153
}

0 commit comments

Comments
 (0)