release-request-b6ab0986-e497-499a-b55d-25aa156be8cf-for-git_oc-dr1-release-4233813 snap-temp-L73000000087867826

Change-Id: I18bd16108c8b1fb89bd3cceec51a423d56a3303c
diff --git a/Common/src/com/googlecode/android_scripting/facade/ConnectivityManagerFacade.java b/Common/src/com/googlecode/android_scripting/facade/ConnectivityManagerFacade.java
index 482f343..98bfed5 100644
--- a/Common/src/com/googlecode/android_scripting/facade/ConnectivityManagerFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/ConnectivityManagerFacade.java
@@ -17,6 +17,8 @@
 package com.googlecode.android_scripting.facade;
 
 import android.app.Service;
+import android.app.usage.NetworkStats.Bucket;
+import android.app.usage.NetworkStatsManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -28,11 +30,13 @@
 import android.net.Network;
 import android.net.NetworkCapabilities;
 import android.net.NetworkInfo;
+import android.net.NetworkPolicy;
+import android.net.NetworkPolicyManager;
 import android.net.NetworkRequest;
 import android.net.StringNetworkSpecifier;
 import android.os.Bundle;
+import android.os.RemoteException;
 import android.provider.Settings;
-
 import com.googlecode.android_scripting.Log;
 import com.googlecode.android_scripting.facade.wifi.WifiAwareManagerFacade;
 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
@@ -44,13 +48,17 @@
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.List;
 
 /**
  * Access ConnectivityManager functions.
@@ -408,6 +416,8 @@
     }
 
     private final ConnectivityManager mManager;
+    private NetworkPolicyManager mNetPolicyManager;
+    private NetworkStatsManager mNetStatsManager;
     private final Service mService;
     private final Context mContext;
     private final ConnectivityReceiver mConnectivityReceiver;
@@ -425,6 +435,9 @@
         mService = manager.getService();
         mContext = mService.getBaseContext();
         mManager = (ConnectivityManager) mService.getSystemService(Context.CONNECTIVITY_SERVICE);
+        mNetPolicyManager = NetworkPolicyManager.from(mContext);
+        mNetStatsManager = (NetworkStatsManager)
+              mService.getSystemService(Context.NETWORK_STATS_SERVICE);
         mEventFacade = manager.getReceiver(EventFacade.class);
         mConnectivityReceiver = new ConnectivityReceiver();
         mTrackingConnectivityStateChange = false;
@@ -791,9 +804,7 @@
         mManager.stopTethering(type);
     }
 
-    @Rpc(description = "Returns the link local IPv6 address of the interface.")
-    public String connectivityGetLinkLocalIpv6Address(@RpcParameter(name = "ifaceName")
-            String ifaceName) {
+    private Enumeration<InetAddress> getInetAddrsForInterface(String ifaceName) {
         NetworkInterface iface = null;
         try {
             iface = NetworkInterface.getByName(ifaceName);
@@ -801,12 +812,20 @@
             return null;
         }
 
-        if (iface == null) {
+        if (iface == null)
+            return null;
+        return iface.getInetAddresses();
+    }
+
+    @Rpc(description = "Returns the link local IPv6 address of the interface.")
+    public String connectivityGetLinkLocalIpv6Address(@RpcParameter(name = "ifaceName")
+            String ifaceName) {
+        Inet6Address inet6Address = null;
+        Enumeration<InetAddress> inetAddresses = getInetAddrsForInterface(ifaceName);
+        if (inetAddresses == null) {
             return null;
         }
 
-        Inet6Address inet6Address = null;
-        Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
         while (inetAddresses.hasMoreElements()) {
             InetAddress addr = inetAddresses.nextElement();
             if (addr instanceof Inet6Address) {
@@ -824,11 +843,94 @@
         return inet6Address.getHostAddress();
     }
 
+    @Rpc(description = "Return IPv4 address of an interface")
+    public List<String> connectivityGetIPv4Addresses(
+            @RpcParameter(name = "ifaceName") String ifaceName) {
+        Enumeration<InetAddress> inetAddresses
+                = getInetAddrsForInterface(ifaceName);
+        if (inetAddresses == null)
+            return null;
+
+        List<String> inetAddrs = new ArrayList<String>();
+        while (inetAddresses.hasMoreElements()) {
+            InetAddress addr = inetAddresses.nextElement();
+            if (addr instanceof Inet4Address) {
+                Inet4Address inet4Address =  (Inet4Address) addr;
+                inetAddrs.add(inet4Address.getHostAddress());
+            }
+        }
+
+        return inetAddrs;
+    }
+
+    @Rpc(description = "Return IPv6 addrs of an interface except link local")
+    public List<String> connectivityGetIPv6Addresses(
+            @RpcParameter(name = "ifaceName") String ifaceName) {
+        Enumeration<InetAddress> inetAddresses
+                = getInetAddrsForInterface(ifaceName);
+        if (inetAddresses == null)
+            return null;
+
+        List<String> inetAddrs = new ArrayList<String>();
+        while (inetAddresses.hasMoreElements()) {
+            InetAddress addr = inetAddresses.nextElement();
+            if (addr instanceof Inet6Address) {
+                if (((Inet6Address) addr).isLinkLocalAddress())
+                    continue;
+                Inet6Address inet6Address =  (Inet6Address) addr;
+                inetAddrs.add(inet6Address.getHostAddress());
+            }
+        }
+
+        return inetAddrs;
+    }
+
     @Rpc(description = "Returns active link properties")
     public LinkProperties connectivityGetActiveLinkProperties() {
         return mManager.getActiveLinkProperties();
     }
 
+    @Rpc(description = "Factory reset of network policies")
+    public void connectivityFactoryResetNetworkPolicies(String subscriberId) {
+        mNetPolicyManager.factoryReset(subscriberId);
+    }
+
+    @Rpc(description = "Set data usage limit for subscription ID")
+    public void connectivitySetDataUsageLimit(
+          String subscriberId, String dataLimit) {
+        NetworkPolicy[] allPolicies = mNetPolicyManager.getNetworkPolicies();
+        for(int i=0; i<allPolicies.length; i++) {
+            String subId = allPolicies[i].template.getSubscriberId();
+            if(subId!=null && subId.equals(subscriberId)) {
+                allPolicies[i].limitBytes = Long.valueOf(dataLimit);
+                break;
+            }
+        }
+        mNetPolicyManager.setNetworkPolicies(allPolicies);
+    }
+
+    @Rpc(description = "Get network stats for device")
+    public long connectivityQuerySummaryForDevice(
+          String subscriberId, Long startTime, Long endTime)
+          throws SecurityException, RemoteException {
+        Bucket bucket = mNetStatsManager.querySummaryForDevice(
+              ConnectivityManager.TYPE_MOBILE, subscriberId, startTime, endTime);
+        return bucket.getTxBytes() + bucket.getRxBytes();
+    }
+
+    @Rpc(description = "Returns all interfaces on the android deivce")
+    public List<NetworkInterface> connectivityGetNetworkInterfaces() {
+        List<NetworkInterface> interfaces = null;
+        try {
+            interfaces = Collections.list(
+                  NetworkInterface.getNetworkInterfaces());
+        } catch (SocketException e) {
+            return null;
+        };
+
+        return interfaces;
+    }
+
     @Override
     public void shutdown() {
         connectivityStopTrackingConnectivityStateChange();