Prevent corruption of Wifi scan battery stats

Ensure that the previous wifi scan is noted as completed before noting
that the next one started. This may happen if a scan starts before the
previous one completes.

Bug: 24250984
Change-Id: I2aaa6131cb5dd4b9deae3bea6e19570f6e24632d
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index 2785931..107fee3 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -1897,6 +1897,9 @@
 
     // If workSource is not null, blame is given to it, otherwise blame is given to callingUid.
     private void noteScanStart(int callingUid, WorkSource workSource) {
+        if (lastStartScanTimeStamp != 0) {
+            noteScanEnd();
+        }
         long now = System.currentTimeMillis();
         lastStartScanTimeStamp = now;
         lastScanDuration = 0;
@@ -1931,6 +1934,7 @@
     }
 
     private void noteScanEnd() {
+        closeRadioScanStats();
         long now = System.currentTimeMillis();
         if (lastStartScanTimeStamp != 0) {
             lastScanDuration = now - lastStartScanTimeStamp;
@@ -6021,7 +6025,6 @@
                 case WifiMonitor.SCAN_RESULTS_EVENT:
                 case WifiMonitor.SCAN_FAILED_EVENT:
                     maybeRegisterNetworkFactory(); // Make sure our NetworkFactory is registered
-                    closeRadioScanStats();
                     noteScanEnd();
                     setScanResults();
                     if (mIsFullScanOngoing || mSendScanResultsBroadcast) {