Query usage event for a longer time period and then trim the usage

events outside the expected period to make sure the app usage calculation near the boundaries are correct.

Test: make RunSettingsRoboTests + manual
Bug: 264858898
Change-Id: I9f6aa5a09a537f48a26a08b7dff8ae81e8a16e2a
This commit is contained in:
Kuan Wang
2023-01-13 12:37:05 +08:00
parent 8fce459806
commit 4a6b26558d
4 changed files with 139 additions and 115 deletions

View File

@@ -74,6 +74,11 @@ public final class DatabaseUtils {
public static final String QUERY_KEY_USERID = "userid";
public static final long INVALID_USER_ID = Integer.MIN_VALUE;
/**
* The buffer hours to query app usage events that may have begun or ended out of the final
* desired time frame.
*/
public static final long USAGE_QUERY_BUFFER_HOURS = Duration.ofHours(3).toMillis();
/** A content URI to access battery usage states data. */
public static final Uri BATTERY_CONTENT_URI =
@@ -138,7 +143,10 @@ public final class DatabaseUtils {
final long startTimestampOfLevelData) {
final long startTime = System.currentTimeMillis();
final long sixDaysAgoTimestamp = getTimestampSixDaysAgo(calendar);
final long queryTimestamp = Math.max(startTimestampOfLevelData, sixDaysAgoTimestamp);
// Query a longer time period and then trim to the original time period in order to make
// sure the app usage calculation near the boundaries is correct.
final long queryTimestamp =
Math.max(startTimestampOfLevelData, sixDaysAgoTimestamp) - USAGE_QUERY_BUFFER_HOURS;
Log.d(TAG, "sixDayAgoTimestamp: " + sixDaysAgoTimestamp);
final String queryUserIdString = userIds.stream()
.map(userId -> String.valueOf(userId))