Fix race condition that adapter does not get handleEngineUp

- Removed redundant injectFeatureConfig() call, as it this
happens twice, once when the first context is created which
would have been too early and once when LocApi open success.
- Resolved a race condition that second adapter added may not
have been in LocApi's adapter list yet when handleEngineUp()
is broadcast.

Change-Id: I8ecc18eab6b450c326c0be1abc011f70285439aa
CRs-Fixed: 2397902
diff --git a/core/LocApiBase.cpp b/core/LocApiBase.cpp
index c2ee411..13b1c71 100644
--- a/core/LocApiBase.cpp
+++ b/core/LocApiBase.cpp
@@ -31,6 +31,7 @@
 
 #include <dlfcn.h>
 #include <inttypes.h>
+#include <gps_extended_c.h>
 #include <LocApiBase.h>
 #include <LocAdapterBase.h>
 #include <log_util.h>
@@ -95,7 +96,10 @@
     }
     inline virtual void proc() const {
         mLocApi->close();
-        mLocApi->open(mLocApi->getEvtMask());
+        if (LOC_API_ADAPTER_ERR_SUCCESS == mLocApi->open(mLocApi->getEvtMask())) {
+            // Notify adapters that engine up after SSR
+            mLocApi->handleEngineUpEvent();
+        }
     }
     inline void locallog() const {
         LOC_LOGV("LocSsrMsg");
@@ -107,13 +111,17 @@
 
 struct LocOpenMsg : public LocMsg {
     LocApiBase* mLocApi;
-    inline LocOpenMsg(LocApiBase* locApi) :
-            LocMsg(), mLocApi(locApi)
+    LocAdapterBase* mAdapter;
+    inline LocOpenMsg(LocApiBase* locApi, LocAdapterBase* adapter = nullptr) :
+            LocMsg(), mLocApi(locApi), mAdapter(adapter)
     {
         locallog();
     }
     inline virtual void proc() const {
-        mLocApi->open(mLocApi->getEvtMask());
+        if (LOC_API_ADAPTER_ERR_SUCCESS == mLocApi->open(mLocApi->getEvtMask()) &&
+            nullptr != mAdapter) {
+            mAdapter->handleEngineUpEvent();
+        }
     }
     inline void locallog() const {
         LOC_LOGv("LocOpen Mask: %" PRIx64 "\n", mLocApi->getEvtMask());
@@ -222,7 +230,7 @@
     for (int i = 0; i < MAX_ADAPTERS && mLocAdapters[i] != adapter; i++) {
         if (mLocAdapters[i] == NULL) {
             mLocAdapters[i] = adapter;
-            mMsgTask->sendMsg(new LocOpenMsg(this));
+            mMsgTask->sendMsg(new LocOpenMsg(this,  adapter));
             break;
         }
     }
@@ -295,8 +303,6 @@
 
 void LocApiBase::handleEngineUpEvent()
 {
-    LocDualContext::injectFeatureConfig(mContext);
-
     // loop through adapters, and deliver to all adapters.
     TO_ALL_LOCADAPTERS(mLocAdapters[i]->handleEngineUpEvent());
 }
diff --git a/core/LocDualContext.cpp b/core/LocDualContext.cpp
index 180d9dc..9851d61 100644
--- a/core/LocDualContext.cpp
+++ b/core/LocDualContext.cpp
@@ -55,7 +55,6 @@
 const MsgTask* LocDualContext::mMsgTask = NULL;
 ContextBase* LocDualContext::mFgContext = NULL;
 ContextBase* LocDualContext::mBgContext = NULL;
-ContextBase* LocDualContext::mInjectContext = NULL;
 // the name must be shorter than 15 chars
 const char* LocDualContext::mLocationHalName = "Loc_hal_worker";
 #ifndef USE_GLIB
@@ -91,11 +90,6 @@
         mFgContext = new LocDualContext(msgTask,
                                         mFgExclMask);
     }
-    if(NULL == mInjectContext) {
-        LOC_LOGD("%s:%d]: mInjectContext is FgContext", __func__, __LINE__);
-        mInjectContext = mFgContext;
-        injectFeatureConfig(mInjectContext);
-    }
     pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
 
     if (firstMsg) {
@@ -116,11 +110,6 @@
         mBgContext = new LocDualContext(msgTask,
                                         mBgExclMask);
     }
-    if(NULL == mInjectContext) {
-        LOC_LOGD("%s:%d]: mInjectContext is BgContext", __func__, __LINE__);
-        mInjectContext = mBgContext;
-        injectFeatureConfig(mInjectContext);
-    }
     pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
 
     if (firstMsg) {
@@ -132,13 +121,9 @@
 
 void LocDualContext :: injectFeatureConfig(ContextBase *curContext)
 {
-    LOC_LOGD("%s:%d]: Enter", __func__, __LINE__);
-    if(curContext == mInjectContext) {
-        LOC_LOGD("%s:%d]: Calling LBSProxy (%p) to inject feature config",
-                 __func__, __LINE__, ((LocDualContext *)mInjectContext)->mLBSProxy);
-        ((LocDualContext *)mInjectContext)->mLBSProxy->injectFeatureConfig(curContext);
-    }
-    LOC_LOGD("%s:%d]: Exit", __func__, __LINE__);
+    LOC_LOGD("%s:%d]: Calling LBSProxy (%p) to inject feature config",
+             __func__, __LINE__, ((LocDualContext *)curContext)->mLBSProxy);
+    ((LocDualContext *)curContext)->mLBSProxy->injectFeatureConfig(curContext);
 }
 
 LocDualContext::LocDualContext(const MsgTask* msgTask,
diff --git a/core/LocDualContext.h b/core/LocDualContext.h
index 3b3ce2c..edfbfb7 100644
--- a/core/LocDualContext.h
+++ b/core/LocDualContext.h
@@ -40,7 +40,6 @@
     static const MsgTask* mMsgTask;
     static ContextBase* mFgContext;
     static ContextBase* mBgContext;
-    static ContextBase* mInjectContext;
     static const MsgTask* getMsgTask(LocThread::tCreate tCreator,
                                      const char* name, bool joinable = true);
     static const MsgTask* getMsgTask(const char* name, bool joinable = true);