feat: anyMatches

This commit is contained in:
lisonge 2024-06-12 21:59:58 +08:00
parent e8a93e644c
commit 89e87cbd0c
2 changed files with 39 additions and 55 deletions

View File

@ -1,7 +1,6 @@
package li.songe.gkd.data package li.songe.gkd.data
import android.graphics.Rect import android.graphics.Rect
import androidx.compose.runtime.Immutable
import com.blankj.utilcode.util.LogUtils import com.blankj.utilcode.util.LogUtils
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonArray
@ -23,7 +22,7 @@ import li.songe.selector.Selector
import net.objecthunter.exp4j.Expression import net.objecthunter.exp4j.Expression
import net.objecthunter.exp4j.ExpressionBuilder import net.objecthunter.exp4j.ExpressionBuilder
@Immutable
@Serializable @Serializable
data class RawSubscription( data class RawSubscription(
val id: Long, val id: Long,
@ -105,7 +104,7 @@ data class RawSubscription(
} }
} }
@Immutable
@Serializable @Serializable
data class RawApp( data class RawApp(
val id: String, val id: String,
@ -113,17 +112,14 @@ data class RawSubscription(
val groups: List<RawAppGroup> = emptyList(), val groups: List<RawAppGroup> = emptyList(),
) )
@Immutable
@Serializable @Serializable
data class RawCategory(val key: Int, val name: String, val enable: Boolean?) data class RawCategory(val key: Int, val name: String, val enable: Boolean?)
@Immutable
@Serializable @Serializable
data class Position( data class Position(
val left: String?, val left: String?, val top: String?, val right: String?, val bottom: String?
val top: String?,
val right: String?,
val bottom: String?
) { ) {
private val leftExp by lazy { getExpression(left) } private val leftExp by lazy { getExpression(left) }
private val topExp by lazy { getExpression(top) } private val topExp by lazy { getExpression(top) }
@ -140,10 +136,7 @@ data class RawSubscription(
fun calc(rect: Rect): Pair<Float, Float>? { fun calc(rect: Rect): Pair<Float, Float>? {
if (!isValid) return null if (!isValid) return null
arrayOf( arrayOf(
leftExp, leftExp, topExp, rightExp, bottomExp
topExp,
rightExp,
bottomExp
).forEach { exp -> ).forEach { exp ->
if (exp != null) { if (exp != null) {
setVariables(exp, rect) setVariables(exp, rect)
@ -204,9 +197,10 @@ data class RawSubscription(
val position: Position? val position: Position?
val matches: List<String>? val matches: List<String>?
val excludeMatches: List<String>? val excludeMatches: List<String>?
val anyMatches: List<String>?
} }
@Immutable
sealed interface RawGroupProps : RawCommonProps { sealed interface RawGroupProps : RawCommonProps {
val name: String val name: String
val key: Int val key: Int
@ -237,7 +231,7 @@ data class RawSubscription(
val apps: List<RawGlobalApp>? val apps: List<RawGlobalApp>?
} }
@Immutable
@Serializable @Serializable
data class RawGlobalApp( data class RawGlobalApp(
val id: String, val id: String,
@ -250,7 +244,7 @@ data class RawSubscription(
override val excludeVersionCodes: List<Long>?, override val excludeVersionCodes: List<Long>?,
) : RawAppRuleProps ) : RawAppRuleProps
@Immutable
@Serializable @Serializable
data class RawGlobalGroup( data class RawGlobalGroup(
override val name: String, override val name: String,
@ -293,7 +287,7 @@ data class RawSubscription(
} }
} }
@Immutable
@Serializable @Serializable
data class RawGlobalRule( data class RawGlobalRule(
override val actionCd: Long?, override val actionCd: Long?,
@ -314,15 +308,15 @@ data class RawSubscription(
override val preKeys: List<Int>?, override val preKeys: List<Int>?,
override val action: String?, override val action: String?,
override val position: Position?, override val position: Position?,
override val matches: List<String>, override val matches: List<String>?,
override val excludeMatches: List<String>?, override val excludeMatches: List<String>?,
override val anyMatches: List<String>?,
override val matchAnyApp: Boolean?, override val matchAnyApp: Boolean?,
override val matchSystemApp: Boolean?, override val matchSystemApp: Boolean?,
override val matchLauncher: Boolean?, override val matchLauncher: Boolean?,
override val apps: List<RawGlobalApp>? override val apps: List<RawGlobalApp>?
) : RawRuleProps, RawGlobalRuleProps ) : RawRuleProps, RawGlobalRuleProps
@Immutable
@Serializable @Serializable
data class RawAppGroup( data class RawAppGroup(
override val name: String, override val name: String,
@ -363,7 +357,6 @@ data class RawSubscription(
} }
} }
@Immutable
@Serializable @Serializable
data class RawAppRule( data class RawAppRule(
override val name: String?, override val name: String?,
@ -373,6 +366,7 @@ data class RawSubscription(
override val position: Position?, override val position: Position?,
override val matches: List<String>?, override val matches: List<String>?,
override val excludeMatches: List<String>?, override val excludeMatches: List<String>?,
override val anyMatches: List<String>?,
override val actionCdKey: Int?, override val actionCdKey: Int?,
override val actionMaximumKey: Int?, override val actionMaximumKey: Int?,
@ -400,9 +394,9 @@ data class RawSubscription(
companion object { companion object {
private fun RawGroupProps.getErrorDesc(): String? { private fun RawGroupProps.getErrorDesc(): String? {
val allSelectorStrings = val allSelectorStrings = rules.map { r ->
rules.map { r -> (r.matches ?: emptyList()) + (r.excludeMatches ?: emptyList()) } listOfNotNull(r.matches, r.excludeMatches, r.anyMatches).flatten()
.flatten() }.flatten()
val allSelector = allSelectorStrings.map { s -> Selector.parseOrNull(s) } val allSelector = allSelectorStrings.map { s -> Selector.parseOrNull(s) }
@ -423,12 +417,7 @@ data class RawSubscription(
} }
private val expVars = arrayOf( private val expVars = arrayOf(
"left", "left", "top", "right", "bottom", "width", "height"
"top",
"right",
"bottom",
"width",
"height"
) )
private fun setVariables(exp: Expression, rect: Rect) { private fun setVariables(exp: Expression, rect: Rect) {
@ -481,8 +470,7 @@ data class RawSubscription(
} }
private fun getStringIArray( private fun getStringIArray(
jsonObject: JsonObject? = null, jsonObject: JsonObject? = null, name: String
name: String
): List<String>? { ): List<String>? {
return when (val element = jsonObject?.get(name)) { return when (val element = jsonObject?.get(name)) {
JsonNull, null -> null JsonNull, null -> null
@ -583,6 +571,7 @@ data class RawSubscription(
excludeActivityIds = getStringIArray(jsonObject, "excludeActivityIds"), excludeActivityIds = getStringIArray(jsonObject, "excludeActivityIds"),
matches = getStringIArray(jsonObject, "matches"), matches = getStringIArray(jsonObject, "matches"),
excludeMatches = getStringIArray(jsonObject, "excludeMatches"), excludeMatches = getStringIArray(jsonObject, "excludeMatches"),
anyMatches = getStringIArray(jsonObject, "anyMatches"),
key = getInt(jsonObject, "key"), key = getInt(jsonObject, "key"),
name = getString(jsonObject, "name"), name = getString(jsonObject, "name"),
actionCd = getLong(jsonObject, "actionCd") ?: getLong(jsonObject, "cd"), actionCd = getLong(jsonObject, "actionCd") ?: getLong(jsonObject, "cd"),
@ -704,7 +693,8 @@ data class RawSubscription(
action = getString(jsonObject, "action"), action = getString(jsonObject, "action"),
preKeys = getIntIArray(jsonObject, "preKeys"), preKeys = getIntIArray(jsonObject, "preKeys"),
excludeMatches = getStringIArray(jsonObject, "excludeMatches"), excludeMatches = getStringIArray(jsonObject, "excludeMatches"),
matches = getStringIArray(jsonObject, "matches") ?: error("miss matches"), matches = getStringIArray(jsonObject, "matches"),
anyMatches = getStringIArray(jsonObject, "anyMatches"),
order = getInt(jsonObject, "order"), order = getInt(jsonObject, "order"),
forcedTime = getLong(jsonObject, "forcedTime"), forcedTime = getLong(jsonObject, "forcedTime"),
position = getPosition(jsonObject), position = getPosition(jsonObject),
@ -713,8 +703,7 @@ data class RawSubscription(
private fun jsonToGlobalGroups(jsonObject: JsonObject, groupIndex: Int): RawGlobalGroup { private fun jsonToGlobalGroups(jsonObject: JsonObject, groupIndex: Int): RawGlobalGroup {
return RawGlobalGroup( return RawGlobalGroup(
key = getInt(jsonObject, "key") key = getInt(jsonObject, "key") ?: error("miss group[$groupIndex].key"),
?: error("miss group[$groupIndex].key"),
name = getString(jsonObject, "name") ?: error("miss group[$groupIndex].name"), name = getString(jsonObject, "name") ?: error("miss group[$groupIndex].name"),
desc = getString(jsonObject, "desc"), desc = getString(jsonObject, "desc"),
enable = getBoolean(jsonObject, "enable"), enable = getBoolean(jsonObject, "enable"),
@ -770,8 +759,7 @@ data class RawSubscription(
} ?: emptyList()), } ?: emptyList()),
globalGroups = (rootJson["globalGroups"]?.jsonArray?.mapIndexed { index, jsonElement -> globalGroups = (rootJson["globalGroups"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToGlobalGroups(jsonElement.jsonObject, index) jsonToGlobalGroups(jsonElement.jsonObject, index)
} ?: emptyList()) } ?: emptyList()))
)
} }
private fun <T> List<T>.findDuplicatedItem(predicate: (T) -> Any?): T? { private fun <T> List<T>.findDuplicatedItem(predicate: (T) -> Any?): T? {
@ -854,15 +842,4 @@ data class RawSubscription(
return g return g
} }
} }
} }

View File

@ -22,8 +22,9 @@ sealed class ResolvedRule(
val key = rule.key val key = rule.key
val index = group.rules.indexOfFirst { r -> r === rule } val index = group.rules.indexOfFirst { r -> r === rule }
private val preKeys = (rule.preKeys ?: emptyList()).toSet() private val preKeys = (rule.preKeys ?: emptyList()).toSet()
private val matches = rule.matches?.map { s -> Selector.parse(s) } ?: emptyList() private val matches = (rule.matches ?: emptyList()).map { s -> Selector.parse(s) }
private val excludeMatches = (rule.excludeMatches ?: emptyList()).map { s -> Selector.parse(s) } private val excludeMatches = (rule.excludeMatches ?: emptyList()).map { s -> Selector.parse(s) }
private val anyMatches = (rule.anyMatches ?: emptyList()).map { s -> Selector.parse(s) }
private val resetMatch = rule.resetMatch ?: group.resetMatch private val resetMatch = rule.resetMatch ?: group.resetMatch
val matchDelay = rule.matchDelay ?: group.matchDelay ?: 0L val matchDelay = rule.matchDelay ?: group.matchDelay ?: 0L
@ -48,7 +49,7 @@ sealed class ResolvedRule(
} ?: group.actionMaximum } ?: group.actionMaximum
private val slowSelectors by lazy { private val slowSelectors by lazy {
(matches + excludeMatches).filterNot { s -> (matches + excludeMatches + anyMatches).filterNot { s ->
((quickFind && s.canQf) || s.isMatchRoot) && !s.connectKeys.contains( ((quickFind && s.canQf) || s.isMatchRoot) && !s.connectKeys.contains(
"<<" "<<"
) )
@ -138,17 +139,23 @@ sealed class ResolvedRule(
try { try {
if (nodeInfo == null) return null if (nodeInfo == null) return null
var target: AccessibilityNodeInfo? = null var target: AccessibilityNodeInfo? = null
if (anyMatches.isNotEmpty()) {
for (selector in anyMatches) {
target = nodeInfo.querySelector(selector, quickFind, transform)
?: break
}
if (target == null) return null
}
for (selector in matches) { for (selector in matches) {
target = nodeInfo.querySelector(selector, quickFind, transform) target = nodeInfo.querySelector(selector, quickFind, transform)
?: return null ?: return null
} }
for (selector in excludeMatches) { for (selector in excludeMatches) {
if (nodeInfo.querySelector( nodeInfo.querySelector(
selector, selector,
quickFind, quickFind,
transform transform
) != null )?.let { return null }
) return null
} }
return target return target
} finally { } finally {