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

Commit bb95cb9f authored by Brian Muramatsu's avatar Brian Muramatsu
Browse files

Fix GPS settings change listener in LocManager

Bug 7051185

- Register a ContentObserver to track settings changes rather than
  opening up a Cursor with a ContentQueryMap.

- Move updateProvidersLocked into init to assure that the
  ContentObserver does not miss any changes.

- Move blacklist and fudger creation before loadProvidersLocked to
  improve code readability.

Change-Id: I4d3e19fa33401c384bc2b00658d4336ea119e0e5
parent f1ffb4f7
Loading
Loading
Loading
Loading
+19 −22
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.database.Cursor;
import android.location.Address;
import android.location.Criteria;
@@ -87,7 +88,7 @@ import java.util.Set;
 * The service class that manages LocationProviders and issues location
 * updates and alerts.
 */
public class LocationManagerService extends ILocationManager.Stub implements Observer, Runnable {
public class LocationManagerService extends ILocationManager.Stub implements Runnable {
    private static final String TAG = "LocationManagerService";
    public static final boolean D = false;

@@ -207,24 +208,30 @@ public class LocationManagerService extends ILocationManager.Stub implements Obs
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);
        mPackageManager = mContext.getPackageManager();

        mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
        mBlacklist.init();
        mLocationFudger = new LocationFudger();

        synchronized (mLock) {
            loadProvidersLocked();
        }
        mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
        mBlacklist.init();

        mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
        mLocationFudger = new LocationFudger();

        // listen for settings changes
        ContentResolver resolver = mContext.getContentResolver();
        Cursor settingsCursor = resolver.query(Settings.Secure.CONTENT_URI, null,
                "(" + NameValueTable.NAME + "=?)",
                new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED}, null);
        ContentQueryMap query = new ContentQueryMap(settingsCursor, NameValueTable.NAME, true,
                mLocationHandler);
        settingsCursor.close();
        query.addObserver(this);
        mContext.getContentResolver().registerContentObserver(
                Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true, 
                new ContentObserver(mLocationHandler) {
           @Override
            public void onChange(boolean selfChange) {
               synchronized (mLock) {
                   updateProvidersLocked();
               }
            }
        });
        mPackageMonitor.register(mContext, Looper.myLooper(), true);

        updateProvidersLocked();
    }

    private void loadProvidersLocked() {
@@ -299,8 +306,6 @@ public class LocationManagerService extends ILocationManager.Stub implements Obs
        if (mGeocodeProvider == null) {
            Slog.e(TAG,  "no geocoder provider found");
        }

        updateProvidersLocked();
    }

    /**
@@ -544,14 +549,6 @@ public class LocationManagerService extends ILocationManager.Stub implements Obs
        }
    }

    /** Settings Observer callback */
    @Override
    public void update(Observable o, Object arg) {
        synchronized (mLock) {
            updateProvidersLocked();
        }
    }

    private void addProviderLocked(LocationProviderInterface provider) {
        mProviders.add(provider);
        mProvidersByName.put(provider.getName(), provider);