From 2d471ca1e141d58a3bea996b4940a5894fd1edce Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Mon, 30 Dec 2019 20:11:36 +0800 Subject: [PATCH 1/4] Add setSubscriptionOverride() to system API Add new API setSubscriptionOverride() in NetworkPolicyManager and rename constants OVERRIDE_* to SUBSCRIPTION_OVERRIDE_*. Make them @SystemApi for mainline support. Bug: 138306002 Test: atest FrameworksNetTests atest FrameworksTelephonyTests Change-Id: I56c777aa66d6f455695f133f9889979c13cd1bc8 --- api/system-current.txt | 6 + .../android/net/NetworkPolicyManager.java | 135 ++++++++++++++++-- .../telephony/SubscriptionManager.java | 16 +-- 3 files changed, 135 insertions(+), 22 deletions(-) diff --git a/api/system-current.txt b/api/system-current.txt index 4bd2ed574e96b..817b9f81d05bb 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -6162,6 +6162,12 @@ package android.net { field public final android.net.WifiKey wifiKey; } + public class NetworkPolicyManager { + method public void setSubscriptionOverride(int, int, int, long, @NonNull String); + field public static final int SUBSCRIPTION_OVERRIDE_CONGESTED = 2; // 0x2 + field public static final int SUBSCRIPTION_OVERRIDE_UNMETERED = 1; // 0x1 + } + public class NetworkProvider { ctor public NetworkProvider(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String); method @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) public void declareNetworkRequestUnfulfillable(@NonNull android.net.NetworkRequest); diff --git a/core/java/android/net/NetworkPolicyManager.java b/core/java/android/net/NetworkPolicyManager.java index de962f8f25e3d..03ec24506375e 100644 --- a/core/java/android/net/NetworkPolicyManager.java +++ b/core/java/android/net/NetworkPolicyManager.java @@ -18,6 +18,9 @@ package android.net; import static android.content.pm.PackageManager.GET_SIGNATURES; +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.SystemApi; import android.annotation.SystemService; import android.app.ActivityManager; import android.compat.annotation.UnsupportedAppUsage; @@ -38,6 +41,8 @@ import android.util.Range; import com.google.android.collect.Sets; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.time.ZonedDateTime; import java.util.HashSet; import java.util.Iterator; @@ -48,14 +53,24 @@ import java.util.Iterator; * {@hide} */ @SystemService(Context.NETWORK_POLICY_SERVICE) +@SystemApi public class NetworkPolicyManager { /* POLICY_* are masks and can be ORed, although currently they are not.*/ - /** No specific network policy, use system default. */ + /** + * No specific network policy, use system default. + * @hide + */ public static final int POLICY_NONE = 0x0; - /** Reject network usage on metered networks when application in background. */ + /** + * Reject network usage on metered networks when application in background. + * @hide + */ public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1; - /** Allow metered network use in the background even when in data usage save mode. */ + /** + * Allow metered network use in the background even when in data usage save mode. + * @hide + */ public static final int POLICY_ALLOW_METERED_BACKGROUND = 0x4; /* @@ -74,49 +89,98 @@ public class NetworkPolicyManager { * * See network-policy-restrictions.md for more info. */ - /** No specific rule was set */ + /** + * No specific rule was set + * @hide + */ public static final int RULE_NONE = 0; - /** Allow traffic on metered networks. */ + /** + * Allow traffic on metered networks. + * @hide + */ public static final int RULE_ALLOW_METERED = 1 << 0; - /** Temporarily allow traffic on metered networks because app is on foreground. */ + /** + * Temporarily allow traffic on metered networks because app is on foreground. + * @hide + */ public static final int RULE_TEMPORARY_ALLOW_METERED = 1 << 1; - /** Reject traffic on metered networks. */ + /** + * Reject traffic on metered networks. + * @hide + */ public static final int RULE_REJECT_METERED = 1 << 2; - /** Network traffic should be allowed on all networks (metered or non-metered), although - * metered-network restrictions could still apply. */ + /** + * Network traffic should be allowed on all networks (metered or non-metered), although + * metered-network restrictions could still apply. + * @hide + */ public static final int RULE_ALLOW_ALL = 1 << 5; - /** Reject traffic on all networks. */ + /** + * Reject traffic on all networks. + * @hide + */ public static final int RULE_REJECT_ALL = 1 << 6; - /** Mask used to get the {@code RULE_xxx_METERED} rules */ + /** + * Mask used to get the {@code RULE_xxx_METERED} rules + * @hide + */ public static final int MASK_METERED_NETWORKS = 0b00001111; - /** Mask used to get the {@code RULE_xxx_ALL} rules */ + /** + * Mask used to get the {@code RULE_xxx_ALL} rules + * @hide + */ public static final int MASK_ALL_NETWORKS = 0b11110000; + /** @hide */ public static final int FIREWALL_RULE_DEFAULT = 0; + /** @hide */ public static final String FIREWALL_CHAIN_NAME_NONE = "none"; + /** @hide */ public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable"; + /** @hide */ public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby"; + /** @hide */ public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave"; private static final boolean ALLOW_PLATFORM_APP_POLICY = true; + /** @hide */ public static final int FOREGROUND_THRESHOLD_STATE = ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; /** * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it * applies to. + * @hide */ public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE"; - public static final int OVERRIDE_UNMETERED = 1 << 0; - public static final int OVERRIDE_CONGESTED = 1 << 1; + /** + * Mask used to check if an override value is marked as unmetered. + */ + public static final int SUBSCRIPTION_OVERRIDE_UNMETERED = 1 << 0; + + /** + * Mask used to check if an override value is marked as congested. + */ + public static final int SUBSCRIPTION_OVERRIDE_CONGESTED = 1 << 1; + + /** + * @hide + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(flag = true, prefix = { "SUBSCRIPTION_OVERRIDE_" }, value = { + SUBSCRIPTION_OVERRIDE_UNMETERED, + SUBSCRIPTION_OVERRIDE_CONGESTED + }) + public @interface SubscriptionOverrideMask {} private final Context mContext; @UnsupportedAppUsage private INetworkPolicyManager mService; + /** @hide */ public NetworkPolicyManager(Context context, INetworkPolicyManager service) { if (service == null) { throw new IllegalArgumentException("missing INetworkPolicyManager"); @@ -125,6 +189,7 @@ public class NetworkPolicyManager { mService = service; } + /** @hide */ @UnsupportedAppUsage public static NetworkPolicyManager from(Context context) { return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); @@ -135,6 +200,7 @@ public class NetworkPolicyManager { * * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, * although it is not validated. + * @hide */ @UnsupportedAppUsage public void setUidPolicy(int uid, int policy) { @@ -152,6 +218,7 @@ public class NetworkPolicyManager { * * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, * although it is not validated. + * @hide */ public void addUidPolicy(int uid, int policy) { try { @@ -168,6 +235,7 @@ public class NetworkPolicyManager { * * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, * although it is not validated. + * @hide */ public void removeUidPolicy(int uid, int policy) { try { @@ -177,6 +245,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public int getUidPolicy(int uid) { try { @@ -186,6 +255,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public int[] getUidsWithPolicy(int policy) { try { @@ -195,6 +265,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public void registerListener(INetworkPolicyListener listener) { try { @@ -204,6 +275,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public void unregisterListener(INetworkPolicyListener listener) { try { @@ -213,6 +285,7 @@ public class NetworkPolicyManager { } } + /** @hide */ public void setNetworkPolicies(NetworkPolicy[] policies) { try { mService.setNetworkPolicies(policies); @@ -221,6 +294,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public NetworkPolicy[] getNetworkPolicies() { try { @@ -230,6 +304,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public void setRestrictBackground(boolean restrictBackground) { try { @@ -239,6 +314,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public boolean getRestrictBackground() { try { @@ -248,6 +324,32 @@ public class NetworkPolicyManager { } } + /** + * Override connections to be temporarily marked as either unmetered or congested, + * along with automatic timeouts if desired. + * + * @param subId the subscriber ID this override applies to. + * @param overrideMask the bitmask that specifies which of the overrides is being + * set or cleared. + * @param overrideValue the override values to set or clear. + * @param timeoutMillis the timeout after which the requested override will + * be automatically cleared, or {@code 0} to leave in the + * requested state until explicitly cleared, or the next reboot, + * whichever happens first + * @param callingPackage the name of the package making the call. + * + */ + public void setSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask, + @SubscriptionOverrideMask int overrideValue, long timeoutMillis, + @NonNull String callingPackage) { + try { + mService.setSubscriptionOverride(subId, overrideMask, overrideValue, timeoutMillis, + callingPackage); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Resets network policy settings back to factory defaults. * @@ -286,6 +388,7 @@ public class NetworkPolicyManager { /** * Check if given UID can have a {@link #setUidPolicy(int, int)} defined, * usually to protect critical system services. + * @hide */ @Deprecated public static boolean isUidValidForPolicy(Context context, int uid) { @@ -353,6 +456,7 @@ public class NetworkPolicyManager { /** * Returns true if {@param procState} is considered foreground and as such will be allowed * to access network when the device is idle or in battery saver mode. Otherwise, false. + * @hide */ public static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) { return procState <= FOREGROUND_THRESHOLD_STATE; @@ -361,16 +465,19 @@ public class NetworkPolicyManager { /** * Returns true if {@param procState} is considered foreground and as such will be allowed * to access network when the device is in data saver mode. Otherwise, false. + * @hide */ public static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) { return procState <= FOREGROUND_THRESHOLD_STATE; } + /** @hide */ public static String resolveNetworkId(WifiConfiguration config) { return WifiInfo.removeDoubleQuotes(config.isPasspoint() ? config.providerFriendlyName : config.SSID); } + /** @hide */ public static String resolveNetworkId(String ssid) { return WifiInfo.removeDoubleQuotes(ssid); } diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java index 8de5b8541f56d..096cb7f48a2d9 100644 --- a/telephony/java/android/telephony/SubscriptionManager.java +++ b/telephony/java/android/telephony/SubscriptionManager.java @@ -16,8 +16,8 @@ package android.telephony; -import static android.net.NetworkPolicyManager.OVERRIDE_CONGESTED; -import static android.net.NetworkPolicyManager.OVERRIDE_UNMETERED; +import static android.net.NetworkPolicyManager.SUBSCRIPTION_OVERRIDE_CONGESTED; +import static android.net.NetworkPolicyManager.SUBSCRIPTION_OVERRIDE_UNMETERED; import android.Manifest; import android.annotation.CallbackExecutor; @@ -2663,9 +2663,9 @@ public class SubscriptionManager { public void setSubscriptionOverrideUnmetered(int subId, boolean overrideUnmetered, @DurationMillisLong long timeoutMillis) { try { - final int overrideValue = overrideUnmetered ? OVERRIDE_UNMETERED : 0; - getNetworkPolicy().setSubscriptionOverride(subId, OVERRIDE_UNMETERED, overrideValue, - timeoutMillis, mContext.getOpPackageName()); + final int overrideValue = overrideUnmetered ? SUBSCRIPTION_OVERRIDE_UNMETERED : 0; + getNetworkPolicy().setSubscriptionOverride(subId, SUBSCRIPTION_OVERRIDE_UNMETERED, + overrideValue, timeoutMillis, mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -2698,9 +2698,9 @@ public class SubscriptionManager { public void setSubscriptionOverrideCongested(int subId, boolean overrideCongested, @DurationMillisLong long timeoutMillis) { try { - final int overrideValue = overrideCongested ? OVERRIDE_CONGESTED : 0; - getNetworkPolicy().setSubscriptionOverride(subId, OVERRIDE_CONGESTED, overrideValue, - timeoutMillis, mContext.getOpPackageName()); + final int overrideValue = overrideCongested ? SUBSCRIPTION_OVERRIDE_CONGESTED : 0; + getNetworkPolicy().setSubscriptionOverride(subId, SUBSCRIPTION_OVERRIDE_CONGESTED, + overrideValue, timeoutMillis, mContext.getOpPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } From 58d955936969b243eee8ed75e1ca822a1b9d570a Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Thu, 16 Jan 2020 21:53:55 +0800 Subject: [PATCH 2/4] Add setSubscriptionPlans() and getSubscriptionPlans() @SystemApi Add two interfaces to communicate with NetworkPolicyManagerService and make them @SystemApi for mainline support. Bug: 138306002 Test: atest FrameworksNetTests atest FrameworksTelephonyTests Change-Id: I9f1168bbc70dce9b2b107e1f946737b1d85599c7 --- api/system-current.txt | 2 ++ .../android/net/NetworkPolicyManager.java | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/api/system-current.txt b/api/system-current.txt index 817b9f81d05bb..46bacc107a86e 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -6163,7 +6163,9 @@ package android.net { } public class NetworkPolicyManager { + method @NonNull public android.telephony.SubscriptionPlan[] getSubscriptionPlans(int, @NonNull String); method public void setSubscriptionOverride(int, int, int, long, @NonNull String); + method public void setSubscriptionPlans(int, @NonNull android.telephony.SubscriptionPlan[], @NonNull String); field public static final int SUBSCRIPTION_OVERRIDE_CONGESTED = 2; // 0x2 field public static final int SUBSCRIPTION_OVERRIDE_UNMETERED = 1; // 0x1 } diff --git a/core/java/android/net/NetworkPolicyManager.java b/core/java/android/net/NetworkPolicyManager.java index 03ec24506375e..0b3f2591b21a6 100644 --- a/core/java/android/net/NetworkPolicyManager.java +++ b/core/java/android/net/NetworkPolicyManager.java @@ -337,7 +337,6 @@ public class NetworkPolicyManager { * requested state until explicitly cleared, or the next reboot, * whichever happens first * @param callingPackage the name of the package making the call. - * */ public void setSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask, @SubscriptionOverrideMask int overrideValue, long timeoutMillis, @@ -350,6 +349,37 @@ public class NetworkPolicyManager { } } + /** + * Set the subscription plans for a specific subscriber. + * + * @param subId the subscriber this relationship applies to. + * @param plans the list of plans. + * @param callingPackage the name of the package making the call + */ + public void setSubscriptionPlans(int subId, @NonNull SubscriptionPlan[] plans, + @NonNull String callingPackage) { + try { + mService.setSubscriptionPlans(subId, plans, callingPackage); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** + * Get subscription plans for the given subscription id. + * + * @param subId the subscriber to get the subscription plans for. + * @param callingPackage the name of the package making the call. + */ + @NonNull + public SubscriptionPlan[] getSubscriptionPlans(int subId, @NonNull String callingPackage) { + try { + return mService.getSubscriptionPlans(subId, callingPackage); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Resets network policy settings back to factory defaults. * From 7e9636055444460a3f4e1379ac052ae04f16e472 Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Thu, 16 Jan 2020 23:31:02 +0800 Subject: [PATCH 3/4] Use Context.NETWORK_POLICY_SERVICE to get network policy service INetworkPolicyManager is a raw AIDL file which won't expose as system API. Let's communicate with network policy service through NetworkPolicyManager instead. Context.NETWORK_POLICY_SERVICE is @SystemApi. Bug: 138306002 Test: atest FrameworksNetTests atest FrameworksTelephonyTests Change-Id: I41bc1cce08f81e2d822250f2d620bff7afe4329b --- .../telephony/SubscriptionManager.java | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java index 096cb7f48a2d9..63a85fa2845c0 100644 --- a/telephony/java/android/telephony/SubscriptionManager.java +++ b/telephony/java/android/telephony/SubscriptionManager.java @@ -45,6 +45,7 @@ import android.content.res.Resources; import android.database.ContentObserver; import android.net.INetworkPolicyManager; import android.net.NetworkCapabilities; +import android.net.NetworkPolicyManager; import android.net.Uri; import android.os.Binder; import android.os.Build; @@ -957,6 +958,11 @@ public class SubscriptionManager { mContext = context; } + private NetworkPolicyManager getNetworkPolicyManager() { + return (NetworkPolicyManager) mContext + .getSystemService(Context.NETWORK_POLICY_SERVICE); + } + /** * @deprecated developers should always obtain references directly from * {@link Context#getSystemService(Class)}. @@ -967,7 +973,7 @@ public class SubscriptionManager { .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); } - private final INetworkPolicyManager getNetworkPolicy() { + private INetworkPolicyManager getINetworkPolicyManager() { if (mNetworkPolicy == null) { mNetworkPolicy = INetworkPolicyManager.Stub.asInterface( TelephonyFrameworkInitializer @@ -2587,14 +2593,10 @@ public class SubscriptionManager { * outlined above. */ public @NonNull List getSubscriptionPlans(int subId) { - try { - SubscriptionPlan[] subscriptionPlans = - getNetworkPolicy().getSubscriptionPlans(subId, mContext.getOpPackageName()); - return subscriptionPlans == null - ? Collections.emptyList() : Arrays.asList(subscriptionPlans); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + SubscriptionPlan[] subscriptionPlans = + getNetworkPolicyManager().getSubscriptionPlans(subId, mContext.getOpPackageName()); + return subscriptionPlans == null + ? Collections.emptyList() : Arrays.asList(subscriptionPlans); } /** @@ -2620,18 +2622,14 @@ public class SubscriptionManager { * defined in {@link SubscriptionPlan}. */ public void setSubscriptionPlans(int subId, @NonNull List plans) { - try { - getNetworkPolicy().setSubscriptionPlans(subId, - plans.toArray(new SubscriptionPlan[plans.size()]), mContext.getOpPackageName()); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + getNetworkPolicyManager().setSubscriptionPlans(subId, + plans.toArray(new SubscriptionPlan[plans.size()]), mContext.getOpPackageName()); } /** @hide */ private String getSubscriptionPlansOwner(int subId) { try { - return getNetworkPolicy().getSubscriptionPlansOwner(subId); + return getINetworkPolicyManager().getSubscriptionPlansOwner(subId); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -2662,13 +2660,10 @@ public class SubscriptionManager { */ public void setSubscriptionOverrideUnmetered(int subId, boolean overrideUnmetered, @DurationMillisLong long timeoutMillis) { - try { - final int overrideValue = overrideUnmetered ? SUBSCRIPTION_OVERRIDE_UNMETERED : 0; - getNetworkPolicy().setSubscriptionOverride(subId, SUBSCRIPTION_OVERRIDE_UNMETERED, - overrideValue, timeoutMillis, mContext.getOpPackageName()); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + + final int overrideValue = overrideUnmetered ? SUBSCRIPTION_OVERRIDE_UNMETERED : 0; + getNetworkPolicyManager().setSubscriptionOverride(subId, SUBSCRIPTION_OVERRIDE_UNMETERED, + overrideValue, timeoutMillis, mContext.getOpPackageName()); } /** @@ -2697,13 +2692,9 @@ public class SubscriptionManager { */ public void setSubscriptionOverrideCongested(int subId, boolean overrideCongested, @DurationMillisLong long timeoutMillis) { - try { - final int overrideValue = overrideCongested ? SUBSCRIPTION_OVERRIDE_CONGESTED : 0; - getNetworkPolicy().setSubscriptionOverride(subId, SUBSCRIPTION_OVERRIDE_CONGESTED, - overrideValue, timeoutMillis, mContext.getOpPackageName()); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + final int overrideValue = overrideCongested ? SUBSCRIPTION_OVERRIDE_CONGESTED : 0; + getNetworkPolicyManager().setSubscriptionOverride(subId, SUBSCRIPTION_OVERRIDE_CONGESTED, + overrideValue, timeoutMillis, mContext.getOpPackageName()); } /** From 9204f468b066e1cf668686ae739d128a0c2ec2bd Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Wed, 25 Dec 2019 00:26:47 +0800 Subject: [PATCH 4/4] NetworkPolicyManager: Add @SystemApi for mainlne module 1) Add NetworkPolicyManager.registerSubscriptionCallback and NetworkPolicyManager.unregisterSubscriptionCallback for registering and unregister. 2) Create SubscriptionCallback to support new @SystemApi so that caller could use this object to register and unregister. Bug: 138306002 Test: FrameworksNetTests FrameworksTelephonyTests Change-Id: I56833254a93383e8054c96d296bcb54b777a6e33 --- api/system-current.txt | 8 ++ .../android/net/NetworkPolicyManager.java | 85 ++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/api/system-current.txt b/api/system-current.txt index 46bacc107a86e..29b73b4d712b6 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -6164,12 +6164,20 @@ package android.net { public class NetworkPolicyManager { method @NonNull public android.telephony.SubscriptionPlan[] getSubscriptionPlans(int, @NonNull String); + method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public void registerSubscriptionCallback(@NonNull android.net.NetworkPolicyManager.SubscriptionCallback); method public void setSubscriptionOverride(int, int, int, long, @NonNull String); method public void setSubscriptionPlans(int, @NonNull android.telephony.SubscriptionPlan[], @NonNull String); + method @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) public void unregisterSubscriptionCallback(@NonNull android.net.NetworkPolicyManager.SubscriptionCallback); field public static final int SUBSCRIPTION_OVERRIDE_CONGESTED = 2; // 0x2 field public static final int SUBSCRIPTION_OVERRIDE_UNMETERED = 1; // 0x1 } + public static class NetworkPolicyManager.SubscriptionCallback { + ctor public NetworkPolicyManager.SubscriptionCallback(); + method public void onSubscriptionOverride(int, int, int); + method public void onSubscriptionPlansChanged(int, @NonNull android.telephony.SubscriptionPlan[]); + } + public class NetworkProvider { ctor public NetworkProvider(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String); method @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) public void declareNetworkRequestUnfulfillable(@NonNull android.net.NetworkRequest); diff --git a/core/java/android/net/NetworkPolicyManager.java b/core/java/android/net/NetworkPolicyManager.java index 0b3f2591b21a6..01800c6751fbd 100644 --- a/core/java/android/net/NetworkPolicyManager.java +++ b/core/java/android/net/NetworkPolicyManager.java @@ -20,6 +20,7 @@ import static android.content.pm.PackageManager.GET_SIGNATURES; import android.annotation.IntDef; import android.annotation.NonNull; +import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.SystemService; import android.app.ActivityManager; @@ -46,11 +47,13 @@ import java.lang.annotation.RetentionPolicy; import java.time.ZonedDateTime; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Manager for creating and modifying network policy rules. * - * {@hide} + * @hide */ @SystemService(Context.NETWORK_POLICY_SERVICE) @SystemApi @@ -89,6 +92,7 @@ public class NetworkPolicyManager { * * See network-policy-restrictions.md for more info. */ + /** * No specific rule was set * @hide @@ -120,6 +124,7 @@ public class NetworkPolicyManager { * @hide */ public static final int RULE_REJECT_ALL = 1 << 6; + /** * Mask used to get the {@code RULE_xxx_METERED} rules * @hide @@ -133,7 +138,6 @@ public class NetworkPolicyManager { /** @hide */ public static final int FIREWALL_RULE_DEFAULT = 0; - /** @hide */ public static final String FIREWALL_CHAIN_NAME_NONE = "none"; /** @hide */ @@ -180,6 +184,9 @@ public class NetworkPolicyManager { @UnsupportedAppUsage private INetworkPolicyManager mService; + private final Map + mCallbackMap = new ConcurrentHashMap<>(); + /** @hide */ public NetworkPolicyManager(Context context, INetworkPolicyManager service) { if (service == null) { @@ -285,6 +292,35 @@ public class NetworkPolicyManager { } } + /** @hide */ + @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) + @SystemApi + public void registerSubscriptionCallback(@NonNull SubscriptionCallback callback) { + if (callback == null) { + throw new NullPointerException("Callback cannot be null."); + } + + final SubscriptionCallbackProxy callbackProxy = new SubscriptionCallbackProxy(callback); + if (null != mCallbackMap.putIfAbsent(callback, callbackProxy)) { + throw new IllegalArgumentException("Callback is already registered."); + } + registerListener(callbackProxy); + } + + /** @hide */ + @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY) + @SystemApi + public void unregisterSubscriptionCallback(@NonNull SubscriptionCallback callback) { + if (callback == null) { + throw new NullPointerException("Callback cannot be null."); + } + + final SubscriptionCallbackProxy callbackProxy = mCallbackMap.remove(callback); + if (callbackProxy == null) return; + + unregisterListener(callbackProxy); + } + /** @hide */ public void setNetworkPolicies(NetworkPolicy[] policies) { try { @@ -512,6 +548,51 @@ public class NetworkPolicyManager { return WifiInfo.removeDoubleQuotes(ssid); } + /** @hide */ + @SystemApi + public static class SubscriptionCallback { + /** + * Notify clients of a new override about a given subscription. + * + * @param subId the subscriber this override applies to. + * @param overrideMask a bitmask that specifies which of the overrides is set. + * @param overrideValue a bitmask that specifies the override values. + */ + public void onSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask, + @SubscriptionOverrideMask int overrideValue) {} + + /** + * Notify of subscription plans change about a given subscription. + * + * @param subId the subscriber id that got subscription plans change. + * @param plans the list of subscription plans. + */ + public void onSubscriptionPlansChanged(int subId, @NonNull SubscriptionPlan[] plans) {} + } + + /** + * SubscriptionCallback proxy for SubscriptionCallback object. + * @hide + */ + public class SubscriptionCallbackProxy extends Listener { + private final SubscriptionCallback mCallback; + + SubscriptionCallbackProxy(SubscriptionCallback callback) { + mCallback = callback; + } + + @Override + public void onSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask, + @SubscriptionOverrideMask int overrideValue) { + mCallback.onSubscriptionOverride(subId, overrideMask, overrideValue); + } + + @Override + public void onSubscriptionPlansChanged(int subId, SubscriptionPlan[] plans) { + mCallback.onSubscriptionPlansChanged(subId, plans); + } + } + /** {@hide} */ public static class Listener extends INetworkPolicyListener.Stub { @Override public void onUidRulesChanged(int uid, int uidRules) { }