Ensure standby bucket of an unused app gets treated correctly.

An unused app should be in NEVER bucket but without this change,
when we calculate the standby bucket of an app that was never used,
it could end up in RESTRICTED bucket.

Bug: 206518483
Test: atest tests/tests/app.usage/src/android/app/usage/cts/UsageStatsTest.java
Change-Id: I839041b7aedc87923879ca872c570c35c39b85a5
This commit is contained in:
Sudheer Shanka
2022-03-22 16:54:59 -07:00
parent c3cf521667
commit 2372176762
2 changed files with 12 additions and 14 deletions

View File

@@ -623,7 +623,8 @@ public class AppIdleHistory {
* @param elapsedRealtime current time
* @param screenTimeThresholds Array of screen times, in ascending order, first one is 0
* @param elapsedTimeThresholds Array of elapsed time, in ascending order, first one is 0
* @return The index whose values the app's used time exceeds (in both arrays)
* @return The index whose values the app's used time exceeds (in both arrays) or {@code -1} to
* indicate that the app has never been used.
*/
int getThresholdIndex(String packageName, int userId, long elapsedRealtime,
long[] screenTimeThresholds, long[] elapsedTimeThresholds) {
@@ -631,14 +632,13 @@ public class AppIdleHistory {
AppUsageHistory appUsageHistory = getPackageHistory(userHistory, packageName,
elapsedRealtime, false);
// If we don't have any state for the app, assume never used
if (appUsageHistory == null) return screenTimeThresholds.length - 1;
if (appUsageHistory == null || appUsageHistory.lastUsedElapsedTime < 0
|| appUsageHistory.lastUsedScreenTime < 0) {
return -1;
}
long screenOnDelta = appUsageHistory.lastUsedScreenTime >= 0
? getScreenOnTime(elapsedRealtime) - appUsageHistory.lastUsedScreenTime
: Long.MAX_VALUE;
long elapsedDelta = appUsageHistory.lastUsedElapsedTime >= 0
? getElapsedTime(elapsedRealtime) - appUsageHistory.lastUsedElapsedTime
: Long.MAX_VALUE;
long screenOnDelta = getScreenOnTime(elapsedRealtime) - appUsageHistory.lastUsedScreenTime;
long elapsedDelta = getElapsedTime(elapsedRealtime) - appUsageHistory.lastUsedElapsedTime;
if (DEBUG) Slog.d(TAG, packageName
+ " lastUsedScreen=" + appUsageHistory.lastUsedScreenTime

View File

@@ -898,11 +898,9 @@ public class AppStandbyController
}
}
final long elapsedLastUsedByUserTimeDelta = app.lastUsedByUserElapsedTime >= 0
? elapsedTimeAdjusted - app.lastUsedByUserElapsedTime
: Long.MAX_VALUE;
if (app.lastRestrictAttemptElapsedTime > app.lastUsedByUserElapsedTime
&& elapsedLastUsedByUserTimeDelta
if (app.lastUsedByUserElapsedTime >= 0
&& app.lastRestrictAttemptElapsedTime > app.lastUsedByUserElapsedTime
&& elapsedTimeAdjusted - app.lastUsedByUserElapsedTime
>= mInjector.getAutoRestrictedBucketDelayMs()) {
newBucket = STANDBY_BUCKET_RESTRICTED;
reason = app.lastRestrictReason;
@@ -974,7 +972,7 @@ public class AppStandbyController
long elapsedRealtime) {
int bucketIndex = mAppIdleHistory.getThresholdIndex(packageName, userId,
elapsedRealtime, mAppStandbyScreenThresholds, mAppStandbyElapsedThresholds);
return THRESHOLD_BUCKETS[bucketIndex];
return bucketIndex >= 0 ? THRESHOLD_BUCKETS[bucketIndex] : STANDBY_BUCKET_NEVER;
}
private void notifyBatteryStats(String packageName, int userId, boolean idle) {