Skip to content

Commit

Permalink
Allow uninstalling DMRH when not used for management
Browse files Browse the repository at this point in the history
Bug: 360807442
Test: btest a.d.c.DevicePolicyManagementRoleHolderTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:da522df80568c158fe30896f9a571d05556ee51a)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:28a7456fab08d11e1e95a50abc23a0ade6edede2)
Merged-In: I023f78cef11fb7e8e9a92e2896cf94c9fcd1113b
Change-Id: I023f78cef11fb7e8e9a92e2896cf94c9fcd1113b
  • Loading branch information
Pavel Grafov authored and thestinger committed Dec 2, 2024
1 parent 45fd840 commit 61879fc
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import android.app.ApplicationPackageManager;
import android.app.BroadcastOptions;
import android.app.IActivityManager;
import android.app.admin.DevicePolicyManagerInternal;
import android.app.admin.IDevicePolicyManager;
import android.app.admin.SecurityLog;
import android.app.backup.IBackupManager;
Expand Down Expand Up @@ -3366,8 +3367,10 @@ public boolean isPackageDeviceAdminOnAnyUser(@NonNull Computer snapshot, String
// TODO(b/261957226): centralise this logic in DPM
boolean isPackageDeviceAdmin(String packageName, int userId) {
final IDevicePolicyManager dpm = getDevicePolicyManager();
final DevicePolicyManagerInternal dpmi =
mInjector.getLocalService(DevicePolicyManagerInternal.class);
try {
if (dpm != null) {
if (dpm != null && dpmi != null) {
final ComponentName deviceOwnerComponentName = dpm.getDeviceOwnerComponent(
/* callingUserOnly =*/ false);
final String deviceOwnerPackageName = deviceOwnerComponentName == null ? null
Expand All @@ -3380,17 +3383,31 @@ boolean isPackageDeviceAdmin(String packageName, int userId) {
return true;
}
// Does it contain a device admin for any user?
int[] users;
int[] allUsers = mUserManager.getUserIds();
int[] targetUsers;
if (userId == UserHandle.USER_ALL) {
users = mUserManager.getUserIds();
targetUsers = allUsers;
} else {
users = new int[]{userId};
targetUsers = new int[]{userId};
}
for (int i = 0; i < users.length; ++i) {
if (dpm.packageHasActiveAdmins(packageName, users[i])) {

for (int i = 0; i < targetUsers.length; ++i) {
if (dpm.packageHasActiveAdmins(packageName, targetUsers[i])) {
return true;
}
if (isDeviceManagementRoleHolder(packageName, users[i])) {
}

// If a package is DMRH on a managed user, it should also be treated as an admin on
// that user. If that package is also a system package, it should also be protected
// on other users otherwise "uninstall updates" on an unmanaged user may break
// management on other users because apk version is shared between all users.
var packageState = snapshotComputer().getPackageStateInternal(packageName);
if (packageState == null) {
return false;
}
for (int user : packageState.isSystem() ? allUsers : targetUsers) {
if (isDeviceManagementRoleHolder(packageName, user)
&& dpmi.isUserOrganizationManaged(user)) {
return true;
}
}
Expand Down

0 comments on commit 61879fc

Please sign in to comment.