diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index b9c718c269..30419aebd0 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -6656,7 +6656,6 @@ public class ConnectivityService extends IConnectivityManager.Stub // @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 @@ public class ConnectivityService extends IConnectivityManager.Stub 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 @@ public class ConnectivityService extends IConnectivityManager.Stub 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 @@ public class ConnectivityService extends IConnectivityManager.Stub } } + final Collection 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 @@ public class ConnectivityService extends IConnectivityManager.Stub private void updateLegacyTypeTrackerAndVpnLockdownForRematch( @Nullable final NetworkAgentInfo oldDefaultNetwork, @Nullable final NetworkAgentInfo newDefaultNetwork, - @NonNull final NetworkAgentInfo[] nais) { + @NonNull final Collection 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