Merge "NetworkPolicyManager: Add @SystemApi for mainlne module" am: c8b7cff7f4

Change-Id: Iba93c5f26010f647c78aef359e907d5f3b40fde0
diff --git a/api/system-current.txt b/api/system-current.txt
index 1c43e09..b7d0e5f 100755
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -4613,12 +4613,20 @@
 
   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 0f66c79..14442a2 100644
--- a/core/java/android/net/NetworkPolicyManager.java
+++ b/core/java/android/net/NetworkPolicyManager.java
@@ -20,6 +20,7 @@
 
 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.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 @@
      *
      * See network-policy-restrictions.md for more info.
      */
+
     /**
      * No specific rule was set
      * @hide
@@ -120,6 +124,7 @@
      * @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 @@
 
     /** @hide */
     public static final int FIREWALL_RULE_DEFAULT = 0;
-
     /** @hide */
     public static final String FIREWALL_CHAIN_NAME_NONE = "none";
     /** @hide */
@@ -180,6 +184,9 @@
     @UnsupportedAppUsage
     private INetworkPolicyManager mService;
 
+    private final Map<SubscriptionCallback, SubscriptionCallbackProxy>
+            mCallbackMap = new ConcurrentHashMap<>();
+
     /** @hide */
     public NetworkPolicyManager(Context context, INetworkPolicyManager service) {
         if (service == null) {
@@ -286,6 +293,35 @@
     }
 
     /** @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);
@@ -512,6 +548,51 @@
         return WifiInfo.sanitizeSsid(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) { }