Remove stale connected WifiEntry in handleOnStart

A connected WifiEntry that disconnects during screen off may appear in
the picker list after screen on for a few seconds even if there are no
recent scan results for it. This is due to the stale WifiInfo preventing
the scan update logic from removing it as an out-of-range network.

Fix this by making sure the WifiInfo is cleared before updating the scan
info.

Flag: EXEMPT minor UI fix

Bug: 427107455
Test: atest WifiPickerTrackerTest
Change-Id: I035d9e9b6ffeeba15f0aef45fb75fc26c7051bce
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java
index 81a8004..33fc549 100644
--- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java
+++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/WifiPickerTracker.java
@@ -291,19 +291,18 @@
     @WorkerThread
     @Override
     protected void handleOnStart() {
-        // Update configs and scans
         updateWifiConfigurationsInternal();
         updatePasspointConfigurations(mWifiManager.getPasspointConfigurations());
-        conditionallyUpdateScanResults(true /* pollScans */, true /* timeoutScans */);
 
-        // Trigger callbacks manually now to avoid waiting until the first calls to update state.
-        handleDefaultSubscriptionChanged(SubscriptionManager.getDefaultDataSubscriptionId());
         // Clear any stale connection info in case we missed any NetworkCallback.onLost() while in
         // the stopped state, but don't notify the listener to avoid flicker from disconnected ->
         // connected in case the network is still the same.
         for (WifiEntry entry : getAllWifiEntries()) {
             entry.clearConnectionInfo(false);
         }
+
+        // Trigger callbacks manually now to avoid waiting until the first calls to update state.
+        handleDefaultSubscriptionChanged(SubscriptionManager.getDefaultDataSubscriptionId());
         Network currentNetwork = mWifiManager.getCurrentNetwork();
         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
         if (currentNetwork != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
@@ -322,6 +321,11 @@
                 handleLinkPropertiesChanged(currentNetwork, linkProperties);
             }
         }
+
+        // Update scan results after refreshing the connection info above, or else the old WifiInfo
+        // rssi will prevent the previously connected network from being removed on scan empty.
+        conditionallyUpdateScanResults(true /* pollScans */, true /* timeoutScans */);
+
         notifyOnNumSavedNetworksChanged();
         notifyOnNumSavedSubscriptionsChanged();
         updateWifiEntries();
diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java
index 90b265d..c6b54ba 100644
--- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java
+++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/WifiPickerTrackerTest.java
@@ -2235,6 +2235,7 @@
         wifiPickerTracker.onStart();
         mTestLooper.dispatchAll();
         // Entry should be disconnected.
+        assertThat(wifiPickerTracker.getConnectedWifiEntry()).isNull();
         assertThat(entry.getConnectedState()).isEqualTo(CONNECTED_STATE_DISCONNECTED);
     }