Removing unused int network score from eth factory

Hard coded network scores are no longer used however they still exist
in EthernetNetworkFactory. This change removes this unused code in
favor of the NetworkScore object.

Bug: 209679953
Test: m && atest com.android.server.ethernet.EthernetNetworkFactoryTest

Change-Id: Ic9035964fc0e0bdf1dbd1ee845ba3293c753dd8d
diff --git a/java/com/android/server/ethernet/EthernetNetworkAgent.java b/java/com/android/server/ethernet/EthernetNetworkAgent.java
index 5598fc6..57fbce7 100644
--- a/java/com/android/server/ethernet/EthernetNetworkAgent.java
+++ b/java/com/android/server/ethernet/EthernetNetworkAgent.java
@@ -22,6 +22,7 @@
 import android.net.NetworkAgentConfig;
 import android.net.NetworkCapabilities;
 import android.net.NetworkProvider;
+import android.net.NetworkScore;
 import android.os.Looper;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -41,11 +42,10 @@
             @NonNull Looper looper,
             @NonNull NetworkCapabilities nc,
             @NonNull LinkProperties lp,
-            int networkScore,
             @NonNull NetworkAgentConfig config,
             @Nullable NetworkProvider provider,
             @NonNull Callbacks cb) {
-        super(context, looper, TAG, nc, lp, networkScore, config, provider);
+        super(context, looper, TAG, nc, lp, new NetworkScore.Builder().build(), config, provider);
         mCallbacks = cb;
     }
 
diff --git a/java/com/android/server/ethernet/EthernetNetworkFactory.java b/java/com/android/server/ethernet/EthernetNetworkFactory.java
index cd086e7..26532d7 100644
--- a/java/com/android/server/ethernet/EthernetNetworkFactory.java
+++ b/java/com/android/server/ethernet/EthernetNetworkFactory.java
@@ -79,11 +79,9 @@
         }
 
         public EthernetNetworkAgent makeEthernetNetworkAgent(Context context, Looper looper,
-                NetworkCapabilities nc, LinkProperties lp, int networkScore,
-                NetworkAgentConfig config, NetworkProvider provider,
-                EthernetNetworkAgent.Callbacks cb) {
-            return new EthernetNetworkAgent(context, looper, nc, lp, networkScore, config, provider,
-                    cb);
+                NetworkCapabilities nc, LinkProperties lp, NetworkAgentConfig config,
+                NetworkProvider provider, EthernetNetworkAgent.Callbacks cb) {
+            return new EthernetNetworkAgent(context, looper, nc, lp, config, provider, cb);
         }
 
         public InterfaceParams getNetworkInterfaceByName(String name) {
@@ -293,49 +291,24 @@
         private @Nullable IpConfiguration mIpConfig;
 
         /**
-         * An object to contain all transport type information, including base network score and
-         * the legacy transport type it maps to (if any)
-         */
-        private static class TransportInfo {
-            final int mLegacyType;
-            final int mScore;
-
-            private TransportInfo(int legacyType, int score) {
-                mLegacyType = legacyType;
-                mScore = score;
-            }
-        }
-
-        /**
-         * A map of TRANSPORT_* types to TransportInfo, making scoring and legacy type information
-         * available for each type an ethernet interface could propagate.
+         * A map of TRANSPORT_* types to legacy transport types available for each type an ethernet
+         * interface could propagate.
          *
-         * Unfortunately, base scores for the various transports are not yet centrally located.
-         * They've been lifted from the corresponding NetworkFactory files in the meantime.
-         *
-         * Additionally, there are no legacy type equivalents to LOWPAN or WIFI_AWARE. These types
-         * are set to TYPE_NONE to match the behavior of their own network factories.
+         * There are no legacy type equivalents to LOWPAN or WIFI_AWARE. These types are set to
+         * TYPE_NONE to match the behavior of their own network factories.
          */
-        private static final SparseArray<TransportInfo> sTransports = new SparseArray();
+        private static final SparseArray<Integer> sTransports = new SparseArray();
         static {
-            // LowpanInterfaceTracker.NETWORK_SCORE
-            sTransports.put(NetworkCapabilities.TRANSPORT_LOWPAN,
-                    new TransportInfo(ConnectivityManager.TYPE_NONE, 30));
-            // WifiAwareDataPathStateManager.NETWORK_FACTORY_SCORE_AVAIL
-            sTransports.put(NetworkCapabilities.TRANSPORT_WIFI_AWARE,
-                    new TransportInfo(ConnectivityManager.TYPE_NONE, 1));
-            // EthernetNetworkFactory.NETWORK_SCORE
             sTransports.put(NetworkCapabilities.TRANSPORT_ETHERNET,
-                    new TransportInfo(ConnectivityManager.TYPE_ETHERNET, 70));
-            // BluetoothTetheringNetworkFactory.NETWORK_SCORE
+                    ConnectivityManager.TYPE_ETHERNET);
             sTransports.put(NetworkCapabilities.TRANSPORT_BLUETOOTH,
-                    new TransportInfo(ConnectivityManager.TYPE_BLUETOOTH, 69));
-            // WifiNetworkFactory.SCORE_FILTER / NetworkAgent.WIFI_BASE_SCORE
-            sTransports.put(NetworkCapabilities.TRANSPORT_WIFI,
-                    new TransportInfo(ConnectivityManager.TYPE_WIFI, 60));
-            // TelephonyNetworkFactory.TELEPHONY_NETWORK_SCORE
+                    ConnectivityManager.TYPE_BLUETOOTH);
+            sTransports.put(NetworkCapabilities.TRANSPORT_WIFI, ConnectivityManager.TYPE_WIFI);
             sTransports.put(NetworkCapabilities.TRANSPORT_CELLULAR,
-                    new TransportInfo(ConnectivityManager.TYPE_MOBILE, 50));
+                    ConnectivityManager.TYPE_MOBILE);
+            sTransports.put(NetworkCapabilities.TRANSPORT_LOWPAN, ConnectivityManager.TYPE_NONE);
+            sTransports.put(NetworkCapabilities.TRANSPORT_WIFI_AWARE,
+                    ConnectivityManager.TYPE_NONE);
         }
 
         long refCount = 0;
@@ -397,7 +370,7 @@
             mContext = context;
             mNetworkFactory = networkFactory;
             mDeps = deps;
-            int legacyType = ConnectivityManager.TYPE_NONE;
+            final int legacyType;
             int[] transportTypes = mCapabilities.getTransportTypes();
 
             if (transportTypes.length > 0) {
@@ -432,40 +405,7 @@
          * to legacy TYPE_NONE if there is no known conversion
          */
         private static int getLegacyType(int transport) {
-            TransportInfo transportInfo = sTransports.get(transport, /* if dne */ null);
-            if (transportInfo != null) {
-                return transportInfo.mLegacyType;
-            }
-            return ConnectivityManager.TYPE_NONE;
-        }
-
-        /**
-         * Determines the network score based on the transport associated with the interface.
-         * Ethernet interfaces could propagate a transport types forward. Since we can't
-         * get more information about the statuses of the interfaces on the other end of the local
-         * interface, we'll best-effort assign the score as the base score of the assigned transport
-         * when the link is up. When the link is down, the score is set to zero.
-         *
-         * This function is called with the purpose of assigning and updating the network score of
-         * the member NetworkAgent.
-         */
-        private int getNetworkScore() {
-            // never set the network score below 0.
-            if (!mLinkUp) {
-                return 0;
-            }
-
-            int[] transportTypes = mCapabilities.getTransportTypes();
-            if (transportTypes.length < 1) {
-                Log.w(TAG, "Network interface '" + mLinkProperties.getInterfaceName() + "' has no "
-                        + "transport type associated with it. Score set to zero");
-                return 0;
-            }
-            TransportInfo transportInfo = sTransports.get(transportTypes[0], /* if dne */ null);
-            if (transportInfo != null) {
-                return transportInfo.mScore;
-            }
-            return 0;
+            return sTransports.get(transport, ConnectivityManager.TYPE_NONE);
         }
 
         private void start() {
@@ -502,8 +442,8 @@
                     .setLegacyExtraInfo(mHwAddress)
                     .build();
             mNetworkAgent = mDeps.makeEthernetNetworkAgent(mContext, mHandler.getLooper(),
-                    mCapabilities, mLinkProperties, getNetworkScore(), config,
-                    mNetworkFactory.getProvider(), new EthernetNetworkAgent.Callbacks() {
+                    mCapabilities, mLinkProperties, config, mNetworkFactory.getProvider(),
+                    new EthernetNetworkAgent.Callbacks() {
                         @Override
                         public void onNetworkUnwanted() {
                             // if mNetworkAgent is null, we have already called stop.
@@ -620,7 +560,6 @@
                     + "hwAddress: " + mHwAddress + ", "
                     + "networkCapabilities: " + mCapabilities + ", "
                     + "networkAgent: " + mNetworkAgent + ", "
-                    + "score: " + getNetworkScore() + ", "
                     + "ipClient: " + mIpClient + ","
                     + "linkProperties: " + mLinkProperties
                     + "}";
diff --git a/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java b/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java
index cc03ff2..f4bb15e 100644
--- a/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java
+++ b/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java
@@ -20,7 +20,6 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.same;
@@ -90,14 +89,13 @@
     }
 
     private void setupNetworkAgentMock() {
-        when(mDeps.makeEthernetNetworkAgent(any(), any(), any(), any(), anyInt(), any(), any(),
-                any())).thenAnswer(new AnswerWithArguments() {
+        when(mDeps.makeEthernetNetworkAgent(any(), any(), any(), any(), any(), any(), any()))
+                .thenAnswer(new AnswerWithArguments() {
                                        public EthernetNetworkAgent answer(
                                                Context context,
                                                Looper looper,
                                                NetworkCapabilities nc,
                                                LinkProperties lp,
-                                               int networkScore,
                                                NetworkAgentConfig config,
                                                NetworkProvider provider,
                                                EthernetNetworkAgent.Callbacks cb) {
@@ -190,8 +188,7 @@
         mLooper.dispatchAll();
         // provisioning succeeded, verify that the network agent is created, registered, and marked
         // as connected.
-        verify(mDeps).makeEthernetNetworkAgent(any(), any(), any(), any(), anyInt(), any(), any(),
-                any());
+        verify(mDeps).makeEthernetNetworkAgent(any(), any(), any(), any(), any(), any(), any());
         verify(mNetworkAgent).register();
         verify(mNetworkAgent).markConnected();
         clearInvocations(mDeps);
@@ -256,8 +253,8 @@
         assertTrue(mNetFactory.updateInterfaceLinkState(iface, false));
         // There should not be an active IPClient or NetworkAgent.
         verify(mDeps, never()).makeIpClient(any(), any(), any());
-        verify(mDeps, never()).makeEthernetNetworkAgent(any(), any(), any(), any(), anyInt(), any(),
-            any(), any());
+        verify(mDeps, never())
+                .makeEthernetNetworkAgent(any(), any(), any(), any(), any(), any(), any());
     }
 
     @Test
@@ -382,8 +379,7 @@
 
         mIpClientCallbacks.onProvisioningSuccess(new LinkProperties());
         mLooper.dispatchAll();
-        verify(mDeps).makeEthernetNetworkAgent(any(), any(), any(), any(), anyInt(), any(), any(),
-                any());
+        verify(mDeps).makeEthernetNetworkAgent(any(), any(), any(), any(), any(), any(), any());
 
         // verify that unwanted is ignored
         clearInvocations(mIpClient);