fix: 通知栏快照 activityId 不正确 (#488)

This commit is contained in:
lisonge 2024-01-26 21:09:01 +08:00
parent e06ebbbb2f
commit 08f8633ecc
2 changed files with 37 additions and 9 deletions

View File

@ -7,7 +7,12 @@ import kotlinx.coroutines.delay
import li.songe.gkd.appScope
import li.songe.gkd.debug.SnapshotExt.captureSnapshot
import li.songe.gkd.service.GkdAbService
import li.songe.gkd.service.GkdAbService.Companion.eventExecutor
import li.songe.gkd.service.GkdAbService.Companion.shizukuTopActivityGetter
import li.songe.gkd.service.TopActivity
import li.songe.gkd.service.getAndUpdateCurrentRules
import li.songe.gkd.service.safeActiveWindow
import li.songe.gkd.service.topActivityFlow
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.toast
@ -20,21 +25,34 @@ class SnapshotTileService : TileService() {
return
}
appScope.launchTry(Dispatchers.IO) {
val oldAppId = service.safeActiveWindow?.packageName
val oldAppId = service.safeActiveWindow?.packageName?.toString()
?: return@launchTry toast("获取界面信息根节点失败")
val interval = 500L
val waitTime = 3000L
var i = 0
while (oldAppId.contentEquals(service.safeActiveWindow?.packageName)) {
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
delay(interval)
i++
if (i * interval > waitTime) {
toast("没有检测到界面切换,捕获失败")
while (true) {
val latestAppId =
service.safeActiveWindow?.packageName?.toString() ?: return@launchTry
if (latestAppId != oldAppId) {
eventExecutor.execute {
topActivityFlow.value =
shizukuTopActivityGetter?.invoke() ?: TopActivity(appId = latestAppId)
getAndUpdateCurrentRules()
appScope.launchTry(Dispatchers.IO) {
captureSnapshot()
}
}
return@launchTry
} else {
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
delay(interval)
i++
if (i * interval > waitTime) {
toast("没有检测到界面切换,捕获失败")
return@launchTry
}
}
}
captureSnapshot()
}
}

View File

@ -57,6 +57,7 @@ import li.songe.gkd.util.toast
import li.songe.gkd.util.updateStorage
import li.songe.gkd.util.updateSubscription
import li.songe.selector.Selector
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@ -101,6 +102,12 @@ class GkdAbService : CompositionAbService({
val top = safeGetTasksFc()?.lastOrNull()?.topActivity ?: return null
return TopActivity(appId = top.packageName, activityId = top.className)
}
shizukuTopActivityGetter = {
getShizukuTopActivity()
}
onDestroy {
shizukuTopActivityGetter = null
}
val activityCache = object : LruCache<Pair<String, String>, Boolean>(128) {
override fun create(key: Pair<String, String>): Boolean {
@ -127,7 +134,6 @@ class GkdAbService : CompositionAbService({
var lastContentEventTime = 0L
val queryThread = Dispatchers.IO.limitedParallelism(1)
val actionThread = Dispatchers.IO.limitedParallelism(1)
val eventExecutor = Executors.newSingleThreadExecutor()
onDestroy {
queryThread.cancel()
actionThread.cancel()
@ -490,6 +496,10 @@ class GkdAbService : CompositionAbService({
}) {
companion object {
var shizukuTopActivityGetter: (() -> TopActivity?)? = null
val eventExecutor: ExecutorService by lazy { Executors.newSingleThreadExecutor() }
var service: GkdAbService? = null
val isRunning = MutableStateFlow(false)