Skip to content

Commit

Permalink
perf: skip self app activity log in background
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Aug 4, 2024
1 parent 2644c0b commit a3d8a07
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.dylanc.activityresult.launcher.StartActivityLauncher
import com.ramcosta.composedestinations.DestinationsNavHost
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import li.songe.gkd.composition.CompositionActivity
import li.songe.gkd.composition.CompositionExt.useLifeCycleLog
Expand Down Expand Up @@ -103,8 +105,21 @@ class MainActivity : CompositionActivity({
updateServiceRunning()
}
}

override fun onStart() {
super.onStart()
activityVisibleFlow.update { it + 1 }
}

override fun onStop() {
super.onStop()
activityVisibleFlow.update { it - 1 }
}
}

private val activityVisibleFlow by lazy { MutableStateFlow(0) }
fun isActivityVisible() = activityVisibleFlow.value > 0

fun Activity.navToMainActivity() {
val intent = this.intent?.cloneFilter()
if (intent != null) {
Expand All @@ -116,6 +131,7 @@ fun Activity.navToMainActivity() {
finish()
}


private fun updateServiceRunning() {
ManageService.isRunning.value = ServiceUtils.isServiceRunning(ManageService::class.java)
GkdAbService.isRunning.value = ServiceUtils.isServiceRunning(GkdAbService::class.java)
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/kotlin/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import li.songe.gkd.BuildConfig
import li.songe.gkd.app
import li.songe.gkd.appScope
import li.songe.gkd.data.ActivityLog
Expand All @@ -14,6 +15,7 @@ import li.songe.gkd.data.GlobalRule
import li.songe.gkd.data.ResolvedRule
import li.songe.gkd.data.SubsConfig
import li.songe.gkd.db.DbSet
import li.songe.gkd.isActivityVisible
import li.songe.gkd.util.RuleSummary
import li.songe.gkd.util.getDefaultLauncherAppId
import li.songe.gkd.util.increaseClickCount
Expand All @@ -37,7 +39,12 @@ private val activityLogMutex by lazy { Mutex() }
private var activityLogCount = 0
private var lastActivityChangeTime = 0L
fun updateTopActivity(topActivity: TopActivity) {
if (topActivityFlow.value.appId == topActivity.appId && topActivityFlow.value.activityId == topActivity.activityId) {
val isSameActivity =
topActivityFlow.value.appId == topActivity.appId && topActivityFlow.value.activityId == topActivity.activityId
if (isSameActivity) {
if (isActivityVisible() && topActivity.appId == BuildConfig.APPLICATION_ID) {
return
}
if (topActivityFlow.value.number == topActivity.number) {
return
}
Expand Down

0 comments on commit a3d8a07

Please sign in to comment.