fix: 主线程阻塞导致界面卡顿

This commit is contained in:
lisonge 2023-11-22 17:17:36 +08:00
parent fc99e3337e
commit f40e8d4e35

View File

@ -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()
}
topActivityFlow.value = shizukuTop
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
}
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 (getCurrentRules().rules.isEmpty()) {
return@onAccessibilityEvent
}
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 (evAppId != rightAppId) {
return@launch
}
if (getCurrentRules().rules.isEmpty()) {
return@launch
}
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
newQueryTask()
}
lastQueryTimeFlow.value = event.eventTime
newQueryTask()
}