|
40 | 40 | import android.os.Bundle; |
41 | 41 | import android.os.Handler; |
42 | 42 | import android.os.IBinder; |
43 | | -import android.os.Looper; |
44 | 43 | import android.os.Message; |
45 | 44 | import android.os.PowerManager; |
46 | | -import android.os.Process; |
47 | 45 | import android.os.RemoteException; |
48 | 46 | import android.os.ServiceManager; |
49 | 47 | import android.os.SystemClock; |
|
75 | 73 | import java.util.Date; |
76 | 74 | import java.util.Map.Entry; |
77 | 75 | import java.util.Properties; |
78 | | -import java.util.concurrent.CountDownLatch; |
79 | 76 |
|
80 | 77 | /** |
81 | 78 | * A GPS implementation of LocationProvider used by LocationManager. |
@@ -287,12 +284,8 @@ public GpsRequest(ProviderRequest request, WorkSource source) { |
287 | 284 | private Bundle mLocationExtras = new Bundle(); |
288 | 285 | private ArrayList<Listener> mListeners = new ArrayList<Listener>(); |
289 | 286 |
|
290 | | - // GpsLocationProvider's handler thread |
291 | | - private final Thread mThread; |
292 | | - // Handler for processing events in mThread. |
| 287 | + // Handler for processing events |
293 | 288 | private Handler mHandler; |
294 | | - // Used to signal when our main thread has initialized everything |
295 | | - private final CountDownLatch mInitializedLatch = new CountDownLatch(1); |
296 | 289 |
|
297 | 290 | private String mAGpsApn; |
298 | 291 | private int mAGpsDataConnectionState; |
@@ -437,21 +430,6 @@ public GpsLocationProvider(Context context, ILocationManager ilocationManager) { |
437 | 430 | mWakeupIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_WAKEUP), 0); |
438 | 431 | mTimeoutIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_TIMEOUT), 0); |
439 | 432 |
|
440 | | - IntentFilter intentFilter = new IntentFilter(); |
441 | | - intentFilter.addAction(Intents.DATA_SMS_RECEIVED_ACTION); |
442 | | - intentFilter.addDataScheme("sms"); |
443 | | - intentFilter.addDataAuthority("localhost","7275"); |
444 | | - context.registerReceiver(mBroadcastReciever, intentFilter); |
445 | | - |
446 | | - intentFilter = new IntentFilter(); |
447 | | - intentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION); |
448 | | - try { |
449 | | - intentFilter.addDataType("application/vnd.omaloc-supl-init"); |
450 | | - } catch (IntentFilter.MalformedMimeTypeException e) { |
451 | | - Log.w(TAG, "Malformed SUPL init mime type"); |
452 | | - } |
453 | | - context.registerReceiver(mBroadcastReciever, intentFilter); |
454 | | - |
455 | 433 | mConnMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); |
456 | 434 |
|
457 | 435 | // Battery statistics service to be notified when GPS turns on or off |
@@ -487,26 +465,43 @@ public GpsLocationProvider(Context context, ILocationManager ilocationManager) { |
487 | 465 | Log.w(TAG, "Could not open GPS configuration file " + PROPERTIES_FILE); |
488 | 466 | } |
489 | 467 |
|
490 | | - // wait until we are fully initialized before returning |
491 | | - mThread = new GpsLocationProviderThread(); |
492 | | - mThread.start(); |
493 | | - while (true) { |
494 | | - try { |
495 | | - mInitializedLatch.await(); |
496 | | - break; |
497 | | - } catch (InterruptedException e) { |
498 | | - Thread.currentThread().interrupt(); |
| 468 | + // construct handler, listen for events |
| 469 | + mHandler = new ProviderHandler(); |
| 470 | + listenForBroadcasts(); |
| 471 | + |
| 472 | + // also listen for PASSIVE_PROVIDER updates |
| 473 | + mHandler.post(new Runnable() { |
| 474 | + @Override |
| 475 | + public void run() { |
| 476 | + LocationManager locManager = |
| 477 | + (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 478 | + locManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, |
| 479 | + 0, 0, new NetworkLocationListener(), mHandler.getLooper()); |
499 | 480 | } |
500 | | - } |
| 481 | + }); |
501 | 482 | } |
502 | 483 |
|
503 | | - private void initialize() { |
504 | | - // register our receiver on our thread rather than the main thread |
| 484 | + private void listenForBroadcasts() { |
505 | 485 | IntentFilter intentFilter = new IntentFilter(); |
| 486 | + intentFilter.addAction(Intents.DATA_SMS_RECEIVED_ACTION); |
| 487 | + intentFilter.addDataScheme("sms"); |
| 488 | + intentFilter.addDataAuthority("localhost","7275"); |
| 489 | + mContext.registerReceiver(mBroadcastReciever, intentFilter, null, mHandler); |
| 490 | + |
| 491 | + intentFilter = new IntentFilter(); |
| 492 | + intentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION); |
| 493 | + try { |
| 494 | + intentFilter.addDataType("application/vnd.omaloc-supl-init"); |
| 495 | + } catch (IntentFilter.MalformedMimeTypeException e) { |
| 496 | + Log.w(TAG, "Malformed SUPL init mime type"); |
| 497 | + } |
| 498 | + mContext.registerReceiver(mBroadcastReciever, intentFilter, null, mHandler); |
| 499 | + |
| 500 | + intentFilter = new IntentFilter(); |
506 | 501 | intentFilter.addAction(ALARM_WAKEUP); |
507 | 502 | intentFilter.addAction(ALARM_TIMEOUT); |
508 | 503 | intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
509 | | - mContext.registerReceiver(mBroadcastReciever, intentFilter); |
| 504 | + mContext.registerReceiver(mBroadcastReciever, intentFilter, null, mHandler); |
510 | 505 | } |
511 | 506 |
|
512 | 507 | /** |
@@ -1536,29 +1531,6 @@ public void handleMessage(Message msg) { |
1536 | 1531 | } |
1537 | 1532 | }; |
1538 | 1533 |
|
1539 | | - private final class GpsLocationProviderThread extends Thread { |
1540 | | - |
1541 | | - public GpsLocationProviderThread() { |
1542 | | - super("GpsLocationProvider"); |
1543 | | - } |
1544 | | - |
1545 | | - @Override |
1546 | | - public void run() { |
1547 | | - Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
1548 | | - initialize(); |
1549 | | - Looper.prepare(); |
1550 | | - |
1551 | | - LocationManager locManager = |
1552 | | - (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
1553 | | - mHandler = new ProviderHandler(); |
1554 | | - // signal when we are initialized and ready to go |
1555 | | - mInitializedLatch.countDown(); |
1556 | | - locManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, |
1557 | | - 0, 0, new NetworkLocationListener(), Looper.myLooper()); |
1558 | | - Looper.loop(); |
1559 | | - } |
1560 | | - } |
1561 | | - |
1562 | 1534 | private final class NetworkLocationListener implements LocationListener { |
1563 | 1535 | @Override |
1564 | 1536 | public void onLocationChanged(Location location) { |
|
0 commit comments