From 183ec609e8d76ab650d3515e6750fdb1aeaaaf32 Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Wed, 16 Jun 2021 15:39:20 -0700 Subject: [PATCH] Allow bucket elevation for timed-out restricted apps. Ensure SYSTEM_INTERACTION can bring an app out of the RESTRICTED bucket (temporarily) if the app is only in the bucket due to timeout. Bug: 191297958 Test: atest AppStandbyControllerTests Change-Id: Ie76d224710228a8654d4d57ee0c73bb9441ef28f --- .../android/server/usage/AppIdleHistory.java | 7 +++-- .../usage/AppStandbyControllerTests.java | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java b/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java index 37b3c04314559..d532e20a01581 100644 --- a/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java +++ b/apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java @@ -20,6 +20,7 @@ import static android.app.usage.UsageStatsManager.REASON_MAIN_DEFAULT; import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_USER; import static android.app.usage.UsageStatsManager.REASON_MAIN_MASK; import static android.app.usage.UsageStatsManager.REASON_MAIN_PREDICTED; +import static android.app.usage.UsageStatsManager.REASON_MAIN_TIMEOUT; import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; import static android.app.usage.UsageStatsManager.REASON_SUB_MASK; import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_USER_INTERACTION; @@ -259,8 +260,10 @@ public class AppIdleHistory { int bucketingReason = REASON_MAIN_USAGE | usageReason; final boolean isUserUsage = isUserUsage(bucketingReason); - if (appUsageHistory.currentBucket == STANDBY_BUCKET_RESTRICTED && !isUserUsage) { - // Only user usage should bring an app out of the RESTRICTED bucket. + if (appUsageHistory.currentBucket == STANDBY_BUCKET_RESTRICTED && !isUserUsage + && (appUsageHistory.bucketingReason & REASON_MAIN_MASK) != REASON_MAIN_TIMEOUT) { + // Only user usage should bring an app out of the RESTRICTED bucket, unless the app + // just timed out into RESTRICTED. newBucket = STANDBY_BUCKET_RESTRICTED; bucketingReason = appUsageHistory.bucketingReason; } else { diff --git a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java index a24691791938e..4c157c0e0a73e 100644 --- a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java +++ b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java @@ -1179,6 +1179,36 @@ public class AppStandbyControllerTests { assertBucket(STANDBY_BUCKET_RESTRICTED); } + /** + * Test that an app that "timed out" into the RESTRICTED bucket can be raised out by system + * interaction. + */ + @Test + public void testSystemInteractionOverridesRestrictedTimeout() throws Exception { + reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); + assertBucket(STANDBY_BUCKET_ACTIVE); + + // Long enough that it could have timed out into RESTRICTED. + mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; + mController.checkIdleStates(USER_ID); + assertBucket(STANDBY_BUCKET_RESTRICTED); + + // Report system interaction. + mInjector.mElapsedRealtime += 1000; + reportEvent(mController, SYSTEM_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); + + // Ensure that it's raised out of RESTRICTED for the system interaction elevation duration. + assertBucket(STANDBY_BUCKET_ACTIVE); + mInjector.mElapsedRealtime += 1000; + mController.checkIdleStates(USER_ID); + assertBucket(STANDBY_BUCKET_ACTIVE); + + // Elevation duration over. Should fall back down. + mInjector.mElapsedRealtime += 10 * MINUTE_MS; + mController.checkIdleStates(USER_ID); + assertBucket(STANDBY_BUCKET_RESTRICTED); + } + @Test public void testRestrictedBucketDisabled() throws Exception { mInjector.mIsRestrictedBucketEnabled = false;