axolotl: gps: The callbacks could be stale in GnssMeasurement

CRs-fixed: 2719986

Change-Id: Ibc4700342bf9cbdefd222ac4a65b42323af7ae80
diff --git a/gps/android/2.0/GnssMeasurement.cpp b/gps/android/2.0/GnssMeasurement.cpp
index b21b004..6cb55ca 100644
--- a/gps/android/2.0/GnssMeasurement.cpp
+++ b/gps/android/2.0/GnssMeasurement.cpp
@@ -71,18 +71,15 @@
         return ret;
     }
 
+    clearInterfaces();
+
     mGnssMeasurementCbIface = callback;
     mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
 
     return mApi->measurementSetCallback(callback);
 }
 
-Return<void> GnssMeasurement::close()  {
-    if (mApi == nullptr) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return Void();
-    }
-
+void GnssMeasurement::clearInterfaces() {
     if (mGnssMeasurementCbIface != nullptr) {
         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface = nullptr;
@@ -95,6 +92,15 @@
         mGnssMeasurementCbIface_2_0->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface_2_0 = nullptr;
     }
+}
+
+Return<void> GnssMeasurement::close()  {
+    if (mApi == nullptr) {
+        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
+        return Void();
+    }
+
+    clearInterfaces();
     mApi->measurementClose();
 
     return Void();
@@ -120,6 +126,8 @@
         return ret;
     }
 
+    clearInterfaces();
+
     mGnssMeasurementCbIface_1_1 = callback;
     mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
 
@@ -149,6 +157,8 @@
         return ret;
     }
 
+    clearInterfaces();
+
     mGnssMeasurementCbIface_2_0 = callback;
     mGnssMeasurementCbIface_2_0->linkToDeath(mGnssMeasurementDeathRecipient, 0);
 
diff --git a/gps/android/2.0/GnssMeasurement.h b/gps/android/2.0/GnssMeasurement.h
index 000b00f..7fa66b4 100644
--- a/gps/android/2.0/GnssMeasurement.h
+++ b/gps/android/2.0/GnssMeasurement.h
@@ -75,6 +75,7 @@
     sp<V1_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1 = nullptr;
     sp<V2_0::IGnssMeasurementCallback> mGnssMeasurementCbIface_2_0 = nullptr;
     MeasurementAPIClient* mApi;
+    void clearInterfaces();
 };
 
 }  // namespace implementation
diff --git a/gps/android/2.0/location_api/MeasurementAPIClient.cpp b/gps/android/2.0/location_api/MeasurementAPIClient.cpp
index accf60b..0ebc3fe 100644
--- a/gps/android/2.0/location_api/MeasurementAPIClient.cpp
+++ b/gps/android/2.0/location_api/MeasurementAPIClient.cpp
@@ -75,6 +75,13 @@
     LOC_LOGD("%s]: ()", __FUNCTION__);
 }
 
+void MeasurementAPIClient::clearInterfaces()
+{
+    mGnssMeasurementCbIface = nullptr;
+    mGnssMeasurementCbIface_1_1 = nullptr;
+    mGnssMeasurementCbIface_2_0 = nullptr;
+}
+
 // for GpsInterface
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
@@ -82,6 +89,7 @@
     LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
 
     mMutex.lock();
+    clearInterfaces();
     mGnssMeasurementCbIface = callback;
     mMutex.unlock();
 
@@ -97,6 +105,7 @@
             __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
 
     mMutex.lock();
+    clearInterfaces();
     mGnssMeasurementCbIface_1_1 = callback;
     mMutex.unlock();
 
@@ -112,6 +121,7 @@
         __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
 
     mMutex.lock();
+    clearInterfaces();
     mGnssMeasurementCbIface_2_0 = callback;
     mMutex.unlock();
 
diff --git a/gps/android/2.0/location_api/MeasurementAPIClient.h b/gps/android/2.0/location_api/MeasurementAPIClient.h
index 67f17ae..6c2d38d 100644
--- a/gps/android/2.0/location_api/MeasurementAPIClient.h
+++ b/gps/android/2.0/location_api/MeasurementAPIClient.h
@@ -78,8 +78,8 @@
     sp<V1_0::IGnssMeasurementCallback> mGnssMeasurementCbIface;
     sp<V1_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1;
     sp<V2_0::IGnssMeasurementCallback> mGnssMeasurementCbIface_2_0;
-
     bool mTracking;
+    void clearInterfaces();
 };
 
 }  // namespace implementation