diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index a06cc95bc8..175c33b662 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -6512,23 +6512,7 @@ public class ConnectivityService extends IConnectivityManager.Stub } // An accumulator class to gather the list of changes that result from a rematch. - // TODO : enrich to represent an entire set of changes to apply. private static class NetworkReassignment { - static class NetworkBgStatePair { - @NonNull final NetworkAgentInfo mNetwork; - final boolean mOldBackground; - NetworkBgStatePair(@NonNull final NetworkAgentInfo network, - final boolean oldBackground) { - mNetwork = network; - mOldBackground = oldBackground; - } - - public String toString() { - return "[" + NetworkAgentInfo.toShortString(mNetwork) - + " oldBg=" + mOldBackground + "]"; - } - } - static class RequestReassignment { @NonNull public final NetworkRequestInfo mRequest; @Nullable public final NetworkAgentInfo mOldNetwork; @@ -6548,13 +6532,8 @@ public class ConnectivityService extends IConnectivityManager.Stub } } - @NonNull private final Set mRematchedNetworks = new ArraySet<>(); @NonNull private final ArrayList mReassignments = new ArrayList<>(); - @NonNull Iterable getRematchedNetworks() { - return mRematchedNetworks; - } - @NonNull Iterable getRequestReassignments() { return mReassignments; } @@ -6575,10 +6554,6 @@ public class ConnectivityService extends IConnectivityManager.Stub mReassignments.add(reassignment); } - void addRematchedNetwork(@NonNull final NetworkBgStatePair network) { - mRematchedNetworks.add(network); - } - // Will return null if this reassignment does not change the network assigned to // the passed request. @Nullable @@ -6592,9 +6567,7 @@ public class ConnectivityService extends IConnectivityManager.Stub public String toString() { final StringJoiner sj = new StringJoiner(", " /* delimiter */, "NetReassign [" /* prefix */, "]" /* suffix */); - if (mRematchedNetworks.isEmpty() && mReassignments.isEmpty()) { - return sj.add("no changes").toString(); - } + if (mReassignments.isEmpty()) return sj.add("no changes").toString(); for (final RequestReassignment rr : getRequestReassignments()) { sj.add(rr.toString()); } @@ -6604,15 +6577,7 @@ public class ConnectivityService extends IConnectivityManager.Stub public String debugString() { final StringBuilder sb = new StringBuilder(); sb.append("NetworkReassignment :"); - if (mRematchedNetworks.isEmpty() && mReassignments.isEmpty()) { - return sb.append(" no changes").toString(); - } - final StringJoiner sj = new StringJoiner(", " /* delimiter */, - "\n Rematched networks : " /* prefix */, "" /* suffix */); - for (final NetworkBgStatePair rr : mRematchedNetworks) { - sj.add(rr.mNetwork.toShortString()); - } - sb.append(sj.toString()); + if (mReassignments.isEmpty()) return sb.append(" no changes").toString(); for (final RequestReassignment rr : getRequestReassignments()) { sb.append("\n ").append(rr); } @@ -6660,8 +6625,6 @@ public class ConnectivityService extends IConnectivityManager.Stub for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { if (!nai.everConnected) continue; nais.add(nai); - changes.addRematchedNetwork(new NetworkReassignment.NetworkBgStatePair(nai, - nai.isBackgroundNetwork())); } for (final NetworkRequestInfo nri : mNetworkRequests.values()) { @@ -6694,6 +6657,15 @@ public class ConnectivityService extends IConnectivityManager.Stub private void applyNetworkReassignment(@NonNull final NetworkReassignment changes, final long now) { + final Collection nais = mNetworkAgentInfos.values(); + + // Since most of the time there are only 0 or 1 background networks, it would probably + // be more efficient to just use an ArrayList here. TODO : measure performance + final ArraySet oldBgNetworks = new ArraySet<>(); + for (final NetworkAgentInfo nai : nais) { + if (nai.isBackgroundNetwork()) oldBgNetworks.add(nai); + } + // First, update the lists of satisfied requests in the network agents. This is necessary // because some code later depends on this state to be correct, most prominently computing // the linger status. @@ -6742,8 +6714,6 @@ 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 @@ -6760,15 +6730,17 @@ public class ConnectivityService extends IConnectivityManager.Stub } } - for (final NetworkReassignment.NetworkBgStatePair event : changes.getRematchedNetworks()) { + for (final NetworkAgentInfo nai : nais) { + if (!nai.everConnected) continue; + final boolean oldBackground = oldBgNetworks.contains(nai); // Process listen requests and update capabilities if the background state has // changed for this network. For consistency with previous behavior, send onLost // callbacks before onAvailable. - processNewlyLostListenRequests(event.mNetwork); - if (event.mOldBackground != event.mNetwork.isBackgroundNetwork()) { - applyBackgroundChangeForRematch(event.mNetwork); + processNewlyLostListenRequests(nai); + if (oldBackground != nai.isBackgroundNetwork()) { + applyBackgroundChangeForRematch(nai); } - processNewlySatisfiedListenRequests(event.mNetwork); + processNewlySatisfiedListenRequests(nai); } for (final NetworkAgentInfo nai : lingeredNetworks) { @@ -6859,7 +6831,9 @@ public class ConnectivityService extends IConnectivityManager.Stub // they may get old info. Reverse this after the old startUsing api is removed. // This is on top of the multiple intent sequencing referenced in the todo above. for (NetworkAgentInfo nai : nais) { - addNetworkToLegacyTypeTracker(nai); + if (nai.everConnected) { + addNetworkToLegacyTypeTracker(nai); + } } }