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

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

Merge "Introduce a hidden API in SupervisionManager to set the supervision...

Merge "Introduce a hidden API in SupervisionManager to set the supervision enabled state for the current user or a specific user." into main
parents d79f47b1 f1d8fbad
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,5 +22,6 @@ package android.app.supervision;
 */
interface ISupervisionManager {
    boolean isSupervisionEnabledForUser(int userId);
    void setSupervisionEnabledForUser(int userId, boolean enabled);
    String getActiveSupervisionAppPackage(int userId);
}
+29 −0
Original line number Diff line number Diff line
@@ -100,6 +100,35 @@ public class SupervisionManager {
        }
    }

    /**
     * Sets whether the device is supervised for the current user.
     *
     * @hide
     */
    @UserHandleAware
    public void setSupervisionEnabled(boolean enabled) {
        setSupervisionEnabledForUser(mContext.getUserId(), enabled);
    }

    /**
     * Sets whether the device is supervised for a given user.
     *
     * <p>The caller must be from the same user as the target or hold the {@link
     * android.Manifest.permission#INTERACT_ACROSS_USERS} permission.
     *
     * @hide
     */
    @RequiresPermission(
            value = android.Manifest.permission.INTERACT_ACROSS_USERS,
            conditional = true)
    public void setSupervisionEnabledForUser(@UserIdInt int userId, boolean enabled) {
        try {
            mService.setSupervisionEnabledForUser(userId, enabled);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the package name of the app that is acting as the active supervision app or null if
     * supervision is disabled.
+13 −6
Original line number Diff line number Diff line
@@ -94,6 +94,14 @@ public class SupervisionService extends ISupervisionManager.Stub {
        }
    }

    @Override
    public void setSupervisionEnabledForUser(@UserIdInt int userId, boolean enabled) {
        if (UserHandle.getUserId(Binder.getCallingUid()) != userId) {
            enforcePermission(INTERACT_ACROSS_USERS);
        }
        setSupervisionEnabledForUserInternal(userId, enabled, getSystemSupervisionPackage());
    }

    /**
     * Returns the package name of the active supervision app or null if supervision is disabled.
     */
@@ -160,7 +168,7 @@ public class SupervisionService extends ISupervisionManager.Stub {
     * Sets supervision as enabled or disabled for the given user and, in case supervision is being
     * enabled, the package of the active supervision app.
     */
    private void setSupervisionEnabledForUser(
    private void setSupervisionEnabledForUserInternal(
            @UserIdInt int userId, boolean enabled, @Nullable String supervisionAppPackage) {
        synchronized (getLockObject()) {
            SupervisionUserData data = getUserDataLocked(userId);
@@ -176,16 +184,16 @@ public class SupervisionService extends ISupervisionManager.Stub {
                dpmInternal != null ? dpmInternal.getProfileOwnerAsUser(userId) : null;

        if (po != null && po.getPackageName().equals(getSystemSupervisionPackage())) {
            setSupervisionEnabledForUser(userId, true, po.getPackageName());
            setSupervisionEnabledForUserInternal(userId, true, po.getPackageName());
        } else if (po != null && po.equals(getSupervisionProfileOwnerComponent())) {
            // TODO(b/392071637): Consider not enabling supervision in case profile owner is given
            // to the legacy supervision profile owner component.
            setSupervisionEnabledForUser(userId, true, po.getPackageName());
            setSupervisionEnabledForUserInternal(userId, true, po.getPackageName());
        } else {
            // TODO(b/381428475): Avoid disabling supervision when the app is not the profile owner.
            // This might only be possible after introducing specific and public APIs to enable
            // and disable supervision.
            setSupervisionEnabledForUser(userId, false, /* supervisionAppPackage= */ null);
            setSupervisionEnabledForUserInternal(userId, false, /* supervisionAppPackage= */ null);
        }
    }

@@ -327,8 +335,7 @@ public class SupervisionService extends ISupervisionManager.Stub {

        @Override
        public void setSupervisionEnabledForUser(@UserIdInt int userId, boolean enabled) {
            SupervisionService.this.setSupervisionEnabledForUser(
                    userId, enabled, getSystemSupervisionPackage());
            SupervisionService.this.setSupervisionEnabledForUser(userId, enabled);
        }

        @Override
+4 −4
Original line number Diff line number Diff line
@@ -175,7 +175,7 @@ class SupervisionServiceTest {
    fun isActiveSupervisionApp_supervisionUid_supervisionEnabled_returnsTrue() {
        whenever(mockPackageManager.getPackagesForUid(APP_UID))
            .thenReturn(arrayOf(systemSupervisionPackage))
        service.mInternal.setSupervisionEnabledForUser(USER_ID, true)
        service.setSupervisionEnabledForUser(USER_ID, true)

        assertThat(service.mInternal.isActiveSupervisionApp(APP_UID)).isTrue()
    }
@@ -184,7 +184,7 @@ class SupervisionServiceTest {
    fun isActiveSupervisionApp_supervisionUid_supervisionNotEnabled_returnsFalse() {
        whenever(mockPackageManager.getPackagesForUid(APP_UID))
            .thenReturn(arrayOf(systemSupervisionPackage))
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        service.setSupervisionEnabledForUser(USER_ID, false)

        assertThat(service.mInternal.isActiveSupervisionApp(APP_UID)).isFalse()
    }
@@ -200,10 +200,10 @@ class SupervisionServiceTest {
    fun setSupervisionEnabledForUser() {
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()

        service.mInternal.setSupervisionEnabledForUser(USER_ID, true)
        service.setSupervisionEnabledForUser(USER_ID, true)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()

        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        service.setSupervisionEnabledForUser(USER_ID, false)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
    }