|
35 | 35 | import android.net.ConnectivityManager; |
36 | 36 | import android.net.NetworkInfo; |
37 | 37 | import android.net.Uri; |
| 38 | +import android.os.AsyncTask; |
38 | 39 | import android.os.Binder; |
39 | 40 | import android.os.Bundle; |
40 | 41 | import android.os.Handler; |
@@ -164,6 +165,8 @@ public class GpsLocationProvider implements LocationProviderInterface { |
164 | 165 | private static final int UPDATE_LOCATION = 7; |
165 | 166 | private static final int ADD_LISTENER = 8; |
166 | 167 | private static final int REMOVE_LISTENER = 9; |
| 168 | + private static final int INJECT_NTP_TIME_FINISHED = 10; |
| 169 | + private static final int DOWNLOAD_XTRA_DATA_FINISHED = 11; |
167 | 170 |
|
168 | 171 | // Request setid |
169 | 172 | private static final int AGPS_RIL_REQUEST_SETID_IMSI = 1; |
@@ -229,10 +232,15 @@ public GpsRequest(ProviderRequest request, WorkSource source) { |
229 | 232 | // true if we have network connectivity |
230 | 233 | private boolean mNetworkAvailable; |
231 | 234 |
|
| 235 | + // states for injecting ntp and downloading xtra data |
| 236 | + private static final int STATE_PENDING_NETWORK = 0; |
| 237 | + private static final int STATE_DOWNLOADING = 1; |
| 238 | + private static final int STATE_IDLE = 2; |
| 239 | + |
232 | 240 | // flags to trigger NTP or XTRA data download when network becomes available |
233 | 241 | // initialized to true so we do NTP and XTRA when the network comes up after booting |
234 | | - private boolean mInjectNtpTimePending = true; |
235 | | - private boolean mDownloadXtraDataPending = true; |
| 242 | + private int mInjectNtpTimePending = STATE_PENDING_NETWORK; |
| 243 | + private int mDownloadXtraDataPending = STATE_PENDING_NETWORK; |
236 | 244 |
|
237 | 245 | // set to true if the GPS engine does not do on-demand NTP time requests |
238 | 246 | private boolean mPeriodicTimeInjection; |
@@ -569,81 +577,105 @@ private void handleUpdateNetworkState(int state, NetworkInfo info) { |
569 | 577 | } |
570 | 578 |
|
571 | 579 | if (mNetworkAvailable) { |
572 | | - if (mInjectNtpTimePending) { |
| 580 | + if (mInjectNtpTimePending == STATE_PENDING_NETWORK) { |
573 | 581 | sendMessage(INJECT_NTP_TIME, 0, null); |
574 | 582 | } |
575 | | - if (mDownloadXtraDataPending) { |
| 583 | + if (mDownloadXtraDataPending == STATE_PENDING_NETWORK) { |
576 | 584 | sendMessage(DOWNLOAD_XTRA_DATA, 0, null); |
577 | 585 | } |
578 | 586 | } |
579 | 587 | } |
580 | 588 |
|
581 | 589 | private void handleInjectNtpTime() { |
| 590 | + if (mInjectNtpTimePending == STATE_DOWNLOADING) { |
| 591 | + // already downloading data |
| 592 | + return; |
| 593 | + } |
582 | 594 | if (!mNetworkAvailable) { |
583 | 595 | // try again when network is up |
584 | | - mInjectNtpTimePending = true; |
| 596 | + mInjectNtpTimePending = STATE_PENDING_NETWORK; |
585 | 597 | return; |
586 | 598 | } |
587 | | - mInjectNtpTimePending = false; |
| 599 | + mInjectNtpTimePending = STATE_DOWNLOADING; |
588 | 600 |
|
589 | | - long delay; |
| 601 | + AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { |
| 602 | + @Override |
| 603 | + public void run() { |
| 604 | + long delay; |
590 | 605 |
|
591 | | - // force refresh NTP cache when outdated |
592 | | - if (mNtpTime.getCacheAge() >= NTP_INTERVAL) { |
593 | | - mNtpTime.forceRefresh(); |
594 | | - } |
| 606 | + // force refresh NTP cache when outdated |
| 607 | + if (mNtpTime.getCacheAge() >= NTP_INTERVAL) { |
| 608 | + mNtpTime.forceRefresh(); |
| 609 | + } |
595 | 610 |
|
596 | | - // only update when NTP time is fresh |
597 | | - if (mNtpTime.getCacheAge() < NTP_INTERVAL) { |
598 | | - long time = mNtpTime.getCachedNtpTime(); |
599 | | - long timeReference = mNtpTime.getCachedNtpTimeReference(); |
600 | | - long certainty = mNtpTime.getCacheCertainty(); |
601 | | - long now = System.currentTimeMillis(); |
602 | | - |
603 | | - Log.d(TAG, "NTP server returned: " |
604 | | - + time + " (" + new Date(time) |
605 | | - + ") reference: " + timeReference |
606 | | - + " certainty: " + certainty |
607 | | - + " system time offset: " + (time - now)); |
608 | | - |
609 | | - native_inject_time(time, timeReference, (int) certainty); |
610 | | - delay = NTP_INTERVAL; |
611 | | - } else { |
612 | | - if (DEBUG) Log.d(TAG, "requestTime failed"); |
613 | | - delay = RETRY_INTERVAL; |
614 | | - } |
| 611 | + // only update when NTP time is fresh |
| 612 | + if (mNtpTime.getCacheAge() < NTP_INTERVAL) { |
| 613 | + long time = mNtpTime.getCachedNtpTime(); |
| 614 | + long timeReference = mNtpTime.getCachedNtpTimeReference(); |
| 615 | + long certainty = mNtpTime.getCacheCertainty(); |
| 616 | + long now = System.currentTimeMillis(); |
| 617 | + |
| 618 | + Log.d(TAG, "NTP server returned: " |
| 619 | + + time + " (" + new Date(time) |
| 620 | + + ") reference: " + timeReference |
| 621 | + + " certainty: " + certainty |
| 622 | + + " system time offset: " + (time - now)); |
| 623 | + |
| 624 | + native_inject_time(time, timeReference, (int) certainty); |
| 625 | + delay = NTP_INTERVAL; |
| 626 | + } else { |
| 627 | + if (DEBUG) Log.d(TAG, "requestTime failed"); |
| 628 | + delay = RETRY_INTERVAL; |
| 629 | + } |
615 | 630 |
|
616 | | - if (mPeriodicTimeInjection) { |
617 | | - // send delayed message for next NTP injection |
618 | | - // since this is delayed and not urgent we do not hold a wake lock here |
619 | | - mHandler.removeMessages(INJECT_NTP_TIME); |
620 | | - mHandler.sendMessageDelayed(Message.obtain(mHandler, INJECT_NTP_TIME), delay); |
621 | | - } |
| 631 | + mHandler.sendMessage(Message.obtain(mHandler, INJECT_NTP_TIME_FINISHED)); |
| 632 | + |
| 633 | + if (mPeriodicTimeInjection) { |
| 634 | + // send delayed message for next NTP injection |
| 635 | + // since this is delayed and not urgent we do not hold a wake lock here |
| 636 | + mHandler.removeMessages(INJECT_NTP_TIME); |
| 637 | + mHandler.sendMessageDelayed(Message.obtain(mHandler, INJECT_NTP_TIME), delay); |
| 638 | + } |
| 639 | + } |
| 640 | + }); |
622 | 641 | } |
623 | 642 |
|
624 | 643 | private void handleDownloadXtraData() { |
| 644 | + if (mDownloadXtraDataPending == STATE_DOWNLOADING) { |
| 645 | + // already downloading data |
| 646 | + return; |
| 647 | + } |
625 | 648 | if (!mNetworkAvailable) { |
626 | 649 | // try again when network is up |
627 | | - mDownloadXtraDataPending = true; |
| 650 | + mDownloadXtraDataPending = STATE_PENDING_NETWORK; |
628 | 651 | return; |
629 | 652 | } |
630 | | - mDownloadXtraDataPending = false; |
| 653 | + mDownloadXtraDataPending = STATE_DOWNLOADING; |
| 654 | + |
| 655 | + AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { |
| 656 | + @Override |
| 657 | + public void run() { |
| 658 | + GpsXtraDownloader xtraDownloader = new GpsXtraDownloader(mContext, mProperties); |
| 659 | + byte[] data = xtraDownloader.downloadXtraData(); |
| 660 | + if (data != null) { |
| 661 | + if (DEBUG) { |
| 662 | + Log.d(TAG, "calling native_inject_xtra_data"); |
| 663 | + } |
| 664 | + native_inject_xtra_data(data, data.length); |
| 665 | + } |
631 | 666 |
|
| 667 | + mHandler.sendMessage(Message.obtain(mHandler, DOWNLOAD_XTRA_DATA_FINISHED)); |
632 | 668 |
|
633 | | - GpsXtraDownloader xtraDownloader = new GpsXtraDownloader(mContext, mProperties); |
634 | | - byte[] data = xtraDownloader.downloadXtraData(); |
635 | | - if (data != null) { |
636 | | - if (DEBUG) { |
637 | | - Log.d(TAG, "calling native_inject_xtra_data"); |
| 669 | + if (data == null) { |
| 670 | + // try again later |
| 671 | + // since this is delayed and not urgent we do not hold a wake lock here |
| 672 | + mHandler.removeMessages(DOWNLOAD_XTRA_DATA); |
| 673 | + mHandler.sendMessageDelayed(Message.obtain(mHandler, DOWNLOAD_XTRA_DATA), |
| 674 | + RETRY_INTERVAL); |
| 675 | + } |
638 | 676 | } |
639 | | - native_inject_xtra_data(data, data.length); |
640 | | - } else { |
641 | | - // try again later |
642 | | - // since this is delayed and not urgent we do not hold a wake lock here |
643 | | - mHandler.removeMessages(DOWNLOAD_XTRA_DATA); |
644 | | - mHandler.sendMessageDelayed(Message.obtain(mHandler, DOWNLOAD_XTRA_DATA), |
645 | | - RETRY_INTERVAL); |
646 | | - } |
| 677 | + |
| 678 | + }); |
647 | 679 | } |
648 | 680 |
|
649 | 681 | private void handleUpdateLocation(Location location) { |
@@ -1474,6 +1506,12 @@ public void handleMessage(Message msg) { |
1474 | 1506 | handleDownloadXtraData(); |
1475 | 1507 | } |
1476 | 1508 | break; |
| 1509 | + case INJECT_NTP_TIME_FINISHED: |
| 1510 | + mInjectNtpTimePending = STATE_IDLE; |
| 1511 | + break; |
| 1512 | + case DOWNLOAD_XTRA_DATA_FINISHED: |
| 1513 | + mDownloadXtraDataPending = STATE_IDLE; |
| 1514 | + break; |
1477 | 1515 | case UPDATE_LOCATION: |
1478 | 1516 | handleUpdateLocation((Location)msg.obj); |
1479 | 1517 | break; |
|
0 commit comments