Skip to content

Commit

Permalink
feat: 自定义禁用+matchSystemApp
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jan 19, 2024
1 parent 3e446ba commit 7768bd4
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 127 deletions.
1 change: 1 addition & 0 deletions app/src/main/kotlin/li/songe/gkd/data/AppInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ data class AppInfo(
val icon: Drawable,
val versionCode: Int,
val versionName: String,
val isSystem: Boolean,
)
5 changes: 3 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/data/AppRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class AppRule(
val appId = app.id
val activityIds = getFixActivityIds(app.id, rule.activityIds ?: group.activityIds)
val excludeActivityIds =
getFixActivityIds(
(getFixActivityIds(
app.id,
rule.excludeActivityIds ?: group.excludeActivityIds
) + (excludeData.activityIds.filter { e -> e.first == appId }.map { e -> e.second })
) + (excludeData.activityIds.filter { e -> e.first == appId }
.map { e -> e.second })).distinct()

override val type = "app"
override fun matchActivity(appId: String, activityId: String?): Boolean {
Expand Down
22 changes: 20 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/data/GlobalRule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package li.songe.gkd.data

import li.songe.gkd.service.launcherAppId
import li.songe.gkd.util.systemAppsFlow

data class GlobalApp(
val id: String,
Expand All @@ -25,6 +26,7 @@ class GlobalRule(

val matchAnyApp = rule.matchAnyApp ?: group.matchAnyApp ?: true
val matchLauncher = rule.matchLauncher ?: group.matchLauncher ?: false
val matchSystemApp = rule.matchSystemApp ?: group.matchSystemApp ?: false
val apps = mutableMapOf<String, GlobalApp>().apply {
(rule.apps ?: group.apps ?: emptyList()).forEach { a ->
this[a.id] = GlobalApp(
Expand All @@ -40,11 +42,27 @@ class GlobalRule(

private val excludeAppIds = apps.filter { e -> !e.value.enable }.keys
override fun matchActivity(appId: String, activityId: String?): Boolean {
if (!matchLauncher && appId == launcherAppId) return false
if (!super.matchActivity(appId, activityId)) return false
// 规则自带禁用
if (excludeAppIds.contains(appId)) {
return false
}
// 用户自定义禁用
if (excludeData.excludeAppIds.contains(appId)) {
return false
}
if (excludeData.includeAppIds.contains(appId)) {
activityId ?: return true
val app = apps[appId] ?: return true
return !app.excludeActivityIds.any { e -> e.startsWith(activityId) }
} else if (activityId != null && excludeData.activityIds.contains(appId to activityId)) {
return false
}
if (!matchLauncher && appId == launcherAppId) {
return false
}
if (!matchSystemApp && systemAppsFlow.value.contains(appId)) {
return false
}
val app = apps[appId] ?: return matchAnyApp
activityId ?: return true
if (app.excludeActivityIds.any { e -> e.startsWith(activityId) }) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/RawSubscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ data class RawSubscription(

interface RawGlobalRuleProps {
val matchAnyApp: Boolean?
val matchSystemApp: Boolean?
val matchLauncher: Boolean?
val apps: List<RawGlobalApp>?
}
Expand Down Expand Up @@ -157,6 +158,7 @@ data class RawSubscription(
override val snapshotUrls: List<String>?,
override val exampleUrls: List<String>?,
override val matchAnyApp: Boolean?,
override val matchSystemApp: Boolean?,
override val matchLauncher: Boolean?,
override val apps: List<RawGlobalApp>?,
override val rules: List<RawGlobalRule>,
Expand Down Expand Up @@ -216,6 +218,7 @@ data class RawSubscription(
override val matches: List<String>,
override val excludeMatches: List<String>?,
override val matchAnyApp: Boolean?,
override val matchSystemApp: Boolean?,
override val matchLauncher: Boolean?,
override val apps: List<RawGlobalApp>?
) : RawRuleProps, RawGlobalRuleProps
Expand Down Expand Up @@ -485,6 +488,7 @@ data class RawSubscription(
actionMaximumKey = getInt(jsonObject, "actionMaximumKey"),
actionCdKey = getInt(jsonObject, "actionCdKey"),
matchAnyApp = getBoolean(jsonObject, "matchAnyApp"),
matchSystemApp = getBoolean(jsonObject, "matchSystemApp"),
matchLauncher = getBoolean(jsonObject, "matchLauncher"),
apps = jsonObject["apps"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToGlobalApp(
Expand Down Expand Up @@ -515,6 +519,7 @@ data class RawSubscription(
exampleUrls = getStringIArray(jsonObject, "exampleUrls"),
actionMaximumKey = getInt(jsonObject, "actionMaximumKey"),
actionCdKey = getInt(jsonObject, "actionCdKey"),
matchSystemApp = getBoolean(jsonObject, "matchSystemApp"),
matchAnyApp = getBoolean(jsonObject, "matchAnyApp"),
matchLauncher = getBoolean(jsonObject, "matchLauncher"),
apps = jsonObject["apps"]?.jsonArray?.mapIndexed { index, jsonElement ->
Expand Down
12 changes: 3 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/data/ResolvedRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,9 @@ sealed class ResolvedRule(
val excludeData = ExcludeData.parse(exclude)

abstract val type: String
open fun matchActivity(appId: String, activityId: String? = null): Boolean {
if (excludeData.appIds.contains(appId)) {
return false
}
activityId ?: return true
return !excludeData.activityIds.any { e ->
e.first == appId && activityId.startsWith(e.second)
}
}

// 范围越精确, 优先级越高
abstract fun matchActivity(appId: String, activityId: String? = null): Boolean

}

Expand Down
49 changes: 33 additions & 16 deletions app/src/main/kotlin/li/songe/gkd/data/SubsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,35 @@ data class SubsConfig(
}

data class ExcludeData(
val appIds: Set<String>, val activityIds: Set<Pair<String, String>>
val appIds: Map<String, Boolean>,
val activityIds: Set<Pair<String, String>>,
) {
val excludeAppIds = appIds.entries.filter { e -> e.value }.map { e -> e.key }.toSet()
val includeAppIds = appIds.entries.filter { e -> !e.value }.map { e -> e.key }.toSet()

companion object {
fun parse(exclude: String?): ExcludeData {
val appIds = mutableSetOf<String>()
val appIds = mutableMapOf<String, Boolean>()
val activityIds = mutableSetOf<Pair<String, String>>()
(exclude ?: "").split('\n', ',')
.filter { s -> s.isNotBlank() && s.count { c -> c == '/' } <= 1 }.forEach { s ->
val a = s.split('/')
val appId = a[0]
val activityId = a.getOrNull(1)
if (activityId != null) {
activityIds.add(appId to activityId)
(exclude ?: "").split('\n', ',').filter { s -> s.isNotBlank() }.map { s -> s.trim() }
.forEach { s ->
if (s[0] == '!') {
appIds[s.substring(1)] = false
} else {
appIds.add(appId)
val a = s.split('/')
val appId = a[0]
val activityId = a.getOrNull(1)
if (activityId != null) {
activityIds.add(appId to activityId)
} else {
appIds[appId] = true
}
}
}
return ExcludeData(appIds = appIds, activityIds = activityIds)
return ExcludeData(
appIds = appIds,
activityIds = activityIds,
)
}

fun parse(appId: String, exclude: String?): ExcludeData {
Expand All @@ -109,7 +120,13 @@ data class ExcludeData(
}

fun ExcludeData.stringify(): String {
return (appIds + activityIds.map { e -> "${e.first}/${e.second}" }).sorted().joinToString("\n")
return (appIds.entries.map { e ->
if (e.value) {
e.key
} else {
"!${e.key}"
}
} + activityIds.map { e -> "${e.first}/${e.second}" }).sorted().joinToString("\n")
}

fun ExcludeData.stringify(appId: String): String {
Expand All @@ -120,11 +137,11 @@ fun ExcludeData.stringify(appId: String): String {
fun ExcludeData.switch(appId: String, activityId: String? = null): ExcludeData {
return if (activityId == null) {
copy(
appIds = appIds.toMutableSet().apply {
if (contains(appId)) {
remove(appId)
appIds = appIds.toMutableMap().apply {
if (get(appId) == true) {
set(appId, false)
} else {
add(appId)
set(appId, true)
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/li/songe/gkd/ui/DebugPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ fun DebugPage() {
Divider()
TextSwitch(
name = "截屏快照",
desc = "当用户截屏时保存快照(需手动替换快照图片),仅支持部分MIUI14",
desc = "当用户截屏时保存快照(需手动替换快照图片),仅支持部分小米设备",
checked = store.captureScreenshot
) {
updateStorage(
Expand Down
Loading

0 comments on commit 7768bd4

Please sign in to comment.