Skip to content

Commit

Permalink
feat: 新增 matchLauncher 字段
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jan 3, 2024
1 parent 11388c8 commit f83e32b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import li.songe.gkd.composition.CompositionActivity
import li.songe.gkd.composition.CompositionExt.useLifeCycleLog
import li.songe.gkd.service.updateLauncherAppId
import li.songe.gkd.ui.NavGraphs
import li.songe.gkd.ui.component.ConfirmDialog
import li.songe.gkd.ui.theme.AppTheme
Expand Down Expand Up @@ -44,6 +45,9 @@ class MainActivity : CompositionActivity({
}
}

// 每次打开页面更新记录桌面 appId
updateLauncherAppId()

setContent {
val navController = rememberNavController()

Expand Down
3 changes: 3 additions & 0 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.TopActivity
import li.songe.gkd.service.launcherAppId

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

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

private val excludeAppIds = apps.filter { e -> !e.value.enable }.keys
override fun matchActivity(topActivity: TopActivity): Boolean {
if (!matchLauncher && topActivity.appId == launcherAppId) return false
if (!super.matchActivity(topActivity)) return false
if (excludeAppIds.contains(topActivity.appId)) {
return false
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 @@ -129,6 +129,7 @@ data class RawSubscription(

interface RawGlobalRuleProps {
val matchAnyApp: Boolean?
val matchLauncher: Boolean?
val apps: List<RawGlobalApp>?
}

Expand Down Expand Up @@ -160,6 +161,7 @@ data class RawSubscription(
override val snapshotUrls: List<String>?,
override val exampleUrls: List<String>?,
override val matchAnyApp: Boolean?,
override val matchLauncher: Boolean?,
override val apps: List<RawGlobalApp>?,
override val rules: List<RawGlobalRule>,
) : RawGroupProps, RawGlobalRuleProps {
Expand Down Expand Up @@ -211,6 +213,7 @@ data class RawSubscription(
override val matches: List<String>,
override val excludeMatches: List<String>?,
override val matchAnyApp: Boolean?,
override val matchLauncher: Boolean?,
override val apps: List<RawGlobalApp>?
) : RawRuleProps, RawGlobalRuleProps

Expand Down Expand Up @@ -472,6 +475,7 @@ data class RawSubscription(
actionMaximumKey = getInt(jsonObject, "actionMaximumKey"),
actionCdKey = getInt(jsonObject, "actionCdKey"),
matchAnyApp = getBoolean(jsonObject, "matchAnyApp"),
matchLauncher = getBoolean(jsonObject, "matchLauncher"),
apps = jsonObject["apps"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToGlobalApp(
jsonElement.jsonObject, index
Expand Down Expand Up @@ -502,6 +506,7 @@ data class RawSubscription(
actionMaximumKey = getInt(jsonObject, "actionMaximumKey"),
actionCdKey = getInt(jsonObject, "actionCdKey"),
matchAnyApp = getBoolean(jsonObject, "matchAnyApp"),
matchLauncher = getBoolean(jsonObject, "matchLauncher"),
apps = jsonObject["apps"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToGlobalApp(
jsonElement.jsonObject, index
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import li.songe.gkd.app
import li.songe.gkd.appScope
import li.songe.gkd.data.AppRule
import li.songe.gkd.data.ClickLog
Expand All @@ -12,6 +13,7 @@ import li.songe.gkd.data.ResolvedRule
import li.songe.gkd.data.SubsConfig
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.AllRules
import li.songe.gkd.util.Ext.getDefaultLauncherAppId
import li.songe.gkd.util.allRulesFlow
import li.songe.gkd.util.increaseClickCount
import li.songe.gkd.util.launchTry
Expand Down Expand Up @@ -103,6 +105,10 @@ fun getCurrentRules(): ActivityRule {
var lastTriggerRule: ResolvedRule? = null
var lastTriggerTime = 0L
var appChangeTime = 0L
var launcherAppId = ""
fun updateLauncherAppId() {
launcherAppId = app.packageManager.getDefaultLauncherAppId() ?: ""
}

val clickLogMutex = Mutex()
fun insertClickLog(rule: ResolvedRule) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import kotlin.coroutines.suspendCoroutine
@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
class GkdAbService : CompositionAbService({
useLifeCycleLog()
updateLauncherAppId()

val context = this as GkdAbService
val scope = useScope()
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/util/Ext.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package li.songe.gkd.util

import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
Expand Down Expand Up @@ -46,4 +47,11 @@ object Ext {
return localAddresses
}

fun PackageManager.getDefaultLauncherAppId(): String? {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME)
val defaultLauncher = this.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
return defaultLauncher?.activityInfo?.packageName
}

}

0 comments on commit f83e32b

Please sign in to comment.