[NS B01] Move the computation loop to a separate function

Bug: 113554781
Test: FrameworksNetTests
Change-Id: I6c28c7af5c600d35aa1e9328b6c988dadb921f51
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index b9c718c..30419ae 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -6656,7 +6656,6 @@
     // @param now the time the rematch starts, as returned by SystemClock.elapsedRealtime();
     private void rematchNetworkAndRequests(@NonNull final NetworkReassignment changes,
             @NonNull final NetworkAgentInfo newNetwork) {
-        ensureRunningOnConnectivityServiceThread();
         if (!newNetwork.everConnected) return;
 
         changes.addRematchedNetwork(new NetworkReassignment.NetworkBgStatePair(newNetwork,
@@ -6696,6 +6695,22 @@
         nri.mSatisfier = newSatisfier;
     }
 
+    @NonNull
+    private NetworkReassignment computeNetworkReassignment() {
+        ensureRunningOnConnectivityServiceThread();
+        final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray(
+                new NetworkAgentInfo[mNetworkAgentInfos.size()]);
+        // Rematch higher scoring networks first to prevent requests first matching a lower
+        // scoring network and then a higher scoring network, which could produce multiple
+        // callbacks.
+        Arrays.sort(nais);
+        final NetworkReassignment changes = computeInitialReassignment();
+        for (final NetworkAgentInfo nai : nais) {
+            rematchNetworkAndRequests(changes, nai);
+        }
+        return changes;
+    }
+
     /**
      * Attempt to rematch all Networks with NetworkRequests.  This may result in Networks
      * being disconnected.
@@ -6709,16 +6724,7 @@
         final long now = SystemClock.elapsedRealtime();
         final NetworkAgentInfo oldDefaultNetwork = getDefaultNetwork();
 
-        final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray(
-                new NetworkAgentInfo[mNetworkAgentInfos.size()]);
-        // Rematch higher scoring networks first to prevent requests first matching a lower
-        // scoring network and then a higher scoring network, which could produce multiple
-        // callbacks.
-        Arrays.sort(nais);
-        final NetworkReassignment changes = computeInitialReassignment();
-        for (final NetworkAgentInfo nai : nais) {
-            rematchNetworkAndRequests(changes, nai);
-        }
+        final NetworkReassignment changes = computeNetworkReassignment();
 
         // Now that the entire rematch is computed, update the lists of satisfied requests in
         // the network agents. This is necessary because some code later depends on this state
@@ -6773,6 +6779,8 @@
             }
         }
 
+        final Collection<NetworkAgentInfo> nais = mNetworkAgentInfos.values();
+
         // Update the linger state before processing listen callbacks, because the background
         // computation depends on whether the network is lingering. Don't send the LOSING callbacks
         // just yet though, because they have to be sent after the listens are processed to keep
@@ -6849,7 +6857,7 @@
     private void updateLegacyTypeTrackerAndVpnLockdownForRematch(
             @Nullable final NetworkAgentInfo oldDefaultNetwork,
             @Nullable final NetworkAgentInfo newDefaultNetwork,
-            @NonNull final NetworkAgentInfo[] nais) {
+            @NonNull final Collection<NetworkAgentInfo> nais) {
         if (oldDefaultNetwork != newDefaultNetwork) {
             // Maintain the illusion : since the legacy API only understands one network at a time,
             // if the default network changed, apps should see a disconnected broadcast for the