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

Commit ac43093b authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Don't hardcode the blocked reasons in AMS.

The blocked reasons that take into procstate
of the uid could change and shouldn't be
hardcoded in AMS.

Bug: 226299593
Test: atest tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Change-Id: I995f7c6e4391f0aa72f6ea58057cf56ef80be708
parent 525fad6c
Loading
Loading
Loading
Loading
+8 −24
Original line number Diff line number Diff line
@@ -49,12 +49,6 @@ import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.net.ConnectivityManager.BLOCKED_METERED_REASON_DATA_SAVER;
import static android.net.ConnectivityManager.BLOCKED_METERED_REASON_USER_RESTRICTED;
import static android.net.ConnectivityManager.BLOCKED_REASON_APP_STANDBY;
import static android.net.ConnectivityManager.BLOCKED_REASON_BATTERY_SAVER;
import static android.net.ConnectivityManager.BLOCKED_REASON_DOZE;
import static android.net.ConnectivityManager.BLOCKED_REASON_LOW_POWER_STANDBY;
import static android.net.ConnectivityManager.BLOCKED_REASON_NONE;
import static android.os.FactoryTest.FACTORY_TEST_OFF;
import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_CRITICAL;
@@ -136,6 +130,7 @@ import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.am.MemoryStatUtil.hasMemcg;
import static com.android.server.am.ProcessList.ProcStartHandler;
import static com.android.server.net.NetworkPolicyManagerInternal.updateBlockedReasonsWithProcState;
import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CLEANUP;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
@@ -17296,7 +17291,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                    final long procStateSeq = mProcessList.getNextProcStateSeq();
                    mNetworkPolicyUidObserver.onUidStateChanged(uid, PROCESS_STATE_TOP,
                            procStateSeq, PROCESS_CAPABILITY_ALL);
                    if (thread != null && isNetworkingBlockedForUid(uid)) {
                    if (thread != null && shouldWaitForNetworkRulesUpdate(uid)) {
                        thread.setNetworkBlockSeq(procStateSeq);
                    }
                } catch (RemoteException e) {
@@ -17305,29 +17300,18 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
        }
        private boolean isNetworkingBlockedForUid(int uid) {
        private boolean shouldWaitForNetworkRulesUpdate(int uid) {
            synchronized (mUidNetworkBlockedReasons) {
                // TODO: We can consider only those blocked reasons that will be overridden
                // by the TOP state. For other ones, there is no point in waiting.
                // TODO: We can reuse this data in
                // ProcessList#incrementProcStateSeqAndNotifyAppsLOSP instead of calling into
                // NetworkManagementService.
                final int uidBlockedReasons = mUidNetworkBlockedReasons.get(
                        uid, BLOCKED_REASON_NONE);
                if (uidBlockedReasons == BLOCKED_REASON_NONE) {
                    return false;
                }
                final int topExemptedBlockedReasons = BLOCKED_REASON_BATTERY_SAVER
                        | BLOCKED_REASON_DOZE
                        | BLOCKED_REASON_APP_STANDBY
                        | BLOCKED_REASON_LOW_POWER_STANDBY
                        | BLOCKED_METERED_REASON_DATA_SAVER
                        | BLOCKED_METERED_REASON_USER_RESTRICTED;
                final int effectiveBlockedReasons =
                        uidBlockedReasons & ~topExemptedBlockedReasons;
                // Only consider it as blocked if it is not blocked by a reason
                // that is not exempted by app being in the top state.
                return effectiveBlockedReasons == BLOCKED_REASON_NONE;
                // We should only inform the uid to block if it is currently blocked but will be
                // unblocked once it comes to the TOP state.
                return uidBlockedReasons != BLOCKED_REASON_NONE
                        && updateBlockedReasonsWithProcState(uidBlockedReasons, PROCESS_STATE_TOP)
                        == BLOCKED_REASON_NONE;
            }
        }
+9 −1
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.server.net;

import static com.android.server.net.NetworkPolicyManagerService.UidBlockedState.getAllowedReasonsForProcState;
import static com.android.server.net.NetworkPolicyManagerService.UidBlockedState.getEffectiveBlockedReasons;

import android.annotation.Nullable;
import android.net.Network;
import android.os.PowerExemptionManager.ReasonCode;
@@ -84,7 +87,6 @@ public abstract class NetworkPolicyManagerInternal {
    public abstract void setMeteredRestrictedPackages(
            Set<String> packageNames, int userId);


    /**
     * Similar to {@link #setMeteredRestrictedPackages(Set, int)} but updates the restricted
     * packages list asynchronously.
@@ -97,4 +99,10 @@ public abstract class NetworkPolicyManagerInternal {

    /** Informs that the Low Power Standby allowlist has changed */
    public abstract void setLowPowerStandbyAllowlist(int[] uids);

    /** Update the {@code blockedReasons} taking into account the {@code procState} of the uid */
    public static int updateBlockedReasonsWithProcState(int blockedReasons, int procState) {
        final int allowedReasons = getAllowedReasonsForProcState(procState);
        return getEffectiveBlockedReasons(blockedReasons, allowedReasons);
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -6315,6 +6315,17 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            return effectiveBlockedReasons;
        }

        static int getAllowedReasonsForProcState(int procState) {
            if (procState > NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE) {
                return ALLOWED_REASON_NONE;
            } else if (procState <= NetworkPolicyManager.TOP_THRESHOLD_STATE) {
                return ALLOWED_REASON_TOP | ALLOWED_REASON_FOREGROUND
                        | ALLOWED_METERED_REASON_FOREGROUND;
            } else {
                return ALLOWED_REASON_FOREGROUND | ALLOWED_METERED_REASON_FOREGROUND;
            }
        }

        @Override
        public String toString() {
            return toString(blockedReasons, allowedReasons, effectiveBlockedReasons);