Skip to content

Commit

Permalink
perf: get app Info
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Mar 9, 2024
1 parent 4704715 commit 2603738
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions app/src/main/kotlin/li/songe/gkd/util/AppInfoState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private val packageReceiver by lazy {
val appId = intent?.data?.schemeSpecificPart ?: return
if (intent.action == Intent.ACTION_PACKAGE_ADDED || intent.action == Intent.ACTION_PACKAGE_REPLACED || intent.action == Intent.ACTION_PACKAGE_REMOVED) {
// update
updateAppInfo(listOf(appId))
updateAppInfo(appId)
}
}
}.apply {
Expand Down Expand Up @@ -68,24 +68,21 @@ private val packageReceiver by lazy {

private val updateAppMutex by lazy { Mutex() }

fun updateAppInfo(appIds: List<String>) {
if (appIds.isEmpty()) return
private fun updateAppInfo(appId: String) {
appScope.launchTry(Dispatchers.IO) {
val packageManager = app.packageManager
updateAppMutex.withLock {
val newMap = appInfoCacheFlow.value.toMutableMap()
appIds.forEach { appId ->
val info = try {
packageManager.getPackageInfo(appId, 0)
} catch (e: PackageManager.NameNotFoundException) {
null
}
val newAppInfo = info?.toAppInfo()
if (newAppInfo != null) {
newMap[appId] = newAppInfo
} else {
newMap.remove(appId)
}
val info = try {
packageManager.getPackageInfo(appId, 0)
} catch (e: PackageManager.NameNotFoundException) {
null
}
val newAppInfo = info?.toAppInfo()
if (newAppInfo != null) {
newMap[appId] = newAppInfo
} else {
newMap.remove(appId)
}
appInfoCacheFlow.value = newMap.toImmutableMap()
}
Expand Down

0 comments on commit 2603738

Please sign in to comment.