Merge "New API: DevicePolicyManager.listForegroundAffiliatedUsers()" into sc-dev

This commit is contained in:
Felipe Leme
2021-02-17 21:12:16 +00:00
committed by Android (Google) Code Review
4 changed files with 51 additions and 5 deletions

View File

@@ -7104,6 +7104,7 @@ package android.app.admin {
method public boolean isUniqueDeviceAttestationSupported(); method public boolean isUniqueDeviceAttestationSupported();
method public boolean isUsbDataSignalingEnabled(); method public boolean isUsbDataSignalingEnabled();
method public boolean isUsingUnifiedPassword(@NonNull android.content.ComponentName); method public boolean isUsingUnifiedPassword(@NonNull android.content.ComponentName);
method @NonNull public java.util.List<android.os.UserHandle> listForegroundAffiliatedUsers();
method public void lockNow(); method public void lockNow();
method public void lockNow(int); method public void lockNow(int);
method public int logoutUser(@NonNull android.content.ComponentName); method public int logoutUser(@NonNull android.content.ComponentName);

View File

@@ -13357,6 +13357,7 @@ public class DevicePolicyManager {
} }
} }
} }
/** /**
* Returns true if the caller is running on a device where the admin can grant * Returns true if the caller is running on a device where the admin can grant
* permissions related to device sensors. * permissions related to device sensors.
@@ -13459,4 +13460,22 @@ public class DevicePolicyManager {
} }
return false; return false;
} }
/**
* Gets the list of {@link #isAffiliatedUser() affiliated} users running on foreground.
*
* @return list of {@link #isAffiliatedUser() affiliated} users running on foreground.
*
* @throws SecurityException if the calling application is not a device owner
*/
@NonNull
public List<UserHandle> listForegroundAffiliatedUsers() {
if (mService == null) return Collections.emptyList();
try {
return mService.listForegroundAffiliatedUsers();
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
}
} }

View File

@@ -507,4 +507,6 @@ interface IDevicePolicyManager {
boolean isUsbDataSignalingEnabled(String callerPackage); boolean isUsbDataSignalingEnabled(String callerPackage);
boolean isUsbDataSignalingEnabledForUser(int userId); boolean isUsbDataSignalingEnabledForUser(int userId);
boolean canUsbDataSignalingBeDisabled(); boolean canUsbDataSignalingBeDisabled();
List<UserHandle> listForegroundAffiliatedUsers();
} }

View File

@@ -6453,7 +6453,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
private void forceWipeUser(int userId, String wipeReasonForUser, boolean wipeSilently) { private void forceWipeUser(int userId, String wipeReasonForUser, boolean wipeSilently) {
boolean success = false; boolean success = false;
try { try {
if (getCurrentForegroundUser() == userId) { if (getCurrentForegroundUserId() == userId) {
mInjector.getIActivityManager().switchUser(UserHandle.USER_SYSTEM); mInjector.getIActivityManager().switchUser(UserHandle.USER_SYSTEM);
} }
@@ -7921,7 +7921,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
Slog.i(LOG_TAG, "Device owner set: " + admin + " on user " + userId); Slog.i(LOG_TAG, "Device owner set: " + admin + " on user " + userId);
if (mInjector.userManagerIsHeadlessSystemUserMode()) { if (mInjector.userManagerIsHeadlessSystemUserMode()) {
int currentForegroundUser = getCurrentForegroundUser(); int currentForegroundUser = getCurrentForegroundUserId();
Slog.i(LOG_TAG, "setDeviceOwner(): setting " + admin Slog.i(LOG_TAG, "setDeviceOwner(): setting " + admin
+ " as profile owner on user " + currentForegroundUser); + " as profile owner on user " + currentForegroundUser);
// Sets profile owner on current foreground user since // Sets profile owner on current foreground user since
@@ -9052,7 +9052,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
return UserHandle.isSameApp(caller.getUid(), Process.SHELL_UID); return UserHandle.isSameApp(caller.getUid(), Process.SHELL_UID);
} }
private @UserIdInt int getCurrentForegroundUser() { private @UserIdInt int getCurrentForegroundUserId() {
try { try {
return mInjector.getIActivityManager().getCurrentUser().id; return mInjector.getIActivityManager().getCurrentUser().id;
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -9061,6 +9061,25 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
return UserHandle.USER_NULL; return UserHandle.USER_NULL;
} }
@Override
public List<UserHandle> listForegroundAffiliatedUsers() {
checkIsDeviceOwner(getCallerIdentity());
int userId = mInjector.binderWithCleanCallingIdentity(() -> getCurrentForegroundUserId());
boolean isAffiliated;
synchronized (getLockObject()) {
isAffiliated = isUserAffiliatedWithDeviceLocked(userId);
}
if (!isAffiliated) return Collections.emptyList();
List<UserHandle> users = new ArrayList<>(1);
users.add(UserHandle.of(userId));
return users;
}
protected int getProfileParentId(int userHandle) { protected int getProfileParentId(int userHandle) {
return mInjector.binderWithCleanCallingIdentity(() -> { return mInjector.binderWithCleanCallingIdentity(() -> {
UserInfo parentUser = mUserManager.getProfileParent(userHandle); UserInfo parentUser = mUserManager.getProfileParent(userHandle);
@@ -12875,7 +12894,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
return CODE_NONSYSTEM_USER_EXISTS; return CODE_NONSYSTEM_USER_EXISTS;
} }
int currentForegroundUser = getCurrentForegroundUser(); int currentForegroundUser = getCurrentForegroundUserId();
if (callingUserId != currentForegroundUser if (callingUserId != currentForegroundUser
&& mInjector.userManagerIsHeadlessSystemUserMode() && mInjector.userManagerIsHeadlessSystemUserMode()
&& currentForegroundUser == UserHandle.USER_SYSTEM) { && currentForegroundUser == UserHandle.USER_SYSTEM) {
@@ -12971,6 +12990,11 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
return CODE_OK; return CODE_OK;
} }
private void checkIsDeviceOwner(CallerIdentity caller) {
Preconditions.checkCallAuthorization(isDeviceOwner(caller), caller.getUid()
+ " is not device owner");
}
private ComponentName getOwnerComponent(String packageName, int userId) { private ComponentName getOwnerComponent(String packageName, int userId) {
if (isDeviceOwnerPackage(packageName, userId)) { if (isDeviceOwnerPackage(packageName, userId)) {
return mOwners.getDeviceOwnerComponent(); return mOwners.getDeviceOwnerComponent();
@@ -15467,7 +15491,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
private boolean isLockTaskFeatureEnabled(int lockTaskFeature) throws RemoteException { private boolean isLockTaskFeatureEnabled(int lockTaskFeature) throws RemoteException {
//TODO(b/175285301): Explicitly get the user's identity to check. //TODO(b/175285301): Explicitly get the user's identity to check.
int lockTaskFeatures = int lockTaskFeatures =
getUserData(getCurrentForegroundUser()).mLockTaskFeatures; getUserData(getCurrentForegroundUserId()).mLockTaskFeatures;
return (lockTaskFeatures & lockTaskFeature) == lockTaskFeature; return (lockTaskFeatures & lockTaskFeature) == lockTaskFeature;
} }