diff --git a/lawnchair/src/app/lawnchair/flowerpot/Flowerpot.kt b/lawnchair/src/app/lawnchair/flowerpot/Flowerpot.kt index 1bdc5d2537..0bad9d5943 100644 --- a/lawnchair/src/app/lawnchair/flowerpot/Flowerpot.kt +++ b/lawnchair/src/app/lawnchair/flowerpot/Flowerpot.kt @@ -152,12 +152,24 @@ class Flowerpot(private val context: Context, val name: String, private val load fun categorizeApps(appList: List?): Map> { val categorizedApps = mutableMapOf>() + val categorizedAppKeys = mutableSetOf() + 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() } diff --git a/lawnchair/src/app/lawnchair/flowerpot/FlowerpotApps.kt b/lawnchair/src/app/lawnchair/flowerpot/FlowerpotApps.kt index 08489014df..e79a5cf308 100644 --- a/lawnchair/src/app/lawnchair/flowerpot/FlowerpotApps.kt +++ b/lawnchair/src/app/lawnchair/flowerpot/FlowerpotApps.kt @@ -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() + private val codeRules = mutableListOf() val categorizedApps = mutableMapOf>() init { populateIntentMatches() + populateCodeRules() } fun updateAppList(appList: List?) { @@ -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() + .mapNotNull { runCatching { CodeRules.get(it.rule, *it.args) }.getOrNull() } + .forEach { codeRules.add(it) } + } }