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

Commit d533fa78 authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Lorenzo Colitti
Browse files

Move BLOCKED_REASON_* constants from NPMS into ConnectivityManager.

These constants will now be including all the reasons for why an
uid's network access can be blocked, instead of only the
restrictions that could be imposed by NPMS.

Bug: 183473548
Test: atest ./tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Merged-In: I4c544415e12adf442fd2415c371b1b70a39c3aa4
Change-Id: I6dcea43fbefa9eac8b5a971b822a5be5422a54b4
parent 8c9b5df9
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -55,14 +55,6 @@ package android.net {
    method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public boolean isUidRestrictedOnMeteredNetworks(int);
    method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public void registerNetworkPolicyCallback(@Nullable java.util.concurrent.Executor, @NonNull android.net.NetworkPolicyManager.NetworkPolicyCallback);
    method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public void unregisterNetworkPolicyCallback(@NonNull android.net.NetworkPolicyManager.NetworkPolicyCallback);
    field public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 262144; // 0x40000
    field public static final int BLOCKED_METERED_REASON_DATA_SAVER = 65536; // 0x10000
    field public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 131072; // 0x20000
    field public static final int BLOCKED_REASON_APP_STANDBY = 4; // 0x4
    field public static final int BLOCKED_REASON_BATTERY_SAVER = 1; // 0x1
    field public static final int BLOCKED_REASON_DOZE = 2; // 0x2
    field public static final int BLOCKED_REASON_NONE = 0; // 0x0
    field public static final int BLOCKED_REASON_RESTRICTED_MODE = 8; // 0x8
  }

  public static interface NetworkPolicyManager.NetworkPolicyCallback {
+8 −95
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.net;

import static android.app.ActivityManager.procStateToString;
import static android.content.pm.PackageManager.GET_SIGNATURES;
import static android.net.ConnectivityManager.BLOCKED_REASON_NONE;

import android.annotation.IntDef;
import android.annotation.NonNull;
@@ -203,78 +204,6 @@ public class NetworkPolicyManager {
    })
    public @interface SubscriptionOverrideMask {}

    /**
     * Flag to indicate that an app is not subject to any restrictions that could result in its
     * network access blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_NONE = 0;

    /**
     * Flag to indicate that an app is subject to Battery saver restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_BATTERY_SAVER = 1 << 0;

    /**
     * Flag to indicate that an app is subject to Doze restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_DOZE = 1 << 1;

    /**
     * Flag to indicate that an app is subject to App Standby restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_APP_STANDBY = 1 << 2;

    /**
     * Flag to indicate that an app is subject to Restricted mode restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_RESTRICTED_MODE = 1 << 3;

    /**
     * Flag to indicate that an app is subject to Data saver restrictions that would
     * result in its metered network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_METERED_REASON_DATA_SAVER = 1 << 16;

    /**
     * Flag to indicate that an app is subject to user restrictions that would
     * result in its metered network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 1 << 17;

    /**
     * Flag to indicate that an app is subject to Device admin restrictions that would
     * result in its metered network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 1 << 18;

    /** @hide */
    public static final int BLOCKED_METERED_REASON_MASK = 0xffff0000;

@@ -344,22 +273,6 @@ public class NetworkPolicyManager {
    /** @hide */
    public static final int ALLOWED_METERED_REASON_MASK = 0xffff0000;

    /**
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(flag = true, prefix = {"BLOCKED_"}, value = {
            BLOCKED_REASON_NONE,
            BLOCKED_REASON_BATTERY_SAVER,
            BLOCKED_REASON_DOZE,
            BLOCKED_REASON_APP_STANDBY,
            BLOCKED_REASON_RESTRICTED_MODE,
            BLOCKED_METERED_REASON_DATA_SAVER,
            BLOCKED_METERED_REASON_USER_RESTRICTED,
            BLOCKED_METERED_REASON_ADMIN_DISABLED,
    })
    public @interface BlockedReason {}

    private final Context mContext;
    @UnsupportedAppUsage
    private INetworkPolicyManager mService;
@@ -883,14 +796,15 @@ public class NetworkPolicyManager {
     * {@code BLOCKED_REASON_*} and/or {@code BLOCKED_METERED_REASON_*} constants.
     *
     * @param blockedReasons Value indicating the reasons for why the network access of an UID is
     *                       blocked. If the value is equal to {@link #BLOCKED_REASON_NONE}, then
     *                       blocked. If the value is equal to
     *                       {@link ConnectivityManager#BLOCKED_REASON_NONE}, then
     *                       it indicates that an app's network access is not blocked.
     * @param meteredNetwork Value indicating whether the network is metered or not.
     * @return Whether network access is blocked or not.
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static boolean isUidBlocked(@BlockedReason int blockedReasons, boolean meteredNetwork) {
    public static boolean isUidBlocked(int blockedReasons, boolean meteredNetwork) {
        if (blockedReasons == BLOCKED_REASON_NONE) {
            return false;
        }
@@ -913,7 +827,7 @@ public class NetworkPolicyManager {
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    @NonNull
    public static String blockedReasonsToString(@BlockedReason int blockedReasons) {
    public static String blockedReasonsToString(int blockedReasons) {
        return DebugUtils.flagsToString(NetworkPolicyManager.class, "BLOCKED_", blockedReasons);
    }

@@ -977,7 +891,7 @@ public class NetworkPolicyManager {
         * @hide
         */
        @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
        default void onUidBlockedReasonChanged(int uid, @BlockedReason int blockedReasons) {}
        default void onUidBlockedReasonChanged(int uid, int blockedReasons) {}
    }

    /** @hide */
@@ -992,8 +906,7 @@ public class NetworkPolicyManager {
        }

        @Override
        public void onBlockedReasonChanged(int uid, @BlockedReason int oldBlockedReasons,
                @BlockedReason int newBlockedReasons) {
        public void onBlockedReasonChanged(int uid, int oldBlockedReasons, int newBlockedReasons) {
            if (oldBlockedReasons != newBlockedReasons) {
                dispatchOnUidBlockedReasonChanged(mExecutor, mCallback, uid, newBlockedReasons);
            }
@@ -1001,7 +914,7 @@ public class NetworkPolicyManager {
    }

    private static void dispatchOnUidBlockedReasonChanged(@Nullable Executor executor,
            @NonNull NetworkPolicyCallback callback, int uid, @BlockedReason int blockedReasons) {
            @NonNull NetworkPolicyCallback callback, int uid, int blockedReasons) {
        if (executor == null) {
            callback.onUidBlockedReasonChanged(uid, blockedReasons);
        } else {
+8 −0
Original line number Diff line number Diff line
@@ -22,6 +22,14 @@ package android.net {
    method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_TEST_NETWORKS, android.Manifest.permission.NETWORK_STACK}) public void simulateDataStall(int, long, @NonNull android.net.Network, @NonNull android.os.PersistableBundle);
    method @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_SETTINGS, android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void startCaptivePortalApp(@NonNull android.net.Network);
    method public void systemReady();
    field public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 262144; // 0x40000
    field public static final int BLOCKED_METERED_REASON_DATA_SAVER = 65536; // 0x10000
    field public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 131072; // 0x20000
    field public static final int BLOCKED_REASON_APP_STANDBY = 4; // 0x4
    field public static final int BLOCKED_REASON_BATTERY_SAVER = 1; // 0x1
    field public static final int BLOCKED_REASON_DOZE = 2; // 0x2
    field public static final int BLOCKED_REASON_NONE = 0; // 0x0
    field public static final int BLOCKED_REASON_RESTRICTED_MODE = 8; // 0x8
    field public static final String PRIVATE_DNS_MODE_OFF = "off";
    field public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
    field public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
+88 −0
Original line number Diff line number Diff line
@@ -826,6 +826,94 @@ public class ConnectivityManager {
    })
    public @interface PrivateDnsMode {}

    /**
     * Flag to indicate that an app is not subject to any restrictions that could result in its
     * network access blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_NONE = 0;

    /**
     * Flag to indicate that an app is subject to Battery saver restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_BATTERY_SAVER = 1 << 0;

    /**
     * Flag to indicate that an app is subject to Doze restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_DOZE = 1 << 1;

    /**
     * Flag to indicate that an app is subject to App Standby restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_APP_STANDBY = 1 << 2;

    /**
     * Flag to indicate that an app is subject to Restricted mode restrictions that would
     * result in its network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_REASON_RESTRICTED_MODE = 1 << 3;

    /**
     * Flag to indicate that an app is subject to Data saver restrictions that would
     * result in its metered network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_METERED_REASON_DATA_SAVER = 1 << 16;

    /**
     * Flag to indicate that an app is subject to user restrictions that would
     * result in its metered network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 1 << 17;

    /**
     * Flag to indicate that an app is subject to Device admin restrictions that would
     * result in its metered network access being blocked.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 1 << 18;

    /**
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(flag = true, prefix = {"BLOCKED_"}, value = {
            BLOCKED_REASON_NONE,
            BLOCKED_REASON_BATTERY_SAVER,
            BLOCKED_REASON_DOZE,
            BLOCKED_REASON_APP_STANDBY,
            BLOCKED_REASON_RESTRICTED_MODE,
            BLOCKED_METERED_REASON_DATA_SAVER,
            BLOCKED_METERED_REASON_USER_RESTRICTED,
            BLOCKED_METERED_REASON_ADMIN_DISABLED,
    })
    public @interface BlockedReason {}

    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
    private final IConnectivityManager mService;

+1 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import static android.net.ConnectivityDiagnosticsManager.DataStallReport.DETECTI
import static android.net.ConnectivityDiagnosticsManager.DataStallReport.KEY_DNS_CONSECUTIVE_TIMEOUTS;
import static android.net.ConnectivityDiagnosticsManager.DataStallReport.KEY_TCP_METRICS_COLLECTION_PERIOD_MILLIS;
import static android.net.ConnectivityDiagnosticsManager.DataStallReport.KEY_TCP_PACKET_FAIL_RATE;
import static android.net.ConnectivityManager.BLOCKED_REASON_NONE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OPPORTUNISTIC;
import static android.net.ConnectivityManager.TYPE_BLUETOOTH;
@@ -75,7 +76,6 @@ import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_TEST;
import static android.net.NetworkCapabilities.TRANSPORT_VPN;
import static android.net.NetworkPolicyManager.BLOCKED_REASON_NONE;
import static android.net.NetworkPolicyManager.blockedReasonsToString;
import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST;
import static android.net.shared.NetworkMonitorUtils.isPrivateDnsValidationRequired;
Loading