Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5de72739 authored by Steve Block's avatar Steve Block Committed by Android (Google) Code Review
Browse files

Merge "Make sure Geolocation is robust to location providers being absent on the device."

parents e188664f 3d9e066a
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -62,9 +62,10 @@ final class GeolocationService implements LocationListener {
    /**
     * Start listening for location updates.
     */
    public void start() {
    public boolean start() {
        registerForLocationUpdates();
        mIsRunning = true;
        return mIsNetworkProviderAvailable || mIsGpsProviderAvailable;
    }

    /**
@@ -87,6 +88,8 @@ final class GeolocationService implements LocationListener {
                // only unregister from all, then reregister with all but the GPS.
                unregisterFromLocationUpdates();
                registerForLocationUpdates();
                // Check that the providers are still available after we re-register.
                maybeReportError("The last location provider is no longer available");
            }
        }
    }
@@ -155,12 +158,17 @@ final class GeolocationService implements LocationListener {
     * Registers this object with the location service.
     */
    private void registerForLocationUpdates() {
        try {
            // Registration may fail if providers are not present on the device.
            try {
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                mIsNetworkProviderAvailable = true;
            } catch(IllegalArgumentException e) { }
            if (mIsGpsEnabled) {
                try {
                    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                    mIsGpsProviderAvailable = true;
                } catch(IllegalArgumentException e) { }
            }
        } catch(SecurityException e) {
            Log.e(TAG, "Caught security exception registering for location updates from system. " +
@@ -173,6 +181,8 @@ final class GeolocationService implements LocationListener {
     */
    private void unregisterFromLocationUpdates() {
        mLocationManager.removeUpdates(this);
        mIsNetworkProviderAvailable = false;
        mIsGpsProviderAvailable = false;
    }

    /**