Merge CR#2716173 & CR#2719986

QC has updated these 2 CRs after we merged them at ag/12009347:
 - CR#2716173: Use Qtimer to improve ElapsedRealTimeNanos accuracy
 - CR#2719986: The callbacks could be stale in GnssMeasurement

Bug: 159768476

Test: Ensure VtsHalGnssV2.0/2.1 are all PASS

CRs-fixed: 2716173
CRs-fixed: 2719986
Change-Id: Ic441a7bcde5ab918d406328f81351a47ac9f1d65
diff --git a/android/2.0/GnssMeasurement.cpp b/android/2.0/GnssMeasurement.cpp
index 721a48c..f09cc16 100644
--- a/android/2.0/GnssMeasurement.cpp
+++ b/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/android/2.0/GnssMeasurement.h b/android/2.0/GnssMeasurement.h
index 000b00f..7fa66b4 100644
--- a/android/2.0/GnssMeasurement.h
+++ b/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/android/2.0/location_api/LocationUtil.cpp b/android/2.0/location_api/LocationUtil.cpp
index 066fd74..7516e91 100644
--- a/android/2.0/location_api/LocationUtil.cpp
+++ b/android/2.0/location_api/LocationUtil.cpp
@@ -137,7 +137,7 @@
             if (qTimerTickCount >= in.elapsedRealTime) {
                 qtimerDiff = qTimerTickCount - in.elapsedRealTime;
             }
-            LOC_LOGd("sinceBootTimeNanos:%" PRIi64 " in.elapsedRealTime=%" PRIi64 ""
+            LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " in.elapsedRealTime=%" PRIi64 ""
                      " qTimerTickCount=%" PRIi64 " qtimerDiff=%" PRIi64 "",
                      sinceBootTimeNanos, in.elapsedRealTime, qTimerTickCount, qtimerDiff);
             uint64_t qTimerDiffNanos = qTimerTicksToNanos(double(qtimerDiff));
@@ -150,28 +150,27 @@
         } else {
             int64_t currentTimeNanos = currentTime.tv_sec*1000000000 + currentTime.tv_nsec;
             int64_t locationTimeNanos = in.timestamp*1000000;
-            LOC_LOGD("%s]: sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
+            LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
                      " locationTimeNanos:%" PRIi64 "",
-                    __FUNCTION__, sinceBootTimeNanos, currentTimeNanos, locationTimeNanos);
+                     sinceBootTimeNanos, currentTimeNanos, locationTimeNanos);
             if (currentTimeNanos >= locationTimeNanos) {
                 int64_t ageTimeNanos = currentTimeNanos - locationTimeNanos;
-                LOC_LOGD("%s]: ageTimeNanos:%" PRIi64 ")", __FUNCTION__, ageTimeNanos);
-                // the max trusted propagation time 30s for ageTimeNanos to avoid user setting
+                LOC_LOGv("ageTimeNanos:%" PRIi64 ")", ageTimeNanos);
+                // the max trusted propagation time 100ms for ageTimeNanos to avoid user setting
                 // wrong time, it will affect elapsedRealtimeNanos
-                if (ageTimeNanos >= 0 && ageTimeNanos <= 30000000000) {
+                if (ageTimeNanos <= 100000000) {
                     out.elapsedRealtime.flags |= ElapsedRealtimeFlags::HAS_TIMESTAMP_NS;
                     out.elapsedRealtime.timestampNs = sinceBootTimeNanos - ageTimeNanos;
                     out.elapsedRealtime.flags |= ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS;
                     // time uncertainty is the max value between abs(AP_UTC - MP_UTC) and 100ms, to
                     // verify if user change the sys time
                     out.elapsedRealtime.timeUncertaintyNs =
-                            std::max((int64_t)abs(currentTimeNanos - locationTimeNanos),
-                                     (int64_t)100000000);
+                            std::max(ageTimeNanos, (int64_t)100000000);
                 }
             }
         }
     }
-    LOC_LOGd("out.elapsedRealtime.timestampNs=%" PRIi64 ""
+    LOC_LOGv("out.elapsedRealtime.timestampNs=%" PRIi64 ""
              " out.elapsedRealtime.timeUncertaintyNs=%" PRIi64 ""
              " out.elapsedRealtime.flags=0x%X",
              out.elapsedRealtime.timestampNs,
diff --git a/android/2.0/location_api/MeasurementAPIClient.cpp b/android/2.0/location_api/MeasurementAPIClient.cpp
index d850ff7..33046fc 100644
--- a/android/2.0/location_api/MeasurementAPIClient.cpp
+++ b/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();
 
@@ -437,7 +447,7 @@
             if (qTimerTickCount >= in.clock.elapsedRealTime) {
                 qtimerDiff = qTimerTickCount - in.clock.elapsedRealTime;
             }
-            LOC_LOGd("sinceBootTimeNanos:%" PRIi64 " in.clock.elapsedRealTime=%" PRIi64 ""
+            LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " in.clock.elapsedRealTime=%" PRIi64 ""
                      " qTimerTickCount=%" PRIi64 " qtimerDiff=%" PRIi64 "",
                      sinceBootTimeNanos, in.clock.elapsedRealTime, qTimerTickCount, qtimerDiff);
             uint64_t qTimerDiffNanos = qTimerTicksToNanos(double(qtimerDiff));
@@ -459,20 +469,23 @@
                         - (int64_t)in.clock.biasNs - (int64_t)in.clock.leapSecond * 1000000000
                         + (int64_t)UTC_TO_GPS_SECONDS * 1000000000;
 
-                LOC_LOGd("sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
+                LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
                          " measTimeNanos:%" PRIi64 "",
                          sinceBootTimeNanos, currentTimeNanos, measTimeNanos);
                 if (currentTimeNanos >= measTimeNanos) {
                     int64_t ageTimeNanos = currentTimeNanos - measTimeNanos;
-                    LOC_LOGD("%s]: ageTimeNanos:%" PRIi64 ")", __FUNCTION__, ageTimeNanos);
-                    if (ageTimeNanos >= 0 && ageTimeNanos <= sinceBootTimeNanos) {
+                    LOC_LOGv("ageTimeNanos:%" PRIi64 ")", ageTimeNanos);
+                    // the max trusted propagation time 100ms for ageTimeNanos to avoid user
+                    // setting wrong time, it will affect elapsedRealtimeNanos
+                    if (ageTimeNanos <= 100000000) {
                         elapsedRealtime.flags |= V2_0::ElapsedRealtimeFlags::HAS_TIMESTAMP_NS;
                         elapsedRealtime.timestampNs = sinceBootTimeNanos - ageTimeNanos;
                         elapsedRealtime.flags |=
                                 V2_0::ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS;
-                        // time uncertainty is 1 ms since it is calculated from utc time that
-                        // is in ms
-                        elapsedRealtime.timeUncertaintyNs = 1000000;
+                        // time uncertainty is the max value between abs(AP_UTC - MP_UTC) and 100ms,
+                        // to verify if user change the sys time
+                        elapsedRealtime.timeUncertaintyNs =
+                                std::max(ageTimeNanos, (int64_t)100000000);
                     }
                 }
             } else {
@@ -480,7 +493,7 @@
             }
         }
     }
-    LOC_LOGd("elapsedRealtime.timestampNs=%" PRIi64 ""
+    LOC_LOGv("elapsedRealtime.timestampNs=%" PRIi64 ""
              " elapsedRealtime.timeUncertaintyNs=%" PRIi64 " elapsedRealtime.flags=0x%X",
              elapsedRealtime.timestampNs,
              elapsedRealtime.timeUncertaintyNs, elapsedRealtime.flags);
diff --git a/android/2.0/location_api/MeasurementAPIClient.h b/android/2.0/location_api/MeasurementAPIClient.h
index 4146a13..731344f 100644
--- a/android/2.0/location_api/MeasurementAPIClient.h
+++ b/android/2.0/location_api/MeasurementAPIClient.h
@@ -77,8 +77,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
diff --git a/android/2.1/location_api/LocationUtil.cpp b/android/2.1/location_api/LocationUtil.cpp
index e773886..1c73a22 100644
--- a/android/2.1/location_api/LocationUtil.cpp
+++ b/android/2.1/location_api/LocationUtil.cpp
@@ -138,7 +138,7 @@
             if (qTimerTickCount >= in.elapsedRealTime) {
                 qtimerDiff = qTimerTickCount - in.elapsedRealTime;
             }
-            LOC_LOGd("sinceBootTimeNanos:%" PRIi64 " in.elapsedRealTime=%" PRIi64 ""
+            LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " in.elapsedRealTime=%" PRIi64 ""
                      " qTimerTickCount=%" PRIi64 " qtimerDiff=%" PRIi64 "",
                      sinceBootTimeNanos, in.elapsedRealTime, qTimerTickCount, qtimerDiff);
             uint64_t qTimerDiffNanos = qTimerTicksToNanos(double(qtimerDiff));
@@ -151,28 +151,27 @@
         } else {
             int64_t currentTimeNanos = currentTime.tv_sec*1000000000 + currentTime.tv_nsec;
             int64_t locationTimeNanos = in.timestamp*1000000;
-            LOC_LOGD("%s]: sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
+            LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
                      " locationTimeNanos:%" PRIi64 "",
-                    __FUNCTION__, sinceBootTimeNanos, currentTimeNanos, locationTimeNanos);
+                     sinceBootTimeNanos, currentTimeNanos, locationTimeNanos);
             if (currentTimeNanos >= locationTimeNanos) {
                 int64_t ageTimeNanos = currentTimeNanos - locationTimeNanos;
-                LOC_LOGD("%s]: ageTimeNanos:%" PRIi64 ")", __FUNCTION__, ageTimeNanos);
-                // the max trusted propagation time 30s for ageTimeNanos to avoid user setting
+                LOC_LOGv("ageTimeNanos:%" PRIi64 ")", ageTimeNanos);
+                // the max trusted propagation time 100ms for ageTimeNanos to avoid user setting
                 // wrong time, it will affect elapsedRealtimeNanos
-                if (ageTimeNanos >= 0 && ageTimeNanos <= 30000000000) {
+                if (ageTimeNanos <= 100000000) {
                     out.elapsedRealtime.flags |= ElapsedRealtimeFlags::HAS_TIMESTAMP_NS;
                     out.elapsedRealtime.timestampNs = sinceBootTimeNanos - ageTimeNanos;
                     out.elapsedRealtime.flags |= ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS;
                     // time uncertainty is the max value between abs(AP_UTC - MP_UTC) and 100ms, to
                     // verify if user change the sys time
                     out.elapsedRealtime.timeUncertaintyNs =
-                            std::max((int64_t)abs(currentTimeNanos - locationTimeNanos),
-                                     (int64_t)100000000);
+                            std::max(ageTimeNanos, (int64_t)100000000);
                 }
             }
         }
     }
-    LOC_LOGd("out.elapsedRealtime.timestampNs=%" PRIi64 ""
+    LOC_LOGv("out.elapsedRealtime.timestampNs=%" PRIi64 ""
              " out.elapsedRealtime.timeUncertaintyNs=%" PRIi64 ""
              " out.elapsedRealtime.flags=0x%X",
              out.elapsedRealtime.timestampNs,
diff --git a/android/2.1/location_api/MeasurementAPIClient.cpp b/android/2.1/location_api/MeasurementAPIClient.cpp
index c2c689a..2d27a5b 100644
--- a/android/2.1/location_api/MeasurementAPIClient.cpp
+++ b/android/2.1/location_api/MeasurementAPIClient.cpp
@@ -87,6 +87,14 @@
     LOC_LOGD("%s]: ()", __FUNCTION__);
 }
 
+void MeasurementAPIClient::clearInterfaces()
+{
+    mGnssMeasurementCbIface = nullptr;
+    mGnssMeasurementCbIface_1_1 = nullptr;
+    mGnssMeasurementCbIface_2_0 = nullptr;
+    mGnssMeasurementCbIface_2_1 = nullptr;
+}
+
 // for GpsInterface
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
@@ -94,9 +102,7 @@
     LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
 
     mMutex.lock();
-    mGnssMeasurementCbIface_1_1 = nullptr;
-    mGnssMeasurementCbIface_2_0 = nullptr;
-    mGnssMeasurementCbIface_2_1 = nullptr;
+    clearInterfaces();
     mGnssMeasurementCbIface = callback;
     mMutex.unlock();
 
@@ -112,9 +118,7 @@
             __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
 
     mMutex.lock();
-    mGnssMeasurementCbIface = nullptr;
-    mGnssMeasurementCbIface_2_0 = nullptr;
-    mGnssMeasurementCbIface_2_1 = nullptr;
+    clearInterfaces();
     mGnssMeasurementCbIface_1_1 = callback;
     mMutex.unlock();
 
@@ -130,9 +134,7 @@
         __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
 
     mMutex.lock();
-    mGnssMeasurementCbIface = nullptr;
-    mGnssMeasurementCbIface_1_1 = nullptr;
-    mGnssMeasurementCbIface_2_1 = nullptr;
+    clearInterfaces();
     mGnssMeasurementCbIface_2_0 = callback;
     mMutex.unlock();
 
@@ -146,9 +148,7 @@
         __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
 
     mMutex.lock();
-    mGnssMeasurementCbIface = nullptr;
-    mGnssMeasurementCbIface_1_1 = nullptr;
-    mGnssMeasurementCbIface_2_0 = nullptr;
+    clearInterfaces();
     mGnssMeasurementCbIface_2_1 = callback;
     mMutex.unlock();
 
@@ -633,7 +633,7 @@
             if (qTimerTickCount >= in.clock.elapsedRealTime) {
                 qtimerDiff = qTimerTickCount - in.clock.elapsedRealTime;
             }
-            LOC_LOGd("sinceBootTimeNanos:%" PRIi64 " in.clock.elapsedRealTime=%" PRIi64 ""
+            LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " in.clock.elapsedRealTime=%" PRIi64 ""
                      " qTimerTickCount=%" PRIi64 " qtimerDiff=%" PRIi64 "",
                      sinceBootTimeNanos, in.clock.elapsedRealTime, qTimerTickCount, qtimerDiff);
             uint64_t qTimerDiffNanos = qTimerTicksToNanos(double(qtimerDiff));
@@ -655,20 +655,25 @@
                         - (int64_t)in.clock.biasNs - (int64_t)in.clock.leapSecond * 1000000000
                         + (int64_t)UTC_TO_GPS_SECONDS * 1000000000;
 
-                LOC_LOGd("sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
+                LOC_LOGv("sinceBootTimeNanos:%" PRIi64 " currentTimeNanos:%" PRIi64 ""
                          " measTimeNanos:%" PRIi64 "",
                          sinceBootTimeNanos, currentTimeNanos, measTimeNanos);
                 if (currentTimeNanos >= measTimeNanos) {
                     int64_t ageTimeNanos = currentTimeNanos - measTimeNanos;
-                    LOC_LOGD("%s]: ageTimeNanos:%" PRIi64 ")", __FUNCTION__, ageTimeNanos);
-                    if (ageTimeNanos >= 0 && ageTimeNanos <= sinceBootTimeNanos) {
+                    LOC_LOGv("ageTimeNanos:%" PRIi64 ")", ageTimeNanos);
+                    // the max trusted propagation time 100ms for ageTimeNanos to avoid user
+                    // setting wrong time, it will affect elapsedRealtimeNanos
+                    if (ageTimeNanos <= 100000000) {
                         elapsedRealtime.flags |= V2_0::ElapsedRealtimeFlags::HAS_TIMESTAMP_NS;
                         elapsedRealtime.timestampNs = sinceBootTimeNanos - ageTimeNanos;
                         elapsedRealtime.flags |=
                                 V2_0::ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS;
                         // time uncertainty is 1 ms since it is calculated from utc time that
                         // is in ms
-                        elapsedRealtime.timeUncertaintyNs = 1000000;
+                        // time uncertainty is the max value between abs(AP_UTC - MP_UTC) and 100ms,
+                        // to verify if user change the sys time
+                        elapsedRealtime.timeUncertaintyNs =
+                                std::max(ageTimeNanos, (int64_t)100000000);
                     }
                 }
             } else {
@@ -676,7 +681,7 @@
             }
         }
     }
-    LOC_LOGd("elapsedRealtime.timestampNs=%" PRIi64 ""
+    LOC_LOGv("elapsedRealtime.timestampNs=%" PRIi64 ""
              " elapsedRealtime.timeUncertaintyNs=%" PRIi64 " elapsedRealtime.flags=0x%X",
              elapsedRealtime.timestampNs,
              elapsedRealtime.timeUncertaintyNs, elapsedRealtime.flags);
diff --git a/android/2.1/location_api/MeasurementAPIClient.h b/android/2.1/location_api/MeasurementAPIClient.h
index 4a6cb8f..e275e74 100755
--- a/android/2.1/location_api/MeasurementAPIClient.h
+++ b/android/2.1/location_api/MeasurementAPIClient.h
@@ -83,8 +83,8 @@
     sp<V1_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1;
     sp<V2_0::IGnssMeasurementCallback> mGnssMeasurementCbIface_2_0;
     sp<V2_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_2_1;
-
     bool mTracking;
+    void clearInterfaces();
 };
 
 }  // namespace implementation