From ad93dbf214defbdee6b0ce8ee2970546244e5984 Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Mon, 16 Nov 2026 14:22:51 +0900 Subject: [PATCH] =?UTF-8?q?=ED=99=88=20=EB=B3=B5=EA=B7=80=20=EC=8B=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20=ED=86=B5=EA=B3=84=20=EB=8F=99=EA=B8=B0?= =?UTF-8?q?=ED=99=94=20=EB=A9=94=EC=9D=B8=20=EC=8A=A4=EB=A0=88=EB=93=9C=20?= =?UTF-8?q?=EB=B8=94=EB=A1=9C=ED=82=B9=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LauncherActivity.onResume(): WorkersDb.syncSystemUsageStats()를 lifecycleScope.launch(Dispatchers.IO)로 비동기 실행 - WorkersDb.syncSystemUsageStats(): 3분 쿨다운 적용 및 AppInfo 일괄 조회(associateBy)로 N+1 Realm 쿼리 제거 --- .../kotlin/bums/lunatic/launcher/LauncherActivity.kt | 6 +++++- .../kotlin/bums/lunatic/launcher/workers/WorkersDb.kt | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt b/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt index 42e2df20..5b6cc2c3 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt @@ -22,6 +22,7 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper +import androidx.lifecycle.lifecycleScope import android.provider.OpenableColumns import android.provider.Settings import android.view.GestureDetector @@ -1076,7 +1077,10 @@ open class LauncherActivity : CommonActivity() { super.onResume() Blog.LOGE("LauncherActivity onResume") if (checkUsageStatsPermission()) { - WorkersDb.syncSystemUsageStats(applicationContext) + val appCtx = applicationContext + lifecycleScope.launch(Dispatchers.IO) { + WorkersDb.syncSystemUsageStats(appCtx) + } } else { requestPermission() } diff --git a/core/core-database/src/main/kotlin/bums/lunatic/launcher/workers/WorkersDb.kt b/core/core-database/src/main/kotlin/bums/lunatic/launcher/workers/WorkersDb.kt index f5b8ebe8..cff3ec72 100644 --- a/core/core-database/src/main/kotlin/bums/lunatic/launcher/workers/WorkersDb.kt +++ b/core/core-database/src/main/kotlin/bums/lunatic/launcher/workers/WorkersDb.kt @@ -391,7 +391,14 @@ object WorkersDb { } } + @Volatile + private var lastUsageSyncTime = 0L + fun syncSystemUsageStats(context: Context) { + val now = System.currentTimeMillis() + if (now - lastUsageSyncTime < 3 * 60 * 1000L) return + lastUsageSyncTime = now + val usm = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager val calendar = Calendar.getInstance() val endTime = calendar.timeInMillis @@ -409,12 +416,13 @@ object WorkersDb { val realm = getRealm() // WorkersDb의 getRealm() 활용 realm.writeBlocking { + val appInfoMap = query().find().associateBy { it.pkgName } for (stats in usageStatsList) { val pkgName = stats.packageName val lastUsedTime = stats.lastTimeUsed // 2. 우리 DB(AppInfo)에 저장된 시간보다 최신인지 확인 - val appInfo = query("pkgName == $0", pkgName).first().find() + val appInfo = appInfoMap[pkgName] if (appInfo != null) { // 시스템 기록이 내 DB 기록보다 더 최신이면 -> 런처 밖에서 실행된 것임!