Merge "Build module common libs as system_current"
diff --git a/staticlibs/Android.bp b/staticlibs/Android.bp
index 3f867fc..1462fd6 100644
--- a/staticlibs/Android.bp
+++ b/staticlibs/Android.bp
@@ -31,7 +31,13 @@
 
 java_library {
     name: "net-utils-framework-common",
-    srcs: [":net-utils-framework-common-srcs"],
+    srcs: [
+        ":net-utils-framework-common-srcs",
+        // TODO: avoid including all framework annotations as they end up in library users jars
+        // and need jarjaring
+        ":framework-annotations",
+    ],
+    sdk_version: "system_current",
     jarjar_rules: "jarjar-rules-shared.txt",
     visibility: [
         "//frameworks/base/packages/Tethering",
@@ -46,7 +52,11 @@
 
 java_library {
     name: "net-utils-services-common",
-    srcs: ["src_servicescommon/**/*.java"],
+    srcs: [
+        "src_servicescommon/**/*.java",
+        ":framework-annotations",
+    ],
+    sdk_version: "system_current",
     visibility: [
         "//frameworks/base/services",
         "//frameworks/base/packages/Tethering",
diff --git a/staticlibs/jarjar-rules-shared.txt b/staticlibs/jarjar-rules-shared.txt
index 4a2b653..b22771d 100644
--- a/staticlibs/jarjar-rules-shared.txt
+++ b/staticlibs/jarjar-rules-shared.txt
@@ -1 +1,3 @@
 rule android.net.util.** com.android.net.module.util.@1
+rule android.annotation.** com.android.net.module.annotation.@1
+rule com.android.internal.annotations.** com.android.net.module.annotation.@1
\ No newline at end of file
diff --git a/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java b/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java
index 5345789..e71f8fb 100644
--- a/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java
+++ b/staticlibs/src_frameworkcommon/android/net/util/MacAddressUtils.java
@@ -20,11 +20,9 @@
 import android.annotation.Nullable;
 import android.net.MacAddress;
 
-import com.android.internal.util.BitUtils;
-import com.android.internal.util.Preconditions;
-
 import java.security.SecureRandom;
 import java.util.Arrays;
+import java.util.Objects;
 import java.util.Random;
 
 /**
@@ -98,14 +96,15 @@
      * Convert a byte address to long address.
      */
     public static long longAddrFromByteAddr(byte[] addr) {
-        Preconditions.checkNotNull(addr);
+        Objects.requireNonNull(addr);
         if (!isMacAddress(addr)) {
             throw new IllegalArgumentException(
                     Arrays.toString(addr) + " was not a valid MAC address");
         }
         long longAddr = 0;
         for (byte b : addr) {
-            longAddr = (longAddr << 8) + BitUtils.uint8(b);
+            final int uint8Byte = b & 255;
+            longAddr = (longAddr << 8) + uint8Byte;
         }
         return longAddr;
     }
diff --git a/staticlibs/src_servicescommon/android/net/NetworkFactory.java b/staticlibs/src_servicescommon/android/net/NetworkFactory.java
index 479a191..571f796 100644
--- a/staticlibs/src_servicescommon/android/net/NetworkFactory.java
+++ b/staticlibs/src_servicescommon/android/net/NetworkFactory.java
@@ -18,21 +18,19 @@
 
 import android.annotation.NonNull;
 import android.content.Context;
-import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
 import android.os.Messenger;
 import android.util.Log;
-import android.util.SparseArray;
 
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.util.IndentingPrintWriter;
-import com.android.internal.util.Protocol;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -67,8 +65,6 @@
 
     private static final boolean DBG = true;
     private static final boolean VDBG = false;
-
-    private static final int BASE = Protocol.BASE_NETWORK_FACTORY;
     /**
      * Pass a network request to the bearer.  If the bearer believes it can
      * satisfy the request it should connect to the network and create a
@@ -93,33 +89,33 @@
      * msg.arg2 = the ID of the NetworkProvider currently responsible for the
      *            NetworkAgent handling this request, or NetworkProvider.ID_NONE if none.
      */
-    public static final int CMD_REQUEST_NETWORK = BASE;
+    public static final int CMD_REQUEST_NETWORK = 1;
 
     /**
      * Cancel a network request
      * msg.obj = NetworkRequest
      */
-    public static final int CMD_CANCEL_REQUEST = BASE + 1;
+    public static final int CMD_CANCEL_REQUEST = 2;
 
     /**
      * Internally used to set our best-guess score.
      * msg.arg1 = new score
      */
-    private static final int CMD_SET_SCORE = BASE + 2;
+    private static final int CMD_SET_SCORE = 3;
 
     /**
      * Internally used to set our current filter for coarse bandwidth changes with
      * technology changes.
      * msg.obj = new filter
      */
-    private static final int CMD_SET_FILTER = BASE + 3;
+    private static final int CMD_SET_FILTER = 4;
 
     private final Context mContext;
     private final ArrayList<Message> mPreConnectedQueue = new ArrayList<Message>();
     private final String LOG_TAG;
 
-    private final SparseArray<NetworkRequestInfo> mNetworkRequests =
-            new SparseArray<NetworkRequestInfo>();
+    private final Map<NetworkRequest, NetworkRequestInfo> mNetworkRequests =
+            new HashMap<>();
 
     private int mScore;
     private NetworkCapabilities mCapabilityFilter;
@@ -158,7 +154,8 @@
         };
 
         mMessenger = new Messenger(this);
-        mProviderId = ConnectivityManager.from(mContext).registerNetworkProvider(mProvider);
+        mProviderId = ((ConnectivityManager) mContext.getSystemService(
+            Context.CONNECTIVITY_SERVICE)).registerNetworkProvider(mProvider);
     }
 
     public void unregister() {
@@ -168,7 +165,8 @@
         }
         if (DBG) log("Unregistering NetworkFactory");
 
-        ConnectivityManager.from(mContext).unregisterNetworkProvider(mProvider);
+        ((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
+            .unregisterNetworkProvider(mProvider);
         mProvider = null;
     }
 
@@ -240,14 +238,14 @@
      */
     @VisibleForTesting
     protected void handleAddRequest(NetworkRequest request, int score, int servingProviderId) {
-        NetworkRequestInfo n = mNetworkRequests.get(request.requestId);
+        NetworkRequestInfo n = mNetworkRequests.get(request);
         if (n == null) {
             if (DBG) {
                 log("got request " + request + " with score " + score
                         + " and providerId " + servingProviderId);
             }
             n = new NetworkRequestInfo(request, score, servingProviderId);
-            mNetworkRequests.put(n.request.requestId, n);
+            mNetworkRequests.put(n.request, n);
         } else {
             if (VDBG) {
                 log("new score " + score + " for existing request " + request
@@ -263,9 +261,9 @@
 
     @VisibleForTesting
     protected void handleRemoveRequest(NetworkRequest request) {
-        NetworkRequestInfo n = mNetworkRequests.get(request.requestId);
+        NetworkRequestInfo n = mNetworkRequests.get(request);
         if (n != null) {
-            mNetworkRequests.remove(request.requestId);
+            mNetworkRequests.remove(request);
             if (n.requested) releaseNetworkFor(n.request);
         }
     }
@@ -334,7 +332,7 @@
             && (n.score < mScore || n.providerId == mProviderId)
             // If this factory can't satisfy the capability needs of this request, then it
             // should not be tracked.
-            && n.request.networkCapabilities.satisfiedByNetworkCapabilities(mCapabilityFilter)
+            && n.request.satisfiedBy(mCapabilityFilter)
             // Finally if the concrete implementation of the factory rejects the request, then
             // don't track it.
             && acceptRequest(n.request, n.score);
@@ -350,14 +348,12 @@
             // - This factory can't satisfy the capability needs of the request
             // - The concrete implementation of the factory rejects the request
             && ((n.score > mScore && n.providerId != mProviderId)
-                    || !n.request.networkCapabilities.satisfiedByNetworkCapabilities(
-                            mCapabilityFilter)
+                    || !n.request.satisfiedBy(mCapabilityFilter)
                     || !acceptRequest(n.request, n.score));
     }
 
     private void evalRequests() {
-        for (int i = 0; i < mNetworkRequests.size(); i++) {
-            NetworkRequestInfo n = mNetworkRequests.valueAt(i);
+        for (NetworkRequestInfo n : mNetworkRequests.values()) {
             evalRequest(n);
         }
     }
@@ -384,7 +380,12 @@
     protected void releaseRequestAsUnfulfillableByAnyFactory(NetworkRequest r) {
         post(() -> {
             if (DBG) log("releaseRequestAsUnfulfillableByAnyFactory: " + r);
-            ConnectivityManager.from(mContext).declareNetworkRequestUnfulfillable(r);
+            final NetworkProvider provider = mProvider;
+            if (provider == null) {
+                Log.e(LOG_TAG, "Ignoring attempt to release unregistered request as unfulfillable");
+                return;
+            }
+            provider.declareNetworkRequestUnfulfillable(r);
         });
     }
 
@@ -428,13 +429,10 @@
     }
 
     public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
-        final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");
-        pw.println(toString());
-        pw.increaseIndent();
-        for (int i = 0; i < mNetworkRequests.size(); i++) {
-            pw.println(mNetworkRequests.valueAt(i));
+        writer.println(toString());
+        for (NetworkRequestInfo n : mNetworkRequests.values()) {
+            writer.println("  " + n);
         }
-        pw.decreaseIndent();
     }
 
     @Override