Skip to content

Commit

Permalink
fix: 主线程阻塞导致界面卡顿
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 22, 2023
1 parent fc99e33 commit f40e8d4
Showing 1 changed file with 50 additions and 46 deletions.
96 changes: 50 additions & 46 deletions app/src/main/java/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
}


Expand Down

0 comments on commit f40e8d4

Please sign in to comment.