DO NOT MERGE - Merge pi-platform-release (PPRL.190205.001) into stage-aosp-master

Bug: 124234733
Change-Id: Ie8a6a0a0139e4a6821288af1aa4d0a9d4b277143
diff --git a/Android.mk b/Android.mk
index 3e0e388..e8db66e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,5 +1,16 @@
-# TODO:  Find a better way to separate build configs for ADP vs non-ADP devices
-ifneq ($(BOARD_IS_AUTOMOTIVE),true)
+#set TARGET_USES_HARDWARE_QCOM_GPS to false to disable this project.
+
+ifeq ($(TARGET_USES_HARDWARE_QCOM_GPS),)
+  ifneq ($(filter sdm845 sdm710, $(TARGET_BOARD_PLATFORM)),)
+    TARGET_USES_HARDWARE_QCOM_GPS := false
+  else ifeq ($(BOARD_IS_AUTOMOTIVE),true)
+    TARGET_USES_HARDWARE_QCOM_GPS := false
+  else
+    TARGET_USES_HARDWARE_QCOM_GPS := true
+  endif
+endif
+
+ifeq ($(TARGET_USES_HARDWARE_QCOM_GPS),true)
   ifneq ($(BOARD_VENDOR_QCOM_GPS_LOC_API_HARDWARE),)
     LOCAL_PATH := $(call my-dir)
     ifeq ($(BOARD_VENDOR_QCOM_LOC_PDK_FEATURE_SET),true)
@@ -10,8 +21,7 @@
       else ifneq ($(filter msm8992,$(TARGET_BOARD_PLATFORM)),)
         #For msm8992 use msm8994
         include $(call all-named-subdir-makefiles,msm8994)
-      else ifneq ($(filter msm8960 msm8084 msm8994 msm8996 msm8998 sdm845 sdm710,$(TARGET_BOARD_PLATFORM)),)
-        #For these, use their platform name as the subdirectory
+      else ifneq ($(filter msm8960 msm8084 msm8994 msm8996 msm8998,$(TARGET_BOARD_PLATFORM)),)
         include $(call all-named-subdir-makefiles,$(TARGET_BOARD_PLATFORM))
       else ifeq ($(filter msm8916,$(TARGET_BOARD_PLATFORM)),)
         #For all other targets besides msm8916
@@ -20,9 +30,7 @@
       endif #TARGET_BOARD_PLATFORM
 
     else
-      ifneq ($(filter sdm845 sdm710,$(TARGET_BOARD_PLATFORM)),)
-        include $(call all-named-subdir-makefiles,$(TARGET_BOARD_PLATFORM))
-      else ifneq ($(filter msm8909 msm8226 ,$(TARGET_BOARD_PLATFORM)),)
+      ifneq ($(filter msm8909 msm8226 ,$(TARGET_BOARD_PLATFORM)),)
         ifeq ($(TARGET_SUPPORTS_QCOM_3100),true)
           # For SD3100.
           include $(call all-named-subdir-makefiles,msm8909w_3100)
diff --git a/msm8998/core/LocApiBase.cpp b/msm8998/core/LocApiBase.cpp
index 7e2ea25..019f44e 100644
--- a/msm8998/core/LocApiBase.cpp
+++ b/msm8998/core/LocApiBase.cpp
@@ -157,6 +157,35 @@
     return inSession;
 }
 
+bool LocApiBase::needReport(const UlpLocation& ulpLocation,
+    enum loc_sess_status status,
+    LocPosTechMask techMask)
+{
+    bool reported = false;
+
+    if (LOC_SESS_SUCCESS == status) {
+        // this is a final fix
+        LocPosTechMask mask =
+            LOC_POS_TECH_MASK_SATELLITE | LOC_POS_TECH_MASK_SENSORS | LOC_POS_TECH_MASK_HYBRID;
+        // it is a Satellite fix or a sensor fix
+        reported = (mask & techMask);
+    }
+    else if (LOC_SESS_INTERMEDIATE == status &&
+        LOC_SESS_INTERMEDIATE == ContextBase::mGps_conf.INTERMEDIATE_POS) {
+        // this is a intermediate fix and we accept intermediate
+
+        // it is NOT the case that
+        // there is inaccuracy; and
+        // we care about inaccuracy; and
+        // the inaccuracy exceeds our tolerance
+        reported = !((ulpLocation.gpsLocation.flags & LOC_GPS_LOCATION_HAS_ACCURACY) &&
+            (ContextBase::mGps_conf.ACCURACY_THRES != 0) &&
+            (ulpLocation.gpsLocation.accuracy > ContextBase::mGps_conf.ACCURACY_THRES));
+    }
+
+    return reported;
+}
+
 void LocApiBase::addAdapter(LocAdapterBase* adapter)
 {
     for (int i = 0; i < MAX_ADAPTERS && mLocAdapters[i] != adapter; i++) {
diff --git a/msm8998/core/LocApiBase.h b/msm8998/core/LocApiBase.h
index 86610e7..d714ad9 100644
--- a/msm8998/core/LocApiBase.h
+++ b/msm8998/core/LocApiBase.h
@@ -103,6 +103,9 @@
     inline void sendMsg(const LocMsg* msg) const {
         mMsgTask->sendMsg(msg);
     }
+    static bool needReport(const UlpLocation& ulpLocation,
+        enum loc_sess_status status,
+        LocPosTechMask techMask);
     void addAdapter(LocAdapterBase* adapter);
     void removeAdapter(LocAdapterBase* adapter);
 
diff --git a/msm8998/gnss/Agps.cpp b/msm8998/gnss/Agps.cpp
index f5b8efe..e671daa 100644
--- a/msm8998/gnss/Agps.cpp
+++ b/msm8998/gnss/Agps.cpp
@@ -205,7 +205,10 @@
     switch (mState) {
 
         case AGPS_STATE_RELEASED:
-            LOC_LOGE("Unexpected event RELEASED in state %d", mState);
+            /* Subscriber list should be empty if we are in released state */
+            if (!mSubscriberList.empty()) {
+                LOC_LOGE("Unexpected event RELEASED in RELEASED state");
+            }
             break;
 
         case AGPS_STATE_ACQUIRED:
diff --git a/msm8998/gnss/GnssAdapter.cpp b/msm8998/gnss/GnssAdapter.cpp
index e14cf28..fadf350 100644
--- a/msm8998/gnss/GnssAdapter.cpp
+++ b/msm8998/gnss/GnssAdapter.cpp
@@ -64,7 +64,8 @@
     mControlCallbacks(),
     mPowerVoteId(0),
     mNiData(),
-    mAgpsManager()
+    mAgpsManager(),
+    mAgpsInitialized(false)
 {
     LOC_LOGD("%s]: Constructor %p", __func__, this);
     mUlpPositionMode.mode = LOC_POSITION_MODE_INVALID;
@@ -1004,6 +1005,9 @@
             mask |= LOC_API_ADAPTER_BIT_GNSS_MEASUREMENT;
         }
     }
+    if (true == getAgpsInitialized()) {
+        mask |= LOC_API_ADAPTER_BIT_LOCATION_SERVER_REQUEST;
+    }
     updateEvtMask(mask, LOC_REGISTRATION_MASK_SET);
 }
 
@@ -1839,38 +1843,30 @@
     sendMsg(new MsgReportPosition(*this, ulpLocation, locationExtended, status, techMask));
 }
 
+bool
+GnssAdapter::needReport(const UlpLocation& ulpLocation,
+                        enum loc_sess_status status,
+                        LocPosTechMask techMask) {
+    bool reported = false;
+
+    reported = LocApiBase::needReport(ulpLocation, status, techMask);
+    return reported;
+}
+
 void
 GnssAdapter::reportPosition(const UlpLocation& ulpLocation,
                             const GpsLocationExtended& locationExtended,
                             enum loc_sess_status status,
                             LocPosTechMask techMask)
 {
-    bool reported = false;
-    // what's in the if is... (line by line)
-    // 1. this is a final fix; and
-    //   1.1 it is a Satellite fix; or
-    //   1.2 it is a sensor fix
-    // 2. (must be intermediate fix... implicit)
-    //   2.1 we accepte intermediate; and
-    //   2.2 it is NOT the case that
-    //   2.2.1 there is inaccuracy; and
-    //   2.2.2 we care about inaccuracy; and
-    //   2.2.3 the inaccuracy exceeds our tolerance
-    if ((LOC_SESS_SUCCESS == status &&
-              ((LOC_POS_TECH_MASK_SATELLITE |
-                LOC_POS_TECH_MASK_SENSORS   |
-                LOC_POS_TECH_MASK_HYBRID) &
-               techMask)) ||
-             (LOC_SESS_INTERMEDIATE == ContextBase::mGps_conf.INTERMEDIATE_POS &&
-              !((ulpLocation.gpsLocation.flags &
-                 LOC_GPS_LOCATION_HAS_ACCURACY) &&
-                (ContextBase::mGps_conf.ACCURACY_THRES != 0) &&
-                (ulpLocation.gpsLocation.accuracy >
-                 ContextBase::mGps_conf.ACCURACY_THRES)))) {
+    bool reported = needReport(ulpLocation, status, techMask);
+    mGnssSvIdUsedInPosAvail = false;
+    if (reported) {
         if (locationExtended.flags & GPS_LOCATION_EXTENDED_HAS_GNSS_SV_USED_DATA) {
             mGnssSvIdUsedInPosAvail = true;
             mGnssSvIdUsedInPosition = locationExtended.gnss_sv_used_ids;
         }
+
         for (auto it=mClientData.begin(); it != mClientData.end(); ++it) {
             if (nullptr != it->second.trackingCb) {
                 Location location = {};
@@ -1883,11 +1879,6 @@
                 it->second.gnssLocationInfoCb(locationInfo);
             }
         }
-        reported = true;
-    } else {
-        LOC_LOGI("%s: not reported. Status: %d, techMask: %d, flags %d, accuracy %f",
-                __func__, (int)status, (int)techMask, (int)ulpLocation.gpsLocation.flags,
-                (float)ulpLocation.gpsLocation.accuracy);
     }
 
     if (NMEA_PROVIDER_AP == ContextBase::mGps_conf.NMEA_PROVIDER && !mTrackingSessions.empty()) {
@@ -2392,6 +2383,7 @@
 
             mAgpsManager->createAgpsStateMachines();
 
+            mAdapter.setAgpsInitialized(true);
             /* Register for AGPS event mask */
             mAdapter.updateEvtMask(LOC_API_ADAPTER_BIT_LOCATION_SERVER_REQUEST,
                                    LOC_REGISTRATION_MASK_ENABLED);
diff --git a/msm8998/gnss/GnssAdapter.h b/msm8998/gnss/GnssAdapter.h
index d857558..81f14e2 100644
--- a/msm8998/gnss/GnssAdapter.h
+++ b/msm8998/gnss/GnssAdapter.h
@@ -99,6 +99,7 @@
     /* ==== AGPS ========================================================*/
     // This must be initialized via initAgps()
     AgpsManager mAgpsManager;
+    bool mAgpsInitialized;
 
     /*==== CONVERSION ===================================================================*/
     static void convertOptions(LocPosMode& out, const LocationOptions& options);
@@ -191,6 +192,8 @@
             const char* apnName, int apnLen, LocApnIpType ipType);
     void dataConnClosedCommand(AGpsExtType agpsType);
     void dataConnFailedCommand(AGpsExtType agpsType);
+    inline void setAgpsInitialized(bool value) { mAgpsInitialized = value; }
+    inline bool getAgpsInitialized() { return mAgpsInitialized; }
 
     /* ======== RESPONSES ================================================================== */
     void reportResponse(LocationError err, uint32_t sessionId);
@@ -224,6 +227,8 @@
     virtual bool reportDataCallClosed();
 
     /* ======== UTILITIES ================================================================= */
+    bool needReport(const UlpLocation& ulpLocation,
+            enum loc_sess_status status, LocPosTechMask techMask);
     void reportPosition(const UlpLocation &ulpLocation,
                         const GpsLocationExtended &locationExtended,
                         enum loc_sess_status status,