Merge "Normalize user active logic across Location APIs"
This commit is contained in:
committed by
Android (Google) Code Review
commit
1681b437d2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1469,18 +1469,9 @@ public class LocationProviderManager extends
|
||||
|
||||
public @Nullable Location getLastLocation(CallerIdentity identity,
|
||||
@PermissionLevel int permissionLevel, boolean ignoreLocationSettings) {
|
||||
if (mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
|
||||
identity.getPackageName())) {
|
||||
if (!isActive(ignoreLocationSettings, identity)) {
|
||||
return null;
|
||||
}
|
||||
if (!ignoreLocationSettings) {
|
||||
if (!isEnabled(identity.getUserId())) {
|
||||
return null;
|
||||
}
|
||||
if (!identity.isSystem() && !mUserHelper.isCurrentUserId(identity.getUserId())) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// lastly - note app ops
|
||||
if (!mAppOpsHelper.noteOpNoThrow(LocationPermissions.asAppOp(permissionLevel),
|
||||
@@ -1905,20 +1896,16 @@ public class LocationProviderManager extends
|
||||
Preconditions.checkState(Thread.holdsLock(mLock));
|
||||
}
|
||||
|
||||
CallerIdentity identity = registration.getIdentity();
|
||||
|
||||
if (!registration.isPermitted()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!registration.getRequest().isLocationSettingsIgnored()) {
|
||||
if (!isEnabled(identity.getUserId())) {
|
||||
return false;
|
||||
}
|
||||
if (!identity.isSystem() && !mUserHelper.isCurrentUserId(identity.getUserId())) {
|
||||
return false;
|
||||
}
|
||||
boolean locationSettingsIgnored = registration.getRequest().isLocationSettingsIgnored();
|
||||
if (!isActive(locationSettingsIgnored, registration.getIdentity())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!locationSettingsIgnored) {
|
||||
switch (mLocationPowerSaveModeHelper.getLocationPowerSaveMode()) {
|
||||
case LOCATION_MODE_FOREGROUND_ONLY:
|
||||
if (!registration.isForeground()) {
|
||||
@@ -1944,8 +1931,32 @@ public class LocationProviderManager extends
|
||||
}
|
||||
}
|
||||
|
||||
return !mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
|
||||
identity.getPackageName());
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isActive(boolean locationSettingsIgnored, CallerIdentity identity) {
|
||||
if (identity.isSystemServer()) {
|
||||
if (!locationSettingsIgnored) {
|
||||
if (!isEnabled(mUserHelper.getCurrentUserId())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!locationSettingsIgnored) {
|
||||
if (!isEnabled(identity.getUserId())) {
|
||||
return false;
|
||||
}
|
||||
if (!mUserHelper.isCurrentUserId(identity.getUserId())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (mSettingsHelper.isLocationPackageBlacklisted(identity.getUserId(),
|
||||
identity.getPackageName())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@GuardedBy("mLock")
|
||||
|
||||
@@ -99,6 +99,11 @@ public class FakeUserInfoHelper extends UserInfoHelper {
|
||||
return ArrayUtils.contains(getProfileIds(mCurrentUserId), userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCurrentUserId() {
|
||||
return mCurrentUserId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int[] getProfileIds(int userId) {
|
||||
IntArray profiles = mProfiles.get(userId);
|
||||
|
||||
Reference in New Issue
Block a user