summaryrefslogtreecommitdiffstats
path: root/location/java
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2010-03-24 10:14:55 -0400
committerMike Lockwood <lockwood@android.com>2010-03-24 12:38:42 -0400
commit89096317104722219f73a5c3cc424e6e98c90a61 (patch)
tree60b9f9c80ab3e93234908ab739eeb1cad1e244ef /location/java
parent7f49b9e47416808d7ef5de77b4094fd83f50134d (diff)
downloadframeworks_base-89096317104722219f73a5c3cc424e6e98c90a61.zip
frameworks_base-89096317104722219f73a5c3cc424e6e98c90a61.tar.gz
frameworks_base-89096317104722219f73a5c3cc424e6e98c90a61.tar.bz2
Wait until GpsLocationProvider is fully initialized before returning from the constructor.
Otherwise we are left with a race condition that might result in the provider being called before it is ready. BUG: 2539392 Change-Id: I05f4a2501c43d5fa95c0e68f5a963015cf9c6e8f Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'location/java')
-rwxr-xr-xlocation/java/com/android/internal/location/GpsLocationProvider.java50
1 files changed, 37 insertions, 13 deletions
diff --git a/location/java/com/android/internal/location/GpsLocationProvider.java b/location/java/com/android/internal/location/GpsLocationProvider.java
index 8dc206c..15d692c 100755
--- a/location/java/com/android/internal/location/GpsLocationProvider.java
+++ b/location/java/com/android/internal/location/GpsLocationProvider.java
@@ -63,13 +63,14 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import java.util.Map.Entry;
+import java.util.concurrent.CountDownLatch;
/**
* A GPS implementation of LocationProvider used by LocationManager.
*
* {@hide}
*/
-public class GpsLocationProvider implements LocationProviderInterface, Runnable {
+public class GpsLocationProvider implements LocationProviderInterface {
private static final String TAG = "GpsLocationProvider";
@@ -190,7 +191,7 @@ public class GpsLocationProvider implements LocationProviderInterface, Runnable
private static final int NO_FIX_TIMEOUT = 60;
// true if we are enabled
- private boolean mEnabled;
+ private volatile boolean mEnabled;
// true if we have network connectivity
private boolean mNetworkAvailable;
@@ -236,7 +237,13 @@ public class GpsLocationProvider implements LocationProviderInterface, Runnable
private Bundle mLocationExtras = new Bundle();
private ArrayList<Listener> mListeners = new ArrayList<Listener>();
+ // GpsLocationProvider's handler thread
+ private final Thread mThread;
+ // Handler for processing events in mThread.
private Handler mHandler;
+ // Used to signal when our main thread has initialized everything
+ private final CountDownLatch mInitializedLatch = new CountDownLatch(1);
+ // Thread for receiving events from the native code
private Thread mEventThread;
private String mAGpsApn;
@@ -389,8 +396,17 @@ public class GpsLocationProvider implements LocationProviderInterface, Runnable
Log.w(TAG, "Could not open GPS configuration file " + PROPERTIES_FILE);
}
- Thread thread = new Thread(null, this, "GpsLocationProvider");
- thread.start();
+ // wait until we are fully initialized before returning
+ mThread = new GpsLocationProviderThread();
+ mThread.start();
+ while (true) {
+ try {
+ mInitializedLatch.await();
+ break;
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
}
private void initialize() {
@@ -673,7 +689,7 @@ public class GpsLocationProvider implements LocationProviderInterface, Runnable
}
private void handleDisable() {
- if (DEBUG) Log.d(TAG, "handleEnable");
+ if (DEBUG) Log.d(TAG, "handleDisable");
if (!mEnabled) return;
mEnabled = false;
@@ -1327,7 +1343,7 @@ public class GpsLocationProvider implements LocationProviderInterface, Runnable
// native_wait_for_event() will callback to us via reportLocation(), reportStatus(), etc.
// this is necessary because native code cannot call Java on a thread that the JVM does
// not know about.
- private class GpsEventThread extends Thread {
+ private final class GpsEventThread extends Thread {
public GpsEventThread() {
super("GpsEventThread");
@@ -1384,13 +1400,21 @@ public class GpsLocationProvider implements LocationProviderInterface, Runnable
}
};
- public void run()
- {
- Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
- initialize();
- Looper.prepare();
- mHandler = new ProviderHandler();
- Looper.loop();
+ private final class GpsLocationProviderThread extends Thread {
+
+ public GpsLocationProviderThread() {
+ super("GpsLocationProvider");
+ }
+
+ public void run() {
+ Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
+ initialize();
+ Looper.prepare();
+ mHandler = new ProviderHandler();
+ // signal when we are initialized and ready to go
+ mInitializedLatch.countDown();
+ Looper.loop();
+ }
}
// for GPS SV statistics