Skip to content

Commit

Permalink
perf: 缓存 activity 信息,避免重复查询
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Dec 27, 2023
1 parent a8ad0c9 commit 933fd91
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.PixelFormat
import android.os.Build
import android.util.LruCache
import android.view.Display
import android.view.View
import android.view.WindowManager
Expand Down Expand Up @@ -98,20 +99,25 @@ class GkdAbService : CompositionAbService({
return TopActivity(appId = top.packageName, activityId = top.className)
}

val activityCache = object : LruCache<Pair<String, String>, Boolean>(128) {
override fun create(key: Pair<String, String>): Boolean {
return kotlin.runCatching {
packageManager.getActivityInfo(
ComponentName(
key.first, key.second
), 0
)
}.getOrNull() != null
}
}

fun isActivity(
appId: String,
activityId: String,
): Boolean {
if (appId == topActivityFlow.value.appId && activityId == topActivityFlow.value.activityId) return true
val r = (try {
packageManager.getActivityInfo(
ComponentName(
appId, activityId
), 0
)
} catch (e: PackageManager.NameNotFoundException) {
null
} != null)
if (appId == topActivityFlow.value.appId && activityId == topActivityFlow.value?.activityId) return true
val cacheKey = Pair(appId, activityId)
val r = activityCache.get(cacheKey)
return r
}

Expand Down

0 comments on commit 933fd91

Please sign in to comment.