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

Commit f8d0705b authored by Kholoud Mohamed's avatar Kholoud Mohamed
Browse files

Fixes for ProvisioningTest

Bug: 289515470
Test: btest android.devicepolicy.cts.ProvisioningTest
Change-Id: If156e8d2c1bd812ab3c0df5531d45e87a729dd4c
parent e32f8861
Loading
Loading
Loading
Loading
+74 −36
Original line number Diff line number Diff line
@@ -9524,7 +9524,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();
@@ -9540,7 +9541,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        return true;
    }
    private int getHeadlessDeviceOwnerMode() {
    private int getHeadlessDeviceOwnerModeForDeviceOwner() {
        synchronized (getLockObject()) {
            ActiveAdmin deviceOwner = getDeviceOwnerAdminLocked();
            if (deviceOwner == null) {
@@ -9550,6 +9551,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().
     */
@@ -12312,7 +12328,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");
        }
@@ -16718,8 +16735,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;
@@ -16728,11 +16758,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;
    }
@@ -16755,14 +16785,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);
@@ -16797,16 +16832,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;
@@ -16852,7 +16885,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;
@@ -16876,17 +16909,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);
        }
@@ -21054,7 +21095,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,
@@ -21540,8 +21581,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,
@@ -21553,17 +21593,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
@@ -21578,7 +21617,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.");
@@ -24343,6 +24381,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());
    }
}