From 1a1d2783cb306debec333a0bb1f8db30ddd58c36 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Wed, 4 Dec 2019 20:01:46 +0900 Subject: [PATCH] [NS A44 1/2] Update linger state before processing listens To compute accurately whether a network is in the background, the linger state needs to be updated. Do that before computing whether a network is in the background and possibly calling applyBackgroundChangeForRematch. However ! As of this patch, rematchNetworksAndRequests computes a wrong value when adding to the list of affected networks, because it is looking at intermediate global state. Somehow this used to compensate exactly for the way reading back the state was wrong. There might have been a few undetected bugs there, but none is known. As such, as of this patch, rematchNetworksAndRequests still computes a wrong value while the computation when applying that state now computes the right one, so the tests do not pass. This patch must be checked in together with A44 2/2 which will fix the computation in rematchNetworksAndRequests, but is kept separate for ease of review. Bug: 113554781 Test: ConnectivityServiceTest Change-Id: Iaeed0d11bfa09f292f232ae020e944e430bc0184 --- .../android/server/ConnectivityService.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index b3b1722664..197b0f14a8 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -6777,17 +6777,10 @@ public class ConnectivityService extends IConnectivityManager.Stub } } - for (final NetworkReassignment.NetworkBgStatePair event : changes.getRematchedNetworks()) { - // 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); - } - processNewlySatisfiedListenRequests(event.mNetwork); - } - + // 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 + // backward compatibility. final ArrayList lingeredNetworks = new ArrayList<>(); for (final NetworkAgentInfo nai : nais) { // Rematching may have altered the linger state of some networks, so update all linger @@ -6800,6 +6793,17 @@ public class ConnectivityService extends IConnectivityManager.Stub } } + for (final NetworkReassignment.NetworkBgStatePair event : changes.getRematchedNetworks()) { + // 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); + } + processNewlySatisfiedListenRequests(event.mNetwork); + } + for (final NetworkAgentInfo nai : lingeredNetworks) { notifyNetworkLosing(nai, now); } @@ -7200,8 +7204,6 @@ public class ConnectivityService extends IConnectivityManager.Stub NetworkRequest nr = networkAgent.requestAt(i); NetworkRequestInfo nri = mNetworkRequests.get(nr); if (VDBG) log(" sending notification for " + nr); - // TODO: if we're in the middle of a rematch, can we send a CAP_CHANGED callback for - // a network that no longer satisfies the listen? if (nri.mPendingIntent == null) { callCallbackForRequest(nri, networkAgent, notifyType, arg1); } else {