Cache location upon the prepare shutdown event.

Bug: b/74255015
Test: atest CarLocationServiceTest
Change-Id: I7d241c4c89e8b24e4712bbf370bdf992354c96a3
diff --git a/service/src/com/android/car/CarLocationService.java b/service/src/com/android/car/CarLocationService.java
index f616c28..92c83bc 100644
--- a/service/src/com/android/car/CarLocationService.java
+++ b/service/src/com/android/car/CarLocationService.java
@@ -49,7 +49,8 @@
  * This service stores the last known location from {@link LocationManager} when a car is parked
  * and restores the location when the car is powered on.
  */
-public class CarLocationService extends BroadcastReceiver implements CarServiceBase {
+public class CarLocationService extends BroadcastReceiver implements CarServiceBase,
+        CarPowerManagementService.PowerEventProcessingHandler {
     private static final String TAG = "CarLocationService";
     private static final String FILENAME = "location_cache.json";
     private static final boolean DBG = false;
@@ -62,15 +63,18 @@
     private final Object mLock = new Object();
 
     private final Context mContext;
+    private final CarPowerManagementService mCarPowerManagementService;
     private final CarSensorService mCarSensorService;
     private final CarSensorEventListener mCarSensorEventListener;
     private int mTaskCount = 0;
     private HandlerThread mHandlerThread;
     private Handler mHandler;
 
-    public CarLocationService(Context context, CarSensorService carSensorService) {
+    public CarLocationService(Context context, CarPowerManagementService carPowerManagementService,
+            CarSensorService carSensorService) {
         logd("constructed");
         mContext = context;
+        mCarPowerManagementService = carPowerManagementService;
         mCarSensorService = carSensorService;
         mCarSensorEventListener = new CarSensorEventListener();
     }
@@ -85,6 +89,7 @@
         mContext.registerReceiver(this, filter);
         mCarSensorService.registerOrUpdateSensorListener(
                 CarSensorManager.SENSOR_TYPE_IGNITION_STATE, 0, mCarSensorEventListener);
+        mCarPowerManagementService.registerPowerEventProcessingHandler(this);
     }
 
     @Override
@@ -103,8 +108,23 @@
     }
 
     @Override
+    public long onPrepareShutdown(boolean shuttingDown) {
+        logd("onPrepareShutdown " + shuttingDown);
+        asyncOperation(() -> storeLocation());
+        return 0;
+    }
+
+    @Override
+    public void onPowerOn(boolean displayOn) { }
+
+    @Override
+    public int getWakeupTime() {
+        return 0;
+    }
+
+    @Override
     public void onReceive(Context context, Intent intent) {
-        logd("onReceive" + intent);
+        logd("onReceive " + intent);
         String action = intent.getAction();
         if (action == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
             asyncOperation(() -> loadLocation());
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index 5fcbf6c..95e4bcf 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -32,6 +32,7 @@
 import android.util.Log;
 import android.util.Slog;
 import android.util.TimingsTraceLog;
+
 import com.android.car.cluster.InstrumentClusterService;
 import com.android.car.hal.VehicleHal;
 import com.android.car.internal.FeatureConfiguration;
@@ -39,6 +40,7 @@
 import com.android.car.systeminterface.SystemInterface;
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.car.ICarServiceHelper;
+
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -113,7 +115,8 @@
         mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
         mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
         mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
-        mCarLocationService = new CarLocationService(mContext, mCarSensorService);
+        mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,
+                mCarSensorService);
         mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
         mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
         mCarAudioService = new CarAudioService(serviceContext);