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

Commit 887da176 authored by Azhara Assanova's avatar Azhara Assanova
Browse files

[AAPM] Update createSupportIntentForPolicy

For 2G and MTE features, if the dialog type is unknown, we set it to
disabled setting as we know they don't have interactions.

Bug: 389080341
Test: atest FrameworksCoreTests:android.security.advancedprotection.AdvancedProtectionManagerTest
Test: visual test
Flag: EXEMPT bug fix
Change-Id: Iad0d132970e350c1c1955f2fb9f4055c55066d88
parent 503e2595
Loading
Loading
Loading
Loading
+29 −3
Original line number Diff line number Diff line
@@ -349,7 +349,8 @@ public final class AdvancedProtectionManager {
     *
     * @param featureId The feature identifier.
     * @param type The type of the feature describing the action that needs to be explained
     *                 in the dialog or null for default explanation.
     *                 in the dialog or {@link #SUPPORT_DIALOG_TYPE_UNKNOWN} for default
     *                 explanation.
     * @return Intent An intent to be used to start the dialog-activity that explains a feature was
     *                disabled by advanced protection.
     * @hide
@@ -373,7 +374,27 @@ public final class AdvancedProtectionManager {
        return intent;
    }

    /** @hide */
    /**
     * Called by a feature to display a support dialog when a feature was disabled by advanced
     * protection based on a policy identifier or restriction. This returns an intent that can be
     * used with {@link Context#startActivity(Intent)} to display the dialog.
     *
     * <p>At the moment, if the dialog is for {@link #FEATURE_ID_DISALLOW_CELLULAR_2G} or
     * {@link #FEATURE_ID_ENABLE_MTE} and the provided type is
     * {@link #SUPPORT_DIALOG_TYPE_UNKNOWN}, the type will be changed to
     * {@link #SUPPORT_DIALOG_TYPE_DISABLED_SETTING} in the returned intent, as these features only
     * have a disabled setting UI.
     *
     * <p>Note that this method doesn't check if the feature is actually disabled, i.e. this method
     * will always return an intent.
     *
     * @param identifier The policy identifier or restriction.
     * @param type The type of the feature describing the action that needs to be explained
     *                 in the dialog or {@link #SUPPORT_DIALOG_TYPE_UNKNOWN} for default
     *                 explanation.
     * @return Intent An intent to be used to start the dialog-activity that explains a feature was
     *                disabled by advanced protection.
     * @hide */
    public static @NonNull Intent createSupportIntentForPolicyIdentifierOrRestriction(
            @NonNull String identifier, @SupportDialogType int type) {
        Objects.requireNonNull(identifier);
@@ -382,16 +403,21 @@ public final class AdvancedProtectionManager {
                    + " SUPPORT_DIALOG_TYPE_* APIs.");
        }
        final int featureId;
        int dialogType = type;
        if (DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY.equals(identifier)) {
            featureId = FEATURE_ID_DISALLOW_INSTALL_UNKNOWN_SOURCES;
        } else if (DISALLOW_CELLULAR_2G.equals(identifier)) {
            featureId = FEATURE_ID_DISALLOW_CELLULAR_2G;
            dialogType = (dialogType == SUPPORT_DIALOG_TYPE_UNKNOWN)
                    ? SUPPORT_DIALOG_TYPE_DISABLED_SETTING : dialogType;
        } else if (MEMORY_TAGGING_POLICY.equals(identifier)) {
            featureId = FEATURE_ID_ENABLE_MTE;
            dialogType = (dialogType == SUPPORT_DIALOG_TYPE_UNKNOWN)
                    ? SUPPORT_DIALOG_TYPE_DISABLED_SETTING : dialogType;
        } else {
            throw new UnsupportedOperationException("Unsupported identifier: " + identifier);
        }
        return createSupportIntent(featureId, type);
        return createSupportIntent(featureId, dialogType);
    }

    /** @hide */
+47 −0
Original line number Diff line number Diff line
@@ -16,10 +16,14 @@

package android.security.advancedprotection;

import static android.os.UserManager.DISALLOW_CELLULAR_2G;
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY;
import static android.security.advancedprotection.AdvancedProtectionManager.ACTION_SHOW_ADVANCED_PROTECTION_SUPPORT_DIALOG;
import static android.security.advancedprotection.AdvancedProtectionManager.EXTRA_SUPPORT_DIALOG_FEATURE;
import static android.security.advancedprotection.AdvancedProtectionManager.EXTRA_SUPPORT_DIALOG_TYPE;
import static android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_DISALLOW_CELLULAR_2G;
import static android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_DISALLOW_INSTALL_UNKNOWN_SOURCES;
import static android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_ENABLE_MTE;
import static android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_BLOCKED_INTERACTION;
import static android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_DISABLED_SETTING;
import static android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_UNKNOWN;
@@ -37,6 +41,9 @@ import org.junit.runners.JUnit4;
public class AdvancedProtectionManagerTest {
    private static final int FEATURE_ID_INVALID = -1;
    private static final int SUPPORT_DIALOG_TYPE_INVALID = -1;
    //TODO(b/378931989): Switch to android.app.admin.DevicePolicyIdentifiers.MEMORY_TAGGING_POLICY
    //when the appropriate flag is launched.
    private static final String MEMORY_TAGGING_POLICY = "memoryTagging";

    @Test
    public void testCreateSupportIntent_validFeature_validTypeUnknown_createsIntent() {
@@ -94,4 +101,44 @@ public class AdvancedProtectionManagerTest {
                AdvancedProtectionManager.createSupportIntent(FEATURE_ID_INVALID,
                        SUPPORT_DIALOG_TYPE_INVALID));
    }

    @Test
    public void testCreateSupportIntentForPolicy_2g_typeUnknown_createsIntentForDisabledSetting() {
        Intent intent = AdvancedProtectionManager
                .createSupportIntentForPolicyIdentifierOrRestriction(
                        DISALLOW_CELLULAR_2G, SUPPORT_DIALOG_TYPE_UNKNOWN);

        assertEquals(ACTION_SHOW_ADVANCED_PROTECTION_SUPPORT_DIALOG, intent.getAction());
        assertEquals(FEATURE_ID_DISALLOW_CELLULAR_2G, intent.getIntExtra(
                EXTRA_SUPPORT_DIALOG_FEATURE, FEATURE_ID_INVALID));
        assertEquals(SUPPORT_DIALOG_TYPE_DISABLED_SETTING, intent.getIntExtra(
                EXTRA_SUPPORT_DIALOG_TYPE, SUPPORT_DIALOG_TYPE_INVALID));
    }

    @Test
    public void testCreateSupportIntentForPolicy_mte_typeUnknown_createsIntentForDisabledSetting() {
        Intent intent = AdvancedProtectionManager
                .createSupportIntentForPolicyIdentifierOrRestriction(
                        MEMORY_TAGGING_POLICY, SUPPORT_DIALOG_TYPE_UNKNOWN);

        assertEquals(ACTION_SHOW_ADVANCED_PROTECTION_SUPPORT_DIALOG, intent.getAction());
        assertEquals(FEATURE_ID_ENABLE_MTE, intent.getIntExtra(
                EXTRA_SUPPORT_DIALOG_FEATURE, FEATURE_ID_INVALID));
        assertEquals(SUPPORT_DIALOG_TYPE_DISABLED_SETTING, intent.getIntExtra(
                EXTRA_SUPPORT_DIALOG_TYPE, SUPPORT_DIALOG_TYPE_INVALID));
    }

    @Test
    public void
            testCreateSupportIntentForPolicy_unknownSources_typeUnknown_createsIntentForUnknown() {
        Intent intent = AdvancedProtectionManager
                .createSupportIntentForPolicyIdentifierOrRestriction(
                        DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, SUPPORT_DIALOG_TYPE_UNKNOWN);

        assertEquals(ACTION_SHOW_ADVANCED_PROTECTION_SUPPORT_DIALOG, intent.getAction());
        assertEquals(FEATURE_ID_DISALLOW_INSTALL_UNKNOWN_SOURCES, intent.getIntExtra(
                EXTRA_SUPPORT_DIALOG_FEATURE, FEATURE_ID_INVALID));
        assertEquals(SUPPORT_DIALOG_TYPE_UNKNOWN, intent.getIntExtra(
                EXTRA_SUPPORT_DIALOG_TYPE, SUPPORT_DIALOG_TYPE_INVALID));
    }
}