diff --git a/app/src/main/kotlin/bums/lunatic/launcher/apps/AppDrawerBottomSheet.kt b/app/src/main/kotlin/bums/lunatic/launcher/apps/AppDrawerBottomSheet.kt index e6662b62..565c6be5 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/apps/AppDrawerBottomSheet.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/apps/AppDrawerBottomSheet.kt @@ -404,9 +404,10 @@ class AppDrawerBottomSheet : BottomSheetDialogFragment() { ?.takeIf { it.first == isHiddenSelected } ?.second ?: run { - val scoredItems = WorkersDb.getContextualRecommendations(limit = 18) + val scoredItems = WorkersDb.getContextualRecommendations(limit = 40) val list = mutableListOf() for (item in scoredItems) { + if (list.size >= 18) break if (item.type == "APP") { val app = realm.query("pkgName == $0", item.key).first().find() if (app != null && (app.blockRecommend == false || (isHiddenSelected == app.blockRecommend))) { 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 cff3ec72..466da303 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 @@ -229,47 +229,65 @@ object WorkersDb { // [핵심] 현재 시간 상황에 맞는 추천 리스트 가져오기 fun getContextualRecommendations(limit: Int = 18): List { val realm = getRealm() - val calendar = Calendar.getInstance() + val now = System.currentTimeMillis() + val calendar = Calendar.getInstance().apply { timeInMillis = now } val curMonth = calendar.get(Calendar.MONTH) val curDay = calendar.get(Calendar.DAY_OF_MONTH) val curDow = calendar.get(Calendar.DAY_OF_WEEK) val curHour = calendar.get(Calendar.HOUR_OF_DAY) - val ONE_DAY_TIME = 24 * 60 * 60 * 1000 - // 최근 3개월 데이터만 조회 (너무 오래된 데이터는 노이즈가 됨) - val threeMonthsAgo = System.currentTimeMillis() - (365L * ONE_DAY_TIME) + val isCurWeekend = curDow == Calendar.SATURDAY || curDow == Calendar.SUNDAY + val oneDayMs = 24L * 60 * 60 * 1000L - // 쿼리: 최근 데이터만 가져와서 메모리에서 계산 (복잡한 가중치는 메모리 연산이 빠름) - val logs = realm.query("timestamp > $0", threeMonthsAgo).find() + // 최근 1년치 로그 조회 (연간/월간 반복 패턴 포함) + val cutoffTime = now - (365L * oneDayMs) + val logs = realm.query("timestamp > $0", cutoffTime).find() val scores = HashMap() + val keys = HashMap() val types = HashMap() - // 점수 계산 for (log in logs) { - var score = 1.0 // 기본 점수 (최근에 썼다는 것 자체로 의미 있음) + val daysAgo = ((now - log.timestamp).coerceAtLeast(0L)).toDouble() / oneDayMs + var score = 1.0 // 기본 빈도 점수 - // 1. 시간대 매칭 (가장 중요: 아침에 쓰던 앱은 아침에 쓴다) - if (log.hour == curHour) score += 3.0 - else if (log.hour == curHour - 1 || log.hour == curHour + 1) score += 1.0 + // 1. 시간대 매칭 (자정 23시<->0시 경계 포함 원형 거리) + val rawDiff = kotlin.math.abs(log.hour - curHour) + val hourDiff = minOf(rawDiff, 24 - rawDiff) + score += when (hourDiff) { + 0 -> 5.0 + 1 -> 2.5 + 2 -> 1.0 + else -> 0.0 + } - // 2. 요일 매칭 (주말/평일 패턴) - if (log.dayOfWeek == curDow) score += 2.0 + // 2. 요일 및 평일/주말 패턴 (오늘 당일 로그 중복 보너스 방지) + if (daysAgo >= 1.0) { + if (log.dayOfWeek == curDow) { + score += 3.0 + } else { + val isLogWeekend = log.dayOfWeek == Calendar.SATURDAY || log.dayOfWeek == Calendar.SUNDAY + if (isLogWeekend == isCurWeekend) score += 1.0 + } + } - // 3. "매월 1일" 같은 날짜 패턴 (가중치 매우 높음) - if (log.dayOfMonth == curDay) score += 5.0 + // 3. 매월 N일 반복 패턴 (최소 25일 이상 지난 과거 기록에만 적용) + if (daysAgo >= 25.0 && log.dayOfMonth == curDay) { + score += 6.0 + } - // 4. (옵션) 매년 같은 달 같은 날? (생일 등) - if (log.month == curMonth && log.dayOfMonth == curDay) score += 10.0 + // 4. 매년 같은 월/일 반복 패턴 (최소 330일 이상 지난 기록에만 적용) + if (daysAgo >= 330.0 && log.month == curMonth && log.dayOfMonth == curDay) { + score += 10.0 + } - // 5. 최신성 가중치 (최근 기록일수록 점수 높게) - val daysAgo = (System.currentTimeMillis() - log.timestamp).toDouble() / ONE_DAY_TIME - val decay = 1.0 / (daysAgo + 1) // 오늘이면 1, 9일전이면 0.1 + // 5. 완만한 최신성 감쇠 (7일 경과 시 0.5배, 오늘 기록의 독점 방지) + val decay = 1.0 / (daysAgo / 7.0 + 1.0) - // 최종 점수 누적 - val finalScore = score * decay - scores[log.itemKey] = (scores[log.itemKey] ?: 0.0) + finalScore - types[log.itemKey] = log.itemType + val mapKey = "${log.itemType}:${log.itemKey}" + scores[mapKey] = (scores[mapKey] ?: 0.0) + (score * decay) + keys[mapKey] = log.itemKey + types[mapKey] = log.itemType } // 점수 높은 순으로 정렬하여 상위 N개 반환 @@ -277,8 +295,7 @@ object WorkersDb { .sortedByDescending { it.value } .take(limit) .map { - // 키, 타입, 점수를 묶어서 반환 - ScoredItem(it.key, types[it.key] ?: "APP", it.value) + ScoredItem(keys[it.key] ?: it.key, types[it.key] ?: "APP", it.value) } } @@ -441,11 +458,15 @@ object WorkersDb { ).find() if (existingLog.isEmpty()) { + val logCal = Calendar.getInstance().apply { timeInMillis = lastUsedTime } copyToRealm(AppUsageLog().apply { itemKey = pkgName itemType = "APP" timestamp = lastUsedTime - // month, day 등 날짜 정보 계산해서 넣기... + month = logCal.get(Calendar.MONTH) + dayOfMonth = logCal.get(Calendar.DAY_OF_MONTH) + dayOfWeek = logCal.get(Calendar.DAY_OF_WEEK) + hour = logCal.get(Calendar.HOUR_OF_DAY) }) } }