From f40e8d4e359df5d15ce7649a48253dbfa540aed0 Mon Sep 17 00:00:00 2001 From: lisonge Date: Wed, 22 Nov 2023 17:17:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=BB=E7=BA=BF=E7=A8=8B=E9=98=BB?= =?UTF-8?q?=E5=A1=9E=E5=AF=BC=E8=87=B4=E7=95=8C=E9=9D=A2=E5=8D=A1=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/li/songe/gkd/service/GkdAbService.kt | 96 ++++++++++--------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/li/songe/gkd/service/GkdAbService.kt b/app/src/main/java/li/songe/gkd/service/GkdAbService.kt index d81969af39..674750f176 100644 --- a/app/src/main/java/li/songe/gkd/service/GkdAbService.kt +++ b/app/src/main/java/li/songe/gkd/service/GkdAbService.kt @@ -98,6 +98,7 @@ class GkdAbService : CompositionAbService({ var lastContentEventTime = 0L var job: Job? = null val singleThread = Dispatchers.IO.limitedParallelism(1) + val eventThread = Dispatchers.Default.limitedParallelism(1) onDestroy { singleThread.cancel() } @@ -159,63 +160,66 @@ class GkdAbService : CompositionAbService({ } lastContentEventTime = event.eventTime } - val evAppId = event.packageName?.toString() ?: return@onAccessibilityEvent - val evActivityId = event.className?.toString() ?: return@onAccessibilityEvent - val rightAppId = safeActiveWindow?.packageName?.toString() ?: return@onAccessibilityEvent - - if (rightAppId == evAppId) { - if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { - // tv.danmaku.bili, com.miui.home, com.miui.home.launcher.Launcher - if (isActivity(evAppId, evActivityId)) { - topActivityFlow.value = TopActivity( - evAppId, evActivityId - ) - activityChangeTime = System.currentTimeMillis() - } - } else { - if (event.eventTime - lastTriggerShizukuTime > 300) { - val shizukuTop = getShizukuTopActivity() - if (shizukuTop != null && shizukuTop.appId == rightAppId) { - if (shizukuTop.activityId == evActivityId) { - activityChangeTime = System.currentTimeMillis() + + scope.launch(eventThread) { + val evAppId = event.packageName?.toString() ?: return@launch + val evActivityId = event.className?.toString() ?: return@launch + val rightAppId = safeActiveWindow?.packageName?.toString() ?: return@launch + + if (rightAppId == evAppId) { + if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { + // tv.danmaku.bili, com.miui.home, com.miui.home.launcher.Launcher + if (isActivity(evAppId, evActivityId)) { + topActivityFlow.value = TopActivity( + evAppId, evActivityId + ) + activityChangeTime = System.currentTimeMillis() + } + } else { + if (event.eventTime - lastTriggerShizukuTime > 300) { + val shizukuTop = getShizukuTopActivity() + if (shizukuTop != null && shizukuTop.appId == rightAppId) { + if (shizukuTop.activityId == evActivityId) { + activityChangeTime = System.currentTimeMillis() + } + topActivityFlow.value = shizukuTop } - topActivityFlow.value = shizukuTop + lastTriggerShizukuTime = event.eventTime } - lastTriggerShizukuTime = event.eventTime } } - } + if (rightAppId != topActivityFlow.value?.appId) { + // 从 锁屏,下拉通知栏 返回等情况, 应用不会发送事件, 但是系统组件会发送事件 + val shizukuTop = getShizukuTopActivity() + if (shizukuTop?.appId == rightAppId) { + topActivityFlow.value = shizukuTop + } else { + topActivityFlow.value = TopActivity(rightAppId) + } + } - if (rightAppId != topActivityFlow.value?.appId) { - // 从 锁屏,下拉通知栏 返回等情况, 应用不会发送事件, 但是系统组件会发送事件 - val shizukuTop = getShizukuTopActivity() - if (shizukuTop?.appId == rightAppId) { - topActivityFlow.value = shizukuTop - } else { - topActivityFlow.value = TopActivity(rightAppId) + if (evAppId != rightAppId) { + return@launch } - } - if (getCurrentRules().rules.isEmpty()) { - return@onAccessibilityEvent - } + if (getCurrentRules().rules.isEmpty()) { + return@launch + } - if (evAppId != rightAppId) { - return@onAccessibilityEvent - } - if (!storeFlow.value.enableService) return@onAccessibilityEvent - val jobVal = job - if (jobVal?.isActive == true) { - if (openAdOptimized == true) { - jobVal.cancel() - } else { - return@onAccessibilityEvent + if (!storeFlow.value.enableService) return@launch + val jobVal = job + if (jobVal?.isActive == true) { + if (openAdOptimized == true) { + jobVal.cancel() + } else { + return@launch + } } - } - lastQueryTimeFlow.value = event.eventTime + lastQueryTimeFlow.value = event.eventTime - newQueryTask() + newQueryTask() + } }