Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 12e99772 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

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
parent 04b5ad3c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -4889,6 +4889,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;
+1 −2
Original line number Diff line number Diff line
@@ -2854,8 +2854,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) {
+25 −10
Original line number Diff line number Diff line
@@ -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;
@@ -4042,14 +4044,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 =
@@ -4057,7 +4059,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                                    != isProcStateAllowedWhileInLowPowerStandby(newUidState);
                    if (allowedInLpsChanged) {
                        if (!allowedWhileIdleOrPowerSaveModeChanged) {
                            updateRulesForPowerRestrictionsUL(uid);
                            updateRulesForPowerRestrictionsUL(uid, procState);
                        }
                        updateRuleForLowPowerStandbyUL(uid);
                    }
@@ -4426,7 +4428,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)) {
@@ -4434,7 +4436,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);
@@ -4585,7 +4587,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.
@@ -4633,7 +4635,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                } else {
                    mAppIdleTempWhitelistAppIds.delete(uid);
                }
                updateRuleForAppIdleUL(uid);
                updateRuleForAppIdleUL(uid, PROCESS_STATE_UNKNOWN);
                updateRulesForPowerRestrictionsUL(uid);
            } finally {
                Binder.restoreCallingIdentity(token);
@@ -4659,7 +4661,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;
@@ -4746,7 +4756,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
@@ -4920,7 +4930,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));
    }

    /**
@@ -5028,7 +5043,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) {