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

Commit e0fd693c authored by Nick Pelly's avatar Nick Pelly
Browse files

Improve geofencing: throttle location updates with distance to fence.

Previously any geofence (proximity alert) would turn the GPS on at full rate.
Now, we modify the GPS interval with the distance to the nearest geofence.
A speed of 100m/s is assumed to calculate the next GPS update.

Also
o Major refactor of geofencing code, to make it easier to continue to improve.
o Discard proximity alerts when an app is removed.
o Misc cleanup of nearby code. There are other upcoming changes
  that make this a good time for some house-keeping.

TODO:
The new geofencing heuristics are much better than before, but still
relatively naive. The next steps could be:
- Improve boundary detection
- Improve update thottling for large geofences
- Consider velocity when throttling

Change-Id: Ie6e23d2cb2b931eba5d2a2fc759543bb96e2f7d0
parent 357d9cb8
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -361,10 +361,10 @@ class ContextImpl extends Context {
                    return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
                }});

        registerService(LOCATION_SERVICE, new StaticServiceFetcher() {
                public Object createStaticService() {
        registerService(LOCATION_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    IBinder b = ServiceManager.getService(LOCATION_SERVICE);
                    return new LocationManager(ILocationManager.Stub.asInterface(b));
                    return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
                }});

        registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ interface ILocationManager
    boolean sendExtraCommand(String provider, String command, inout Bundle extras);

    void addProximityAlert(double latitude, double longitude, float distance,
        long expiration, in PendingIntent intent);
        long expiration, in PendingIntent intent, in String packageName);
    void removeProximityAlert(in PendingIntent intent);

    Bundle getProviderInfo(String provider);
+17 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.location;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
@@ -52,7 +53,9 @@ import java.util.List;
 */
public class LocationManager {
    private static final String TAG = "LocationManager";
    private ILocationManager mService;

    private final Context mContext;
    private final ILocationManager mService;
    private final HashMap<GpsStatus.Listener, GpsStatusListenerTransport> mGpsStatusListeners =
            new HashMap<GpsStatus.Listener, GpsStatusListenerTransport>();
    private final HashMap<GpsStatus.NmeaListener, GpsStatusListenerTransport> mNmeaListeners =
@@ -193,6 +196,7 @@ public class LocationManager {
            }
        }

        @Override
        public void onLocationChanged(Location location) {
            Message msg = Message.obtain();
            msg.what = TYPE_LOCATION_CHANGED;
@@ -200,6 +204,7 @@ public class LocationManager {
            mListenerHandler.sendMessage(msg);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Message msg = Message.obtain();
            msg.what = TYPE_STATUS_CHANGED;
@@ -213,6 +218,7 @@ public class LocationManager {
            mListenerHandler.sendMessage(msg);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Message msg = Message.obtain();
            msg.what = TYPE_PROVIDER_ENABLED;
@@ -220,6 +226,7 @@ public class LocationManager {
            mListenerHandler.sendMessage(msg);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Message msg = Message.obtain();
            msg.what = TYPE_PROVIDER_DISABLED;
@@ -260,8 +267,9 @@ public class LocationManager {
     * right way to create an instance of this class is using the
     * factory Context.getSystemService.
     */
    public LocationManager(ILocationManager service) {
    public LocationManager(Context context, ILocationManager service) {
        mService = service;
        mContext = context;
    }

    private LocationProvider createProvider(String name, Bundle info) {
@@ -1086,8 +1094,8 @@ public class LocationManager {
                ", intent = " + intent);
        }
        try {
            mService.addProximityAlert(latitude, longitude, radius,
                                       expiration, intent);
            mService.addProximityAlert(latitude, longitude, radius, expiration, intent,
                    mContext.getPackageName());
        } catch (RemoteException ex) {
            Log.e(TAG, "addProximityAlert: RemoteException", ex);
        }
@@ -1361,6 +1369,7 @@ public class LocationManager {
            mNmeaBuffer = new ArrayList<Nmea>();
        }

        @Override
        public void onGpsStarted() {
            if (mListener != null) {
                Message msg = Message.obtain();
@@ -1369,6 +1378,7 @@ public class LocationManager {
            }
        }

        @Override
        public void onGpsStopped() {
            if (mListener != null) {
                Message msg = Message.obtain();
@@ -1377,6 +1387,7 @@ public class LocationManager {
            }
        }

        @Override
        public void onFirstFix(int ttff) {
            if (mListener != null) {
                mGpsStatus.setTimeToFirstFix(ttff);
@@ -1386,6 +1397,7 @@ public class LocationManager {
            }
        }

        @Override
        public void onSvStatusChanged(int svCount, int[] prns, float[] snrs,
                float[] elevations, float[] azimuths, int ephemerisMask,
                int almanacMask, int usedInFixMask) {
@@ -1401,6 +1413,7 @@ public class LocationManager {
            }
        }

        @Override
        public void onNmeaReceived(long timestamp, String nmea) {
            if (mNmeaListener != null) {
                synchronized (mNmeaBuffer) {
+10 −1
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ public class DummyLocationProvider extends LocationProvider {
     * Returns true if the provider requires access to a
     * data network (e.g., the Internet), false otherwise.
     */
    @Override
    public boolean requiresNetwork() {
        return mRequiresNetwork;
    }
@@ -95,6 +96,7 @@ public class DummyLocationProvider extends LocationProvider {
     * satellite-based positioning system (e.g., GPS), false
     * otherwise.
     */
    @Override
    public boolean requiresSatellite() {
        return mRequiresSatellite;
    }
@@ -104,6 +106,7 @@ public class DummyLocationProvider extends LocationProvider {
     * cellular network (e.g., to make use of cell tower IDs), false
     * otherwise.
     */
    @Override
    public boolean requiresCell() {
        return mRequiresCell;
    }
@@ -113,6 +116,7 @@ public class DummyLocationProvider extends LocationProvider {
     * monetary charge to the user, false if use is free.  It is up to
     * each provider to give accurate information.
     */
    @Override
    public boolean hasMonetaryCost() {
        return mHasMonetaryCost;
    }
@@ -123,6 +127,7 @@ public class DummyLocationProvider extends LocationProvider {
     * under most circumstances but may occassionally not report it
     * should return true.
     */
    @Override
    public boolean supportsAltitude() {
        return mSupportsAltitude;
    }
@@ -133,6 +138,7 @@ public class DummyLocationProvider extends LocationProvider {
     * under most circumstances but may occassionally not report it
     * should return true.
     */
    @Override
    public boolean supportsSpeed() {
        return mSupportsSpeed;
    }
@@ -143,6 +149,7 @@ public class DummyLocationProvider extends LocationProvider {
     * under most circumstances but may occassionally not report it
     * should return true.
     */
    @Override
    public boolean supportsBearing() {
        return mSupportsBearing;
    }
@@ -153,6 +160,7 @@ public class DummyLocationProvider extends LocationProvider {
     * @return the power requirement for this provider, as one of the
     * constants Criteria.POWER_REQUIREMENT_*.
     */
    @Override
    public int getPowerRequirement() {
        return mPowerRequirement;
    }
@@ -164,6 +172,7 @@ public class DummyLocationProvider extends LocationProvider {
     * @return the horizontal accuracy for this provider, as one of the
     * constants Criteria.ACCURACY_*.
     */
    @Override
    public int getAccuracy() {
        return mAccuracy;
    }
+93 −356

File changed.

Preview size limit exceeded, changes collapsed.

Loading