Increase tolerance of the LollipopForegroundAppHelper

This commit is contained in:
Jonas Lochmann 2022-04-11 02:00:00 +02:00
parent c7994e9fa4
commit b2601a2ba0
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36

View file

@ -35,6 +35,7 @@ import java.util.concurrent.Executors
class LollipopForegroundAppHelper(context: Context) : UsageStatsForegroundAppHelper(context) { class LollipopForegroundAppHelper(context: Context) : UsageStatsForegroundAppHelper(context) {
companion object { companion object {
private const val LOG_TAG = "LollipopForegroundApp" private const val LOG_TAG = "LollipopForegroundApp"
private const val QUERY_TIME_TOLERANCE = 2500L
private val foregroundAppThread: Executor by lazy { Executors.newSingleThreadExecutor() } private val foregroundAppThread: Executor by lazy { Executors.newSingleThreadExecutor() }
val enableMultiAppDetectionGeneral = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P val enableMultiAppDetectionGeneral = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
@ -88,15 +89,10 @@ class LollipopForegroundAppHelper(context: Context) : UsageStatsForegroundAppHel
now - 1000 * 60 * 60 * 24 * 7 now - 1000 * 60 * 60 * 24 * 7
} else { } else {
// query data since last query // query data since last query
// note: when the duration is too small, Android returns no data lastQueryTime - Math.max(queryInterval, QUERY_TIME_TOLERANCE)
// due to that, 1 second more than required is queried
// which seems to provide all data
// update: with 1 second, some App switching events were missed
// it seems to always work with 1.5 seconds
lastQueryTime - Math.max(queryInterval, 1500)
} }
usageStatsManager.queryEvents(queryStartTime, now)?.let { usageEvents -> usageStatsManager.queryEvents(queryStartTime, now + QUERY_TIME_TOLERANCE)?.let { usageEvents ->
while (usageEvents.hasNextEvent()) { while (usageEvents.hasNextEvent()) {
usageEvents.getNextEvent(event) usageEvents.getNextEvent(event)