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

Commit 4debc9c6 authored by BK Choi's avatar BK Choi Committed by Automerger Merge Worker
Browse files

Merge "Fix an issue that "Learn More" button was not showing up." into sc-dev am: 2e391ecf

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14920834

Change-Id: Ic903c7c6850e147609d7f7e73e34550323b76a3f
parents 9cf5624a 2e391ecf
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settingslib.enterprise;

import static java.util.Objects.requireNonNull;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -42,9 +43,10 @@ public abstract class ActionDisabledLearnMoreButtonLauncher {
        requireNonNull(enforcedAdmin, "enforcedAdmin cannot be null");

        // The "Learn more" button appears only if the restriction is enforced by an admin in the
        // same profile group. Otherwise the admin package and its policies are not accessible to
        // the current user.
        if (isSameProfileGroup(context, enforcementAdminUserId)) {
        // same profile group or by the device owner. Otherwise the admin package and its policies
        // are not accessible to the current user.
        if (isSameProfileGroup(context, enforcementAdminUserId)
                || isEnforcedByDeviceOwnerOnSystemUserMode(context, enforcementAdminUserId)) {
            setLearnMoreButton(() -> showAdminPolicies(context, enforcedAdmin));
        }
    }
@@ -90,6 +92,15 @@ public abstract class ActionDisabledLearnMoreButtonLauncher {
        return um.isSameProfileGroup(enforcementAdminUserId, um.getUserHandle());
    }

    private boolean isEnforcedByDeviceOwnerOnSystemUserMode(
            Context context, int enforcementAdminUserId) {
        if (enforcementAdminUserId != UserHandle.USER_SYSTEM) {
            return false;
        }
        DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
        return enforcementAdminUserId == dpm.getDeviceOwnerUserId();
    }

    /**
     * Shows the help page using the given {@code url}.
     */
+58 −3
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertThrows;

import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
@@ -61,6 +62,9 @@ public final class ActionDisabledLearnMoreButtonLauncherTest {
    @Mock
    private Context mContext;

    @Mock
    private DevicePolicyManager mDevicePolicyManager;

    @Mock
    private UserManager mUserManager;

@@ -77,6 +81,7 @@ public final class ActionDisabledLearnMoreButtonLauncherTest {
    public void setUp() {
        when(mContext.getUserId()).thenReturn(CONTEXT_USER_ID);
        when(mUserManager.getUserHandle()).thenReturn(CONTEXT_USER_ID);
        when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
    }

@@ -95,8 +100,9 @@ public final class ActionDisabledLearnMoreButtonLauncherTest {
    }

    @Test
    public void testSetupLearnMoreButtonToShowAdminPolicies_differentProfileGroup() {
    public void testSetupLearnMoreButtonToShowAdminPolicies_differentProfileGroup_noDeviceOwner() {
        mockDifferentProfileGroup();
        mockEnforcementAdminIsNotDeviceOwner();

        mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
                ENFORCED_ADMIN);
@@ -105,8 +111,34 @@ public final class ActionDisabledLearnMoreButtonLauncherTest {
    }

    @Test
    public void testSetupLearnMoreButtonToShowAdminPolicies_sameProfileGroup_noComponent() {
    public void testSetupLearnMoreButtonToShowAdminPolicies_differentGroup_noSystemDeviceOwner() {
        mockDifferentProfileGroup();
        mockDeviceOwner(ENFORCEMENT_ADMIN_USER_ID);

        mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
                ENFORCED_ADMIN);

        verify(mLauncher, never()).setLearnMoreButton(any());
    }

    @Test
    public void testSetupLearnMoreButtonToShowAdminPolicies_differentGroup_systemDeviceOwner() {
        mockDifferentProfileGroup();
        mockDeviceOwner(UserHandle.USER_SYSTEM);

        mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, UserHandle.USER_SYSTEM,
                ENFORCED_ADMIN_WITHOUT_COMPONENT);
        tapLearnMore();

        verify(mLauncher, never()).launchShowAdminPolicies(any(), any(), any());
        verify(mLauncher).launchShowAdminSettings(mContext);
        verifyFinishSelf();
    }

    @Test
    public void testSetupLearnMoreButtonToShowAdminPolicies_sameProfileGroup_noDeviceOwner() {
        mockSameProfileGroup();
        mockEnforcementAdminIsNotDeviceOwner();

        mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
                ENFORCED_ADMIN_WITHOUT_COMPONENT);
@@ -118,8 +150,23 @@ public final class ActionDisabledLearnMoreButtonLauncherTest {
    }

    @Test
    public void testSetupLearnMoreButtonToShowAdminPolicies_sameProfileGroup_withComponent() {
    public void testSetupLearnMoreButtonToShowAdminPolicies_sameProfileGroup_noSystemDeviceOwner() {
        mockSameProfileGroup();
        mockDeviceOwner(ENFORCEMENT_ADMIN_USER_ID);

        mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
                ENFORCED_ADMIN_WITHOUT_COMPONENT);
        tapLearnMore();

        verify(mLauncher, never()).launchShowAdminPolicies(any(), any(), any());
        verify(mLauncher).launchShowAdminSettings(mContext);
        verifyFinishSelf();
    }

    @Test
    public void testSetupLearnMoreButtonToShowAdminPolicies_showsLearnMoreButton_withComponent() {
        mockSameProfileGroup();
        mockEnforcementAdminIsNotDeviceOwner();

        mLauncher.setupLearnMoreButtonToShowAdminPolicies(mContext, ENFORCEMENT_ADMIN_USER_ID,
                ENFORCED_ADMIN);
@@ -164,6 +211,14 @@ public final class ActionDisabledLearnMoreButtonLauncherTest {
                .thenReturn(true);
    }

    private void mockEnforcementAdminIsNotDeviceOwner() {
        when(mDevicePolicyManager.getDeviceOwnerUserId()).thenReturn(ENFORCEMENT_ADMIN_USER_ID + 1);
    }

    private void mockDeviceOwner(int userId) {
        when(mDevicePolicyManager.getDeviceOwnerUserId()).thenReturn(userId);
    }

    private void tapLearnMore() {
        verify(mLauncher).setLearnMoreButton(mLearnMoreActionCaptor.capture());
        mLearnMoreActionCaptor.getValue().run();