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

Commit 0d108d8f authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Normalize user active logic across Location APIs

Adds additional logic for location requests coming from the system
server, such that those requests may allowed so long as location is on
for the current user, even if the system user has location disabled.
Even though the system server runs under the system user, it may need
to service requests from or on behalf of other users.

Test: presubmits + manual
Change-Id: Ic9d7762e75fc930b7d26f1d1bf20b272d562939c
parent 6b4c503a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -150,8 +150,8 @@ public final class CallerIdentity {
        return mListenerId;
    }

    /** Returns true if this represents a system identity. */
    public boolean isSystem() {
    /** Returns true if this represents a system server identity. */
    public boolean isSystemServer() {
        return mUid == Process.SYSTEM_UID;
    }

+22 −6
Original line number Diff line number Diff line
@@ -322,12 +322,28 @@ public class GeofenceManager extends

    @Override
    protected boolean isActive(GeofenceRegistration registration) {
        CallerIdentity identity = registration.getIdentity();
        return registration.isPermitted()
                && (identity.isSystem() || mUserInfoHelper.isCurrentUserId(identity.getUserId()))
                && mSettingsHelper.isLocationEnabled(identity.getUserId())
                && !mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
                identity.getPackageName());
        return registration.isPermitted() && isActive(registration.getIdentity());
    }

    private boolean isActive(CallerIdentity identity) {
        if (identity.isSystemServer()) {
            if (!mSettingsHelper.isLocationEnabled(mUserInfoHelper.getCurrentUserId())) {
                return false;
            }
        } else {
            if (!mSettingsHelper.isLocationEnabled(identity.getUserId())) {
                return false;
            }
            if (!mUserInfoHelper.isCurrentUserId(identity.getUserId())) {
                return false;
            }
            if (mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
                    identity.getPackageName())) {
                return false;
            }
        }

        return true;
    }

    @Override
+24 −5
Original line number Diff line number Diff line
@@ -266,11 +266,30 @@ public abstract class GnssListenerMultiplexer<TRequest, TListener extends IInter
        CallerIdentity identity = registration.getIdentity();
        return registration.isPermitted()
                && (registration.isForeground() || isBackgroundRestrictionExempt(identity))
                && (identity.isSystem() || mUserInfoHelper.isCurrentUserId(identity.getUserId()))
                && mLocationManagerInternal.isProviderEnabledForUser(GPS_PROVIDER,
                identity.getUserId())
                && !mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
                identity.getPackageName());
                && isActive(identity);
    }

    private boolean isActive(CallerIdentity identity) {
        if (identity.isSystemServer()) {
            if (!mLocationManagerInternal.isProviderEnabledForUser(GPS_PROVIDER,
                    mUserInfoHelper.getCurrentUserId())) {
                return false;
            }
        } else {
            if (!mLocationManagerInternal.isProviderEnabledForUser(GPS_PROVIDER,
                    identity.getUserId())) {
                return false;
            }
            if (!mUserInfoHelper.isCurrentUserId(identity.getUserId())) {
                return false;
            }
            if (mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
                    identity.getPackageName())) {
                return false;
            }
        }

        return true;
    }

    private boolean isBackgroundRestrictionExempt(CallerIdentity identity) {
+15 −9
Original line number Diff line number Diff line
@@ -88,11 +88,6 @@ public class SystemUserInfoHelper extends UserInfoHelper {
        return mUserManager;
    }

    /**
     * Returns an array of running user ids. This will include all running users, and will also
     * include any profiles of the running users. The caller must never mutate the returned
     * array.
     */
    @Override
    public int[] getRunningUserIds() {
        IActivityManager activityManager = getActivityManager();
@@ -110,10 +105,6 @@ public class SystemUserInfoHelper extends UserInfoHelper {
        }
    }

    /**
     * Returns true if the given user id is either the current user or a profile of the current
     * user.
     */
    @Override
    public boolean isCurrentUserId(@UserIdInt int userId) {
        ActivityManagerInternal activityManagerInternal = getActivityManagerInternal();
@@ -129,6 +120,21 @@ public class SystemUserInfoHelper extends UserInfoHelper {
        }
    }

    @Override
    public @UserIdInt int getCurrentUserId() {
        ActivityManagerInternal activityManagerInternal = getActivityManagerInternal();
        if (activityManagerInternal != null) {
            final long identity = Binder.clearCallingIdentity();
            try {
                return activityManagerInternal.getCurrentUserId();
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
        } else {
            return UserHandle.USER_NULL;
        }
    }

    @Override
    protected int[] getProfileIds(@UserIdInt int userId) {
        UserManager userManager = getUserManager();
+6 −0
Original line number Diff line number Diff line
@@ -132,6 +132,12 @@ public abstract class UserInfoHelper {
     */
    public abstract boolean isCurrentUserId(@UserIdInt int userId);

    /**
     * Returns the current user id. Where possible, prefer to use {@link #isCurrentUserId(int)}
     * instead, as that method has more flexibility.
     */
    public abstract @UserIdInt int getCurrentUserId();

    protected abstract int[] getProfileIds(@UserIdInt int userId);

    /**
Loading