WifiConfigManager: Skip data migration if new store is present

If the new config store files are present, skip data migration from the
older store file. The old store files may still be present in the device
if WifiConfigStoreLegacy.removeStores() failed for some reason during
the earlier migration.
Also, ensure that we attempt to delete all the store files in
WifiConfigStoreLegacy.removeStores(). Returning early on failure in that
method may leave the device in an incoherent state (some old store files
are deleted, while others are not).

Bug: 63376504
Test: Unit tests
Change-Id: If2e65a7e03677040dfccf88298887e21201aff92
(cherry picked from commit 347faef7fe3f5df209da431eb844bc026a1ba007)
diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java
index 25a5a20..460a9b7 100644
--- a/service/java/com/android/server/wifi/WifiConfigManager.java
+++ b/service/java/com/android/server/wifi/WifiConfigManager.java
@@ -2609,8 +2609,8 @@
 
     /**
      * Migrate data from legacy store files. The function performs the following operations:
-     * 1. Check if the legacy store files are present.
-     * 2. If yes, read all the data from the store files.
+     * 1. Check if the legacy store files are present and the new store files are absent on device.
+     * 2. Read all the data from the store files.
      * 3. Save it to the new store files.
      * 4. Delete the legacy store file.
      *
@@ -2621,6 +2621,12 @@
             Log.d(TAG, "Legacy store files not found. No migration needed!");
             return true;
         }
+        if (mWifiConfigStore.areStoresPresent()) {
+            Log.d(TAG, "New store files found. No migration needed!"
+                    + " Remove legacy store files");
+            mWifiConfigStoreLegacy.removeStores();
+            return true;
+        }
         WifiConfigStoreDataLegacy storeData = mWifiConfigStoreLegacy.read();
         Log.d(TAG, "Reading from legacy store completed");
         loadInternalData(storeData.getConfigurations(), new ArrayList<WifiConfiguration>(),
diff --git a/service/java/com/android/server/wifi/WifiConfigStoreLegacy.java b/service/java/com/android/server/wifi/WifiConfigStoreLegacy.java
index 8677755..39e48a5 100644
--- a/service/java/com/android/server/wifi/WifiConfigStoreLegacy.java
+++ b/service/java/com/android/server/wifi/WifiConfigStoreLegacy.java
@@ -301,24 +301,20 @@
         // First remove all networks from wpa_supplicant and save configuration.
         if (!mWifiNative.removeAllNetworks()) {
             Log.e(TAG, "Removing networks from wpa_supplicant failed");
-            return false;
         }
 
         // Now remove the ipconfig.txt file.
         if (!IP_CONFIG_FILE.delete()) {
             Log.e(TAG, "Removing ipconfig.txt failed");
-            return false;
         }
 
         // Now finally remove network history.txt
         if (!NETWORK_HISTORY_FILE.delete()) {
             Log.e(TAG, "Removing networkHistory.txt failed");
-            return false;
         }
 
         if (!PPS_FILE.delete()) {
             Log.e(TAG, "Removing PerProviderSubscription.conf failed");
-            return false;
         }
 
         Log.i(TAG, "All legacy stores removed!");
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
index 9fa67a0..6e8327d 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
@@ -2487,6 +2487,7 @@
                 new WifiConfigStoreDataLegacy(networks, deletedEphermalSSIDs);
 
         when(mWifiConfigStoreLegacy.areStoresPresent()).thenReturn(true);
+        when(mWifiConfigStore.areStoresPresent()).thenReturn(false);
         when(mWifiConfigStoreLegacy.read()).thenReturn(storeData);
 
         // Now trigger the migration from legacy store. This should populate the in memory list with
@@ -2520,6 +2521,24 @@
     }
 
     /**
+     * Verifies the loading of networks using {@link WifiConfigManager#migrateFromLegacyStore()} ()}
+     * does not attempt to migrate data from legacy stores when the new store files are present
+     * (i.e migration was already done once).
+     */
+    @Test
+    public void testNewStoreFilesPresentNoMigrationFromLegacyStore() throws Exception {
+        when(mWifiConfigStore.areStoresPresent()).thenReturn(true);
+        when(mWifiConfigStoreLegacy.areStoresPresent()).thenReturn(true);
+
+        // Now trigger a migration from legacy store.
+        assertTrue(mWifiConfigManager.migrateFromLegacyStore());
+
+        verify(mWifiConfigStoreLegacy, never()).read();
+        // Verify that we went ahead and deleted the old store files.
+        verify(mWifiConfigStoreLegacy).removeStores();
+    }
+
+    /**
      * Verifies the loading of networks using {@link WifiConfigManager#loadFromStore()} does
      * not attempt to read from any of the stores (new or legacy) when the store files are
      * not present.