Fix an issue that "Learn More" button was not showing up.

ActionDisabledByAdming dialog was not showing the "Learn More" button
when restriction is enforced by the device owner for
disallow_factory_reset.

Test: atest ActionDisabledLearnMoreButtonLauncherTest

Bug: 185248897

Change-Id: I835d3aa040bd3e554acc8340d47831a4c89c45de
This commit is contained in:
Bo-Kyung (BK) Choi
2021-06-08 16:08:01 -07:00
committed by Bo-Kyung (BK) Choi
parent 466698f9af
commit 866cd6a1a4
2 changed files with 72 additions and 6 deletions

View File

@@ -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}.
*/

View File

@@ -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();