GPS: Do not call sGpsInterface->init() multiple times

Change-Id: I5f2b55959bd67864168690c1fe619e55edc18268
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/services/java/com/android/server/location/GpsLocationProvider.java b/services/java/com/android/server/location/GpsLocationProvider.java
index e9eb4f0..87271e7 100755
--- a/services/java/com/android/server/location/GpsLocationProvider.java
+++ b/services/java/com/android/server/location/GpsLocationProvider.java
@@ -1228,6 +1228,7 @@
     private void reportAGpsStatus(int type, int status) {
         switch (status) {
             case GPS_REQUEST_AGPS_DATA_CONN:
+                if (DEBUG) Log.d(TAG, "GPS_REQUEST_AGPS_DATA_CONN");
                 // Set mAGpsDataConnectionState before calling startUsingNetworkFeature
                 //  to avoid a race condition with handleUpdateNetworkState()
                 mAGpsDataConnectionState = AGPS_DATA_CONNECTION_OPENING;
@@ -1250,6 +1251,7 @@
                 }
                 break;
             case GPS_RELEASE_AGPS_DATA_CONN:
+                if (DEBUG) Log.d(TAG, "GPS_RELEASE_AGPS_DATA_CONN");
                 if (mAGpsDataConnectionState != AGPS_DATA_CONNECTION_CLOSED) {
                     mConnMgr.stopUsingNetworkFeature(
                             ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_SUPL);
@@ -1258,13 +1260,13 @@
                 }
                 break;
             case GPS_AGPS_DATA_CONNECTED:
-                // Log.d(TAG, "GPS_AGPS_DATA_CONNECTED");
+                if (DEBUG) Log.d(TAG, "GPS_AGPS_DATA_CONNECTED");
                 break;
             case GPS_AGPS_DATA_CONN_DONE:
-                // Log.d(TAG, "GPS_AGPS_DATA_CONN_DONE");
+                if (DEBUG) Log.d(TAG, "GPS_AGPS_DATA_CONN_DONE");
                 break;
             case GPS_AGPS_DATA_CONN_FAILED:
-                // Log.d(TAG, "GPS_AGPS_DATA_CONN_FAILED");
+                if (DEBUG) Log.d(TAG, "GPS_AGPS_DATA_CONN_FAILED");
                 break;
         }
     }
diff --git a/services/jni/com_android_server_location_GpsLocationProvider.cpp b/services/jni/com_android_server_location_GpsLocationProvider.cpp
index 71c7aba..b9ceaa1 100755
--- a/services/jni/com_android_server_location_GpsLocationProvider.cpp
+++ b/services/jni/com_android_server_location_GpsLocationProvider.cpp
@@ -238,15 +238,25 @@
     return interface;
 }
 
+static const GpsInterface* GetGpsInterface() {
+    if (!sGpsInterface) {
+        sGpsInterface = get_gps_interface();
+        if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0) {
+            sGpsInterface = NULL;
+            return NULL;
+        }
+    }
+    return sGpsInterface;
+}
+
 static const AGpsInterface* GetAGpsInterface()
 {
-    if (!sGpsInterface)
-        sGpsInterface = get_gps_interface();
-    if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)
+    const GpsInterface* interface = GetGpsInterface();
+    if (!interface)
         return NULL;
 
     if (!sAGpsInterface) {
-        sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE);
+        sAGpsInterface = (const AGpsInterface*)interface->get_extension(AGPS_INTERFACE);
         if (sAGpsInterface)
             sAGpsInterface->init(&sAGpsCallbacks);
     }
@@ -255,13 +265,12 @@
 
 static const GpsNiInterface* GetNiInterface()
 {
-    if (!sGpsInterface)
-        sGpsInterface = get_gps_interface();
-    if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)
+    const GpsInterface* interface = GetGpsInterface();
+    if (!interface)
         return NULL;
 
     if (!sGpsNiInterface) {
-       sGpsNiInterface = (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE);
+       sGpsNiInterface = (const GpsNiInterface*)interface->get_extension(GPS_NI_INTERFACE);
         if (sGpsNiInterface)
            sGpsNiInterface->init(&sGpsNiCallbacks);
     }
@@ -270,13 +279,12 @@
 
 static const AGpsRilInterface* GetAGpsRilInterface()
 {
-    if (!sGpsInterface)
-        sGpsInterface = get_gps_interface();
-    if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)
+    const GpsInterface* interface = GetGpsInterface();
+    if (!interface)
         return NULL;
 
     if (!sAGpsRilInterface) {
-       sAGpsRilInterface = (const AGpsRilInterface*)sGpsInterface->get_extension(AGPS_RIL_INTERFACE);
+       sAGpsRilInterface = (const AGpsRilInterface*)interface->get_extension(AGPS_RIL_INTERFACE);
         if (sAGpsRilInterface)
             sAGpsRilInterface->init(&sAGpsRilCallbacks);
     }
@@ -297,9 +305,7 @@
 }
 
 static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz) {
-    if (!sGpsInterface)
-        sGpsInterface = get_gps_interface();
-    return (sGpsInterface != NULL);
+    return (sGpsInterface != NULL || get_gps_interface() != NULL);
 }
 
 static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
@@ -308,13 +314,12 @@
     if (!mCallbacksObj)
         mCallbacksObj = env->NewGlobalRef(obj);
 
-    if (!sGpsInterface)
-        sGpsInterface = get_gps_interface();
-    if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)
+    const GpsInterface* interface = GetGpsInterface();
+    if (!interface)
         return false;
 
     if (!sGpsDebugInterface)
-       sGpsDebugInterface = (const GpsDebugInterface*)sGpsInterface->get_extension(GPS_DEBUG_INTERFACE);
+       sGpsDebugInterface = (const GpsDebugInterface*)interface->get_extension(GPS_DEBUG_INTERFACE);
 
     return true;
 }