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

Commit 939d2e13 authored by Yvonne Jiang's avatar Yvonne Jiang Committed by Android (Google) Code Review
Browse files

Merge "Introduce SupervisionManager#hasSupervisionCredentials method." into main

parents 28329855 48ce76cf
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,4 +32,5 @@ interface ISupervisionManager {
    boolean shouldAllowBypassingSupervisionRoleQualification();
    oneway void setSupervisionRecoveryInfo(in SupervisionRecoveryInfo recoveryInfo);
    SupervisionRecoveryInfo getSupervisionRecoveryInfo();
    boolean hasSupervisionCredentials();
}
+17 −0
Original line number Diff line number Diff line
@@ -246,4 +246,21 @@ public class SupervisionManager {
        }
        return null;
    }

    /**
     * Returns whether supervision credentials are set up.
     *
     * @hide
     */
    @RequiresPermission(anyOf = {MANAGE_USERS, QUERY_USERS})
    public boolean hasSupervisionCredentials() {
        if (mService != null) {
            try {
                return mService.hasSupervisionCredentials();
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
        return false;
    }
}
+19 −12
Original line number Diff line number Diff line
@@ -198,20 +198,9 @@ public class SupervisionService extends ISupervisionManager.Stub {
        if (UserHandle.getUserId(Binder.getCallingUid()) != userId) {
            enforcePermission(INTERACT_ACROSS_USERS);
        }
        if (!isSupervisionEnabledForUser(userId)) {
        if (!isSupervisionEnabledForUser(userId) || !hasSupervisionCredentials()) {
            return null;
        }
        // Verify the supervising user profile exists and has a secure credential set.
        final int supervisingUserId = mInjector.getUserManagerInternal().getSupervisingProfileId();
        final long token = Binder.clearCallingIdentity();
        try {
            if (supervisingUserId == UserHandle.USER_NULL
                    || !mInjector.getKeyguardManager().isDeviceSecure(supervisingUserId)) {
                return null;
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        final Intent intent = new Intent(ACTION_CONFIRM_SUPERVISION_CREDENTIALS);
        // explicitly set the package for security
        intent.setPackage("com.android.settings");
@@ -250,6 +239,24 @@ public class SupervisionService extends ISupervisionManager.Stub {
        return true;
    }

    @Override
    public boolean hasSupervisionCredentials() {
        enforceAnyPermission(QUERY_USERS, MANAGE_USERS);

        // Verify the supervising user profile exists and has a secure credential set.
        final int supervisingUserId = mInjector.getUserManagerInternal().getSupervisingProfileId();
        final long token = Binder.clearCallingIdentity();
        try {
            if (supervisingUserId == UserHandle.USER_NULL
                    || !mInjector.getKeyguardManager().isDeviceSecure(supervisingUserId)) {
                return false;
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        return true;
    }

    /**
     * Returns true if there are any non-default non-test users.
     *
+23 −0
Original line number Diff line number Diff line
@@ -420,6 +420,29 @@ class SupervisionServiceTest {
        assertThat(service.supervisionRecoveryInfo.state).isEqualTo(recoveryInfo.state)
    }

    @Test
    fun hasSupervisionCredentials() {
        whenever(mockUserManagerInternal.getSupervisingProfileId()).thenReturn(SUPERVISING_USER_ID)
        whenever(mockKeyguardManager.isDeviceSecure(SUPERVISING_USER_ID)).thenReturn(true)

        assertThat(service.hasSupervisionCredentials()).isTrue()
    }

    @Test
    fun hasSupervisionCredentials_noSupervisingUser_returnsFalse() {
        whenever(mockUserManagerInternal.getSupervisingProfileId()).thenReturn(UserHandle.USER_NULL)

        assertThat(service.hasSupervisionCredentials()).isFalse()
    }

    @Test
    fun hasSupervisionCredentials_supervisingUserMissingSecureLock_returnsFalse() {
        whenever(mockUserManagerInternal.getSupervisingProfileId()).thenReturn(SUPERVISING_USER_ID)
        whenever(mockKeyguardManager.isDeviceSecure(SUPERVISING_USER_ID)).thenReturn(false)

        assertThat(service.hasSupervisionCredentials()).isFalse()
    }

    private val systemSupervisionPackage: String
        get() = context.getResources().getString(R.string.config_systemSupervision)