fix(flowerpot): prevent duplicate apps and categorize all apps

- Track categorized apps using ComponentKey to prevent duplicates
- Add uncategorized apps to "Other" category
- Enhance rule matching to support CodeRules
This commit is contained in:
MrSluffy
2025-11-15 15:38:16 +08:00
parent 5811b2d27f
commit 6de34e5d74
2 changed files with 32 additions and 4 deletions

View File

@@ -152,12 +152,24 @@ class Flowerpot(private val context: Context, val name: String, private val load
fun categorizeApps(appList: List<AppInfo?>?): Map<String, List<AppInfo>> {
val categorizedApps = mutableMapOf<String, MutableList<AppInfo>>()
val categorizedAppKeys = mutableSetOf<String>()
val validAppList = appList?.filterNotNull() ?: emptyList()
pots.values.forEach { pot ->
val potCategories = pot.categorizeApps(appList)
potCategories.forEach { (category, apps) ->
categorizedApps.getOrPut(category) { mutableListOf() }.addAll(apps)
pot.categorizeApps(appList).forEach { (category, apps) ->
apps.forEach { app ->
app.toComponentKey().toString().takeIf { it !in categorizedAppKeys }?.let { key ->
categorizedApps.getOrPut(category) { mutableListOf() }.add(app)
categorizedAppKeys.add(key)
}
}
}
}
validAppList.filter { it.toComponentKey().toString() !in categorizedAppKeys }
.takeIf { it.isNotEmpty() }
?.let { categorizedApps["Other"] = it.toMutableList() }
return categorizedApps.toSortedMap()
}

View File

@@ -19,15 +19,19 @@ package app.lawnchair.flowerpot
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import app.lawnchair.flowerpot.rules.CodeRules
import app.lawnchair.flowerpot.rules.Rules
import com.android.launcher3.model.data.AppInfo
class FlowerpotApps(private val context: Context, private val pot: Flowerpot) {
private val intentMatches = mutableSetOf<String>()
private val codeRules = mutableListOf<CodeRules>()
val categorizedApps = mutableMapOf<String, MutableList<AppInfo>>()
init {
populateIntentMatches()
populateCodeRules()
}
fun updateAppList(appList: List<AppInfo?>?) {
@@ -41,7 +45,7 @@ class FlowerpotApps(private val context: Context, private val pot: Flowerpot) {
.toMap()
val validPackages = appInfoMap.keys.filter { packageName ->
packageName in intentMatches || pot.rules.contains(Rules.Package(packageName))
matchesRules(packageName, appInfoMap[packageName]!!)
}
validPackages.forEach { packageName ->
@@ -50,6 +54,11 @@ class FlowerpotApps(private val context: Context, private val pot: Flowerpot) {
}
}
private fun matchesRules(packageName: String, appInfo: AppInfo): Boolean = packageName in intentMatches || pot.rules.contains(Rules.Package(packageName)) ||
codeRules.isNotEmpty() && runCatching {
codeRules.any { it.matches(context.packageManager.getApplicationInfo(packageName, 0)) }
}.getOrDefault(false)
private fun populateIntentMatches() {
intentMatches.clear()
@@ -64,4 +73,11 @@ class FlowerpotApps(private val context: Context, private val pot: Flowerpot) {
.mapNotNullTo(intentMatches) { it.activityInfo?.packageName }
}
}
private fun populateCodeRules() {
codeRules.clear()
pot.rules.filterIsInstance<Rules.CodeRule>()
.mapNotNull { runCatching { CodeRules.get(it.rule, *it.args) }.getOrNull() }
.forEach { codeRules.add(it) }
}
}