Convert the Ethernet factory to the new network agent

This is a combination of ag/10147368 and ag/10438581.
ag/10147368 implemented this conversion together with
using the planned NetworkScore API, while ag/10438581
reverted the parts that had to do with NetworkScore.
This is just a squash of these two patches and it
brings the factory in line with the code in rvc-dev.

Test: FrameworksNetTests NetworkStackTests EthernetTetheringTest
Change-Id: Ib214fbbd042f2c0ebd01cf5b0ec33d922629677e
Merged-In: Ie7b70750ef0e17141080f4266dea6155c3601569
diff --git a/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java b/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
index ab2cfc7..f5ab8b7 100644
--- a/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
+++ b/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
@@ -16,7 +16,6 @@
 
 package com.android.server.ethernet;
 
-import static android.net.ConnectivityManager.TYPE_ETHERNET;
 import static android.net.shared.LinkPropertiesParcelableUtil.toStableParcelable;
 import static com.android.internal.util.Preconditions.checkNotNull;
 
@@ -29,10 +28,9 @@
 import android.net.IpConfiguration.ProxySettings;
 import android.net.LinkProperties;
 import android.net.NetworkAgent;
+import android.net.NetworkAgentConfig;
 import android.net.NetworkCapabilities;
 import android.net.NetworkFactory;
-import android.net.NetworkInfo;
-import android.net.NetworkInfo.DetailedState;
 import android.net.NetworkRequest;
 import android.net.NetworkSpecifier;
 import android.net.StringNetworkSpecifier;
@@ -52,7 +50,6 @@
 import com.android.internal.util.IndentingPrintWriter;
 
 import java.io.FileDescriptor;
-import java.lang.Math;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.Objects;
 
@@ -221,12 +218,12 @@
         NetworkInterfaceState network = null;
         if (!TextUtils.isEmpty(requestedIface)) {
             NetworkInterfaceState n = mTrackingInterfaces.get(requestedIface);
-            if (n != null && n.statisified(request.networkCapabilities)) {
+            if (n != null && n.satisfied(request.networkCapabilities)) {
                 network = n;
             }
         } else {
             for (NetworkInterfaceState n : mTrackingInterfaces.values()) {
-                if (n.statisified(request.networkCapabilities) && n.mLinkUp) {
+                if (n.satisfied(request.networkCapabilities) && n.mLinkUp) {
                     network = n;
                     break;
                 }
@@ -247,8 +244,8 @@
         private final NetworkCapabilities mCapabilities;
         private final Handler mHandler;
         private final Context mContext;
-        private final NetworkInfo mNetworkInfo;
         private final NetworkFactory mNetworkFactory;
+        private final int mLegacyType;
 
         private static String sTcpBufferSizes = null;  // Lazy initialized.
 
@@ -375,9 +372,7 @@
             }
 
             mHwAddress = hwAddress;
-            mNetworkInfo = new NetworkInfo(legacyType, 0, NETWORK_TYPE, "");
-            mNetworkInfo.setExtraInfo(mHwAddress);
-            mNetworkInfo.setIsAvailable(true);
+            mLegacyType = legacyType;
         }
 
         void setIpConfig(IpConfiguration ipConfig) {
@@ -386,12 +381,12 @@
                 return;
             }
             this.mIpConfig = ipConfig;
-            if (mNetworkInfo.getDetailedState() != DetailedState.DISCONNECTED) {
+            if (mNetworkAgent != null) {
                 restart();
             }
         }
 
-        boolean statisified(NetworkCapabilities requestedCapabilities) {
+        boolean satisfied(NetworkCapabilities requestedCapabilities) {
             return requestedCapabilities.satisfiedByNetworkCapabilities(mCapabilities);
         }
 
@@ -446,11 +441,9 @@
                 return;
             }
             if (DBG) {
-                Log.d(TAG, String.format("starting IpClient(%s): mNetworkInfo=%s", name,
-                        mNetworkInfo));
+                Log.d(TAG, String.format("Starting Ethernet IpClient(%s)", name));
             }
 
-            mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, mHwAddress);
             mIpClientCallback = new IpClientCallbacksImpl();
             IpClientUtil.makeIpClient(mContext, name, mIpClientCallback);
             mIpClientCallback.awaitIpClientStart();
@@ -468,13 +461,15 @@
                 return;
             }
             mLinkProperties = linkProperties;
-            mNetworkInfo.setDetailedState(DetailedState.CONNECTED, null, mHwAddress);
-            mNetworkInfo.setIsAvailable(true);
 
             // Create our NetworkAgent.
-            mNetworkAgent = new NetworkAgent(mHandler.getLooper(), mContext,
-                    NETWORK_TYPE, mNetworkInfo, mCapabilities, mLinkProperties,
-                    getNetworkScore(), mNetworkFactory.getSerialNumber()) {
+            final NetworkAgentConfig config = new NetworkAgentConfig.Builder()
+                    .setLegacyType(mLegacyType)
+                    .setLegacyTypeName(NETWORK_TYPE)
+                    .build();
+            mNetworkAgent = new NetworkAgent(mContext, mHandler.getLooper(),
+                    NETWORK_TYPE, mCapabilities, mLinkProperties,
+                    getNetworkScore(), config, mNetworkFactory.getProvider()) {
                 public void unwanted() {
                     if (this == mNetworkAgent) {
                         stop();
@@ -484,6 +479,9 @@
                     }  // Otherwise, we've already called stop.
                 }
             };
+            mNetworkAgent.register();
+            mNetworkAgent.setLegacyExtraInfo(mHwAddress);
+            mNetworkAgent.setConnected();
         }
 
         void onIpLayerStopped(LinkProperties linkProperties) {
@@ -526,16 +524,11 @@
             }
             mIpClientCallback = null;
 
-            // ConnectivityService will only forget our NetworkAgent if we send it a NetworkInfo object
-            // with a state of DISCONNECTED or SUSPENDED. So we can't simply clear our NetworkInfo here:
-            // that sets the state to IDLE, and ConnectivityService will still think we're connected.
-            //
-            mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, mHwAddress);
             if (mNetworkAgent != null) {
-                updateAgent();
+                mNetworkAgent.unregister();
                 mNetworkAgent = null;
             }
-            clear();
+            mLinkProperties.clear();
         }
 
         private void updateAgent() {
@@ -543,11 +536,9 @@
             if (DBG) {
                 Log.i(TAG, "Updating mNetworkAgent with: " +
                         mCapabilities + ", " +
-                        mNetworkInfo + ", " +
                         mLinkProperties);
             }
             mNetworkAgent.sendNetworkCapabilities(mCapabilities);
-            mNetworkAgent.sendNetworkInfo(mNetworkInfo);
             mNetworkAgent.sendLinkProperties(mLinkProperties);
 
             // As a note, getNetworkScore() is fairly expensive to calculate. This is fine for now
@@ -556,12 +547,6 @@
             mNetworkAgent.sendNetworkScore(getNetworkScore());
         }
 
-        private void clear() {
-            mLinkProperties.clear();
-            mNetworkInfo.setDetailedState(DetailedState.IDLE, null, null);
-            mNetworkInfo.setIsAvailable(false);
-        }
-
         private static void provisionIpClient(IIpClient ipClient, IpConfiguration config,
                 String tcpBufferSizes) {
             if (config.getProxySettings() == ProxySettings.STATIC ||
@@ -612,7 +597,6 @@
                     + "iface: " + name + ", "
                     + "up: " + mLinkUp + ", "
                     + "hwAddress: " + mHwAddress + ", "
-                    + "networkInfo: " + mNetworkInfo + ", "
                     + "networkCapabilities: " + mCapabilities + ", "
                     + "networkAgent: " + mNetworkAgent + ", "
                     + "score: " + getNetworkScore() + ", "