Skip to content

Commit

Permalink
fix: setGeneratedTime failed (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 3, 2024
1 parent db3be41 commit 03cdf98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/service/A11yService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class A11yService : AccessibilityService(), OnCreate, OnA11yConnected, OnA11yEve
val cache = A11yContext(true)

val targetNode = serviceVal.safeActiveWindow?.let {
cache.rootCache = it
cache.querySelector(
it,
selector,
Expand Down Expand Up @@ -275,7 +274,11 @@ private fun A11yService.useMatchRule() {
if (byForced && !rule.checkForced()) continue
lastNode?.let { n ->
val refreshOk = (!lastNodeUsed) || (try {
n.refresh()
val e = n.refresh()
if (e) {
n.setGeneratedTime()
}
e
} catch (_: Exception) {
false
})
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/kotlin/li/songe/gkd/service/NodeExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ import li.songe.selector.UnknownMemberMethodException
import li.songe.selector.UnknownMemberMethodParamsException
import li.songe.selector.initDefaultTypeInfo

// 在主线程调用任意获取新节点或刷新节点的API会阻塞界面导致卡顿

// 某些应用耗时 554ms
val AccessibilityService.safeActiveWindow: AccessibilityNodeInfo?
get() = try {
// java.lang.SecurityException: Call from user 0 as user -2 without permission INTERACT_ACROSS_USERS or INTERACT_ACROSS_USERS_FULL not allowed.
rootInActiveWindow.apply {
a11yContext.rootCache = this
}?.apply {
rootInActiveWindow?.apply {
// https://github.com/gkd-kit/gkd/issues/759
setGeneratedTime()
}
// 在主线程调用会阻塞界面导致卡顿
} catch (e: Exception) {
e.printStackTrace()
null
}.apply {
a11yContext.rootCache = this
}

val AccessibilityService.safeActiveWindowAppId: String?
Expand Down Expand Up @@ -76,8 +78,8 @@ fun AccessibilityNodeInfo.setGeneratedTime() {
fun AccessibilityNodeInfo.isExpired(expiryMillis: Long): Boolean {
val generatedTime = extras.getLong(A11Y_NODE_TIME_KEY, -1)
if (generatedTime == -1L) {
setGeneratedTime()
return false
// https://github.com/gkd-kit/gkd/issues/759
return true
}
return (System.currentTimeMillis() - generatedTime) > expiryMillis
}
Expand Down

0 comments on commit 03cdf98

Please sign in to comment.