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

Commit e5e69c41 authored by Nino Jagar's avatar Nino Jagar
Browse files

Check content protection consent

Bug: 302380608
Test: Unit tests
Change-Id: I7af7f3ee725a8c822cf62ece4063cc25c1e0aab2
parent e619e972
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -40,6 +40,9 @@ public class ContentProtectionConsentManager {

    private static final String KEY_PACKAGE_VERIFIER_USER_CONSENT = "package_verifier_user_consent";

    private static final String KEY_CONTENT_PROTECTION_USER_CONSENT =
            "content_protection_user_consent";

    @NonNull private final ContentResolver mContentResolver;

    @NonNull private final DevicePolicyManagerInternal mDevicePolicyManagerInternal;
@@ -50,6 +53,8 @@ public class ContentProtectionConsentManager {

    private volatile boolean mCachedPackageVerifierConsent;

    private volatile boolean mCachedContentProtectionConsent;

    public ContentProtectionConsentManager(
            @NonNull Handler handler,
            @NonNull ContentResolver contentResolver,
@@ -63,14 +68,18 @@ public class ContentProtectionConsentManager {
                /* notifyForDescendants= */ false,
                mContentObserver,
                UserHandle.USER_ALL);

        mCachedPackageVerifierConsent = isPackageVerifierConsentGranted();
        mCachedContentProtectionConsent = isContentProtectionConsentGranted();
    }

    /**
     * Returns true if all the consents are granted
     */
    public boolean isConsentGranted(@UserIdInt int userId) {
        return mCachedPackageVerifierConsent && !isUserOrganizationManaged(userId);
        return mCachedPackageVerifierConsent
                && mCachedContentProtectionConsent
                && !isUserOrganizationManaged(userId);
    }

    private boolean isPackageVerifierConsentGranted() {
@@ -80,6 +89,13 @@ public class ContentProtectionConsentManager {
                >= 1;
    }

    private boolean isContentProtectionConsentGranted() {
        // Not always cached internally
        return Settings.Global.getInt(
                        mContentResolver, KEY_CONTENT_PROTECTION_USER_CONSENT, /* def= */ 0)
                >= 0;
    }

    private boolean isUserOrganizationManaged(@UserIdInt int userId) {
        // Cached internally
        return mDevicePolicyManagerInternal.isUserOrganizationManaged(userId);
@@ -101,6 +117,9 @@ public class ContentProtectionConsentManager {
                case KEY_PACKAGE_VERIFIER_USER_CONSENT:
                    mCachedPackageVerifierConsent = isPackageVerifierConsentGranted();
                    return;
                case KEY_CONTENT_PROTECTION_USER_CONSENT:
                    mCachedContentProtectionConsent = isContentProtectionConsentGranted();
                    return;
                default:
                    Slog.w(TAG, "Ignoring unexpected property: " + property);
            }
+72 −12
Original line number Diff line number Diff line
@@ -55,9 +55,15 @@ public class ContentProtectionConsentManagerTest {

    private static final String KEY_PACKAGE_VERIFIER_USER_CONSENT = "package_verifier_user_consent";

    private static final String KEY_CONTENT_PROTECTION_USER_CONSENT =
            "content_protection_user_consent";

    private static final Uri URI_PACKAGE_VERIFIER_USER_CONSENT =
            Settings.Global.getUriFor(KEY_PACKAGE_VERIFIER_USER_CONSENT);

    private static final Uri URI_CONTENT_PROTECTION_USER_CONSENT =
            Settings.Global.getUriFor(KEY_CONTENT_PROTECTION_USER_CONSENT);

    private static final int VALUE_TRUE = 1;

    private static final int VALUE_FALSE = -1;
@@ -96,7 +102,18 @@ public class ContentProtectionConsentManagerTest {
    @Test
    public void isConsentGranted_packageVerifierNotGranted() {
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_FALSE);
                createContentProtectionConsentManager(VALUE_FALSE, VALUE_TRUE);

        boolean actual = manager.isConsentGranted(TEST_USER_ID);

        assertThat(actual).isFalse();
        verifyZeroInteractions(mMockDevicePolicyManagerInternal);
    }

    @Test
    public void isConsentGranted_contentProtectionNotGranted() {
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_TRUE, VALUE_FALSE);

        boolean actual = manager.isConsentGranted(TEST_USER_ID);

@@ -106,7 +123,8 @@ public class ContentProtectionConsentManagerTest {

    @Test
    public void isConsentGranted_packageVerifierGranted_userNotManaged() {
        ContentProtectionConsentManager manager = createContentProtectionConsentManager(VALUE_TRUE);
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_TRUE, VALUE_TRUE);

        boolean actual = manager.isConsentGranted(TEST_USER_ID);

@@ -118,7 +136,8 @@ public class ContentProtectionConsentManagerTest {
    public void isConsentGranted_packageVerifierGranted_userManaged() {
        when(mMockDevicePolicyManagerInternal.isUserOrganizationManaged(TEST_USER_ID))
                .thenReturn(true);
        ContentProtectionConsentManager manager = createContentProtectionConsentManager(VALUE_TRUE);
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_TRUE, VALUE_TRUE);

        boolean actual = manager.isConsentGranted(TEST_USER_ID);

@@ -128,7 +147,7 @@ public class ContentProtectionConsentManagerTest {
    @Test
    public void isConsentGranted_packageVerifierDefault() {
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_DEFAULT);
                createContentProtectionConsentManager(VALUE_DEFAULT, VALUE_TRUE);

        boolean actual = manager.isConsentGranted(TEST_USER_ID);

@@ -137,15 +156,27 @@ public class ContentProtectionConsentManagerTest {
    }

    @Test
    public void contentObserver() throws Exception {
        ContentProtectionConsentManager manager = createContentProtectionConsentManager(VALUE_TRUE);
    public void isConsentGranted_contentProtectionDefault() {
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_TRUE, VALUE_DEFAULT);

        boolean actual = manager.isConsentGranted(TEST_USER_ID);

        assertThat(actual).isTrue();
        verify(mMockDevicePolicyManagerInternal).isUserOrganizationManaged(TEST_USER_ID);
    }

    @Test
    public void contentObserver_packageVerifier() {
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_TRUE, VALUE_DEFAULT);
        boolean firstActual = manager.isConsentGranted(TEST_USER_ID);

        Settings.Global.putInt(
                mTestableContentResolver, KEY_PACKAGE_VERIFIER_USER_CONSENT, VALUE_FALSE);
        // Observer has to be called manually, mTestableContentResolver is not propagating
        manager.mContentObserver.onChange(
                /* selfChange= */ false, URI_PACKAGE_VERIFIER_USER_CONSENT, TEST_USER_ID);
        notifyContentObserver(
                manager,
                URI_PACKAGE_VERIFIER_USER_CONSENT,
                KEY_PACKAGE_VERIFIER_USER_CONSENT,
                VALUE_FALSE);
        boolean secondActual = manager.isConsentGranted(TEST_USER_ID);

        assertThat(firstActual).isTrue();
@@ -153,6 +184,31 @@ public class ContentProtectionConsentManagerTest {
        verify(mMockDevicePolicyManagerInternal).isUserOrganizationManaged(TEST_USER_ID);
    }

    @Test
    public void contentObserver_contentProtection() {
        ContentProtectionConsentManager manager =
                createContentProtectionConsentManager(VALUE_TRUE, VALUE_DEFAULT);
        boolean firstActual = manager.isConsentGranted(TEST_USER_ID);

        notifyContentObserver(
                manager,
                URI_CONTENT_PROTECTION_USER_CONSENT,
                KEY_CONTENT_PROTECTION_USER_CONSENT,
                VALUE_FALSE);
        boolean secondActual = manager.isConsentGranted(TEST_USER_ID);

        assertThat(firstActual).isTrue();
        assertThat(secondActual).isFalse();
        verify(mMockDevicePolicyManagerInternal).isUserOrganizationManaged(TEST_USER_ID);
    }

    private void notifyContentObserver(
            ContentProtectionConsentManager manager, Uri uri, String key, int value) {
        Settings.Global.putInt(mTestableContentResolver, key, value);
        // Observer has to be called manually, mTestableContentResolver is not propagating
        manager.mContentObserver.onChange(/* selfChange= */ false, uri, TEST_USER_ID);
    }

    private ContentProtectionConsentManager createContentProtectionConsentManager(
            ContentResolver contentResolver) {
        return new ContentProtectionConsentManager(
@@ -162,11 +218,15 @@ public class ContentProtectionConsentManagerTest {
    }

    private ContentProtectionConsentManager createContentProtectionConsentManager(
            int valuePackageVerifierUserConsent) {
            int valuePackageVerifierUserConsent, int valueContentProtectionUserConsent) {
        Settings.Global.putInt(
                mTestableContentResolver,
                KEY_PACKAGE_VERIFIER_USER_CONSENT,
                valuePackageVerifierUserConsent);
        Settings.Global.putInt(
                mTestableContentResolver,
                KEY_CONTENT_PROTECTION_USER_CONSENT,
                valueContentProtectionUserConsent);
        return createContentProtectionConsentManager(mTestableContentResolver);
    }
}