Avoid checking for idle state when the app is in top state. am: 99b5c0cbf6 am: 595316feff

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2065614

Change-Id: I75d424b0345382761eef7f896a9a619d21b2324a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Sudheer Shanka
2022-04-15 18:36:50 +00:00
committed by Automerger Merge Worker
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 */ /** @hide */
public static String procStateToString(int procState) { public static String procStateToString(int procState) {
final String procStateStr; 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 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 // to be an app interaction. Being the top activity doesn't count while the display
// is sleeping, nor do short foreground services. // is sleeping, nor do short foreground services.
if (state.getCurProcState() <= PROCESS_STATE_TOP if (ActivityManager.isProcStateConsideredInteraction(state.getCurProcState())) {
|| state.getCurProcState() == PROCESS_STATE_BOUND_TOP) {
isInteraction = true; isInteraction = true;
state.setFgInteractionTime(0); state.setFgInteractionTime(0);
} else if (state.getCurProcState() <= PROCESS_STATE_FOREGROUND_SERVICE) { } 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.OBSERVE_NETWORK_POLICY;
import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.READ_PRIVILEGED_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_IMMUTABLE;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.content.Intent.ACTION_PACKAGE_ADDED; import static android.content.Intent.ACTION_PACKAGE_ADDED;
@@ -4059,14 +4061,14 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState)
!= isProcStateAllowedWhileIdleOrPowerSaveMode(newUidState); != isProcStateAllowedWhileIdleOrPowerSaveMode(newUidState);
if (allowedWhileIdleOrPowerSaveModeChanged) { if (allowedWhileIdleOrPowerSaveModeChanged) {
updateRuleForAppIdleUL(uid); updateRuleForAppIdleUL(uid, procState);
if (mDeviceIdleMode) { if (mDeviceIdleMode) {
updateRuleForDeviceIdleUL(uid); updateRuleForDeviceIdleUL(uid);
} }
if (mRestrictPower) { if (mRestrictPower) {
updateRuleForRestrictPowerUL(uid); updateRuleForRestrictPowerUL(uid);
} }
updateRulesForPowerRestrictionsUL(uid); updateRulesForPowerRestrictionsUL(uid, procState);
} }
if (mLowPowerStandbyActive) { if (mLowPowerStandbyActive) {
boolean allowedInLpsChanged = boolean allowedInLpsChanged =
@@ -4074,7 +4076,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
!= isProcStateAllowedWhileInLowPowerStandby(newUidState); != isProcStateAllowedWhileInLowPowerStandby(newUidState);
if (allowedInLpsChanged) { if (allowedInLpsChanged) {
if (!allowedWhileIdleOrPowerSaveModeChanged) { if (!allowedWhileIdleOrPowerSaveModeChanged) {
updateRulesForPowerRestrictionsUL(uid); updateRulesForPowerRestrictionsUL(uid, procState);
} }
updateRuleForLowPowerStandbyUL(uid); updateRuleForLowPowerStandbyUL(uid);
} }
@@ -4443,7 +4445,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
} }
@GuardedBy("mUidRulesFirstLock") @GuardedBy("mUidRulesFirstLock")
void updateRuleForAppIdleUL(int uid) { void updateRuleForAppIdleUL(int uid, int uidProcessState) {
if (!isUidValidForDenylistRulesUL(uid)) return; if (!isUidValidForDenylistRulesUL(uid)) return;
if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) {
@@ -4451,7 +4453,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
} }
try { try {
int appId = UserHandle.getAppId(uid); int appId = UserHandle.getAppId(uid);
if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid, uidProcessState)
&& !isUidForegroundOnRestrictPowerUL(uid)) { && !isUidForegroundOnRestrictPowerUL(uid)) {
setUidFirewallRuleUL(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); setUidFirewallRuleUL(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY);
if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL DENY " + uid); if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL DENY " + uid);
@@ -4602,7 +4604,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
final UserInfo user = users.get(i); final UserInfo user = users.get(i);
int uid = UserHandle.getUid(user.id, appId); int uid = UserHandle.getUid(user.id, appId);
// Update external firewall rules. // Update external firewall rules.
updateRuleForAppIdleUL(uid); updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRuleForDeviceIdleUL(uid); updateRuleForDeviceIdleUL(uid);
updateRuleForRestrictPowerUL(uid); updateRuleForRestrictPowerUL(uid);
// Update internal rules. // Update internal rules.
@@ -4650,7 +4652,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
} else { } else {
mAppIdleTempWhitelistAppIds.delete(uid); mAppIdleTempWhitelistAppIds.delete(uid);
} }
updateRuleForAppIdleUL(uid); updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRulesForPowerRestrictionsUL(uid); updateRulesForPowerRestrictionsUL(uid);
} finally { } finally {
Binder.restoreCallingIdentity(token); Binder.restoreCallingIdentity(token);
@@ -4676,7 +4678,15 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
/** Returns if the UID is currently considered idle. */ /** Returns if the UID is currently considered idle. */
@VisibleForTesting @VisibleForTesting
boolean isUidIdle(int uid) { boolean isUidIdle(int uid) {
return isUidIdle(uid, PROCESS_STATE_UNKNOWN);
}
private boolean isUidIdle(int uid, int uidProcessState) {
synchronized (mUidRulesFirstLock) { synchronized (mUidRulesFirstLock) {
if (uidProcessState != PROCESS_STATE_UNKNOWN && isProcStateConsideredInteraction(
uidProcessState)) {
return false;
}
if (mAppIdleTempWhitelistAppIds.get(uid)) { if (mAppIdleTempWhitelistAppIds.get(uid)) {
// UID is temporarily allowlisted. // UID is temporarily allowlisted.
return false; return false;
@@ -4763,7 +4773,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
private void updateRestrictionRulesForUidUL(int uid) { private void updateRestrictionRulesForUidUL(int uid) {
// Methods below only changes the firewall rules for the power-related modes. // Methods below only changes the firewall rules for the power-related modes.
updateRuleForDeviceIdleUL(uid); updateRuleForDeviceIdleUL(uid);
updateRuleForAppIdleUL(uid); updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRuleForRestrictPowerUL(uid); updateRuleForRestrictPowerUL(uid);
// If the uid has the necessary permissions, then it should be added to the restricted mode // 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") @GuardedBy("mUidRulesFirstLock")
private void updateRulesForPowerRestrictionsUL(int uid) { 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); PackageManager.MATCH_UNINSTALLED_PACKAGES, userId);
synchronized (mUidRulesFirstLock) { synchronized (mUidRulesFirstLock) {
mLogger.appIdleStateChanged(uid, idle); mLogger.appIdleStateChanged(uid, idle);
updateRuleForAppIdleUL(uid); updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
updateRulesForPowerRestrictionsUL(uid); updateRulesForPowerRestrictionsUL(uid);
} }
} catch (NameNotFoundException nnfe) { } catch (NameNotFoundException nnfe) {