Avoid checking for idle state when the app is in top state.

When we know that an app moved to a procstate that is considered
an interaction event, the app will not be in idle state. So, avoid
calling into UsageStatsService for idle state in this case.

Bug: 209338078
Test: atest tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Change-Id: I5648db595d2fd10a151da13e472fd905bcafe182
Merged-In: I5648db595d2fd10a151da13e472fd905bcafe182
This commit is contained in:
Sudheer Shanka
2022-03-23 01:35:30 -07:00
parent c3580425a7
commit 99b5c0cbf6
3 changed files with 31 additions and 12 deletions

View File

@@ -4657,6 +4657,11 @@ public class ActivityManager {
}
}
/** @hide */
public static boolean isProcStateConsideredInteraction(@ProcessState int procState) {
return (procState <= PROCESS_STATE_TOP || procState == PROCESS_STATE_BOUND_TOP);
}
/** @hide */
public static String procStateToString(int procState) {
final String procStateStr;

View File

@@ -2911,8 +2911,7 @@ public class OomAdjuster {
// To avoid some abuse patterns, we are going to be careful about what we consider
// to be an app interaction. Being the top activity doesn't count while the display
// is sleeping, nor do short foreground services.
if (state.getCurProcState() <= PROCESS_STATE_TOP
|| state.getCurProcState() == PROCESS_STATE_BOUND_TOP) {
if (ActivityManager.isProcStateConsideredInteraction(state.getCurProcState())) {
isInteraction = true;
state.setFgInteractionTime(0);
} else if (state.getCurProcState() <= PROCESS_STATE_FOREGROUND_SERVICE) {

View File

@@ -26,6 +26,8 @@ import static android.Manifest.permission.NETWORK_STACK;
import static android.Manifest.permission.OBSERVE_NETWORK_POLICY;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE;
import static android.app.ActivityManager.PROCESS_STATE_UNKNOWN;
import static android.app.ActivityManager.isProcStateConsideredInteraction;
import static android.app.PendingIntent.FLAG_IMMUTABLE;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.content.Intent.ACTION_PACKAGE_ADDED;
@@ -4059,14 +4061,14 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState)
!= isProcStateAllowedWhileIdleOrPowerSaveMode(newUidState);
if (allowedWhileIdleOrPowerSaveModeChanged) {
updateRuleForAppIdleUL(uid);
updateRuleForAppIdleUL(uid, procState);
if (mDeviceIdleMode) {
updateRuleForDeviceIdleUL(uid);
}
if (mRestrictPower) {
updateRuleForRestrictPowerUL(uid);
}
updateRulesForPowerRestrictionsUL(uid);
updateRulesForPowerRestrictionsUL(uid, procState);
}
if (mLowPowerStandbyActive) {
boolean allowedInLpsChanged =
@@ -4074,7 +4076,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
!= isProcStateAllowedWhileInLowPowerStandby(newUidState);
if (allowedInLpsChanged) {
if (!allowedWhileIdleOrPowerSaveModeChanged) {
updateRulesForPowerRestrictionsUL(uid);
updateRulesForPowerRestrictionsUL(uid, procState);
}
updateRuleForLowPowerStandbyUL(uid);
}
@@ -4443,7 +4445,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
}
@GuardedBy("mUidRulesFirstLock")
void updateRuleForAppIdleUL(int uid) {
void updateRuleForAppIdleUL(int uid, int uidProcessState) {
if (!isUidValidForDenylistRulesUL(uid)) return;
if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) {
@@ -4451,7 +4453,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
}
try {
int appId = UserHandle.getAppId(uid);
if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid)
if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid, uidProcessState)
&& !isUidForegroundOnRestrictPowerUL(uid)) {
setUidFirewallRuleUL(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY);
if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL DENY " + uid);
@@ -4602,7 +4604,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
final UserInfo user = users.get(i);
int uid = UserHandle.getUid(user.id, appId);
// Update external firewall rules.
updateRuleForAppIdleUL(uid);
updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRuleForDeviceIdleUL(uid);
updateRuleForRestrictPowerUL(uid);
// Update internal rules.
@@ -4650,7 +4652,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
} else {
mAppIdleTempWhitelistAppIds.delete(uid);
}
updateRuleForAppIdleUL(uid);
updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRulesForPowerRestrictionsUL(uid);
} finally {
Binder.restoreCallingIdentity(token);
@@ -4676,7 +4678,15 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
/** Returns if the UID is currently considered idle. */
@VisibleForTesting
boolean isUidIdle(int uid) {
return isUidIdle(uid, PROCESS_STATE_UNKNOWN);
}
private boolean isUidIdle(int uid, int uidProcessState) {
synchronized (mUidRulesFirstLock) {
if (uidProcessState != PROCESS_STATE_UNKNOWN && isProcStateConsideredInteraction(
uidProcessState)) {
return false;
}
if (mAppIdleTempWhitelistAppIds.get(uid)) {
// UID is temporarily allowlisted.
return false;
@@ -4763,7 +4773,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
private void updateRestrictionRulesForUidUL(int uid) {
// Methods below only changes the firewall rules for the power-related modes.
updateRuleForDeviceIdleUL(uid);
updateRuleForAppIdleUL(uid);
updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRuleForRestrictPowerUL(uid);
// If the uid has the necessary permissions, then it should be added to the restricted mode
@@ -4937,7 +4947,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
*/
@GuardedBy("mUidRulesFirstLock")
private void updateRulesForPowerRestrictionsUL(int uid) {
updateRulesForPowerRestrictionsUL(uid, isUidIdle(uid));
updateRulesForPowerRestrictionsUL(uid, PROCESS_STATE_UNKNOWN);
}
@GuardedBy("mUidRulesFirstLock")
private void updateRulesForPowerRestrictionsUL(int uid, int uidProcState) {
updateRulesForPowerRestrictionsUL(uid, isUidIdle(uid, uidProcState));
}
/**
@@ -5045,7 +5060,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
PackageManager.MATCH_UNINSTALLED_PACKAGES, userId);
synchronized (mUidRulesFirstLock) {
mLogger.appIdleStateChanged(uid, idle);
updateRuleForAppIdleUL(uid);
updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRulesForPowerRestrictionsUL(uid);
}
} catch (NameNotFoundException nnfe) {