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

Commit 2f666ce7 authored by Eduard Dumitrescul's avatar Eduard Dumitrescul Committed by Evelyn Torres
Browse files

Do not allow to set device owner while accounts present

Bug: 417988098
Test: manual
Flag: EXEMPT BUGFIX
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:ebc43259914fe7892c36faaef09c1c1eedb2663f
Merged-In: I51c7253b124ab524b817b17dc0aabbdb5ab12ac7
Change-Id: I51c7253b124ab524b817b17dc0aabbdb5ab12ac7
parent ec28c04f
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -326,6 +326,7 @@ import com.android.internal.widget.LockSettingsInternal;
import com.android.internal.widget.LockscreenCredential;
import com.android.internal.widget.PasswordValidationError;
import com.android.net.module.util.ProxyUtils;
import com.android.server.accounts.AccountManagerService;
import com.android.server.LocalServices;
import com.android.server.LockGuard;
import com.android.server.PersistentDataBlockManagerInternal;
@@ -8487,6 +8488,17 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        // Cannot be called while holding the lock:
        final boolean hasIncompatibleAccountsOrNonAdb =
                hasIncompatibleAccountsOrNonAdbNoLock(caller, userId, admin);
        if (!hasIncompatibleAccountsOrNonAdb) {
            synchronized (getLockObject()) {
                if (!isAdminTestOnlyLocked(admin, userId) && hasAccountsOnAnyUser()) {
                    Slogf.w(LOG_TAG,
                            "Non test-only owner can't be installed with existing accounts.");
                    return false;
                }
            }
        }
        synchronized (getLockObject()) {
            enforceCanSetDeviceOwnerLocked(caller, admin, userId, hasIncompatibleAccountsOrNonAdb);
            Preconditions.checkArgument(isPackageInstalledForUser(admin.getPackageName(), userId),
@@ -18083,6 +18095,11 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        );
    }
    private boolean hasAccountsOnAnyUser() {
        AccountManagerService accountManagerService = AccountManagerService.getSingleton();
        return accountManagerService.getAllAccounts().length != 0;
    }
    /**
     * Truncates char sequence to maximum length, nulls are ignored.
     */