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

Commit 74882cf5 authored by Soonil Nagarkar's avatar Soonil Nagarkar Committed by Android (Google) Code Review
Browse files

Merge "Fix logging bugs" into sc-dev

parents 74059170 1dc7b05c
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -54,6 +54,20 @@ public final class CallerIdentity {
        return new CallerIdentity(uid, pid, packageName, attributionTag, listenerId);
    }

    /**
     * Returns a CallerIdentity with PID and listener ID information stripped. This is mostly
     * useful for aggregating information for debug purposes, and should not be used in any API with
     * security requirements.
     */
    public static CallerIdentity forAggregation(CallerIdentity callerIdentity) {
        if (callerIdentity.getPid() == 0 && callerIdentity.getListenerId() == null) {
            return callerIdentity;
        }

        return new CallerIdentity(callerIdentity.getUid(), 0, callerIdentity.getPackageName(),
                callerIdentity.getAttributionTag(), null);
    }

    /**
     * Creates a CallerIdentity for the current process and context.
     */
@@ -180,17 +194,6 @@ public final class CallerIdentity {
        }
    }

    /**
     * Returns a CallerIdentity corrosponding to this CallerIdentity but with a null listener id.
     */
    public CallerIdentity stripListenerId() {
        if (mListenerId == null) {
            return this;
        } else {
            return new CallerIdentity(mUid, mPid, mPackageName, mAttributionTag, null);
        }
    }

    @Override
    public String toString() {
        int length = 10 + mPackageName.length();
+19 −21
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static android.location.provider.LocationProviderBase.ACTION_NETWORK_PROV

import static com.android.server.location.LocationPermissions.PERMISSION_COARSE;
import static com.android.server.location.LocationPermissions.PERMISSION_FINE;
import static com.android.server.location.eventlog.LocationEventLog.EVENT_LOG;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

@@ -158,10 +159,9 @@ public class LocationManagerService extends ILocationManager.Stub {

        public Lifecycle(Context context) {
            super(context);
            LocationEventLog eventLog = new LocationEventLog();
            mUserInfoHelper = new LifecycleUserInfoHelper(context);
            mSystemInjector = new SystemInjector(context, mUserInfoHelper, eventLog);
            mService = new LocationManagerService(context, mSystemInjector, eventLog);
            mSystemInjector = new SystemInjector(context, mUserInfoHelper);
            mService = new LocationManagerService(context, mSystemInjector);
        }

        @Override
@@ -233,7 +233,6 @@ public class LocationManagerService extends ILocationManager.Stub {

    private final Context mContext;
    private final Injector mInjector;
    private final LocationEventLog mEventLog;
    private final LocalService mLocalService;

    private final GeofenceManager mGeofenceManager;
@@ -261,18 +260,20 @@ public class LocationManagerService extends ILocationManager.Stub {
    @GuardedBy("mLock")
    private @Nullable OnProviderLocationTagsChangeListener mOnProviderLocationTagsChangeListener;

    LocationManagerService(Context context, Injector injector, LocationEventLog eventLog) {
    LocationManagerService(Context context, Injector injector) {
        mContext = context.createAttributionContext(ATTRIBUTION_TAG);
        mInjector = injector;
        mEventLog = eventLog;
        mLocalService = new LocalService();
        LocalServices.addService(LocationManagerInternal.class, mLocalService);

        mGeofenceManager = new GeofenceManager(mContext, injector);

        mInjector.getSettingsHelper().addOnLocationEnabledChangedListener(
                this::onLocationModeChanged);

        // set up passive provider first since it will be required for all other location providers,
        // which are loaded later once the system is ready.
        mPassiveManager = new PassiveLocationProviderManager(mContext, injector, mEventLog);
        mPassiveManager = new PassiveLocationProviderManager(mContext, injector);
        addLocationProviderManager(mPassiveManager, new PassiveLocationProvider(mContext));

        // TODO: load the gps provider here as well, which will require refactoring
@@ -313,7 +314,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            }

            LocationProviderManager manager = new LocationProviderManager(mContext, mInjector,
                    mEventLog, providerName, mPassiveManager);
                    providerName, mPassiveManager);
            addLocationProviderManager(manager, null);
            return manager;
        }
@@ -335,7 +336,7 @@ public class LocationManagerService extends ILocationManager.Stub {
                            Settings.Global.LOCATION_ENABLE_STATIONARY_THROTTLE, 1) != 0;
                    if (enableStationaryThrottling) {
                        realProvider = new StationaryThrottlingLocationProvider(manager.getName(),
                                mInjector, realProvider, mEventLog);
                                mInjector, realProvider);
                    }
                }
                manager.setRealProvider(realProvider);
@@ -355,9 +356,6 @@ public class LocationManagerService extends ILocationManager.Stub {
    }

    void onSystemReady() {
        mInjector.getSettingsHelper().addOnLocationEnabledChangedListener(
                this::onLocationModeChanged);

        if (Build.IS_DEBUGGABLE) {
            // on debug builds, watch for location noteOps while location is off. there are some
            // scenarios (emergency location) where this is expected, but generally this should
@@ -385,7 +383,7 @@ public class LocationManagerService extends ILocationManager.Stub {
                com.android.internal.R.string.config_networkLocationProviderPackageName);
        if (networkProvider != null) {
            LocationProviderManager networkManager = new LocationProviderManager(mContext,
                    mInjector, mEventLog, NETWORK_PROVIDER, mPassiveManager);
                    mInjector, NETWORK_PROVIDER, mPassiveManager);
            addLocationProviderManager(networkManager, networkProvider);
        } else {
            Log.w(TAG, "no network location provider found");
@@ -404,7 +402,7 @@ public class LocationManagerService extends ILocationManager.Stub {
                com.android.internal.R.string.config_fusedLocationProviderPackageName);
        if (fusedProvider != null) {
            LocationProviderManager fusedManager = new LocationProviderManager(mContext, mInjector,
                    mEventLog, FUSED_PROVIDER, mPassiveManager);
                    FUSED_PROVIDER, mPassiveManager);
            addLocationProviderManager(fusedManager, fusedProvider);
        } else {
            Log.wtf(TAG, "no fused location provider found");
@@ -419,7 +417,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            mGnssManagerService.onSystemReady();

            LocationProviderManager gnssManager = new LocationProviderManager(mContext, mInjector,
                    mEventLog, GPS_PROVIDER, mPassiveManager);
                    GPS_PROVIDER, mPassiveManager);
            addLocationProviderManager(gnssManager, mGnssManagerService.getGnssLocationProvider());
        }

@@ -476,7 +474,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            Log.d(TAG, "[u" + userId + "] location enabled = " + enabled);
        }

        mEventLog.logLocationEnabled(userId, enabled);
        EVENT_LOG.logLocationEnabled(userId, enabled);

        Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION)
                .putExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled)
@@ -1268,7 +1266,7 @@ public class LocationManagerService extends ILocationManager.Stub {

                ipw.println("Event Log:");
                ipw.increaseIndent();
                mEventLog.iterate(manager.getName(), ipw::println);
                EVENT_LOG.iterate(manager.getName(), ipw::println);
                ipw.decreaseIndent();
                return;
            }
@@ -1313,7 +1311,7 @@ public class LocationManagerService extends ILocationManager.Stub {
        ipw.println("Historical Aggregate Location Provider Data:");
        ipw.increaseIndent();
        ArrayMap<String, ArrayMap<CallerIdentity, LocationEventLog.AggregateStats>> aggregateStats =
                mEventLog.copyAggregateStats();
                EVENT_LOG.copyAggregateStats();
        for (int i = 0; i < aggregateStats.size(); i++) {
            ipw.print(aggregateStats.keyAt(i));
            ipw.println(":");
@@ -1344,7 +1342,7 @@ public class LocationManagerService extends ILocationManager.Stub {

        ipw.println("Event Log:");
        ipw.increaseIndent();
        mEventLog.iterate(ipw::println);
        EVENT_LOG.iterate(ipw::println);
        ipw.decreaseIndent();
    }

@@ -1456,7 +1454,7 @@ public class LocationManagerService extends ILocationManager.Stub {
        @GuardedBy("this")
        private boolean mSystemReady;

        SystemInjector(Context context, UserInfoHelper userInfoHelper, LocationEventLog eventLog) {
        SystemInjector(Context context, UserInfoHelper userInfoHelper) {
            mContext = context;

            mUserInfoHelper = userInfoHelper;
@@ -1466,7 +1464,7 @@ public class LocationManagerService extends ILocationManager.Stub {
                    mAppOpsHelper);
            mSettingsHelper = new SystemSettingsHelper(context);
            mAppForegroundHelper = new SystemAppForegroundHelper(context);
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context, eventLog);
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context);
            mScreenInteractiveHelper = new SystemScreenInteractiveHelper(context);
            mDeviceStationaryHelper = new SystemDeviceStationaryHelper();
            mDeviceIdleHelper = new SystemDeviceIdleHelper(context);
+43 −16
Original line number Diff line number Diff line
@@ -44,6 +44,8 @@ import com.android.internal.util.Preconditions;
/** In memory event log for location events. */
public class LocationEventLog extends LocalEventLog {

    public static final LocationEventLog EVENT_LOG = new LocationEventLog();

    private static int getLogSize() {
        if (Build.IS_DEBUGGABLE || D) {
            return 500;
@@ -52,16 +54,17 @@ public class LocationEventLog extends LocalEventLog {
        }
    }

    private static final int EVENT_LOCATION_ENABLED = 1;
    private static final int EVENT_PROVIDER_ENABLED = 2;
    private static final int EVENT_PROVIDER_MOCKED = 3;
    private static final int EVENT_PROVIDER_REGISTER_CLIENT = 4;
    private static final int EVENT_PROVIDER_UNREGISTER_CLIENT = 5;
    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_PROVIDER_STATIONARY_THROTTLED = 9;
    private static final int EVENT_LOCATION_POWER_SAVE_MODE_CHANGE = 10;
    private static final int EVENT_USER_SWITCHED = 1;
    private static final int EVENT_LOCATION_ENABLED = 2;
    private static final int EVENT_PROVIDER_ENABLED = 3;
    private static final int EVENT_PROVIDER_MOCKED = 4;
    private static final int EVENT_PROVIDER_REGISTER_CLIENT = 5;
    private static final int EVENT_PROVIDER_UNREGISTER_CLIENT = 6;
    private static final int EVENT_PROVIDER_UPDATE_REQUEST = 7;
    private static final int EVENT_PROVIDER_RECEIVE_LOCATION = 8;
    private static final int EVENT_PROVIDER_DELIVER_LOCATION = 9;
    private static final int EVENT_PROVIDER_STATIONARY_THROTTLED = 10;
    private static final int EVENT_LOCATION_POWER_SAVE_MODE_CHANGE = 11;

    @GuardedBy("mAggregateStats")
    private final ArrayMap<String, ArrayMap<CallerIdentity, AggregateStats>> mAggregateStats;
@@ -90,19 +93,24 @@ public class LocationEventLog extends LocalEventLog {
                packageMap = new ArrayMap<>(2);
                mAggregateStats.put(provider, packageMap);
            }
            CallerIdentity stripped = identity.stripListenerId();
            AggregateStats stats = packageMap.get(stripped);
            CallerIdentity aggregate = CallerIdentity.forAggregation(identity);
            AggregateStats stats = packageMap.get(aggregate);
            if (stats == null) {
                stats = new AggregateStats();
                packageMap.put(stripped, stats);
                packageMap.put(aggregate, stats);
            }
            return stats;
        }
    }

    /** Logs a user switched event. */
    public void logUserSwitched(int userIdFrom, int userIdTo) {
        addLogEvent(EVENT_USER_SWITCHED, userIdFrom, userIdTo);
    }

    /** Logs a location enabled/disabled event. */
    public void logLocationEnabled(int userId, boolean enabled) {
        addLogEvent(EVENT_LOCATION_POWER_SAVE_MODE_CHANGE, userId, enabled);
        addLogEvent(EVENT_LOCATION_ENABLED, userId, enabled);
    }

    /** Logs a location provider enabled/disabled event. */
@@ -183,8 +191,10 @@ public class LocationEventLog extends LocalEventLog {
    @Override
    protected LogEvent createLogEvent(long timeDelta, int event, Object... args) {
        switch (event) {
            case EVENT_USER_SWITCHED:
                return new UserSwitchedEvent(timeDelta, (Integer) args[0], (Integer) args[1]);
            case EVENT_LOCATION_ENABLED:
                return new LocationEnabledEvent(timeDelta, (Integer) args[1], (Boolean) args[2]);
                return new LocationEnabledEvent(timeDelta, (Integer) args[0], (Boolean) args[1]);
            case EVENT_PROVIDER_ENABLED:
                return new ProviderEnabledEvent(timeDelta, (String) args[0], (Integer) args[1],
                        (Boolean) args[2]);
@@ -397,6 +407,23 @@ public class LocationEventLog extends LocalEventLog {
        }
    }

    private static final class UserSwitchedEvent extends LogEvent {

        private final int mUserIdFrom;
        private final int mUserIdTo;

        UserSwitchedEvent(long timeDelta, int userIdFrom, int userIdTo) {
            super(timeDelta);
            mUserIdFrom = userIdFrom;
            mUserIdTo = userIdTo;
        }

        @Override
        public String getLogString() {
            return "current user switched from u" + mUserIdFrom + " to u" + mUserIdTo;
        }
    }

    private static final class LocationEnabledEvent extends LogEvent {

        private final int mUserId;
@@ -410,7 +437,7 @@ public class LocationEventLog extends LocalEventLog {

        @Override
        public String getLogString() {
            return "[u" + mUserId + "] location setting " + (mEnabled ? "enabled" : "disabled");
            return "location [u" + mUserId + "] " + (mEnabled ? "enabled" : "disabled");
        }
    }

+3 −6
Original line number Diff line number Diff line
@@ -20,12 +20,11 @@ import static android.os.PowerManager.locationPowerSaveModeToString;

import static com.android.server.location.LocationManagerService.D;
import static com.android.server.location.LocationManagerService.TAG;
import static com.android.server.location.eventlog.LocationEventLog.EVENT_LOG;

import android.os.PowerManager.LocationPowerSaveMode;
import android.util.Log;

import com.android.server.location.eventlog.LocationEventLog;

import java.util.concurrent.CopyOnWriteArrayList;

/**
@@ -43,11 +42,9 @@ public abstract class LocationPowerSaveModeHelper {
        void onLocationPowerSaveModeChanged(@LocationPowerSaveMode int locationPowerSaveMode);
    }

    private final LocationEventLog mLocationEventLog;
    private final CopyOnWriteArrayList<LocationPowerSaveModeChangedListener> mListeners;

    public LocationPowerSaveModeHelper(LocationEventLog locationEventLog) {
        mLocationEventLog = locationEventLog;
    public LocationPowerSaveModeHelper() {
        mListeners = new CopyOnWriteArrayList<>();
    }

@@ -72,7 +69,7 @@ public abstract class LocationPowerSaveModeHelper {
            Log.d(TAG, "location power save mode is now " + locationPowerSaveModeToString(
                    locationPowerSaveMode));
        }
        mLocationEventLog.logLocationPowerSaveMode(locationPowerSaveMode);
        EVENT_LOG.logLocationPowerSaveMode(locationPowerSaveMode);

        for (LocationPowerSaveModeChangedListener listener : mListeners) {
            listener.onLocationPowerSaveModeChanged(locationPowerSaveMode);
+1 −3
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.os.PowerSaveState;
import com.android.internal.util.Preconditions;
import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.location.eventlog.LocationEventLog;

import java.util.Objects;
import java.util.function.Consumer;
@@ -42,8 +41,7 @@ public class SystemLocationPowerSaveModeHelper extends LocationPowerSaveModeHelp
    @LocationPowerSaveMode
    private volatile int mLocationPowerSaveMode;

    public SystemLocationPowerSaveModeHelper(Context context, LocationEventLog locationEventLog) {
        super(locationEventLog);
    public SystemLocationPowerSaveModeHelper(Context context) {
        mContext = context;
    }

Loading