홈 복귀 시 사용 통계 동기화 메인 스레드 블로킹 제거
- LauncherActivity.onResume(): WorkersDb.syncSystemUsageStats()를 lifecycleScope.launch(Dispatchers.IO)로 비동기 실행 - WorkersDb.syncSystemUsageStats(): 3분 쿨다운 적용 및 AppInfo 일괄 조회(associateBy)로 N+1 Realm 쿼리 제거
This commit is contained in:
@@ -22,6 +22,7 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import android.provider.OpenableColumns
|
import android.provider.OpenableColumns
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import android.view.GestureDetector
|
import android.view.GestureDetector
|
||||||
@@ -1076,7 +1077,10 @@ open class LauncherActivity : CommonActivity() {
|
|||||||
super.onResume()
|
super.onResume()
|
||||||
Blog.LOGE("LauncherActivity onResume")
|
Blog.LOGE("LauncherActivity onResume")
|
||||||
if (checkUsageStatsPermission()) {
|
if (checkUsageStatsPermission()) {
|
||||||
WorkersDb.syncSystemUsageStats(applicationContext)
|
val appCtx = applicationContext
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
WorkersDb.syncSystemUsageStats(appCtx)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
requestPermission()
|
requestPermission()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -391,7 +391,14 @@ object WorkersDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var lastUsageSyncTime = 0L
|
||||||
|
|
||||||
fun syncSystemUsageStats(context: Context) {
|
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 usm = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
|
||||||
val calendar = Calendar.getInstance()
|
val calendar = Calendar.getInstance()
|
||||||
val endTime = calendar.timeInMillis
|
val endTime = calendar.timeInMillis
|
||||||
@@ -409,12 +416,13 @@ object WorkersDb {
|
|||||||
val realm = getRealm() // WorkersDb의 getRealm() 활용
|
val realm = getRealm() // WorkersDb의 getRealm() 활용
|
||||||
|
|
||||||
realm.writeBlocking {
|
realm.writeBlocking {
|
||||||
|
val appInfoMap = query<AppInfo>().find().associateBy { it.pkgName }
|
||||||
for (stats in usageStatsList) {
|
for (stats in usageStatsList) {
|
||||||
val pkgName = stats.packageName
|
val pkgName = stats.packageName
|
||||||
val lastUsedTime = stats.lastTimeUsed
|
val lastUsedTime = stats.lastTimeUsed
|
||||||
|
|
||||||
// 2. 우리 DB(AppInfo)에 저장된 시간보다 최신인지 확인
|
// 2. 우리 DB(AppInfo)에 저장된 시간보다 최신인지 확인
|
||||||
val appInfo = query<AppInfo>("pkgName == $0", pkgName).first().find()
|
val appInfo = appInfoMap[pkgName]
|
||||||
|
|
||||||
if (appInfo != null) {
|
if (appInfo != null) {
|
||||||
// 시스템 기록이 내 DB 기록보다 더 최신이면 -> 런처 밖에서 실행된 것임!
|
// 시스템 기록이 내 DB 기록보다 더 최신이면 -> 런처 밖에서 실행된 것임!
|
||||||
|
|||||||
Reference in New Issue
Block a user