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

Commit 13b498a8 authored by Yvonne Jiang's avatar Yvonne Jiang Committed by Vitor Carvalho
Browse files

Support EnforcingAdmin in addition to existing EnforcedAdmin in admin dialog controllers.

This allows restrictions set via the SUPERVISION role authority to be represented with the correct dialog customizations.

Test: atest SupervisedDeviceActionDisabledByAdminControllerTest
Flag: android.app.supervision.flags.deprecate_dpm_supervision_apis
Bug: 399481325
Change-Id: I5e20e90a90c20dc1e2d73dd551004f6db18ce673
parent 35669e44
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settingslib.enterprise;

import android.annotation.UserIdInt;
import android.app.admin.EnforcingAdmin;
import android.content.Context;
import android.content.DialogInterface;

@@ -54,15 +55,32 @@ public interface ActionDisabledByAdminController {

    /**
     * Updates the enforced admin
     * @deprecated Please use the same method that takes {@link EnforcingAdmin}.
     */
    @Deprecated
    void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, @UserIdInt int adminUserId);

    /**
     * Returns a listener for handling positive button clicks
     * Updates the enforcing admin
     */
    void updateEnforcingAdmin(@NonNull EnforcingAdmin admin);

    /**
     * Returns a listener for handling positive button clicks.
     * @deprecated Please use the same method that takes {@link EnforcingAdmin}.
     */
    @Nullable
    default DialogInterface.OnClickListener getPositiveButtonListener(@NonNull Context context,
            @NonNull RestrictedLockUtils.EnforcedAdmin enforcedAdmin) {
        return null;
    }

    /**
     * Returns a listener for handling positive button clicks
     */
    @Nullable
    default DialogInterface.OnClickListener getPositiveButtonListener(@NonNull Context context,
            @NonNull EnforcingAdmin enforcingAdmin) {
        return null;
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -19,6 +19,9 @@ package com.android.settingslib.enterprise;
import static java.util.Objects.requireNonNull;

import android.annotation.UserIdInt;
import android.app.admin.EnforcingAdmin;

import androidx.annotation.NonNull;

import com.android.internal.util.Preconditions;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
@@ -31,6 +34,7 @@ abstract class BaseActionDisabledByAdminController

    protected @UserIdInt int mEnforcementAdminUserId;
    protected EnforcedAdmin mEnforcedAdmin;
    protected EnforcingAdmin mEnforcingAdmin;
    protected ActionDisabledLearnMoreButtonLauncher mLauncher;
    protected final DeviceAdminStringProvider mStringProvider;

@@ -50,6 +54,12 @@ abstract class BaseActionDisabledByAdminController
        mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
    }

    @Override
    public final void updateEnforcingAdmin(@NonNull EnforcingAdmin admin) {
        assertInitialized();
        mEnforcingAdmin = requireNonNull(admin, "admin cannot be null");
    }

    protected final void assertInitialized() {
        Preconditions.checkState(mLauncher != null, "must call initialize() first");
    }
+17 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib.enterprise;

import android.app.admin.EnforcingAdmin;
import android.app.supervision.flags.Flags;
import android.content.ComponentName;
import android.content.Context;
@@ -69,14 +70,29 @@ final class SupervisedDeviceActionDisabledByAdminController
                || TextUtils.isEmpty(enforcedAdmin.component.getPackageName())) {
            return null;
        }
        return getPositiveButtonListener(context, enforcedAdmin.component.getPackageName());
    }

    @Nullable
    @Override
    public DialogInterface.OnClickListener getPositiveButtonListener(@NonNull Context context,
            @NonNull EnforcingAdmin enforcingAdmin) {
        if (TextUtils.isEmpty(enforcingAdmin.getPackageName())) {
            return null;
        }
        return getPositiveButtonListener(context, enforcingAdmin.getPackageName());
    }

    @Nullable
    private DialogInterface.OnClickListener getPositiveButtonListener(@NonNull Context context,
            @NonNull String packageName) {
        final Intent intent = new Intent(Settings.ACTION_MANAGE_SUPERVISOR_RESTRICTED_SETTING)
                .setData(new Uri.Builder()
                        .scheme("policy")
                        .appendPath("user_restrictions")
                        .appendPath(mRestriction)
                        .build())
                .setPackage(enforcedAdmin.component.getPackageName());
                .setPackage(packageName);
        ComponentName resolvedSupervisionActivity =
                intent.resolveActivity(context.getPackageManager());
        if (resolvedSupervisionActivity == null) {
+10 −1
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.settingslib.enterprise;

import static com.google.common.truth.Truth.assertWithMessage;

import android.app.admin.DeviceAdminAuthority;
import android.app.admin.EnforcingAdmin;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
@@ -36,12 +38,19 @@ public final class ActionDisabledByAdminControllerTestUtils {

    static final String SUPPORT_MESSAGE = "support message";

    static final String ADMIN_PACKAGE_NAME = "some.package.name";
    static final ComponentName ADMIN_COMPONENT =
            new ComponentName("some.package.name", "some.package.name.SomeClass");
            new ComponentName(ADMIN_PACKAGE_NAME, "some.package.name.SomeClass");
    static final EnforcedAdmin ENFORCED_ADMIN = new EnforcedAdmin(
                    ADMIN_COMPONENT, UserHandle.of(ENFORCEMENT_ADMIN_USER_ID));
    static final EnforcedAdmin ENFORCED_ADMIN_WITHOUT_COMPONENT = new EnforcedAdmin(
            /* component= */ null, UserHandle.of(ENFORCEMENT_ADMIN_USER_ID));
    static final EnforcingAdmin ENFORCING_ADMIN =
            new EnforcingAdmin(
                    ADMIN_PACKAGE_NAME,
                    DeviceAdminAuthority.DEVICE_ADMIN_AUTHORITY,
                    UserHandle.of(ENFORCEMENT_ADMIN_USER_ID),
                    ADMIN_COMPONENT);

    static final String URL = "https://testexample.com";

+24 −0
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@
package com.android.settingslib.enterprise;

import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ADMIN_COMPONENT;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ADMIN_PACKAGE_NAME;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCED_ADMIN;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCING_ADMIN;
import static com.android.settingslib.enterprise.ActionDisabledByAdminControllerTestUtils.ENFORCEMENT_ADMIN_USER_ID;
import static com.android.settingslib.enterprise.FakeDeviceAdminStringProvider.DEFAULT_DEVICE_ADMIN_STRING_PROVIDER;

@@ -87,6 +89,28 @@ public class SupervisedDeviceActionDisabledByAdminControllerTest {
        assertEquals(ADMIN_COMPONENT.getPackageName(), nextIntent.getPackage());
    }

    @Test
    public void buttonClicked_enforcingAdmin() {
        Uri restrictionUri = Uri.parse("policy:/user_restrictions/no_add_user");
        Intent intent = new Intent(Settings.ACTION_MANAGE_SUPERVISOR_RESTRICTED_SETTING)
                .setData(restrictionUri)
                .setPackage(ADMIN_PACKAGE_NAME);
        ResolveInfo resolveInfo = ShadowResolveInfo.newResolveInfo("Admin Activity",
                ADMIN_COMPONENT.getPackageName(), "InfoActivity");
        shadowOf(mContext.getPackageManager()).addResolveInfoForIntent(intent, resolveInfo);

        DialogInterface.OnClickListener listener =
                mController.getPositiveButtonListener(mContext, ENFORCING_ADMIN);
        assertNotNull("Supervision controller must supply a non-null listener", listener);
        listener.onClick(mock(DialogInterface.class), 0 /* which */);

        Intent nextIntent = shadowOf(RuntimeEnvironment.application).getNextStartedActivity();
        assertEquals(Settings.ACTION_MANAGE_SUPERVISOR_RESTRICTED_SETTING,
                nextIntent.getAction());
        assertEquals(restrictionUri, nextIntent.getData());
        assertEquals(ADMIN_PACKAGE_NAME, nextIntent.getPackage());
    }

    @Test
    public void noButton() {
        // No supervisor restricted setting Activity