Fix bucket change issues.

1. Prevent invalid state when tests call a method written for tests. The
   test method (forceIdleState) would force the app into the RARE
   bucket, regardless of the app's exemption status. However, on actual
   devices, the app's min bucket is taken into account before the bucket
   is changed. Change the test method to also consider the min bucket
   before changing the app's bucket.
2. Actually notify app standby listeners when the bucket changes in all
   instances.
3. Tell batterystats when an app is forced into idle. Previously, we
   were only ever telling batterystats when an app is no longer idle, so
   batterystats was never told when an app became idle.

Bug: 162862794
Bug: 227993380
Test: atest --rerun-until-failure 100 com.android.cts.net.HostsideRestrictBackgroundNetworkTests --use-modules-in
Change-Id: If517c1b3063af6758ab7a7a4cf7f1f67decba90c
This commit is contained in:
Kweku Adams
2022-04-29 18:03:34 +00:00
parent 50f19f212e
commit d312267935
2 changed files with 14 additions and 5 deletions

View File

@@ -979,6 +979,7 @@ public class AppIdleHistory {
dumpBucketExpiryTimes(idpw, appUsageHistory, totalElapsedTime);
idpw.print(" lastJob=");
TimeUtils.formatDuration(totalElapsedTime - appUsageHistory.lastJobRunTime, idpw);
idpw.print(" lastInformedBucket=" + appUsageHistory.lastInformedBucket);
if (appUsageHistory.lastRestrictAttemptElapsedTime > 0) {
idpw.print(" lastRestrictAttempt=");
TimeUtils.formatDuration(

View File

@@ -1155,6 +1155,12 @@ public class AppStandbyController
final int appId = getAppId(packageName);
if (appId < 0) return;
final int minBucket = getAppMinBucket(packageName, appId, userId);
if (idle && minBucket < AppIdleHistory.IDLE_BUCKET_CUTOFF) {
Slog.e(TAG, "Tried to force an app to be idle when its min bucket is "
+ standbyBucketToString(minBucket));
return;
}
final long elapsedRealtime = mInjector.elapsedRealtime();
final boolean previouslyIdle = isAppIdleFiltered(packageName, appId,
@@ -1166,12 +1172,10 @@ public class AppStandbyController
final boolean stillIdle = isAppIdleFiltered(packageName, appId,
userId, elapsedRealtime);
// Inform listeners if necessary
maybeInformListeners(packageName, userId, elapsedRealtime, standbyBucket,
REASON_MAIN_FORCED_BY_USER, false);
if (previouslyIdle != stillIdle) {
maybeInformListeners(packageName, userId, elapsedRealtime, standbyBucket,
REASON_MAIN_FORCED_BY_USER, false);
if (!stillIdle) {
notifyBatteryStats(packageName, userId, idle);
}
notifyBatteryStats(packageName, userId, stillIdle);
}
}
@@ -1934,6 +1938,8 @@ public class AppStandbyController
}
mAppIdleHistory.setAppStandbyBucket(
packageName, userId, elapsedRealtime, newBucket, newReason);
maybeInformListeners(packageName, userId, elapsedRealtime, newBucket,
newReason, false);
}
}
@@ -2490,6 +2496,8 @@ public class AppStandbyController
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INFORM_LISTENERS:
// TODO(230875908): Properly notify BatteryStats when apps change from active to
// idle, and vice versa
StandbyUpdateRecord r = (StandbyUpdateRecord) msg.obj;
informListeners(r.packageName, r.userId, r.bucket, r.reason,
r.isUserInteraction);