Loading packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java +14 −3 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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)); } } Loading Loading @@ -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}. */ Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncherTest.java +58 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -61,6 +62,9 @@ public final class ActionDisabledLearnMoreButtonLauncherTest { @Mock private Context mContext; @Mock private DevicePolicyManager mDevicePolicyManager; @Mock private UserManager mUserManager; Loading @@ -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); } Loading @@ -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); Loading @@ -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); Loading @@ -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); Loading Loading @@ -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(); Loading Loading
packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java +14 −3 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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)); } } Loading Loading @@ -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}. */ Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncherTest.java +58 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -61,6 +62,9 @@ public final class ActionDisabledLearnMoreButtonLauncherTest { @Mock private Context mContext; @Mock private DevicePolicyManager mDevicePolicyManager; @Mock private UserManager mUserManager; Loading @@ -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); } Loading @@ -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); Loading @@ -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); Loading @@ -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); Loading Loading @@ -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(); Loading