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

Commit daea8fc2 authored by Vitor Carvalho's avatar Vitor Carvalho Committed by Android (Google) Code Review
Browse files

Merge "Stop disabling supervision when the supervision app is not the PO." into main

parents daca9110 f17eac30
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -177,23 +177,24 @@ public class SupervisionService extends ISupervisionManager.Stub {
        }
    }

    /** Ensures that supervision is enabled when the supervision app is the profile owner. */
    /**
     * Ensures that supervision is enabled when the supervision app is the profile owner.
     *
     * <p>The state syncing with the DevicePolicyManager can only enable supervision and never
     * disable. Supervision can only be disabled explicitly via calls to the
     * {@link #setSupervisionEnabledForUser} method.
     */
    private void syncStateWithDevicePolicyManager(@UserIdInt int userId) {
        final DevicePolicyManagerInternal dpmInternal = mInjector.getDpmInternal();
        final ComponentName po =
                dpmInternal != null ? dpmInternal.getProfileOwnerAsUser(userId) : null;

        if (po != null && po.getPackageName().equals(getSystemSupervisionPackage())) {
            setSupervisionEnabledForUserInternal(userId, true, po.getPackageName());
            setSupervisionEnabledForUserInternal(userId, true, getSystemSupervisionPackage());
        } 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.
            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.
            setSupervisionEnabledForUserInternal(userId, false, /* supervisionAppPackage= */ null);
        }
    }

@@ -279,7 +280,7 @@ public class SupervisionService extends ISupervisionManager.Stub {
        }

        @VisibleForTesting
        @SuppressLint("MissingPermission") // not needed for a service
        @SuppressLint("MissingPermission")  // not needed for a system service
        void registerProfileOwnerListener() {
            IntentFilter poIntentFilter = new IntentFilter();
            poIntentFilter.addAction(DevicePolicyManager.ACTION_PROFILE_OWNER_CHANGED);
+13 −1
Original line number Diff line number Diff line
@@ -159,6 +159,18 @@ class SupervisionServiceTest {
            .isEqualTo(supervisionProfileOwnerComponent.packageName)
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun profileOwnerChanged_supervisionAppIsNotProfileOwner_doesNotDisableSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, true)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(ComponentName("other.package", "MainActivity"))

        broadcastProfileOwnerChanged(USER_ID)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun profileOwnerChanged_supervisionAppIsNotProfileOwner_doesNotEnableSupervision() {
@@ -258,7 +270,7 @@ class SupervisionServiceTest {

    private companion object {
        const val USER_ID = 100
        val APP_UID = USER_ID * UserHandle.PER_USER_RANGE
        const val APP_UID = USER_ID * UserHandle.PER_USER_RANGE
    }
}