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

Commit 9627429f authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Implement stationary location throttling

Throttle location providers completely when the device is completely
still, and provide "faked" locations instead.

Bug: 172688700
Test: manual + atest StationaryThrottlingLocationProviderTest
Change-Id: Iba4e50ebf78cd8c6a9f2433ecb6e967d3a533c63
parent b9137796
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -10779,6 +10779,13 @@ public final class Settings {
        public static final String LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST =
                "location_ignore_settings_package_whitelist";
        /**
         * Whether to throttle location when the device is in doze and still.
         * @hide
         */
        public static final String LOCATION_ENABLE_STATIONARY_THROTTLE =
                "location_enable_stationary_throttle";
        /**
        * Whether TV will switch to MHL port when a mobile device is plugged in.
        * (0 = false, 1 = true)
+6 −1
Original line number Diff line number Diff line
@@ -569,7 +569,12 @@ public class Location implements Parcelable {

    /** @hide */
    public long getElapsedRealtimeAgeMillis() {
        return NANOSECONDS.toMillis(getElapsedRealtimeAgeNanos());
        return getElapsedRealtimeAgeMillis(SystemClock.elapsedRealtime());
    }

    /** @hide */
    public long getElapsedRealtimeAgeMillis(long referenceRealtimeMs) {
        return referenceRealtimeMs - NANOSECONDS.toMillis(mElapsedRealtimeNanos);
    }

    /**
+1 −0
Original line number Diff line number Diff line
@@ -323,6 +323,7 @@ public class SettingsBackupTest {
                    Settings.Global.LOCATION_BACKGROUND_THROTTLE_INTERVAL_MS,
                    Settings.Global.LOCATION_BACKGROUND_THROTTLE_PROXIMITY_ALERT_INTERVAL_MS,
                    Settings.Global.LOCATION_BACKGROUND_THROTTLE_PACKAGE_WHITELIST,
                    Settings.Global.LOCATION_ENABLE_STATIONARY_THROTTLE,
                    Settings.Global.LOCATION_IGNORE_SETTINGS_PACKAGE_WHITELIST,
                    Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED,
                    Settings.Global.LOCK_SOUND,
+32 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ import android.os.RemoteException;
import android.os.UserHandle;
import android.os.WorkSource;
import android.os.WorkSource.WorkChain;
import android.provider.Settings;
import android.stats.location.LocationStatsEnums;
import android.util.ArrayMap;
import android.util.IndentingPrintWriter;
@@ -97,6 +98,8 @@ import com.android.server.location.gnss.hal.GnssNative;
import com.android.server.location.injector.AlarmHelper;
import com.android.server.location.injector.AppForegroundHelper;
import com.android.server.location.injector.AppOpsHelper;
import com.android.server.location.injector.DeviceIdleHelper;
import com.android.server.location.injector.DeviceStationaryHelper;
import com.android.server.location.injector.EmergencyHelper;
import com.android.server.location.injector.Injector;
import com.android.server.location.injector.LocationAttributionHelper;
@@ -108,6 +111,8 @@ import com.android.server.location.injector.SettingsHelper;
import com.android.server.location.injector.SystemAlarmHelper;
import com.android.server.location.injector.SystemAppForegroundHelper;
import com.android.server.location.injector.SystemAppOpsHelper;
import com.android.server.location.injector.SystemDeviceIdleHelper;
import com.android.server.location.injector.SystemDeviceStationaryHelper;
import com.android.server.location.injector.SystemEmergencyHelper;
import com.android.server.location.injector.SystemLocationPermissionsHelper;
import com.android.server.location.injector.SystemLocationPowerSaveModeHelper;
@@ -120,6 +125,7 @@ import com.android.server.location.provider.LocationProviderManager;
import com.android.server.location.provider.MockLocationProvider;
import com.android.server.location.provider.PassiveLocationProvider;
import com.android.server.location.provider.PassiveLocationProviderManager;
import com.android.server.location.provider.StationaryThrottlingLocationProvider;
import com.android.server.location.provider.proxy.ProxyLocationProvider;
import com.android.server.pm.permission.LegacyPermissionManagerInternal;

@@ -313,6 +319,18 @@ public class LocationManagerService extends ILocationManager.Stub {

            manager.startManager();
            if (realProvider != null) {

                // custom logic wrapping all non-passive providers
                if (manager != mPassiveManager) {
                    boolean enableStationaryThrottling = Settings.Global.getInt(
                            mContext.getContentResolver(),
                            Settings.Global.LOCATION_ENABLE_STATIONARY_THROTTLE, 1) != 0;
                    if (enableStationaryThrottling) {
                        realProvider = new StationaryThrottlingLocationProvider(manager.getName(),
                                mInjector, realProvider, mEventLog);
                    }
                }

                manager.setRealProvider(realProvider);
            }
            mProviderManagers.add(manager);
@@ -1368,6 +1386,8 @@ public class LocationManagerService extends ILocationManager.Stub {
        private final SystemAppForegroundHelper mAppForegroundHelper;
        private final SystemLocationPowerSaveModeHelper mLocationPowerSaveModeHelper;
        private final SystemScreenInteractiveHelper mScreenInteractiveHelper;
        private final SystemDeviceStationaryHelper mDeviceStationaryHelper;
        private final SystemDeviceIdleHelper mDeviceIdleHelper;
        private final LocationAttributionHelper mLocationAttributionHelper;
        private final LocationUsageLogger mLocationUsageLogger;

@@ -1391,6 +1411,8 @@ public class LocationManagerService extends ILocationManager.Stub {
            mAppForegroundHelper = new SystemAppForegroundHelper(context);
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context, eventLog);
            mScreenInteractiveHelper = new SystemScreenInteractiveHelper(context);
            mDeviceStationaryHelper = new SystemDeviceStationaryHelper();
            mDeviceIdleHelper = new SystemDeviceIdleHelper(context);
            mLocationAttributionHelper = new LocationAttributionHelper(mAppOpsHelper);
            mLocationUsageLogger = new LocationUsageLogger();
        }
@@ -1450,6 +1472,16 @@ public class LocationManagerService extends ILocationManager.Stub {
            return mScreenInteractiveHelper;
        }

        @Override
        public DeviceStationaryHelper getDeviceStationaryHelper() {
            return mDeviceStationaryHelper;
        }

        @Override
        public DeviceIdleHelper getDeviceIdleHelper() {
            return mDeviceIdleHelper;
        }

        @Override
        public LocationAttributionHelper getLocationAttributionHelper() {
            return mLocationAttributionHelper;
+27 −1
Original line number Diff line number Diff line
@@ -60,7 +60,8 @@ public class LocationEventLog extends LocalEventLog {
    private static final int EVENT_PROVIDER_UPDATE_REQUEST = 6;
    private static final int EVENT_PROVIDER_RECEIVE_LOCATION = 7;
    private static final int EVENT_PROVIDER_DELIVER_LOCATION = 8;
    private static final int EVENT_LOCATION_POWER_SAVE_MODE_CHANGE = 9;
    private static final int EVENT_PROVIDER_STATIONARY_THROTTLED = 9;
    private static final int EVENT_LOCATION_POWER_SAVE_MODE_CHANGE = 10;

    @GuardedBy("mAggregateStats")
    private final ArrayMap<String, ArrayMap<String, AggregateStats>> mAggregateStats;
@@ -167,6 +168,11 @@ public class LocationEventLog extends LocalEventLog {
        getAggregateStats(provider, identity.getPackageName()).markLocationDelivered();
    }

    /** Logs that a provider has entered or exited stationary throttling. */
    public void logProviderStationaryThrottled(String provider, boolean throttled) {
        addLogEvent(EVENT_PROVIDER_STATIONARY_THROTTLED, provider, throttled);
    }

    /** Logs that the location power save mode has changed. */
    public void logLocationPowerSaveMode(
            @LocationPowerSaveMode int locationPowerSaveMode) {
@@ -198,6 +204,9 @@ public class LocationEventLog extends LocalEventLog {
            case EVENT_PROVIDER_DELIVER_LOCATION:
                return new ProviderDeliverLocationEvent(timeDelta, (String) args[0],
                        (Integer) args[1], (CallerIdentity) args[2]);
            case EVENT_PROVIDER_STATIONARY_THROTTLED:
                return new ProviderStationaryThrottledEvent(timeDelta, (String) args[0],
                        (Boolean) args[1]);
            case EVENT_LOCATION_POWER_SAVE_MODE_CHANGE:
                return new LocationPowerSaveModeEvent(timeDelta, (Integer) args[0]);
            default:
@@ -332,6 +341,23 @@ public class LocationEventLog extends LocalEventLog {
        }
    }

    private static final class ProviderStationaryThrottledEvent extends ProviderEvent {

        private final boolean mStationaryThrottled;

        private ProviderStationaryThrottledEvent(long timeDelta, String provider,
                boolean stationaryThrottled) {
            super(timeDelta, provider);
            mStationaryThrottled = stationaryThrottled;
        }

        @Override
        public String getLogString() {
            return mProvider + " provider stationary/idle " + (mStationaryThrottled ? "throttled"
                    : "unthrottled");
        }
    }

    private static final class LocationPowerSaveModeEvent extends LogEvent {

        @LocationPowerSaveMode
Loading