Merge "Delete the location cache after SUSPEND_EXIT." into qt-dev
diff --git a/service/src/com/android/car/CarLocationService.java b/service/src/com/android/car/CarLocationService.java
index ad86741..43d4a46 100644
--- a/service/src/com/android/car/CarLocationService.java
+++ b/service/src/com/android/car/CarLocationService.java
@@ -125,6 +125,11 @@
                     }
                 });
                 break;
+            case CarPowerStateListener.SUSPEND_EXIT:
+                deleteCacheFile();
+                if (future != null) {
+                    future.complete(null);
+                }
             default:
                 // This service does not need to do any work for these events but should still
                 // notify the CarPowerManager that it may proceed.
@@ -153,7 +158,7 @@
             boolean locationEnabled = locationManager.isLocationEnabled();
             logd("isLocationEnabled(): " + locationEnabled);
             if (!locationEnabled) {
-                asyncOperation(() -> deleteCacheFile());
+                deleteCacheFile();
             }
         } else if (action == LocationManager.PROVIDERS_CHANGED_ACTION
                 && shouldCheckLocationPermissions()) {
@@ -163,7 +168,7 @@
                     locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
             logd("isProviderEnabled('gps'): " + gpsEnabled);
             if (!gpsEnabled) {
-                asyncOperation(() -> deleteCacheFile());
+                deleteCacheFile();
             }
         }
     }
@@ -188,7 +193,6 @@
         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         if (location == null) {
             logd("Not storing null location");
-            deleteCacheFile();
         } else {
             logd("Storing location: " + location);
             AtomicFile atomicFile = new AtomicFile(getLocationCacheFile());
@@ -250,6 +254,7 @@
         long currentTime = System.currentTimeMillis();
         if (location.getTime() + TTL_THIRTY_DAYS_MS < currentTime) {
             logd("Location expired.");
+            deleteCacheFile();
         } else {
             location.setTime(currentTime);
             long elapsedTime = SystemClock.elapsedRealtimeNanos();
@@ -297,7 +302,6 @@
                 }
             }
             reader.endObject();
-            deleteCacheFile();
         } catch (FileNotFoundException e) {
             Log.d(TAG, "Location cache file not found.");
         } catch (IOException e) {
diff --git a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
index f653709..4290354 100644
--- a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
@@ -292,10 +292,10 @@
 
     /**
      * Test that the {@link CarLocationService} stores the {@link LocationManager}'s last known
-     * location upon power state-changed SUSPEND events.
+     * location upon power state-changed SHUTDOWN_PREPARE events.
      */
     @Test
-    public void testStoresLocationUponStateChanged() throws Exception {
+    public void testStoresLocationUponShutdownPrepare() throws Exception {
         long currentTime = System.currentTimeMillis();
         long elapsedTime = SystemClock.elapsedRealtimeNanos();
         Location timbuktu = new Location(LocationManager.GPS_PROVIDER);
@@ -367,12 +367,28 @@
         mCarLocationService.init();
         mCarLocationService.onReceive(mMockContext,
                 new Intent(LocationManager.MODE_CHANGED_ACTION));
-        mLatch.await();
         verify(mMockLocationManager, times(1)).isLocationEnabled();
         assertFalse(getLocationCacheFile().exists());
     }
 
     /**
+     * Test that the {@link CarLocationService} deletes location_cache.json when the system resumes
+     * from suspend-to-ram.
+     */
+    @Test
+    public void testDeletesCacheFileUponSuspendExit() throws Exception {
+        when(mMockContext.getSystemService(Context.LOCATION_SERVICE))
+                .thenReturn(mMockLocationManager);
+        when(mMockLocationManager.isLocationEnabled()).thenReturn(false);
+        mCarLocationService.init();
+        CompletableFuture<Void> future = new CompletableFuture<>();
+        mCarLocationService.onStateChanged(CarPowerStateListener.SUSPEND_EXIT, future);
+        assertTrue(future.isDone());
+        verify(mMockLocationManager, times(0)).isLocationEnabled();
+        assertFalse(getLocationCacheFile().exists());
+    }
+
+    /**
      * Test that the {@link CarLocationService} deletes location_cache.json when the GPS location
      * provider is disabled.
      */
@@ -385,7 +401,6 @@
         mCarLocationService.init();
         mCarLocationService.onReceive(mMockContext,
                 new Intent(LocationManager.PROVIDERS_CHANGED_ACTION));
-        mLatch.await();
         verify(mMockLocationManager, times(1))
                 .isProviderEnabled(LocationManager.GPS_PROVIDER);
         assertFalse(getLocationCacheFile().exists());