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

Commit 7be520b4 authored by Rajeev Kumar's avatar Rajeev Kumar Committed by Android (Google) Code Review
Browse files

Merge "Allow device owner type to be set more than once"

parents 6c966b35 4b58bce4
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -18330,12 +18330,16 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    private void setDeviceOwnerTypeLocked(ComponentName admin,
            @DeviceOwnerType int deviceOwnerType) {
        String packageName = admin.getPackageName();
        boolean isAdminTestOnly;
        verifyDeviceOwnerTypePreconditionsLocked(admin);
        Preconditions.checkState(!mOwners.isDeviceOwnerTypeSetForDeviceOwner(packageName),
                "The device owner type has already been set for " + packageName);
        mOwners.setDeviceOwnerType(packageName, deviceOwnerType);
        isAdminTestOnly = isAdminTestOnlyLocked(admin, mOwners.getDeviceOwnerUserId());
        Preconditions.checkState(isAdminTestOnly
                        || !mOwners.isDeviceOwnerTypeSetForDeviceOwner(packageName),
                "Test only admins can only set the device owner type more than once");
        mOwners.setDeviceOwnerType(packageName, deviceOwnerType, isAdminTestOnly);
    }
    @Override
+5 −3
Original line number Diff line number Diff line
@@ -637,13 +637,15 @@ class Owners {
        }
    }

    void setDeviceOwnerType(String packageName, @DeviceOwnerType int deviceOwnerType) {
    void setDeviceOwnerType(String packageName, @DeviceOwnerType int deviceOwnerType,
            boolean isAdminTestOnly) {
        synchronized (mLock) {
            if (!hasDeviceOwner()) {
                Slog.e(TAG, "Attempting to set a device owner type when there is no device owner");
                return;
            } else if (isDeviceOwnerTypeSetForDeviceOwner(packageName)) {
                Slog.e(TAG, "Device owner type for " + packageName + " has already been set");
            } else if (!isAdminTestOnly && isDeviceOwnerTypeSetForDeviceOwner(packageName)) {
                Slog.e(TAG, "Setting the device owner type more than once is only allowed"
                        + " for test only admins");
                return;
            }

+5 −15
Original line number Diff line number Diff line
@@ -7737,30 +7737,20 @@ public class DevicePolicyManagerTest extends DpmTestBase {

        dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED);

        int returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1);
        assertThat(dpms.mOwners.hasDeviceOwner()).isTrue();
        assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED);

        assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED);
        initializeDpms();

        returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1);
        assertThat(dpms.mOwners.hasDeviceOwner()).isTrue();
        assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED);
        assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED);
    }

    @Test
    public void testSetDeviceOwnerType_asDeviceOwner_throwsExceptionWhenSetDeviceOwnerTypeAgain()
    public void testSetDeviceOwnerType_asDeviceOwner_setDeviceOwnerTypeTwice_success()
            throws Exception {
        setDeviceOwner();
        dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT);

        dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED);

        int returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1);
        assertThat(dpms.mOwners.hasDeviceOwner()).isTrue();
        assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED);

        assertThrows(IllegalStateException.class,
                () -> dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT));
        assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED);
    }

    @Test
+5 −4
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ public class OwnersTest extends DpmTestBase {
            assertThat(owners.getProfileOwnerUserRestrictionsNeedsMigration(21)).isFalse();

            owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(),
                    DEVICE_OWNER_TYPE_FINANCED);
                    DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false);
            // There is no device owner, so the default owner type should be returned.
            assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo(
                    DEVICE_OWNER_TYPE_DEFAULT);
@@ -367,7 +367,7 @@ public class OwnersTest extends DpmTestBase {
            owners.setDeviceOwnerUserRestrictionsMigrated();

            owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(),
                    DEVICE_OWNER_TYPE_FINANCED);
                    DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false);
            assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo(
                    DEVICE_OWNER_TYPE_FINANCED);

@@ -399,7 +399,7 @@ public class OwnersTest extends DpmTestBase {
            owners.setProfileOwnerUserRestrictionsMigrated(11);

            owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(),
                    DEVICE_OWNER_TYPE_DEFAULT);
                    DEVICE_OWNER_TYPE_DEFAULT, /* isAdminTestOnly= */ false);
            // The previous device owner type should persist.
            assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo(
                    DEVICE_OWNER_TYPE_FINANCED);
@@ -585,7 +585,8 @@ public class OwnersTest extends DpmTestBase {
        assertThat(owners.getProfileOwnerFile(11).exists()).isTrue();

        String previousDeviceOwnerPackageName = owners.getDeviceOwnerPackageName();
        owners.setDeviceOwnerType(previousDeviceOwnerPackageName, DEVICE_OWNER_TYPE_FINANCED);
        owners.setDeviceOwnerType(previousDeviceOwnerPackageName, DEVICE_OWNER_TYPE_FINANCED,
                /* isAdminTestOnly= */ false);
        assertThat(owners.getDeviceOwnerType(previousDeviceOwnerPackageName)).isEqualTo(
                DEVICE_OWNER_TYPE_FINANCED);
        owners.setDeviceOwnerProtectedPackages(