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

Commit fc61bed0 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove LocationAttributionHelper and minor cleanups"

parents 0b77c138 98e9d59b
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -112,7 +112,6 @@ 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;
import com.android.server.location.injector.LocationPermissionsHelper;
import com.android.server.location.injector.LocationPowerSaveModeHelper;
import com.android.server.location.injector.LocationUsageLogger;
@@ -1705,7 +1704,6 @@ public class LocationManagerService extends ILocationManager.Stub implements
        private final SystemScreenInteractiveHelper mScreenInteractiveHelper;
        private final SystemDeviceStationaryHelper mDeviceStationaryHelper;
        private final SystemDeviceIdleHelper mDeviceIdleHelper;
        private final LocationAttributionHelper mLocationAttributionHelper;
        private final LocationUsageLogger mLocationUsageLogger;

        // lazily instantiated since they may not always be used
@@ -1731,7 +1729,6 @@ public class LocationManagerService extends ILocationManager.Stub implements
            mScreenInteractiveHelper = new SystemScreenInteractiveHelper(context);
            mDeviceStationaryHelper = new SystemDeviceStationaryHelper();
            mDeviceIdleHelper = new SystemDeviceIdleHelper(context);
            mLocationAttributionHelper = new LocationAttributionHelper(mAppOpsHelper);
            mLocationUsageLogger = new LocationUsageLogger();
        }

@@ -1807,11 +1804,6 @@ public class LocationManagerService extends ILocationManager.Stub implements
            return mDeviceIdleHelper;
        }

        @Override
        public LocationAttributionHelper getLocationAttributionHelper() {
            return mLocationAttributionHelper;
        }

        @Override
        public synchronized EmergencyHelper getEmergencyHelper() {
            if (mEmergencyCallHelper == null) {
+4 −5
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.location.gnss;

import static android.app.AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION;

import static com.android.server.location.gnss.GnssManagerService.D;
import static com.android.server.location.gnss.GnssManagerService.TAG;

@@ -32,7 +34,6 @@ import android.util.Log;
import com.android.server.location.gnss.hal.GnssNative;
import com.android.server.location.injector.AppOpsHelper;
import com.android.server.location.injector.Injector;
import com.android.server.location.injector.LocationAttributionHelper;
import com.android.server.location.injector.LocationUsageLogger;
import com.android.server.location.injector.SettingsHelper;

@@ -68,25 +69,23 @@ public final class GnssMeasurementsProvider extends
        @Nullable
        @Override
        protected void onActive() {
            mLocationAttributionHelper.reportHighPowerLocationStart(getIdentity());
            mAppOpsHelper.startOpNoThrow(OP_MONITOR_HIGH_POWER_LOCATION, getIdentity());
        }

        @Nullable
        @Override
        protected void onInactive() {
            mLocationAttributionHelper.reportHighPowerLocationStop(getIdentity());
            mAppOpsHelper.finishOp(OP_MONITOR_HIGH_POWER_LOCATION, getIdentity());
        }
    }

    private final AppOpsHelper mAppOpsHelper;
    private final LocationAttributionHelper mLocationAttributionHelper;
    private final LocationUsageLogger mLogger;
    private final GnssNative mGnssNative;

    public GnssMeasurementsProvider(Injector injector, GnssNative gnssNative) {
        super(injector);
        mAppOpsHelper = injector.getAppOpsHelper();
        mLocationAttributionHelper = injector.getLocationAttributionHelper();
        mLogger = injector.getLocationUsageLogger();
        mGnssNative = gnssNative;

+0 −3
Original line number Diff line number Diff line
@@ -58,9 +58,6 @@ public interface Injector {
    /** Returns a DeviceIdleHelper. */
    DeviceIdleHelper getDeviceIdleHelper();

    /** Returns a LocationAttributionHelper. */
    LocationAttributionHelper getLocationAttributionHelper();

    /** Returns an EmergencyHelper. */
    EmergencyHelper getEmergencyHelper();

+0 −122
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.location.injector;

import static android.app.AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION;
import static android.app.AppOpsManager.OP_MONITOR_LOCATION;

import static com.android.server.location.LocationManagerService.D;
import static com.android.server.location.LocationManagerService.TAG;

import android.location.util.identity.CallerIdentity;
import android.util.ArrayMap;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;

import java.util.Map;

/**
 * Helps manage appop monitoring for multiple location clients.
 */
public class LocationAttributionHelper {

    private final AppOpsHelper mAppOpsHelper;

    @GuardedBy("this")
    private final Map<CallerIdentity, Integer> mAttributions;
    @GuardedBy("this")
    private final Map<CallerIdentity, Integer> mHighPowerAttributions;

    public LocationAttributionHelper(AppOpsHelper appOpsHelper) {
        mAppOpsHelper = appOpsHelper;

        mAttributions = new ArrayMap<>();
        mHighPowerAttributions = new ArrayMap<>();
    }

    /**
     * Report normal location usage for the given caller in the given bucket, with a unique key.
     */
    public synchronized void reportLocationStart(CallerIdentity identity) {
        identity = CallerIdentity.forAggregation(identity);

        int count = mAttributions.getOrDefault(identity, 0);
        if (count == 0) {
            if (mAppOpsHelper.startOpNoThrow(OP_MONITOR_LOCATION, identity)) {
                mAttributions.put(identity, 1);
            }
        } else {
            mAttributions.put(identity, count + 1);
        }
    }

    /**
     * Report normal location usage has stopped for the given caller in the given bucket, with a
     * unique key.
     */
    public synchronized void reportLocationStop(CallerIdentity identity) {
        identity = CallerIdentity.forAggregation(identity);

        int count = mAttributions.getOrDefault(identity, 0);
        if (count == 1) {
            mAttributions.remove(identity);
            mAppOpsHelper.finishOp(OP_MONITOR_LOCATION, identity);
        } else if (count > 1) {
            mAttributions.put(identity, count - 1);
        }
    }

    /**
     * Report high power location usage for the given caller in the given bucket, with a unique
     * key.
     */
    public synchronized void reportHighPowerLocationStart(CallerIdentity identity) {
        identity = CallerIdentity.forAggregation(identity);

        int count = mHighPowerAttributions.getOrDefault(identity, 0);
        if (count == 0) {
            if (D) {
                Log.v(TAG, "starting high power location attribution for " + identity);
            }
            if (mAppOpsHelper.startOpNoThrow(OP_MONITOR_HIGH_POWER_LOCATION, identity)) {
                mHighPowerAttributions.put(identity, 1);
            }
        } else {
            mHighPowerAttributions.put(identity, count + 1);
        }
    }

    /**
     * Report high power location usage has stopped for the given caller in the given bucket,
     * with a unique key.
     */
    public synchronized void reportHighPowerLocationStop(CallerIdentity identity) {
        identity = CallerIdentity.forAggregation(identity);

        int count = mHighPowerAttributions.getOrDefault(identity, 0);
        if (count == 1) {
            if (D) {
                Log.v(TAG, "stopping high power location attribution for " + identity);
            }
            mHighPowerAttributions.remove(identity);
            mAppOpsHelper.finishOp(OP_MONITOR_HIGH_POWER_LOCATION, identity);
        } else if (count > 1) {
            mHighPowerAttributions.put(identity, count - 1);
        }
    }
}
+16 −16
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.location.provider;

import static android.app.AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION;
import static android.app.AppOpsManager.OP_MONITOR_LOCATION;
import static android.app.compat.CompatChanges.isChangeEnabled;
import static android.location.LocationManager.DELIVER_HISTORICAL_LOCATIONS;
import static android.location.LocationManager.GPS_PROVIDER;
@@ -32,6 +34,7 @@ import static android.os.PowerManager.LOCATION_MODE_FOREGROUND_ONLY;
import static android.os.PowerManager.LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF;
import static android.os.PowerManager.LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF;

import static com.android.internal.util.ConcurrentUtils.DIRECT_EXECUTOR;
import static com.android.server.location.LocationManagerService.D;
import static com.android.server.location.LocationManagerService.TAG;
import static com.android.server.location.LocationPermissions.PERMISSION_COARSE;
@@ -98,7 +101,6 @@ import com.android.server.location.injector.AppForegroundHelper;
import com.android.server.location.injector.AppForegroundHelper.AppForegroundListener;
import com.android.server.location.injector.AppOpsHelper;
import com.android.server.location.injector.Injector;
import com.android.server.location.injector.LocationAttributionHelper;
import com.android.server.location.injector.LocationPermissionsHelper;
import com.android.server.location.injector.LocationPermissionsHelper.LocationPermissionsListener;
import com.android.server.location.injector.LocationPowerSaveModeHelper;
@@ -191,7 +193,7 @@ public class LocationProviderManager extends

        private final ILocationListener mListener;

        protected LocationListenerTransport(ILocationListener listener) {
        LocationListenerTransport(ILocationListener listener) {
            mListener = Objects.requireNonNull(listener);
        }

@@ -300,7 +302,7 @@ public class LocationProviderManager extends

        private final ILocationCallback mCallback;

        protected GetCurrentLocationTransport(ILocationCallback callback) {
        GetCurrentLocationTransport(ILocationCallback callback) {
            mCallback = Objects.requireNonNull(callback);
        }

@@ -411,7 +413,7 @@ public class LocationProviderManager extends
            EVENT_LOG.logProviderClientActive(mName, getIdentity());

            if (!getRequest().isHiddenFromAppOps()) {
                mLocationAttributionHelper.reportLocationStart(getIdentity());
                mAppOpsHelper.startOpNoThrow(OP_MONITOR_LOCATION, getIdentity());
            }
            onHighPowerUsageChanged();

@@ -426,7 +428,7 @@ public class LocationProviderManager extends

            onHighPowerUsageChanged();
            if (!getRequest().isHiddenFromAppOps()) {
                mLocationAttributionHelper.reportLocationStop(getIdentity());
                mAppOpsHelper.finishOp(OP_MONITOR_LOCATION, getIdentity());
            }

            onProviderListenerInactive();
@@ -500,11 +502,9 @@ public class LocationProviderManager extends

                if (!getRequest().isHiddenFromAppOps()) {
                    if (mIsUsingHighPower) {
                        mLocationAttributionHelper.reportHighPowerLocationStart(
                                getIdentity());
                        mAppOpsHelper.startOpNoThrow(OP_MONITOR_HIGH_POWER_LOCATION, getIdentity());
                    } else {
                        mLocationAttributionHelper.reportHighPowerLocationStop(
                                getIdentity());
                        mAppOpsHelper.finishOp(OP_MONITOR_HIGH_POWER_LOCATION, getIdentity());
                    }
                }
            }
@@ -1015,7 +1015,7 @@ public class LocationProviderManager extends
    protected final class LocationListenerRegistration extends LocationRegistration implements
            IBinder.DeathRecipient {

        protected LocationListenerRegistration(LocationRequest request, CallerIdentity identity,
        LocationListenerRegistration(LocationRequest request, CallerIdentity identity,
                LocationListenerTransport transport, @PermissionLevel int permissionLevel) {
            super(request, identity, transport, permissionLevel);
        }
@@ -1080,7 +1080,7 @@ public class LocationProviderManager extends
    protected final class LocationPendingIntentRegistration extends LocationRegistration implements
            PendingIntent.CancelListener {

        protected LocationPendingIntentRegistration(LocationRequest request,
        LocationPendingIntentRegistration(LocationRequest request,
                CallerIdentity identity, LocationPendingIntentTransport transport,
                @PermissionLevel int permissionLevel) {
            super(request, identity, transport, permissionLevel);
@@ -1089,13 +1089,15 @@ public class LocationProviderManager extends
        @GuardedBy("mLock")
        @Override
        protected void onLocationListenerRegister() {
            ((PendingIntent) getKey()).registerCancelListener(this);
            if (!((PendingIntent) getKey()).addCancelListener(DIRECT_EXECUTOR, this)) {
                remove();
            }
        }

        @GuardedBy("mLock")
        @Override
        protected void onLocationListenerUnregister() {
            ((PendingIntent) getKey()).unregisterCancelListener(this);
            ((PendingIntent) getKey()).removeCancelListener(this);
        }

        @Override
@@ -1138,7 +1140,7 @@ public class LocationProviderManager extends

        private long mExpirationRealtimeMs = Long.MAX_VALUE;

        protected GetCurrentLocationListenerRegistration(LocationRequest request,
        GetCurrentLocationListenerRegistration(LocationRequest request,
                CallerIdentity identity, LocationTransport transport, int permissionLevel) {
            super(request, identity, transport, permissionLevel);
        }
@@ -1347,7 +1349,6 @@ public class LocationProviderManager extends
    protected final AppForegroundHelper mAppForegroundHelper;
    protected final LocationPowerSaveModeHelper mLocationPowerSaveModeHelper;
    protected final ScreenInteractiveHelper mScreenInteractiveHelper;
    protected final LocationAttributionHelper mLocationAttributionHelper;
    protected final LocationUsageLogger mLocationUsageLogger;
    protected final LocationFudger mLocationFudger;

@@ -1415,7 +1416,6 @@ public class LocationProviderManager extends
        mAppForegroundHelper = injector.getAppForegroundHelper();
        mLocationPowerSaveModeHelper = injector.getLocationPowerSaveModeHelper();
        mScreenInteractiveHelper = injector.getScreenInteractiveHelper();
        mLocationAttributionHelper = injector.getLocationAttributionHelper();
        mLocationUsageLogger = injector.getLocationUsageLogger();
        mLocationFudger = new LocationFudger(mSettingsHelper.getCoarseLocationAccuracyM());

Loading