Explicitly pass old score to updateCapabilities

This allows simplification of getCurrentScore function in
NetworkAgentInfo and its return value to depend on everValidated and
lastValidated.

Bug: 31075769
Change-Id: I0b3c85e3a61b006733e900e0a231424878317476
This commit is contained in:
Hugo Benichi
2016-09-15 18:18:48 +09:00
parent 702b0cb04a
commit f15b2824b6
2 changed files with 9 additions and 9 deletions

View File

@@ -2138,7 +2138,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: " Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: "
+ nai.networkCapabilities + " -> " + networkCapabilities); + nai.networkCapabilities + " -> " + networkCapabilities);
} }
updateCapabilities(nai, networkCapabilities); updateCapabilities(nai.getCurrentScore(), nai, networkCapabilities);
break; break;
} }
case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: {
@@ -2215,7 +2215,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
final int oldScore = nai.getCurrentScore(); final int oldScore = nai.getCurrentScore();
nai.lastValidated = valid; nai.lastValidated = valid;
nai.everValidated |= valid; nai.everValidated |= valid;
updateCapabilities(nai, nai.networkCapabilities); updateCapabilities(oldScore, nai, nai.networkCapabilities);
// If score has changed, rebroadcast to NetworkFactories. b/17726566 // If score has changed, rebroadcast to NetworkFactories. b/17726566
if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai); if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai);
} }
@@ -2239,9 +2239,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
// If captive portal status has changed, update capabilities. // If captive portal status has changed, update capabilities.
if (nai != null && (visible != nai.lastCaptivePortalDetected)) { if (nai != null && (visible != nai.lastCaptivePortalDetected)) {
final int oldScore = nai.getCurrentScore();
nai.lastCaptivePortalDetected = visible; nai.lastCaptivePortalDetected = visible;
nai.everCaptivePortalDetected |= visible; nai.everCaptivePortalDetected |= visible;
updateCapabilities(nai, nai.networkCapabilities); updateCapabilities(oldScore, nai, nai.networkCapabilities);
} }
if (!visible) { if (!visible) {
mNotifier.clearNotification(netId); mNotifier.clearNotification(netId);
@@ -4459,10 +4460,13 @@ public class ConnectivityService extends IConnectivityManager.Stub
* augmented with any stateful capabilities implied from {@code networkAgent} * augmented with any stateful capabilities implied from {@code networkAgent}
* (e.g., validated status and captive portal status). * (e.g., validated status and captive portal status).
* *
* @param oldScore score of the network before any of the changes that prompted us
* to call this function.
* @param nai the network having its capabilities updated. * @param nai the network having its capabilities updated.
* @param networkCapabilities the new network capabilities. * @param networkCapabilities the new network capabilities.
*/ */
private void updateCapabilities(NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) { private void updateCapabilities(
int oldScore, NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) {
// Don't modify caller's NetworkCapabilities. // Don't modify caller's NetworkCapabilities.
networkCapabilities = new NetworkCapabilities(networkCapabilities); networkCapabilities = new NetworkCapabilities(networkCapabilities);
if (nai.lastValidated) { if (nai.lastValidated) {
@@ -4476,7 +4480,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
} }
if (!Objects.equals(nai.networkCapabilities, networkCapabilities)) { if (!Objects.equals(nai.networkCapabilities, networkCapabilities)) {
final int oldScore = nai.getCurrentScore();
if (nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) != if (nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) !=
networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) {
try { try {

View File

@@ -354,10 +354,7 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
} }
int score = currentScore; int score = currentScore;
// Use NET_CAPABILITY_VALIDATED here instead of lastValidated, this allows if (!lastValidated && !pretendValidated) {
// ConnectivityService.updateCapabilities() to compute the old score prior to updating
// networkCapabilities (with a potentially different validated state).
if (!networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED) && !pretendValidated) {
score -= UNVALIDATED_SCORE_PENALTY; score -= UNVALIDATED_SCORE_PENALTY;
} }
if (score < 0) score = 0; if (score < 0) score = 0;