Make the NetworkRequest list private to NetworkAgentInfo.
This allows us to keep track of how many live requests a network is satisfying without having to count them every time. Bug: 23113288 Change-Id: Ic4756676491e09071dbf80b7c48da3be028d68eb
This commit is contained in:
@@ -1904,8 +1904,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
pw.increaseIndent();
|
||||
pw.println("Requests:");
|
||||
pw.increaseIndent();
|
||||
for (int i = 0; i < nai.networkRequests.size(); i++) {
|
||||
pw.println(nai.networkRequests.valueAt(i).toString());
|
||||
for (int i = 0; i < nai.numNetworkRequests(); i++) {
|
||||
pw.println(nai.requestAt(i).toString());
|
||||
}
|
||||
pw.decreaseIndent();
|
||||
pw.println("Lingered:");
|
||||
@@ -2265,7 +2265,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);
|
||||
if (nai != null) {
|
||||
if (DBG) {
|
||||
log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size());
|
||||
log(nai.name() + " got DISCONNECTED, was satisfying " + nai.numNetworkRequests());
|
||||
}
|
||||
// A network agent has disconnected.
|
||||
// TODO - if we move the logic to the network agent (have them disconnect
|
||||
@@ -2302,15 +2302,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
mNetworkForNetId.remove(nai.network.netId);
|
||||
}
|
||||
// Remove all previously satisfied requests.
|
||||
for (int i = 0; i < nai.networkRequests.size(); i++) {
|
||||
NetworkRequest request = nai.networkRequests.valueAt(i);
|
||||
for (int i = 0; i < nai.numNetworkRequests(); i++) {
|
||||
NetworkRequest request = nai.requestAt(i);
|
||||
NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId);
|
||||
if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) {
|
||||
mNetworkForRequestId.remove(request.requestId);
|
||||
sendUpdatedScoreToFactories(request, 0);
|
||||
}
|
||||
}
|
||||
if (nai.networkRequests.get(mDefaultRequest.requestId) != null) {
|
||||
if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) {
|
||||
removeDataActivityTracking(nai);
|
||||
notifyLockdownVpn(nai);
|
||||
requestNetworkTransitionWakelock(nai.name());
|
||||
@@ -2402,7 +2402,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
// If this Network is already the highest scoring Network for a request, or if
|
||||
// there is hope for it to become one if it validated, then it is needed.
|
||||
if (nri.request.isRequest() && nai.satisfies(nri.request) &&
|
||||
(nai.networkRequests.get(nri.request.requestId) != null ||
|
||||
(nai.isSatisfyingRequest(nri.request.requestId) ||
|
||||
// Note that this catches two important cases:
|
||||
// 1. Unvalidated cellular will not be reaped when unvalidated WiFi
|
||||
// is currently satisfying the request. This is desirable when
|
||||
@@ -2448,12 +2448,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
// network satisfying it, so this loop is wasteful
|
||||
boolean wasKept = false;
|
||||
for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
|
||||
if (nai.networkRequests.get(nri.request.requestId) != null) {
|
||||
nai.networkRequests.remove(nri.request.requestId);
|
||||
if (nai.isSatisfyingRequest(nri.request.requestId)) {
|
||||
nai.removeRequest(nri.request.requestId);
|
||||
if (VDBG) {
|
||||
log(" Removing from current network " + nai.name() +
|
||||
", leaving " + nai.networkRequests.size() +
|
||||
" requests.");
|
||||
", leaving " + nai.numNetworkRequests() + " requests.");
|
||||
}
|
||||
if (unneeded(nai)) {
|
||||
if (DBG) log("no live requests for " + nai.name() + "; disconnecting");
|
||||
@@ -2480,8 +2479,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
if (wasKept) {
|
||||
// check if any of the remaining requests for this network are for the
|
||||
// same legacy type - if so, don't remove the nai
|
||||
for (int i = 0; i < nai.networkRequests.size(); i++) {
|
||||
NetworkRequest otherRequest = nai.networkRequests.valueAt(i);
|
||||
for (int i = 0; i < nai.numNetworkRequests(); i++) {
|
||||
NetworkRequest otherRequest = nai.requestAt(i);
|
||||
if (otherRequest.legacyType == nri.request.legacyType &&
|
||||
otherRequest.isRequest()) {
|
||||
if (DBG) log(" still have other legacy request - leaving");
|
||||
@@ -2503,7 +2502,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
// listens don't have a singular affectedNetwork. Check all networks to see
|
||||
// if this listen request applies and remove it.
|
||||
for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
|
||||
nai.networkRequests.remove(nri.request.requestId);
|
||||
nai.removeRequest(nri.request.requestId);
|
||||
if (nri.request.networkCapabilities.hasSignalStrength() &&
|
||||
nai.satisfiesImmutableCapabilitiesOf(nri.request)) {
|
||||
updateSignalStrengthThresholds(nai, "RELEASE", nri.request);
|
||||
@@ -4426,8 +4425,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
}
|
||||
|
||||
private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) {
|
||||
for (int i = 0; i < nai.networkRequests.size(); i++) {
|
||||
NetworkRequest nr = nai.networkRequests.valueAt(i);
|
||||
for (int i = 0; i < nai.numNetworkRequests(); i++) {
|
||||
NetworkRequest nr = nai.requestAt(i);
|
||||
// Don't send listening requests to factories. b/17393458
|
||||
if (!nr.isRequest()) continue;
|
||||
sendUpdatedScoreToFactories(nr, nai.getCurrentScore());
|
||||
@@ -4519,13 +4518,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
}
|
||||
|
||||
private void teardownUnneededNetwork(NetworkAgentInfo nai) {
|
||||
for (int i = 0; i < nai.networkRequests.size(); i++) {
|
||||
NetworkRequest nr = nai.networkRequests.valueAt(i);
|
||||
if (nai.numRequestNetworkRequests() != 0) {
|
||||
for (int i = 0; i < nai.numNetworkRequests(); i++) {
|
||||
NetworkRequest nr = nai.requestAt(i);
|
||||
// Ignore listening requests.
|
||||
if (!nr.isRequest()) continue;
|
||||
loge("Dead network still had at least " + nr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
nai.asyncChannel.disconnect();
|
||||
}
|
||||
|
||||
@@ -4625,7 +4626,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
if (VDBG) log("rematch for " + newNetwork.name());
|
||||
if (currentNetwork != null) {
|
||||
if (VDBG) log(" accepting network in place of " + currentNetwork.name());
|
||||
currentNetwork.networkRequests.remove(nri.request.requestId);
|
||||
currentNetwork.removeRequest(nri.request.requestId);
|
||||
currentNetwork.networkLingered.add(nri.request);
|
||||
affectedNetworks.add(currentNetwork);
|
||||
} else {
|
||||
@@ -4649,7 +4650,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
oldDefaultNetwork = currentNetwork;
|
||||
}
|
||||
}
|
||||
} else if (newNetwork.networkRequests.get(nri.request.requestId) != null) {
|
||||
} else if (newNetwork.isSatisfyingRequest(nri.request.requestId)) {
|
||||
// If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri",
|
||||
// mark it as no longer satisfying "nri". Because networks are processed by
|
||||
// rematchAllNetworkAndRequests() in descending score order, "currentNetwork" will
|
||||
@@ -4661,7 +4662,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
log("Network " + newNetwork.name() + " stopped satisfying" +
|
||||
" request " + nri.request.requestId);
|
||||
}
|
||||
newNetwork.networkRequests.remove(nri.request.requestId);
|
||||
newNetwork.removeRequest(nri.request.requestId);
|
||||
if (currentNetwork == newNetwork) {
|
||||
mNetworkForRequestId.remove(nri.request.requestId);
|
||||
sendUpdatedScoreToFactories(nri.request, 0);
|
||||
@@ -4766,8 +4767,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
// (notification callbacks) and then uses the old api (getNetworkInfo(type))
|
||||
// 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 (int i = 0; i < newNetwork.networkRequests.size(); i++) {
|
||||
NetworkRequest nr = newNetwork.networkRequests.valueAt(i);
|
||||
for (int i = 0; i < newNetwork.numNetworkRequests(); i++) {
|
||||
NetworkRequest nr = newNetwork.requestAt(i);
|
||||
if (nr.legacyType != TYPE_NONE && nr.isRequest()) {
|
||||
// legacy type tracker filters out repeat adds
|
||||
mLegacyTypeTracker.add(nr.legacyType, newNetwork);
|
||||
@@ -5027,7 +5028,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo());
|
||||
}
|
||||
NetworkAgentInfo newDefaultAgent = null;
|
||||
if (nai.networkRequests.get(mDefaultRequest.requestId) != null) {
|
||||
if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) {
|
||||
newDefaultAgent = getDefaultNetwork();
|
||||
if (newDefaultAgent != null) {
|
||||
intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO,
|
||||
@@ -5047,8 +5048,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
|
||||
protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) {
|
||||
if (VDBG) log("notifyType " + notifyTypeToName(notifyType) + " for " + networkAgent.name());
|
||||
for (int i = 0; i < networkAgent.networkRequests.size(); i++) {
|
||||
NetworkRequest nr = networkAgent.networkRequests.valueAt(i);
|
||||
for (int i = 0; i < networkAgent.numNetworkRequests(); i++) {
|
||||
NetworkRequest nr = networkAgent.requestAt(i);
|
||||
NetworkRequestInfo nri = mNetworkRequests.get(nr);
|
||||
if (VDBG) log(" sending notification for " + nr);
|
||||
if (nri.mPendingIntent == null) {
|
||||
|
||||
@@ -162,11 +162,13 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
|
||||
private static final int MAXIMUM_NETWORK_SCORE = 100;
|
||||
|
||||
// The list of NetworkRequests being satisfied by this Network.
|
||||
public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>();
|
||||
private final SparseArray<NetworkRequest> mNetworkRequests = new SparseArray<>();
|
||||
// The list of NetworkRequests that this Network previously satisfied with the highest
|
||||
// score. A non-empty list indicates that if this Network was validated it is lingered.
|
||||
// NOTE: This list is only used for debugging.
|
||||
public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>();
|
||||
// How many of the satisfied requests are actual requests and not listens.
|
||||
private int mNumRequestNetworkRequests = 0;
|
||||
|
||||
public final Messenger messenger;
|
||||
public final AsyncChannel asyncChannel;
|
||||
@@ -188,18 +190,63 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
|
||||
networkMisc = misc;
|
||||
}
|
||||
|
||||
// Functions for manipulating the requests satisfied by this network.
|
||||
//
|
||||
// These functions must only called on ConnectivityService's main thread.
|
||||
|
||||
/**
|
||||
* Add {@code networkRequest} to this network as it's satisfied by this network.
|
||||
* NOTE: This function must only be called on ConnectivityService's main thread.
|
||||
* @return true if {@code networkRequest} was added or false if {@code networkRequest} was
|
||||
* already present.
|
||||
*/
|
||||
public boolean addRequest(NetworkRequest networkRequest) {
|
||||
if (networkRequests.get(networkRequest.requestId) == networkRequest) return false;
|
||||
networkRequests.put(networkRequest.requestId, networkRequest);
|
||||
NetworkRequest existing = mNetworkRequests.get(networkRequest.requestId);
|
||||
if (existing == networkRequest) return false;
|
||||
if (existing != null && existing.isRequest()) mNumRequestNetworkRequests--;
|
||||
mNetworkRequests.put(networkRequest.requestId, networkRequest);
|
||||
if (networkRequest.isRequest()) mNumRequestNetworkRequests++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified request from this network.
|
||||
*/
|
||||
public void removeRequest(int requestId) {
|
||||
NetworkRequest existing = mNetworkRequests.get(requestId);
|
||||
if (existing != null && existing.isRequest()) mNumRequestNetworkRequests--;
|
||||
mNetworkRequests.remove(requestId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this network is currently satisfying the request with the specified ID.
|
||||
*/
|
||||
public boolean isSatisfyingRequest(int id) {
|
||||
return mNetworkRequests.get(id) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request at the specified position in the list of requests satisfied by this
|
||||
* network.
|
||||
*/
|
||||
public NetworkRequest requestAt(int index) {
|
||||
return mNetworkRequests.valueAt(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of requests currently satisfied by this network for which
|
||||
* {@link android.net.NetworkRequest#isRequest} returns {@code true}.
|
||||
*/
|
||||
public int numRequestNetworkRequests() {
|
||||
return mNumRequestNetworkRequests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of requests of any type currently satisfied by this network.
|
||||
*/
|
||||
public int numNetworkRequests() {
|
||||
return mNetworkRequests.size();
|
||||
}
|
||||
|
||||
// Does this network satisfy request?
|
||||
public boolean satisfies(NetworkRequest request) {
|
||||
return created &&
|
||||
|
||||
Reference in New Issue
Block a user