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

Commit 3d9e066a authored by Steve Block's avatar Steve Block
Browse files

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

Bug: 2692830
Change-Id: I19f88e20f00fe7ecfb77c06197213d273bd80411
parent da355518
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;
    }

    /**