Skip to content

Commit

Permalink
perf: event query
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Mar 2, 2024
1 parent fe6b49a commit 6206b86
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 55 deletions.
43 changes: 19 additions & 24 deletions app/src/main/kotlin/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package li.songe.gkd.service

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import li.songe.gkd.app
import li.songe.gkd.appScope
import li.songe.gkd.data.AppRule
import li.songe.gkd.data.ClickLog
import li.songe.gkd.data.GlobalRule
Expand All @@ -15,7 +13,6 @@ import li.songe.gkd.db.DbSet
import li.songe.gkd.util.RuleSummary
import li.songe.gkd.util.getDefaultLauncherAppId
import li.songe.gkd.util.increaseClickCount
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.recordStoreFlow
import li.songe.gkd.util.ruleSummaryFlow

Expand Down Expand Up @@ -113,27 +110,25 @@ fun updateLauncherAppId() {
}

val clickLogMutex = Mutex()
fun insertClickLog(rule: ResolvedRule) {
appScope.launchTry(Dispatchers.IO) {
clickLogMutex.withLock {
increaseClickCount()
val clickLog = ClickLog(
appId = topActivityFlow.value.appId,
activityId = topActivityFlow.value.activityId,
subsId = rule.subsItem.id,
subsVersion = rule.rawSubs.version,
groupKey = rule.g.group.key,
groupType = when (rule) {
is AppRule -> SubsConfig.AppGroupType
is GlobalRule -> SubsConfig.GlobalGroupType
},
ruleIndex = rule.index,
ruleKey = rule.key,
)
DbSet.clickLogDao.insert(clickLog)
if (recordStoreFlow.value.clickCount % 100 == 0) {
DbSet.clickLogDao.deleteKeepLatest()
}
suspend fun insertClickLog(rule: ResolvedRule) {
clickLogMutex.withLock {
increaseClickCount()
val clickLog = ClickLog(
appId = topActivityFlow.value.appId,
activityId = topActivityFlow.value.activityId,
subsId = rule.subsItem.id,
subsVersion = rule.rawSubs.version,
groupKey = rule.g.group.key,
groupType = when (rule) {
is AppRule -> SubsConfig.AppGroupType
is GlobalRule -> SubsConfig.GlobalGroupType
},
ruleIndex = rule.index,
ruleKey = rule.key,
)
DbSet.clickLogDao.insert(clickLog)
if (recordStoreFlow.value.clickCount % 100 == 0) {
DbSet.clickLogDao.deleteKeepLatest()
}
}
}
90 changes: 59 additions & 31 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import li.songe.gkd.BuildConfig
import li.songe.gkd.appScope
import li.songe.gkd.composition.CompositionAbService
import li.songe.gkd.composition.CompositionExt.useLifeCycleLog
import li.songe.gkd.composition.CompositionExt.useScope
Expand Down Expand Up @@ -131,18 +132,26 @@ class GkdAbService : CompositionAbService({
queryThread.cancel()
actionThread.cancel()
}
val events = mutableListOf<AccessibilityNodeInfo>()
var queryTaskJob: Job? = null
fun newQueryTask(eventNode: AccessibilityNodeInfo? = null) {
if (!storeFlow.value.enableService) return
val ctx = if (System.currentTimeMillis() - appChangeTime < 5000L) {
Dispatchers.IO
} else {
queryThread
}
val common = ctx === queryThread
queryTaskJob = scope.launchTry(ctx) {
fun newQueryTask(byEvent: Boolean = false) {
queryTaskJob = scope.launchTry(queryThread) {
var latestEvent = synchronized(events) {
val size = events.size
if (size == 0 && byEvent) return@launchTry
val pair = if (size > 1) {
if (BuildConfig.DEBUG) {
Log.d("latestEvent", "丢弃事件=$size")
}
null
} else {
events.lastOrNull()
}
events.clear()
pair
}
val activityRule = getAndUpdateCurrentRules()
for (rule in (activityRule.currentRules)) {
for (rule in (activityRule.currentRules)) { // 规则数量有可能过多导致耗时过长
val statusCode = rule.status
if (statusCode == RuleStatus.Status3 && rule.matchDelayJob == null) {
rule.matchDelayJob = scope.launch(queryThread) {
Expand All @@ -152,7 +161,20 @@ class GkdAbService : CompositionAbService({
}
}
if (statusCode != RuleStatus.StatusOk) continue
val nodeVal = (eventNode ?: safeActiveWindow) ?: continue
latestEvent?.let { n ->
val refreshOk = try {
n.refresh()
} catch (e: Exception) {
false
}
if (!refreshOk) {
if (BuildConfig.DEBUG) {
Log.d("latestEvent", "最新事件已过期")
}
latestEvent = null
}
}
val nodeVal = (latestEvent ?: safeActiveWindow) ?: continue
val rightAppId = nodeVal.packageName?.toString() ?: break
val matchApp = rule.matchActivity(
rightAppId
Expand All @@ -172,11 +194,8 @@ class GkdAbService : CompositionAbService({
return@launchTry
}
if (!matchApp) continue
val target =
rule.query(nodeVal, if (common) defaultCacheTransform else null)
if (common) {
defaultCacheTransform.indexCache.clear()
}
val target = rule.query(nodeVal, defaultCacheTransform)
defaultCacheTransform.indexCache.clear()
if (target == null) {
continue
}
Expand All @@ -189,20 +208,20 @@ class GkdAbService : CompositionAbService({
}
continue
}
scope.launch(actionThread) {
if (rule.status != RuleStatus.StatusOk) return@launch
val actionResult = rule.performAction(context, target)
if (actionResult.result) {
rule.trigger()
scope.launch(actionThread) {
delay(300)
if (queryTaskJob?.isActive != true) {
newQueryTask()
}
}
if (storeFlow.value.toastWhenClick) {
toast(storeFlow.value.clickToast)
if (rule.status != RuleStatus.StatusOk) break
val actionResult = rule.performAction(context, target)
if (actionResult.result) {
rule.trigger()
scope.launch(actionThread) {
delay(300)
if (queryTaskJob?.isActive != true) {
newQueryTask()
}
}
if (storeFlow.value.toastWhenClick) {
toast(storeFlow.value.clickToast)
}
appScope.launchTry(Dispatchers.IO) {
insertClickLog(rule)
LogUtils.d(
rule.statusText(),
Expand Down Expand Up @@ -311,8 +330,17 @@ class GkdAbService : CompositionAbService({
if (evAppId != rightAppId) {
return@launch
}

newQueryTask(eventNode)
if (!storeFlow.value.enableService) return@launch
synchronized(events) {
val eventLog = events.lastOrNull()
if (eventNode != null) {
if (eventLog == eventNode) {
events.removeLast()
}
events.add(eventNode)
}
}
newQueryTask(eventNode != null)
}
}

Expand Down

0 comments on commit 6206b86

Please sign in to comment.