[NS A24] Add an object to represent changes in assignment

Reupload of I703db6d3f039bd67a90fad0eadffc6cfed9a50ee

Test: ConnectivityServiceTest
Change-Id: I9ef468a17ebcfa684e5614b25dc06fc67eb71c79
This commit is contained in:
Chalard Jean
2019-12-02 15:34:05 +09:00
parent 3b74d613e0
commit 9fc27eab94

View File

@@ -6241,6 +6241,26 @@ 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;
}
}
@NonNull private final Set<NetworkBgStatePair> mRematchedNetworks = new ArraySet<>();
void addRematchedNetwork(@NonNull final NetworkBgStatePair network) {
mRematchedNetworks.add(network);
}
}
private ArrayMap<NetworkRequestInfo, NetworkAgentInfo> computeRequestReassignmentForNetwork( private ArrayMap<NetworkRequestInfo, NetworkAgentInfo> computeRequestReassignmentForNetwork(
@NonNull final NetworkAgentInfo newNetwork) { @NonNull final NetworkAgentInfo newNetwork) {
final int score = newNetwork.getCurrentScore(); final int score = newNetwork.getCurrentScore();
@@ -6286,8 +6306,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
// needed. A network is needed if it is the best network for // needed. A network is needed if it is the best network for
// one or more NetworkRequests, or if it is a VPN. // one or more NetworkRequests, or if it is a VPN.
// //
// - Tears down newNetwork if it just became validated // - Writes into the passed reassignment object all changes that should be done for
// but turns out to be unneeded. // rematching this network with all requests, to be applied later.
// //
// NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy,
// it does not remove NetworkRequests that other Networks could better satisfy. // it does not remove NetworkRequests that other Networks could better satisfy.
@@ -6295,15 +6315,23 @@ public class ConnectivityService extends IConnectivityManager.Stub
// This function should be used when possible instead of {@code rematchAllNetworksAndRequests} // This function should be used when possible instead of {@code rematchAllNetworksAndRequests}
// as it performs better by a factor of the number of Networks. // as it performs better by a factor of the number of Networks.
// //
// TODO : stop writing to the passed reassignment. This is temporarily more useful, but
// it's unidiomatic Java and it's hard to read.
//
// @param changes a currently-building list of changes to write to
// @param newNetwork is the network to be matched against NetworkRequests. // @param newNetwork is the network to be matched against NetworkRequests.
// @param now the time the rematch starts, as returned by SystemClock.elapsedRealtime(); // @param now the time the rematch starts, as returned by SystemClock.elapsedRealtime();
private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, long now) { private void rematchNetworkAndRequests(@NonNull final NetworkReassignment changes,
@NonNull final NetworkAgentInfo newNetwork, final long now) {
ensureRunningOnConnectivityServiceThread(); ensureRunningOnConnectivityServiceThread();
if (!newNetwork.everConnected) return; if (!newNetwork.everConnected) return;
boolean isNewDefault = false; boolean isNewDefault = false;
NetworkAgentInfo oldDefaultNetwork = null; NetworkAgentInfo oldDefaultNetwork = null;
final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork(); final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork();
changes.addRematchedNetwork(new NetworkReassignment.NetworkBgStatePair(newNetwork,
wasBackgroundNetwork));
final int score = newNetwork.getCurrentScore(); final int score = newNetwork.getCurrentScore();
if (VDBG || DDBG) log("rematching " + newNetwork.name()); if (VDBG || DDBG) log("rematching " + newNetwork.name());
@@ -6460,8 +6488,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
// scoring network and then a higher scoring network, which could produce multiple // scoring network and then a higher scoring network, which could produce multiple
// callbacks. // callbacks.
Arrays.sort(nais); Arrays.sort(nais);
final NetworkReassignment changes = new NetworkReassignment();
for (final NetworkAgentInfo nai : nais) { for (final NetworkAgentInfo nai : nais) {
rematchNetworkAndRequests(nai, now); rematchNetworkAndRequests(changes, nai, now);
} }
final NetworkAgentInfo newDefaultNetwork = getDefaultNetwork(); final NetworkAgentInfo newDefaultNetwork = getDefaultNetwork();