Snap for 10453563 from e3b913f679647534152b51aed8f2338fd746f369 to mainline-art-release

Change-Id: I79f7b8d0b776d31e205e3780d1bd979667823baa
diff --git a/android/1.0/GnssMeasurement.cpp b/android/1.0/GnssMeasurement.cpp
index 7af9e27..9a99371 100644
--- a/android/1.0/GnssMeasurement.cpp
+++ b/android/1.0/GnssMeasurement.cpp
@@ -18,11 +18,15 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #define LOG_TAG "LocSvc_GnssMeasurementInterface"
 
 #include <log_util.h>
-#include <MeasurementAPIClient.h>
-#include "GnssMeasurement.h"
+#include <GnssMeasurement.h>
 
 namespace android {
 namespace hardware {
@@ -52,44 +56,47 @@
 }
 
 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
-
-Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
         const sp<V1_0::IGnssMeasurementCallback>& callback)  {
+    return setCallback(callback, mGnssMeasurementCbIface, GNSS_POWER_MODE_INVALID);
+}
 
-    Return<IGnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
+template <typename T>
+Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+        const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode) {
+    Return<GnssMeasurement::GnssMeasurementStatus> ret =
+            IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
         return ret;
     }
+    if (myCallback != callback) {
+        if (nullptr == callback) {
+            LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
+            mApi->measurementSetCallback(callback);
+            close();
+        } else {
+            if (nullptr != myCallback) {
+                myCallback->unlinkToDeath(mGnssMeasurementDeathRecipient);
+            }
+            myCallback = callback;
+            myCallback->linkToDeath(mGnssMeasurementDeathRecipient, 0);
+            ret = mApi->measurementSetCallback(callback, powerMode);
+        }
+    }
 
-    mGnssMeasurementCbIface = callback;
-    mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    return mApi->measurementSetCallback(callback);
-
+    return ret;
 }
 
 Return<void> GnssMeasurement::close()  {
-    if (mApi == nullptr) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return Void();
+    if (mApi != nullptr) {
+        mApi->measurementSetCallback<V1_0::IGnssMeasurementCallback>(nullptr);
     }
 
     if (mGnssMeasurementCbIface != nullptr) {
         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface = nullptr;
     }
-    mApi->measurementClose();
 
     return Void();
 }
diff --git a/android/1.0/GnssMeasurement.h b/android/1.0/GnssMeasurement.h
index 4247dbf..5511b69 100644
--- a/android/1.0/GnssMeasurement.h
+++ b/android/1.0/GnssMeasurement.h
@@ -18,12 +18,18 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #ifndef ANDROID_HARDWARE_GNSS_V1_0_GNSSMEASUREMENT_H
 #define ANDROID_HARDWARE_GNSS_V1_0_GNSSMEASUREMENT_H
 
 #include <android/hardware/gnss/1.0/IGnssMeasurement.h>
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
+#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -62,7 +68,9 @@
         sp<GnssMeasurement> mGnssMeasurement;
     };
 
- private:
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> setCallback(
+            const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode);
     sp<GnssMeasurementDeathRecipient> mGnssMeasurementDeathRecipient = nullptr;
     sp<V1_0::IGnssMeasurementCallback> mGnssMeasurementCbIface = nullptr;
     MeasurementAPIClient* mApi;
diff --git a/android/1.0/location_api/MeasurementAPIClient.cpp b/android/1.0/location_api/MeasurementAPIClient.cpp
index 8dd039b..ef947f7 100644
--- a/android/1.0/location_api/MeasurementAPIClient.cpp
+++ b/android/1.0/location_api/MeasurementAPIClient.cpp
@@ -63,19 +63,6 @@
     LOC_LOGD("%s]: ()", __FUNCTION__);
 }
 
-// for GpsInterface
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
-{
-    LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
-
-    mMutex.lock();
-    mGnssMeasurementCbIface = callback;
-    mMutex.unlock();
-
-    return startTracking();
-}
-
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::startTracking()
 {
@@ -83,16 +70,6 @@
     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
     locationCallbacks.size = sizeof(LocationCallbacks);
 
-    locationCallbacks.trackingCb = nullptr;
-    locationCallbacks.batchingCb = nullptr;
-    locationCallbacks.geofenceBreachCb = nullptr;
-    locationCallbacks.geofenceStatusCb = nullptr;
-    locationCallbacks.gnssLocationInfoCb = nullptr;
-    locationCallbacks.gnssNiCb = nullptr;
-    locationCallbacks.gnssSvCb = nullptr;
-    locationCallbacks.gnssNmeaCb = nullptr;
-
-    locationCallbacks.gnssMeasurementsCb = nullptr;
     if (mGnssMeasurementCbIface != nullptr) {
         locationCallbacks.gnssMeasurementsCb =
             [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
@@ -109,7 +86,7 @@
     options.mode = GNSS_SUPL_MODE_STANDALONE;
 
     mTracking = true;
-    LOC_LOGD("%s]: start tracking session", __FUNCTION__);
+    LOC_LOGd();
     locAPIStartTracking(options);
     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
 }
diff --git a/android/1.0/location_api/MeasurementAPIClient.h b/android/1.0/location_api/MeasurementAPIClient.h
index 225deac..1c86882 100644
--- a/android/1.0/location_api/MeasurementAPIClient.h
+++ b/android/1.0/location_api/MeasurementAPIClient.h
@@ -53,8 +53,15 @@
     MeasurementAPIClient& operator=(const MeasurementAPIClient&) = delete;
 
     // for GpsMeasurementInterface
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
-            const sp<V1_0::IGnssMeasurementCallback>& callback);
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
+            const sp<T>& callback, GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID) {
+        mMutex.lock();
+        setCallbackLocked(callback);
+        mMutex.unlock();
+
+        return startTracking();
+    }
     void measurementClose();
     Return<IGnssMeasurement::GnssMeasurementStatus> startTracking();
 
@@ -62,6 +69,9 @@
     void onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification) final;
 
 private:
+    inline void setCallbackLocked(const sp<V1_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface = callback;
+    }
     virtual ~MeasurementAPIClient();
 
     std::mutex mMutex;
diff --git a/android/1.1/GnssMeasurement.cpp b/android/1.1/GnssMeasurement.cpp
index 6f8c14d..70ceee2 100644
--- a/android/1.1/GnssMeasurement.cpp
+++ b/android/1.1/GnssMeasurement.cpp
@@ -18,10 +18,14 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #define LOG_TAG "LocSvc_GnssMeasurementInterface"
 
 #include <log_util.h>
-#include <MeasurementAPIClient.h>
 #include "GnssMeasurement.h"
 
 namespace android {
@@ -52,37 +56,42 @@
 }
 
 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
-
-Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
         const sp<V1_0::IGnssMeasurementCallback>& callback)  {
+    return setCallback(callback, mGnssMeasurementCbIface, GNSS_POWER_MODE_INVALID);
+}
 
-    Return<IGnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
+template <typename T>
+Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+        const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode) {
+    Return<GnssMeasurement::GnssMeasurementStatus> ret =
+            IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
         return ret;
     }
+    if (myCallback != callback) {
+        if (nullptr == callback) {
+            LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
+            mApi->measurementSetCallback(callback);
+            close();
+        } else {
+            if (nullptr != myCallback) {
+                myCallback->unlinkToDeath(mGnssMeasurementDeathRecipient);
+            }
+            myCallback = callback;
+            myCallback->linkToDeath(mGnssMeasurementDeathRecipient, 0);
+            ret = mApi->measurementSetCallback(callback, powerMode);
+        }
+    }
 
-    mGnssMeasurementCbIface = callback;
-    mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    return mApi->measurementSetCallback(callback);
-
+    return ret;
 }
 
 Return<void> GnssMeasurement::close()  {
-    if (mApi == nullptr) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return Void();
+    if (mApi != nullptr) {
+        mApi->measurementSetCallback<V1_0::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V1_1::IGnssMeasurementCallback>(nullptr);
     }
 
     if (mGnssMeasurementCbIface != nullptr) {
@@ -93,7 +102,6 @@
         mGnssMeasurementCbIface_1_1->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface_1_1 = nullptr;
     }
-    mApi->measurementClose();
 
     return Void();
 }
@@ -101,30 +109,8 @@
 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
         const sp<IGnssMeasurementCallback>& callback, bool enableFullTracking) {
-
-    Return<IGnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_1_1 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    mGnssMeasurementCbIface_1_1 = callback;
-    mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking?
-            GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_1_1(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_1_1,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 
 }  // namespace implementation
diff --git a/android/1.1/GnssMeasurement.h b/android/1.1/GnssMeasurement.h
index 373f0d0..c497c7d 100644
--- a/android/1.1/GnssMeasurement.h
+++ b/android/1.1/GnssMeasurement.h
@@ -18,12 +18,18 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #ifndef ANDROID_HARDWARE_GNSS_V1_1_GNSSMEASUREMENT_H
 #define ANDROID_HARDWARE_GNSS_V1_1_GNSSMEASUREMENT_H
 
 #include <android/hardware/gnss/1.1/IGnssMeasurement.h>
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
+#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -67,7 +73,9 @@
         sp<GnssMeasurement> mGnssMeasurement;
     };
 
- private:
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> setCallback(
+            const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode);
     sp<GnssMeasurementDeathRecipient> mGnssMeasurementDeathRecipient = nullptr;
     sp<V1_0::IGnssMeasurementCallback> mGnssMeasurementCbIface = nullptr;
     sp<IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1 = nullptr;
diff --git a/android/1.1/location_api/MeasurementAPIClient.cpp b/android/1.1/location_api/MeasurementAPIClient.cpp
index af8095c..4ebb4e0 100644
--- a/android/1.1/location_api/MeasurementAPIClient.cpp
+++ b/android/1.1/location_api/MeasurementAPIClient.cpp
@@ -66,34 +66,6 @@
     LOC_LOGD("%s]: ()", __FUNCTION__);
 }
 
-// for GpsInterface
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
-{
-    LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
-
-    mMutex.lock();
-    mGnssMeasurementCbIface = callback;
-    mMutex.unlock();
-
-    return startTracking();
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_1_1(
-        const sp<IGnssMeasurementCallback>& callback,
-        GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-            __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    mGnssMeasurementCbIface_1_1 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::startTracking(
         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
@@ -102,16 +74,6 @@
     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
     locationCallbacks.size = sizeof(LocationCallbacks);
 
-    locationCallbacks.trackingCb = nullptr;
-    locationCallbacks.batchingCb = nullptr;
-    locationCallbacks.geofenceBreachCb = nullptr;
-    locationCallbacks.geofenceStatusCb = nullptr;
-    locationCallbacks.gnssLocationInfoCb = nullptr;
-    locationCallbacks.gnssNiCb = nullptr;
-    locationCallbacks.gnssSvCb = nullptr;
-    locationCallbacks.gnssNmeaCb = nullptr;
-
-    locationCallbacks.gnssMeasurementsCb = nullptr;
     if (mGnssMeasurementCbIface_1_1 != nullptr || mGnssMeasurementCbIface != nullptr) {
         locationCallbacks.gnssMeasurementsCb =
             [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
@@ -132,7 +94,7 @@
     }
 
     mTracking = true;
-    LOC_LOGD("%s]: start tracking session", __FUNCTION__);
+    LOC_LOGd("(powermode: %d) (tbm %d)", (int)powerMode, timeBetweenMeasurement);
     locAPIStartTracking(options);
     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
 }
diff --git a/android/1.1/location_api/MeasurementAPIClient.h b/android/1.1/location_api/MeasurementAPIClient.h
index 5fb307c..e768e77 100644
--- a/android/1.1/location_api/MeasurementAPIClient.h
+++ b/android/1.1/location_api/MeasurementAPIClient.h
@@ -53,12 +53,15 @@
     MeasurementAPIClient& operator=(const MeasurementAPIClient&) = delete;
 
     // for GpsMeasurementInterface
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
-            const sp<V1_0::IGnssMeasurementCallback>& callback);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_1_1(
-            const sp<IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
+            const sp<T>& callback, GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID) {
+        mMutex.lock();
+        setCallbackLocked(callback);
+        mMutex.unlock();
+
+        return startTracking(powerMode);
+    }
     void measurementClose();
     Return<IGnssMeasurement::GnssMeasurementStatus> startTracking(
             GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
@@ -68,6 +71,12 @@
     void onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification) final;
 
 private:
+    inline void setCallbackLocked(const sp<V1_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface = callback;
+    }
+    inline void setCallbackLocked(const sp<V1_1::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_1_1 = callback;
+    }
     virtual ~MeasurementAPIClient();
 
     std::mutex mMutex;
diff --git a/android/2.0/GnssMeasurement.cpp b/android/2.0/GnssMeasurement.cpp
index 6cb55ca..32383b1 100644
--- a/android/2.0/GnssMeasurement.cpp
+++ b/android/2.0/GnssMeasurement.cpp
@@ -18,11 +18,15 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #define LOG_TAG "LocSvc_GnssMeasurementInterface"
 
 #include <log_util.h>
 #include "GnssMeasurement.h"
-#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -54,32 +58,42 @@
 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
         const sp<V1_0::IGnssMeasurementCallback>& callback)  {
+    return setCallback(callback, mGnssMeasurementCbIface, GNSS_POWER_MODE_INVALID);
+}
 
+template <typename T>
+Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+        const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode) {
     Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
+            IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
         return ret;
     }
+    if (myCallback != callback) {
+        if (nullptr == callback) {
+            LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
+            mApi->measurementSetCallback(callback);
+            close();
+        } else {
+            if (nullptr != myCallback) {
+                myCallback->unlinkToDeath(mGnssMeasurementDeathRecipient);
+            }
+            myCallback = callback;
+            myCallback->linkToDeath(mGnssMeasurementDeathRecipient, 0);
+            ret = mApi->measurementSetCallback(callback, powerMode);
+        }
+    }
 
-    clearInterfaces();
-
-    mGnssMeasurementCbIface = callback;
-    mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    return mApi->measurementSetCallback(callback);
+    return ret;
 }
 
 void GnssMeasurement::clearInterfaces() {
+    if (mApi != nullptr) {
+        mApi->measurementSetCallback<V1_0::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V1_1::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V2_0::IGnssMeasurementCallback>(nullptr);
+    }
     if (mGnssMeasurementCbIface != nullptr) {
         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface = nullptr;
@@ -109,63 +123,15 @@
 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
         const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) {
-
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_1_1 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_1_1 = callback;
-    mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking?
-            GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_1_1(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_1_1,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 // Methods from ::android::hardware::gnss::V2_0::IGnssMeasurement follow.
 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
         const sp<V2_0::IGnssMeasurementCallback>& callback,
         bool enableFullTracking) {
-
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_2_0 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_2_0 = callback;
-    mGnssMeasurementCbIface_2_0->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking ?
-        GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_2_0(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_2_0,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 
 }  // namespace implementation
diff --git a/android/2.0/GnssMeasurement.h b/android/2.0/GnssMeasurement.h
index 7fa66b4..37fdc1c 100644
--- a/android/2.0/GnssMeasurement.h
+++ b/android/2.0/GnssMeasurement.h
@@ -18,12 +18,18 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #ifndef ANDROID_HARDWARE_GNSS_V2_0_GNSSMEASUREMENT_H
 #define ANDROID_HARDWARE_GNSS_V2_0_GNSSMEASUREMENT_H
 
 #include <android/hardware/gnss/2.0/IGnssMeasurement.h>
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
+#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -69,7 +75,9 @@
         sp<GnssMeasurement> mGnssMeasurement;
     };
 
- private:
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> setCallback(
+            const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode);
     sp<GnssMeasurementDeathRecipient> mGnssMeasurementDeathRecipient = nullptr;
     sp<V1_0::IGnssMeasurementCallback> mGnssMeasurementCbIface = nullptr;
     sp<V1_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1 = nullptr;
diff --git a/android/2.0/location_api/MeasurementAPIClient.cpp b/android/2.0/location_api/MeasurementAPIClient.cpp
index 425415f..e1cb3f8 100644
--- a/android/2.0/location_api/MeasurementAPIClient.cpp
+++ b/android/2.0/location_api/MeasurementAPIClient.cpp
@@ -82,52 +82,6 @@
     mGnssMeasurementCbIface_2_0 = nullptr;
 }
 
-// for GpsInterface
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
-{
-    LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface = callback;
-    mMutex.unlock();
-
-    return startTracking();
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_1_1(
-        const sp<V1_1::IGnssMeasurementCallback>& callback,
-        GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-            __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_1_1 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_2_0(
-    const sp<V2_0::IGnssMeasurementCallback>& callback,
-    GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-        __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_2_0 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::startTracking(
         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
@@ -136,16 +90,6 @@
     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
     locationCallbacks.size = sizeof(LocationCallbacks);
 
-    locationCallbacks.trackingCb = nullptr;
-    locationCallbacks.batchingCb = nullptr;
-    locationCallbacks.geofenceBreachCb = nullptr;
-    locationCallbacks.geofenceStatusCb = nullptr;
-    locationCallbacks.gnssLocationInfoCb = nullptr;
-    locationCallbacks.gnssNiCb = nullptr;
-    locationCallbacks.gnssSvCb = nullptr;
-    locationCallbacks.gnssNmeaCb = nullptr;
-
-    locationCallbacks.gnssMeasurementsCb = nullptr;
     if (mGnssMeasurementCbIface_2_0 != nullptr ||
         mGnssMeasurementCbIface_1_1 != nullptr ||
         mGnssMeasurementCbIface != nullptr) {
@@ -168,7 +112,7 @@
     }
 
     mTracking = true;
-    LOC_LOGD("%s]: start tracking session", __FUNCTION__);
+    LOC_LOGd("(powermode: %d) (tbm %d)", (int)powerMode, timeBetweenMeasurement);
     locAPIStartTracking(options);
     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
 }
@@ -177,6 +121,7 @@
 void MeasurementAPIClient::measurementClose() {
     LOC_LOGD("%s]: ()", __FUNCTION__);
     mTracking = false;
+    clearInterfaces();
     locAPIStopTracking();
 }
 
diff --git a/android/2.0/location_api/MeasurementAPIClient.h b/android/2.0/location_api/MeasurementAPIClient.h
index 6c2d38d..cd47c08 100644
--- a/android/2.0/location_api/MeasurementAPIClient.h
+++ b/android/2.0/location_api/MeasurementAPIClient.h
@@ -32,7 +32,6 @@
 
 #include <mutex>
 #include <android/hardware/gnss/2.0/IGnssMeasurement.h>
-//#include <android/hardware/gnss/1.1/IGnssMeasurementCallback.h>
 #include <LocationAPIClientBase.h>
 #include <hidl/Status.h>
 #include <gps_extended_c.h>
@@ -53,16 +52,15 @@
     MeasurementAPIClient& operator=(const MeasurementAPIClient&) = delete;
 
     // for GpsMeasurementInterface
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
-            const sp<V1_0::IGnssMeasurementCallback>& callback);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_1_1(
-            const sp<V1_1::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_2_0(
-            const sp<V2_0::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
+            const sp<T>& callback, GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID) {
+        mMutex.lock();
+        setCallbackLocked(callback);
+        mMutex.unlock();
+
+        return startTracking(powerMode);
+    }
     void measurementClose();
     Return<IGnssMeasurement::GnssMeasurementStatus> startTracking(
             GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
@@ -72,6 +70,15 @@
     void onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification) final;
 
 private:
+    inline void setCallbackLocked(const sp<V1_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface = callback;
+    }
+    inline void setCallbackLocked(const sp<V1_1::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_1_1 = callback;
+    }
+    inline void setCallbackLocked(const sp<V2_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_2_0 = callback;
+    }
     virtual ~MeasurementAPIClient();
 
     std::mutex mMutex;
diff --git a/android/2.1/GnssMeasurement.cpp b/android/2.1/GnssMeasurement.cpp
index af75802..16b0bd6 100644
--- a/android/2.1/GnssMeasurement.cpp
+++ b/android/2.1/GnssMeasurement.cpp
@@ -18,11 +18,15 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #define LOG_TAG "LocSvc_GnssMeasurementInterface"
 
 #include <log_util.h>
 #include "GnssMeasurement.h"
-#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -54,32 +58,44 @@
 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
         const sp<V1_0::IGnssMeasurementCallback>& callback)  {
+    return setCallback(callback, mGnssMeasurementCbIface, GNSS_POWER_MODE_INVALID);
+}
 
+template <typename T>
+Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+        const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode) {
     Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
+            IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
         return ret;
     }
 
-    clearInterfaces();
+    if (myCallback != callback) {
+        if (nullptr == callback) {
+            LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
+            mApi->measurementSetCallback(callback);
+            close();
+        } else {
+            if (nullptr != myCallback) {
+                myCallback->unlinkToDeath(mGnssMeasurementDeathRecipient);
+            }
+            myCallback = callback;
+            myCallback->linkToDeath(mGnssMeasurementDeathRecipient, 0);
+            ret = mApi->measurementSetCallback(callback, powerMode);
+        }
+    }
 
-    mGnssMeasurementCbIface = callback;
-    mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    return mApi->measurementSetCallback(callback);
+    return ret;
 }
 
 void GnssMeasurement::clearInterfaces() {
+    if (mApi != nullptr) {
+        mApi->measurementSetCallback<V1_0::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V1_1::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V2_0::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V2_1::IGnssMeasurementCallback>(nullptr);
+    }
     if (mGnssMeasurementCbIface != nullptr) {
         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface = nullptr;
@@ -113,95 +129,24 @@
 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
         const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) {
-
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_1_1 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_1_1 = callback;
-    mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking?
-            GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_1_1(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_1_1,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
+
 // Methods from ::android::hardware::gnss::V2_0::IGnssMeasurement follow.
 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
         const sp<V2_0::IGnssMeasurementCallback>& callback,
         bool enableFullTracking) {
-
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_2_0 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_2_0 = callback;
-    mGnssMeasurementCbIface_2_0->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking ?
-        GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_2_0(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_2_0,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 
 // Methods from ::android::hardware::gnss::V2_1::IGnssMeasurement follow.
 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1(
         const sp<::android::hardware::gnss::V2_1::IGnssMeasurementCallback>& callback,
         bool enableFullTracking) {
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_2_1 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_2_1 = callback;
-    mGnssMeasurementCbIface_2_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking ?
-            GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_2_1(callback, powerMode);
-
+    return setCallback(callback, mGnssMeasurementCbIface_2_1,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 
 }  // namespace implementation
diff --git a/android/2.1/GnssMeasurement.h b/android/2.1/GnssMeasurement.h
index 2ac45c6..9d7a567 100644
--- a/android/2.1/GnssMeasurement.h
+++ b/android/2.1/GnssMeasurement.h
@@ -18,12 +18,18 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #ifndef ANDROID_HARDWARE_GNSS_V2_1_GNSSMEASUREMENT_H
 #define ANDROID_HARDWARE_GNSS_V2_1_GNSSMEASUREMENT_H
 
 #include <android/hardware/gnss/2.1/IGnssMeasurement.h>
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
+#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -74,7 +80,9 @@
         sp<GnssMeasurement> mGnssMeasurement;
     };
 
- private:
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> setCallback(
+            const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode);
     sp<GnssMeasurementDeathRecipient> mGnssMeasurementDeathRecipient = nullptr;
     sp<V1_0::IGnssMeasurementCallback> mGnssMeasurementCbIface = nullptr;
     sp<V1_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1 = nullptr;
diff --git a/android/2.1/location_api/MeasurementAPIClient.cpp b/android/2.1/location_api/MeasurementAPIClient.cpp
index 0028074..96a7a94 100644
--- a/android/2.1/location_api/MeasurementAPIClient.cpp
+++ b/android/2.1/location_api/MeasurementAPIClient.cpp
@@ -95,65 +95,6 @@
     mGnssMeasurementCbIface_2_1 = nullptr;
 }
 
-// for GpsInterface
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
-{
-    LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface = callback;
-    mMutex.unlock();
-
-    return startTracking();
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_1_1(
-        const sp<V1_1::IGnssMeasurementCallback>& callback,
-        GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-            __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_1_1 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_2_0(
-    const sp<V2_0::IGnssMeasurementCallback>& callback,
-    GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-        __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_2_0 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus> MeasurementAPIClient::measurementSetCallback_2_1(
-        const sp<V2_1::IGnssMeasurementCallback>& callback,
-        GnssPowerMode powerMode, uint32_t timeBetweenMeasurement) {
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-        __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_2_1 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::startTracking(
         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
@@ -162,16 +103,6 @@
     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
     locationCallbacks.size = sizeof(LocationCallbacks);
 
-    locationCallbacks.trackingCb = nullptr;
-    locationCallbacks.batchingCb = nullptr;
-    locationCallbacks.geofenceBreachCb = nullptr;
-    locationCallbacks.geofenceStatusCb = nullptr;
-    locationCallbacks.gnssLocationInfoCb = nullptr;
-    locationCallbacks.gnssNiCb = nullptr;
-    locationCallbacks.gnssSvCb = nullptr;
-    locationCallbacks.gnssNmeaCb = nullptr;
-
-    locationCallbacks.gnssMeasurementsCb = nullptr;
     if (mGnssMeasurementCbIface_2_1 != nullptr ||
         mGnssMeasurementCbIface_2_0 != nullptr ||
         mGnssMeasurementCbIface_1_1 != nullptr ||
@@ -195,7 +126,8 @@
     }
 
     mTracking = true;
-    LOC_LOGD("%s]: start tracking session", __FUNCTION__);
+    LOC_LOGd("(powermode: %d) (tbm %d)", (int)powerMode, timeBetweenMeasurement);
+
     locAPIStartTracking(options);
     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
 }
@@ -204,6 +136,7 @@
 void MeasurementAPIClient::measurementClose() {
     LOC_LOGD("%s]: ()", __FUNCTION__);
     mTracking = false;
+    clearInterfaces();
     locAPIStopTracking();
 }
 
diff --git a/android/2.1/location_api/MeasurementAPIClient.h b/android/2.1/location_api/MeasurementAPIClient.h
index 3e8805b..1a3051f 100644
--- a/android/2.1/location_api/MeasurementAPIClient.h
+++ b/android/2.1/location_api/MeasurementAPIClient.h
@@ -32,7 +32,6 @@
 
 #include <mutex>
 #include <android/hardware/gnss/2.1/IGnssMeasurement.h>
-//#include <android/hardware/gnss/1.1/IGnssMeasurementCallback.h>
 #include <android/hardware/gnss/2.1/IGnssMeasurementCallback.h>
 #include <LocationAPIClientBase.h>
 #include <hidl/Status.h>
@@ -54,20 +53,16 @@
     MeasurementAPIClient& operator=(const MeasurementAPIClient&) = delete;
 
     // for GpsMeasurementInterface
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
-            const sp<V1_0::IGnssMeasurementCallback>& callback);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_1_1(
-            const sp<V1_1::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_2_0(
-            const sp<V2_0::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_2_1(
-            const sp<V2_1::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
+            const sp<T>& callback, GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID) {
+        mMutex.lock();
+        setCallbackLocked(callback);
+        mMutex.unlock();
+
+        return startTracking(powerMode);
+    }
+
     void measurementClose();
     Return<IGnssMeasurement::GnssMeasurementStatus> startTracking(
             GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
@@ -77,6 +72,18 @@
     void onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification) final;
 
 private:
+    inline void setCallbackLocked(const sp<V1_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface = callback;
+    }
+    inline void setCallbackLocked(const sp<V1_1::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_1_1 = callback;
+    }
+    inline void setCallbackLocked(const sp<V2_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_2_0 = callback;
+    }
+    inline void setCallbackLocked(const sp<V2_1::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_2_1 = callback;
+    }
     virtual ~MeasurementAPIClient();
 
     std::mutex mMutex;
diff --git a/location/LocationAPIClientBase.cpp b/location/LocationAPIClientBase.cpp
index ea15a76..d4a86c2 100644
--- a/location/LocationAPIClientBase.cpp
+++ b/location/LocationAPIClientBase.cpp
@@ -26,6 +26,11 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #define LOG_NDEBUG 0
 #define LOG_TAG "LocSvc_APIClientBase"
 
@@ -357,7 +362,8 @@
     pthread_mutex_lock(&mMutex);
     if (mLocationAPI) {
         if (mTracking) {
-            LOC_LOGW("%s:%d] Existing tracking session present", __FUNCTION__, __LINE__);
+            pthread_mutex_unlock(&mMutex);
+            locAPIUpdateTrackingOptions(options);
         } else {
             uint32_t session = mLocationAPI->startTracking(options);
             LOC_LOGI("%s:%d] start new session: %d", __FUNCTION__, __LINE__, session);
@@ -367,11 +373,13 @@
             mRequestQueues[REQUEST_TRACKING].reset(session);
             mRequestQueues[REQUEST_TRACKING].push(new StartTrackingRequest(*this));
             mTracking = true;
+            pthread_mutex_unlock(&mMutex);
         }
 
         retVal = LOCATION_ERROR_SUCCESS;
+    } else {
+        pthread_mutex_unlock(&mMutex);
     }
-    pthread_mutex_unlock(&mMutex);
 
     return retVal;
 }