Skip to content

Commit

Permalink
feat: forcedTime
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Mar 4, 2024
1 parent 8c38829 commit 81ced23
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
19 changes: 14 additions & 5 deletions app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ data class RawSubscription(
}


interface RawCommonProps {
sealed interface RawCommonProps {
val actionCd: Long?
val actionDelay: Long?
val quickFind: Boolean?
Expand All @@ -170,11 +170,12 @@ data class RawSubscription(
val actionCdKey: Int?
val actionMaximumKey: Int?
val order: Int?
val forcedTime: Long?
val snapshotUrls: List<String>?
val exampleUrls: List<String>?
}

interface RawRuleProps : RawCommonProps {
sealed interface RawRuleProps : RawCommonProps {
val name: String?
val key: Int?
val preKeys: List<Int>?
Expand All @@ -185,7 +186,7 @@ data class RawSubscription(
}

@Immutable
interface RawGroupProps : RawCommonProps {
sealed interface RawGroupProps : RawCommonProps {
val name: String
val key: Int
val desc: String?
Expand All @@ -198,7 +199,7 @@ data class RawSubscription(
val allExampleUrls: List<String>
}

interface RawAppRuleProps {
sealed interface RawAppRuleProps {
val activityIds: List<String>?
val excludeActivityIds: List<String>?

Expand All @@ -208,7 +209,7 @@ data class RawSubscription(
val excludeVersionCodes: List<Long>?
}

interface RawGlobalRuleProps {
sealed interface RawGlobalRuleProps {
val matchAnyApp: Boolean?
val matchSystemApp: Boolean?
val matchLauncher: Boolean?
Expand Down Expand Up @@ -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<String>?,
override val exampleUrls: List<String>?,
override val matchAnyApp: Boolean?,
Expand Down Expand Up @@ -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<String>?,
override val exampleUrls: List<String>?,
override val name: String?,
Expand Down Expand Up @@ -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?,
Expand Down Expand Up @@ -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?,
Expand Down Expand Up @@ -578,6 +583,7 @@ data class RawSubscription(
versionNames = getStringIArray(jsonObject, "versionNames"),
excludeVersionNames = getStringIArray(jsonObject, "excludeVersionNames"),
position = getPosition(jsonObject),
forcedTime = getLong(jsonObject, "forcedTime"),
)
}

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
)
}
Expand Down Expand Up @@ -713,6 +721,7 @@ data class RawSubscription(
} ?: emptyList(),
order = getInt(jsonObject, "order"),
scopeKeys = getIntIArray(jsonObject, "scopeKeys"),
forcedTime = getLong(jsonObject, "forcedTime"),
)
}

Expand Down
12 changes: 9 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class GkdAbService : CompositionAbService({
}
val events = mutableListOf<AccessibilityNodeInfo>()
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
Expand All @@ -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()
Expand Down Expand Up @@ -189,6 +190,10 @@ class GkdAbService : CompositionAbService({
topActivityFlow.value = TopActivity(appId = rightAppId)
}
getAndUpdateCurrentRules()
if (queryTaskJob?.isActive != true) {
Thread.sleep(300)
newQueryTask()
}
}
}
return@launchTry
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 81ced23

Please sign in to comment.