diff --git a/api/system-current.txt b/api/system-current.txt index f538061b4202b..5b65eaa65becc 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -6203,6 +6203,22 @@ package android.net { field public final android.net.WifiKey wifiKey; } + 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 de962f8f25e3d..01800c6751fbd 100644 --- a/core/java/android/net/NetworkPolicyManager.java +++ b/core/java/android/net/NetworkPolicyManager.java @@ -18,6 +18,10 @@ package android.net; 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; import android.compat.annotation.UnsupportedAppUsage; @@ -38,24 +42,38 @@ 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; +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 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 +92,102 @@ 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; + private final Map + mCallbackMap = new ConcurrentHashMap<>(); + + /** @hide */ public NetworkPolicyManager(Context context, INetworkPolicyManager service) { if (service == null) { throw new IllegalArgumentException("missing INetworkPolicyManager"); @@ -125,6 +196,7 @@ public class NetworkPolicyManager { mService = service; } + /** @hide */ @UnsupportedAppUsage public static NetworkPolicyManager from(Context context) { return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); @@ -135,6 +207,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 +225,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 +242,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 +252,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public int getUidPolicy(int uid) { try { @@ -186,6 +262,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public int[] getUidsWithPolicy(int policy) { try { @@ -195,6 +272,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public void registerListener(INetworkPolicyListener listener) { try { @@ -204,6 +282,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public void unregisterListener(INetworkPolicyListener listener) { try { @@ -213,6 +292,36 @@ 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 { mService.setNetworkPolicies(policies); @@ -221,6 +330,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public NetworkPolicy[] getNetworkPolicies() { try { @@ -230,6 +340,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public void setRestrictBackground(boolean restrictBackground) { try { @@ -239,6 +350,7 @@ public class NetworkPolicyManager { } } + /** @hide */ @UnsupportedAppUsage public boolean getRestrictBackground() { try { @@ -248,6 +360,62 @@ 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(); + } + } + + /** + * 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. * @@ -286,6 +454,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 +522,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,20 +531,68 @@ 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); } + /** @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) { } diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java index 8de5b8541f56d..63a85fa2845c0 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; @@ -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 ? OVERRIDE_UNMETERED : 0; - getNetworkPolicy().setSubscriptionOverride(subId, 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 ? OVERRIDE_CONGESTED : 0; - getNetworkPolicy().setSubscriptionOverride(subId, 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()); } /**