From 81ced236da408a17b592b6b6136bad7a06ee3539 Mon Sep 17 00:00:00 2001 From: lisonge Date: Mon, 4 Mar 2024 16:04:45 +0800 Subject: [PATCH] feat: forcedTime --- .../li/songe/gkd/data/RawSubscription.kt | 19 ++++++++++++++----- .../kotlin/li/songe/gkd/data/ResolvedRule.kt | 12 +++++++++--- .../li/songe/gkd/service/GkdAbService.kt | 18 ++++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt b/app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt index 40429b55b..e0745e32c 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt @@ -159,7 +159,7 @@ data class RawSubscription( } - interface RawCommonProps { + sealed interface RawCommonProps { val actionCd: Long? val actionDelay: Long? val quickFind: Boolean? @@ -170,11 +170,12 @@ data class RawSubscription( val actionCdKey: Int? val actionMaximumKey: Int? val order: Int? + val forcedTime: Long? val snapshotUrls: List? val exampleUrls: List? } - interface RawRuleProps : RawCommonProps { + sealed interface RawRuleProps : RawCommonProps { val name: String? val key: Int? val preKeys: List? @@ -185,7 +186,7 @@ data class RawSubscription( } @Immutable - interface RawGroupProps : RawCommonProps { + sealed interface RawGroupProps : RawCommonProps { val name: String val key: Int val desc: String? @@ -198,7 +199,7 @@ data class RawSubscription( val allExampleUrls: List } - interface RawAppRuleProps { + sealed interface RawAppRuleProps { val activityIds: List? val excludeActivityIds: List? @@ -208,7 +209,7 @@ data class RawSubscription( val excludeVersionCodes: List? } - interface RawGlobalRuleProps { + sealed interface RawGlobalRuleProps { val matchAnyApp: Boolean? val matchSystemApp: Boolean? val matchLauncher: Boolean? @@ -246,6 +247,7 @@ data class RawSubscription( override val actionCdKey: Int?, override val actionMaximumKey: Int?, override val order: Int?, + override val forcedTime: Long?, override val snapshotUrls: List?, override val exampleUrls: List?, override val matchAnyApp: Boolean?, @@ -283,6 +285,7 @@ data class RawSubscription( override val actionCdKey: Int?, override val actionMaximumKey: Int?, override val order: Int?, + override val forcedTime: Long?, override val snapshotUrls: List?, override val exampleUrls: List?, override val name: String?, @@ -313,6 +316,7 @@ data class RawSubscription( override val quickFind: Boolean?, override val actionMaximum: Int?, override val order: Int?, + override val forcedTime: Long?, override val matchDelay: Long?, override val matchTime: Long?, override val resetMatch: String?, @@ -356,6 +360,7 @@ data class RawSubscription( override val quickFind: Boolean?, override val actionMaximum: Int?, override val order: Int?, + override val forcedTime: Long?, override val matchDelay: Long?, override val matchTime: Long?, override val resetMatch: String?, @@ -578,6 +583,7 @@ data class RawSubscription( versionNames = getStringIArray(jsonObject, "versionNames"), excludeVersionNames = getStringIArray(jsonObject, "excludeVersionNames"), position = getPosition(jsonObject), + forcedTime = getLong(jsonObject, "forcedTime"), ) } @@ -614,6 +620,7 @@ data class RawSubscription( actionMaximumKey = getInt(jsonObject, "actionMaximumKey"), actionCdKey = getInt(jsonObject, "actionCdKey"), order = getInt(jsonObject, "order"), + forcedTime = getLong(jsonObject, "forcedTime"), scopeKeys = getIntIArray(jsonObject, "scopeKeys"), versionCodes = getLongIArray(jsonObject, "versionCodes"), excludeVersionCodes = getLongIArray(jsonObject, "excludeVersionCodes"), @@ -678,6 +685,7 @@ data class RawSubscription( excludeMatches = getStringIArray(jsonObject, "excludeMatches"), matches = getStringIArray(jsonObject, "matches") ?: error("miss matches"), order = getInt(jsonObject, "order"), + forcedTime = getLong(jsonObject, "forcedTime"), position = getPosition(jsonObject), ) } @@ -713,6 +721,7 @@ data class RawSubscription( } ?: emptyList(), order = getInt(jsonObject, "order"), scopeKeys = getIntIArray(jsonObject, "scopeKeys"), + forcedTime = getLong(jsonObject, "forcedTime"), ) } diff --git a/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt b/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt index 9a982f7b9..31359418a 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt @@ -22,13 +22,16 @@ sealed class ResolvedRule( val key = rule.key val index = group.rules.indexOf(rule) private val preKeys = (rule.preKeys ?: emptyList()).toSet() - private val resetMatch = rule.resetMatch ?: group.resetMatch private val matches = rule.matches?.map { s -> Selector.parse(s) } ?: emptyList() private val excludeMatches = (rule.excludeMatches ?: emptyList()).map { s -> Selector.parse(s) } + + private val resetMatch = rule.resetMatch ?: group.resetMatch val matchDelay = rule.matchDelay ?: group.matchDelay ?: 0L val actionDelay = rule.actionDelay ?: group.actionDelay ?: 0L private val matchTime = rule.matchTime ?: group.matchTime + private val forcedTime = rule.forcedTime ?: group.forcedTime ?: 0L private val quickFind = rule.quickFind ?: group.quickFind ?: false + val order = rule.order ?: group.order ?: 0 private val actionCdKey = rule.actionCdKey ?: group.actionCdKey private val actionCd = rule.actionCd ?: if (actionCdKey != null) { @@ -44,8 +47,6 @@ sealed class ResolvedRule( null } ?: group.actionMaximum - val order = rule.order ?: group.order ?: 0 - private val slowSelectors by lazy { (matches + excludeMatches).filterNot { s -> ((quickFind && s.canQf) || s.isMatchRoot) && !s.connectKeys.contains( @@ -101,6 +102,11 @@ sealed class ResolvedRule( return false } + fun checkForced(): Boolean { + if (forcedTime <= 0) return false + return System.currentTimeMillis() < matchChangedTime + matchDelay + forcedTime + } + private var actionTriggerTime = Value(0L) fun trigger() { actionTriggerTime.value = System.currentTimeMillis() diff --git a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt index 538a9f205..cee701ec1 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt @@ -134,7 +134,7 @@ class GkdAbService : CompositionAbService({ } val events = mutableListOf() var queryTaskJob: Job? = null - fun newQueryTask(byEvent: Boolean = false) { + fun newQueryTask(byEvent: Boolean = false, byForced: Boolean = false) { queryTaskJob = scope.launchTry(queryThread) { var latestEvent = synchronized(events) { val size = events.size @@ -161,6 +161,7 @@ class GkdAbService : CompositionAbService({ } } if (statusCode != RuleStatus.StatusOk) continue + if (byForced && !rule.checkForced()) continue latestEvent?.let { n -> val refreshOk = try { n.refresh() @@ -189,6 +190,10 @@ class GkdAbService : CompositionAbService({ topActivityFlow.value = TopActivity(appId = rightAppId) } getAndUpdateCurrentRules() + if (queryTaskJob?.isActive != true) { + Thread.sleep(300) + newQueryTask() + } } } return@launchTry @@ -239,11 +244,20 @@ class GkdAbService : CompositionAbService({ newQueryTask() } } + } else { + if (activityRule.currentRules.any { r -> r.checkForced() && r.status.let { s -> s == RuleStatus.StatusOk || s == RuleStatus.Status5 } }) { + scope.launch(actionThread) { + delay(300) + if (queryTaskJob?.isActive != true) { + newQueryTask(byForced = true) + } + } + } } } } - val skipAppIds = listOf("com.android.systemui") + val skipAppIds = setOf("com.android.systemui") onAccessibilityEvent { event -> if (event.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && skipAppIds.contains(event.packageName.toString())