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

Commit aac608a2 authored by Kholoud Mohamed's avatar Kholoud Mohamed Committed by Android (Google) Code Review
Browse files

Merge "Fixes for ProvisioningTest" into main

parents 7d4700f5 f8d0705b
Loading
Loading
Loading
Loading
+74 −36
Original line number Diff line number Diff line
@@ -9523,7 +9523,8 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        if (setProfileOwnerOnCurrentUserIfNecessary
                && mInjector.userManagerIsHeadlessSystemUserMode()
                && getHeadlessDeviceOwnerMode() == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED) {
                && getHeadlessDeviceOwnerModeForDeviceOwner()
                == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED) {
            int currentForegroundUser;
            synchronized (getLockObject()) {
                currentForegroundUser = getCurrentForegroundUserId();
@@ -9539,7 +9540,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        return true;
    }
    private int getHeadlessDeviceOwnerMode() {
    private int getHeadlessDeviceOwnerModeForDeviceOwner() {
        synchronized (getLockObject()) {
            ActiveAdmin deviceOwner = getDeviceOwnerAdminLocked();
            if (deviceOwner == null) {
@@ -9549,6 +9550,21 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
    }
    private int getHeadlessDeviceOwnerModeForDeviceAdmin(
            @Nullable ComponentName deviceAdmin, int userId) {
        synchronized (getLockObject()) {
            if (deviceAdmin == null) {
                return HEADLESS_DEVICE_OWNER_MODE_UNSUPPORTED;
            }
            DeviceAdminInfo adminInfo = findAdmin(
                    deviceAdmin, userId, /* throwForMissingPermission= */ false);
            if (adminInfo == null) {
                return HEADLESS_DEVICE_OWNER_MODE_UNSUPPORTED;
            }
            return adminInfo.getHeadlessDeviceOwnerMode();
        }
    }
    /**
     * This API is cached: invalidate with invalidateBinderCaches().
     */
@@ -12308,7 +12324,8 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        if (Flags.headlessDeviceOwnerSingleUserEnabled()) {
            // Block this method if the device is in headless main user mode
            Preconditions.checkCallAuthorization(
                    getHeadlessDeviceOwnerMode() != HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER,
                    getHeadlessDeviceOwnerModeForDeviceOwner()
                            != HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER,
                    "createAndManageUser was called while in headless single user mode");
        }
@@ -16746,8 +16763,21 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
    }
    private int checkProvisioningPreconditionSkipPermission(String action,
            String packageName, int userId) {
    private int checkProvisioningPreconditionSkipPermission(
            String action, String packageName, int userId) {
        return checkProvisioningPreconditionSkipPermission(
                action, packageName, /* componentName = */ null, userId);
    }
    private int checkProvisioningPreconditionSkipPermission(
            String action, ComponentName componentName, int userId) {
        return checkProvisioningPreconditionSkipPermission(
                action, componentName.getPackageName(), componentName, userId);
    }
    private int checkProvisioningPreconditionSkipPermission(
            String action, String packageName, @Nullable ComponentName componentName, int userId) {
        if (!mHasFeature) {
            logMissingFeatureAction("Cannot check provisioning for action " + action);
            return STATUS_DEVICE_ADMIN_NOT_SUPPORTED;
@@ -16756,11 +16786,11 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            return STATUS_PROVISIONING_NOT_ALLOWED_FOR_NON_DEVELOPER_USERS;
        }
        final int code = checkProvisioningPreConditionSkipPermissionNoLog(
                action, packageName, userId);
                action, packageName, componentName, userId);
        if (code != STATUS_OK) {
            Slogf.d(LOG_TAG, "checkProvisioningPreCondition(" + action + ", " + packageName
                    + ") failed: "
                    + computeProvisioningErrorString(code, mInjector.userHandleGetCallingUserId()));
                    + ") failed: " + computeProvisioningErrorString(
                            code, mInjector.userHandleGetCallingUserId()));
        }
        return code;
    }
@@ -16783,14 +16813,19 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
    }
    private int checkProvisioningPreConditionSkipPermissionNoLog(String action,
            String packageName, int userId) {
            String packageName, @Nullable ComponentName componentName, int userId) {
        if (packageName != null && componentName != null
                && !packageName.equals(componentName.getPackageName())) {
            throw new IllegalArgumentException("PackageName: " + packageName + " is not the same as"
                    + " the package provided in componentName: " + componentName);
        }
        if (action != null) {
            switch (action) {
                case DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE:
                    return checkManagedProfileProvisioningPreCondition(packageName, userId);
                case DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE:
                case DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE:
                    return checkDeviceOwnerProvisioningPreCondition(userId);
                    return checkDeviceOwnerProvisioningPreCondition(componentName, userId);
            }
        }
        throw new IllegalArgumentException("Unknown provisioning action " + action);
@@ -16825,16 +16860,14 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        int ensureSetUpUser = UserHandle.USER_SYSTEM;
        if (isHeadlessSystemUserMode) {
            if (owner != null) {
                adminInfo = findAdmin(owner,
                        deviceOwnerUserId, /* throwForMissingPermission= */ false);
                int headlessDeviceOwnerMode = getHeadlessDeviceOwnerModeForDeviceAdmin(
                        owner, deviceOwnerUserId);
                isHeadlessModeAffiliated =
                        adminInfo.getHeadlessDeviceOwnerMode()
                                == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED;
                        headlessDeviceOwnerMode == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED;
                isHeadlessModeSingleUser =
                        adminInfo.getHeadlessDeviceOwnerMode()
                                == HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER;
                        headlessDeviceOwnerMode == HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER;
                if (!isHeadlessModeAffiliated && !isHeadlessModeSingleUser) {
                    return STATUS_HEADLESS_SYSTEM_USER_MODE_NOT_SUPPORTED;
@@ -16880,7 +16913,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            }
            return STATUS_OK;
        } else {
            // DO has to be user 0
            // DO has to be user 0 if setting affiliated DO
            if ((!isHeadlessSystemUserMode || isHeadlessModeAffiliated)
                    && deviceOwnerUserId != UserHandle.USER_SYSTEM) {
                return STATUS_NOT_SYSTEM_USER;
@@ -16904,17 +16937,25 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                .count() > allowedUsers;
    }
    private int checkDeviceOwnerProvisioningPreCondition(@UserIdInt int callingUserId) {
    private int checkDeviceOwnerProvisioningPreCondition(
            @Nullable ComponentName componentName, @UserIdInt int callingUserId) {
        synchronized (getLockObject()) {
            final int deviceOwnerUserId = mInjector.userManagerIsHeadlessSystemUserMode()
                    && (!Flags.headlessDeviceOwnerProvisioningFixEnabled()
                    || getHeadlessDeviceOwnerMode() == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED)
                    ? UserHandle.USER_SYSTEM
                    : callingUserId;
            int deviceOwnerUserId = -1;
            if (Flags.headlessDeviceOwnerProvisioningFixEnabled()) {
                deviceOwnerUserId = mInjector.userManagerIsHeadlessSystemUserMode()
                        && getHeadlessDeviceOwnerModeForDeviceAdmin(componentName, callingUserId)
                        == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED
                        ? UserHandle.USER_SYSTEM : callingUserId;
            } else {
                deviceOwnerUserId = mInjector.userManagerIsHeadlessSystemUserMode()
                        && getHeadlessDeviceOwnerModeForDeviceOwner()
                        == HEADLESS_DEVICE_OWNER_MODE_AFFILIATED
                        ? UserHandle.USER_SYSTEM : callingUserId;
            }
            Slogf.i(LOG_TAG, "Calling user %d, device owner will be set on user %d",
                    callingUserId, deviceOwnerUserId);
            // hasIncompatibleAccountsOrNonAdb doesn't matter since the caller is not adb.
            return checkDeviceOwnerProvisioningPreConditionLocked(/* owner unknown */ null,
            return checkDeviceOwnerProvisioningPreConditionLocked(componentName,
                    deviceOwnerUserId, callingUserId, /* isAdb= */ false,
                    /* hasIncompatibleAccountsOrNonAdb=*/ true);
        }
@@ -21082,7 +21123,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        final long identity = Binder.clearCallingIdentity();
        try {
            final int result = checkProvisioningPreconditionSkipPermission(
                    ACTION_PROVISION_MANAGED_PROFILE, admin.getPackageName(), caller.getUserId());
                    ACTION_PROVISION_MANAGED_PROFILE, admin, caller.getUserId());
            if (result != STATUS_OK) {
                throw new ServiceSpecificException(
                        ERROR_PRE_CONDITION_FAILED,
@@ -21568,8 +21609,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        final long identity = Binder.clearCallingIdentity();
        try {
            int result = checkProvisioningPreconditionSkipPermission(
                    ACTION_PROVISION_MANAGED_DEVICE, deviceAdmin.getPackageName(),
                    caller.getUserId());
                    ACTION_PROVISION_MANAGED_DEVICE, deviceAdmin, caller.getUserId());
            if (result != STATUS_OK) {
                throw new ServiceSpecificException(
                        ERROR_PRE_CONDITION_FAILED,
@@ -21581,17 +21621,16 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            setTimeAndTimezone(provisioningParams.getTimeZone(), provisioningParams.getLocalTime());
            setLocale(provisioningParams.getLocale());
            boolean isSingleUserMode;
            if (Flags.headlessDeviceOwnerProvisioningFixEnabled()) {
                DeviceAdminInfo adminInfo = findAdmin(
                        deviceAdmin, caller.getUserId(), /* throwForMissingPermission= */ false);
                isSingleUserMode = (adminInfo != null && adminInfo.getHeadlessDeviceOwnerMode()
                        == HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER);
                int headlessDeviceOwnerMode = getHeadlessDeviceOwnerModeForDeviceAdmin(
                        deviceAdmin, caller.getUserId());
                isSingleUserMode =
                        headlessDeviceOwnerMode == HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER;
            } else {
                isSingleUserMode =
                        (getHeadlessDeviceOwnerMode() == HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER);
                        getHeadlessDeviceOwnerModeForDeviceOwner()
                                == HEADLESS_DEVICE_OWNER_MODE_SINGLE_USER;
            }
            int deviceOwnerUserId = Flags.headlessDeviceOwnerSingleUserEnabled()
                    && isSingleUserMode
@@ -21606,7 +21645,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                        "PackageManager failed to remove non required apps.");
            }
            if (!setActiveAdminAndDeviceOwner(deviceOwnerUserId, deviceAdmin)) {
                throw new ServiceSpecificException(
                        ERROR_SET_DEVICE_OWNER_FAILED, "Failed to set device owner.");
@@ -24410,6 +24448,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        enforcePermission(MANAGE_PROFILE_AND_DEVICE_OWNERS, caller.getPackageName(),
                caller.getUserId());
        return Binder.withCleanCallingIdentity(() -> getHeadlessDeviceOwnerMode());
        return Binder.withCleanCallingIdentity(() -> getHeadlessDeviceOwnerModeForDeviceOwner());
    }
}