Loading packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerFactory.java +8 −1 Original line number Diff line number Diff line Loading @@ -18,6 +18,9 @@ package com.android.settingslib.enterprise; import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED; import static com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.DEFAULT_RESOLVE_ACTIVITY_CHECKER; import static com.android.settingslib.enterprise.ManagedDeviceActionDisabledByAdminController.DEFAULT_FOREGROUND_USER_CHECKER; import android.app.admin.DevicePolicyManager; import android.content.Context; import android.hardware.biometrics.BiometricAuthenticator; Loading @@ -43,7 +46,11 @@ public final class ActionDisabledByAdminControllerFactory { } else if (isFinancedDevice(context)) { return new FinancedDeviceActionDisabledByAdminController(stringProvider); } else { return new ManagedDeviceActionDisabledByAdminController(stringProvider, userHandle); return new ManagedDeviceActionDisabledByAdminController( stringProvider, userHandle, DEFAULT_FOREGROUND_USER_CHECKER, DEFAULT_RESOLVE_ACTIVITY_CHECKER); } } Loading packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java +20 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.UserHandle; import android.os.UserManager; Loading @@ -34,6 +35,17 @@ import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; */ public abstract class ActionDisabledLearnMoreButtonLauncher { public static ResolveActivityChecker DEFAULT_RESOLVE_ACTIVITY_CHECKER = (packageManager, url, userHandle) -> packageManager.resolveActivityAsUser( createLearnMoreIntent(url), PackageManager.MATCH_DEFAULT_ONLY, userHandle.getIdentifier()) != null; interface ResolveActivityChecker { boolean canResolveActivityAsUser( PackageManager packageManager, String url, UserHandle userHandle); } /** * Sets up a "learn more" button which shows a screen with device policy settings */ Loading Loading @@ -111,6 +123,14 @@ public abstract class ActionDisabledLearnMoreButtonLauncher { finishSelf(); } protected final boolean canLaunchHelpPage( PackageManager packageManager, String url, UserHandle userHandle, ResolveActivityChecker resolveActivityChecker) { return resolveActivityChecker.canResolveActivityAsUser(packageManager, url, userHandle); } private void showAdminPolicies(Context context, EnforcedAdmin enforcedAdmin) { if (enforcedAdmin.component != null) { launchShowAdminPolicies(context, enforcedAdmin.user, enforcedAdmin.component); Loading packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java +67 −8 Original line number Diff line number Diff line Loading @@ -20,13 +20,14 @@ import static java.util.Objects.requireNonNull; import android.app.admin.DevicePolicyManager; import android.content.Context; import android.content.pm.PackageManager; import android.os.UserHandle; import android.os.UserManager; import android.text.TextUtils; import androidx.annotation.Nullable; import java.util.Objects; import com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.ResolveActivityChecker; /** Loading @@ -35,17 +36,37 @@ import java.util.Objects; final class ManagedDeviceActionDisabledByAdminController extends BaseActionDisabledByAdminController { private final UserHandle mUserHandle; interface ForegroundUserChecker { boolean isUserForeground(Context context, UserHandle userHandle); } public final static ForegroundUserChecker DEFAULT_FOREGROUND_USER_CHECKER = ManagedDeviceActionDisabledByAdminController::isUserForeground; /** * The {@link UserHandle} which is preferred for launching the web help page in * <p>If not able to launch the web help page in this user, the current user will be used as * fallback instead. If the current user cannot open it either, the admin policies page will * be used instead. */ private final UserHandle mPreferredUserHandle; private final ForegroundUserChecker mForegroundUserChecker; private final ResolveActivityChecker mResolveActivityChecker; /** * Constructs a {@link ManagedDeviceActionDisabledByAdminController} * @param userHandle - user on which to launch the help web page, if necessary * @param preferredUserHandle - user on which to launch the help web page, if necessary */ ManagedDeviceActionDisabledByAdminController( DeviceAdminStringProvider stringProvider, UserHandle userHandle) { UserHandle preferredUserHandle, ForegroundUserChecker foregroundUserChecker, ResolveActivityChecker resolveActivityChecker) { super(stringProvider); mUserHandle = requireNonNull(userHandle); mPreferredUserHandle = requireNonNull(preferredUserHandle); mForegroundUserChecker = requireNonNull(foregroundUserChecker); mResolveActivityChecker = requireNonNull(resolveActivityChecker); } @Override Loading @@ -53,14 +74,52 @@ final class ManagedDeviceActionDisabledByAdminController assertInitialized(); String url = mStringProvider.getLearnMoreHelpPageUrl(); if (TextUtils.isEmpty(url)) { if (!TextUtils.isEmpty(url) && canLaunchHelpPageInPreferredOrCurrentUser(context, url, mPreferredUserHandle)) { setupLearnMoreButtonToLaunchHelpPage(context, url, mPreferredUserHandle); } else { mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId, mEnforcedAdmin); } else { mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url, mUserHandle); } } private boolean canLaunchHelpPageInPreferredOrCurrentUser( Context context, String url, UserHandle preferredUserHandle) { PackageManager packageManager = context.getPackageManager(); if (mLauncher.canLaunchHelpPage( packageManager, url, preferredUserHandle, mResolveActivityChecker) && mForegroundUserChecker.isUserForeground(context, preferredUserHandle)) { return true; } return mLauncher.canLaunchHelpPage( packageManager, url, context.getUser(), mResolveActivityChecker); } /** * Sets up the "Learn more" button to launch the web help page in the {@code * preferredUserHandle} user. If not possible to launch it there, it sets up the button to * launch it in the current user instead. */ private void setupLearnMoreButtonToLaunchHelpPage( Context context, String url, UserHandle preferredUserHandle) { PackageManager packageManager = context.getPackageManager(); if (mLauncher.canLaunchHelpPage( packageManager, url, preferredUserHandle, mResolveActivityChecker) && mForegroundUserChecker.isUserForeground(context, preferredUserHandle)) { mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url, preferredUserHandle); } if (mLauncher.canLaunchHelpPage( packageManager, url, context.getUser(), mResolveActivityChecker)) { mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url, context.getUser()); } } private static boolean isUserForeground(Context context, UserHandle userHandle) { return context.createContextAsUser(userHandle, /* flags= */ 0) .getSystemService(UserManager.class).isUserForeground(); } @Override public String getAdminSupportTitle(@Nullable String restriction) { if (restriction == null) { Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java +73 −6 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ import static com.google.common.truth.Truth.assertThat; import android.app.Activity; import android.content.Context; import android.content.pm.ResolveInfo; import android.os.UserHandle; import android.os.UserManager; import androidx.test.core.app.ApplicationProvider; Loading @@ -45,9 +47,11 @@ import org.robolectric.android.controller.ActivityController; @RunWith(RobolectricTestRunner.class) public class ManagedDeviceActionDisabledByAdminControllerTest { private static UserHandle MANAGED_USER = UserHandle.of(123); private static final String RESTRICTION = UserManager.DISALLOW_ADJUST_VOLUME; private static final String EMPTY_URL = ""; private static final String SUPPORT_TITLE_FOR_RESTRICTION = DISALLOW_ADJUST_VOLUME_TITLE; public static final ResolveInfo TEST_RESULT_INFO = new ResolveInfo(); private final Context mContext = ApplicationProvider.getApplicationContext(); private final Activity mActivity = ActivityController.of(new Activity()).get(); Loading @@ -60,8 +64,21 @@ public class ManagedDeviceActionDisabledByAdminControllerTest { } @Test public void setupLearnMoreButton_validUrl_negativeButtonSet() { ManagedDeviceActionDisabledByAdminController controller = createController(URL); public void setupLearnMoreButton_noUrl_negativeButtonSet() { ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL); controller.setupLearnMoreButton(mContext); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES); } @Test public void setupLearnMoreButton_validUrl_foregroundUser_launchesHelpPage() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ true, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ MANAGED_USER); controller.setupLearnMoreButton(mContext); Loading @@ -69,8 +86,38 @@ public class ManagedDeviceActionDisabledByAdminControllerTest { } @Test public void setupLearnMoreButton_noUrl_negativeButtonSet() { ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL); public void setupLearnMoreButton_validUrl_browserInPreferredUser_notForeground_showsAdminPolicies() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ false, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ MANAGED_USER); controller.setupLearnMoreButton(mContext); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES); } @Test public void setupLearnMoreButton_validUrl_browserInCurrentUser_launchesHelpPage() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ false, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ mContext.getUser()); controller.setupLearnMoreButton(mContext); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_LAUNCH_HELP_PAGE); } @Test public void setupLearnMoreButton_validUrl_browserNotOnAnyUser_showsAdminPolicies() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ false, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ null); controller.setupLearnMoreButton(mContext); Loading Loading @@ -110,13 +157,33 @@ public class ManagedDeviceActionDisabledByAdminControllerTest { } private ManagedDeviceActionDisabledByAdminController createController() { return createController(/* url= */ null); return createController( /* url= */ null, /* foregroundUserChecker= */ true, mContext.getUser(), /* userContainingBrowser= */ null); } private ManagedDeviceActionDisabledByAdminController createController(String url) { return createController( url, /* foregroundUserChecker= */ true, mContext.getUser(), /* userContainingBrowser= */ null); } private ManagedDeviceActionDisabledByAdminController createController( String url, boolean isUserForeground, UserHandle preferredUserHandle, UserHandle userContainingBrowser) { ManagedDeviceActionDisabledByAdminController controller = new ManagedDeviceActionDisabledByAdminController( new FakeDeviceAdminStringProvider(url), mContext.getUser()); new FakeDeviceAdminStringProvider(url), preferredUserHandle, /* foregroundUserChecker= */ (context, userHandle) -> isUserForeground, /* resolveActivityChecker= */ (packageManager, __, userHandle) -> userHandle.equals(userContainingBrowser)); controller.initialize(mTestUtils.createLearnMoreButtonLauncher()); controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID); return controller; Loading Loading
packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledByAdminControllerFactory.java +8 −1 Original line number Diff line number Diff line Loading @@ -18,6 +18,9 @@ package com.android.settingslib.enterprise; import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED; import static com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.DEFAULT_RESOLVE_ACTIVITY_CHECKER; import static com.android.settingslib.enterprise.ManagedDeviceActionDisabledByAdminController.DEFAULT_FOREGROUND_USER_CHECKER; import android.app.admin.DevicePolicyManager; import android.content.Context; import android.hardware.biometrics.BiometricAuthenticator; Loading @@ -43,7 +46,11 @@ public final class ActionDisabledByAdminControllerFactory { } else if (isFinancedDevice(context)) { return new FinancedDeviceActionDisabledByAdminController(stringProvider); } else { return new ManagedDeviceActionDisabledByAdminController(stringProvider, userHandle); return new ManagedDeviceActionDisabledByAdminController( stringProvider, userHandle, DEFAULT_FOREGROUND_USER_CHECKER, DEFAULT_RESOLVE_ACTIVITY_CHECKER); } } Loading
packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java +20 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.UserHandle; import android.os.UserManager; Loading @@ -34,6 +35,17 @@ import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; */ public abstract class ActionDisabledLearnMoreButtonLauncher { public static ResolveActivityChecker DEFAULT_RESOLVE_ACTIVITY_CHECKER = (packageManager, url, userHandle) -> packageManager.resolveActivityAsUser( createLearnMoreIntent(url), PackageManager.MATCH_DEFAULT_ONLY, userHandle.getIdentifier()) != null; interface ResolveActivityChecker { boolean canResolveActivityAsUser( PackageManager packageManager, String url, UserHandle userHandle); } /** * Sets up a "learn more" button which shows a screen with device policy settings */ Loading Loading @@ -111,6 +123,14 @@ public abstract class ActionDisabledLearnMoreButtonLauncher { finishSelf(); } protected final boolean canLaunchHelpPage( PackageManager packageManager, String url, UserHandle userHandle, ResolveActivityChecker resolveActivityChecker) { return resolveActivityChecker.canResolveActivityAsUser(packageManager, url, userHandle); } private void showAdminPolicies(Context context, EnforcedAdmin enforcedAdmin) { if (enforcedAdmin.component != null) { launchShowAdminPolicies(context, enforcedAdmin.user, enforcedAdmin.component); Loading
packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java +67 −8 Original line number Diff line number Diff line Loading @@ -20,13 +20,14 @@ import static java.util.Objects.requireNonNull; import android.app.admin.DevicePolicyManager; import android.content.Context; import android.content.pm.PackageManager; import android.os.UserHandle; import android.os.UserManager; import android.text.TextUtils; import androidx.annotation.Nullable; import java.util.Objects; import com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.ResolveActivityChecker; /** Loading @@ -35,17 +36,37 @@ import java.util.Objects; final class ManagedDeviceActionDisabledByAdminController extends BaseActionDisabledByAdminController { private final UserHandle mUserHandle; interface ForegroundUserChecker { boolean isUserForeground(Context context, UserHandle userHandle); } public final static ForegroundUserChecker DEFAULT_FOREGROUND_USER_CHECKER = ManagedDeviceActionDisabledByAdminController::isUserForeground; /** * The {@link UserHandle} which is preferred for launching the web help page in * <p>If not able to launch the web help page in this user, the current user will be used as * fallback instead. If the current user cannot open it either, the admin policies page will * be used instead. */ private final UserHandle mPreferredUserHandle; private final ForegroundUserChecker mForegroundUserChecker; private final ResolveActivityChecker mResolveActivityChecker; /** * Constructs a {@link ManagedDeviceActionDisabledByAdminController} * @param userHandle - user on which to launch the help web page, if necessary * @param preferredUserHandle - user on which to launch the help web page, if necessary */ ManagedDeviceActionDisabledByAdminController( DeviceAdminStringProvider stringProvider, UserHandle userHandle) { UserHandle preferredUserHandle, ForegroundUserChecker foregroundUserChecker, ResolveActivityChecker resolveActivityChecker) { super(stringProvider); mUserHandle = requireNonNull(userHandle); mPreferredUserHandle = requireNonNull(preferredUserHandle); mForegroundUserChecker = requireNonNull(foregroundUserChecker); mResolveActivityChecker = requireNonNull(resolveActivityChecker); } @Override Loading @@ -53,14 +74,52 @@ final class ManagedDeviceActionDisabledByAdminController assertInitialized(); String url = mStringProvider.getLearnMoreHelpPageUrl(); if (TextUtils.isEmpty(url)) { if (!TextUtils.isEmpty(url) && canLaunchHelpPageInPreferredOrCurrentUser(context, url, mPreferredUserHandle)) { setupLearnMoreButtonToLaunchHelpPage(context, url, mPreferredUserHandle); } else { mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId, mEnforcedAdmin); } else { mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url, mUserHandle); } } private boolean canLaunchHelpPageInPreferredOrCurrentUser( Context context, String url, UserHandle preferredUserHandle) { PackageManager packageManager = context.getPackageManager(); if (mLauncher.canLaunchHelpPage( packageManager, url, preferredUserHandle, mResolveActivityChecker) && mForegroundUserChecker.isUserForeground(context, preferredUserHandle)) { return true; } return mLauncher.canLaunchHelpPage( packageManager, url, context.getUser(), mResolveActivityChecker); } /** * Sets up the "Learn more" button to launch the web help page in the {@code * preferredUserHandle} user. If not possible to launch it there, it sets up the button to * launch it in the current user instead. */ private void setupLearnMoreButtonToLaunchHelpPage( Context context, String url, UserHandle preferredUserHandle) { PackageManager packageManager = context.getPackageManager(); if (mLauncher.canLaunchHelpPage( packageManager, url, preferredUserHandle, mResolveActivityChecker) && mForegroundUserChecker.isUserForeground(context, preferredUserHandle)) { mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url, preferredUserHandle); } if (mLauncher.canLaunchHelpPage( packageManager, url, context.getUser(), mResolveActivityChecker)) { mLauncher.setupLearnMoreButtonToLaunchHelpPage(context, url, context.getUser()); } } private static boolean isUserForeground(Context context, UserHandle userHandle) { return context.createContextAsUser(userHandle, /* flags= */ 0) .getSystemService(UserManager.class).isUserForeground(); } @Override public String getAdminSupportTitle(@Nullable String restriction) { if (restriction == null) { Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminControllerTest.java +73 −6 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ import static com.google.common.truth.Truth.assertThat; import android.app.Activity; import android.content.Context; import android.content.pm.ResolveInfo; import android.os.UserHandle; import android.os.UserManager; import androidx.test.core.app.ApplicationProvider; Loading @@ -45,9 +47,11 @@ import org.robolectric.android.controller.ActivityController; @RunWith(RobolectricTestRunner.class) public class ManagedDeviceActionDisabledByAdminControllerTest { private static UserHandle MANAGED_USER = UserHandle.of(123); private static final String RESTRICTION = UserManager.DISALLOW_ADJUST_VOLUME; private static final String EMPTY_URL = ""; private static final String SUPPORT_TITLE_FOR_RESTRICTION = DISALLOW_ADJUST_VOLUME_TITLE; public static final ResolveInfo TEST_RESULT_INFO = new ResolveInfo(); private final Context mContext = ApplicationProvider.getApplicationContext(); private final Activity mActivity = ActivityController.of(new Activity()).get(); Loading @@ -60,8 +64,21 @@ public class ManagedDeviceActionDisabledByAdminControllerTest { } @Test public void setupLearnMoreButton_validUrl_negativeButtonSet() { ManagedDeviceActionDisabledByAdminController controller = createController(URL); public void setupLearnMoreButton_noUrl_negativeButtonSet() { ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL); controller.setupLearnMoreButton(mContext); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES); } @Test public void setupLearnMoreButton_validUrl_foregroundUser_launchesHelpPage() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ true, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ MANAGED_USER); controller.setupLearnMoreButton(mContext); Loading @@ -69,8 +86,38 @@ public class ManagedDeviceActionDisabledByAdminControllerTest { } @Test public void setupLearnMoreButton_noUrl_negativeButtonSet() { ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL); public void setupLearnMoreButton_validUrl_browserInPreferredUser_notForeground_showsAdminPolicies() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ false, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ MANAGED_USER); controller.setupLearnMoreButton(mContext); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_SHOW_ADMIN_POLICIES); } @Test public void setupLearnMoreButton_validUrl_browserInCurrentUser_launchesHelpPage() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ false, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ mContext.getUser()); controller.setupLearnMoreButton(mContext); mTestUtils.assertLearnMoreAction(LEARN_MORE_ACTION_LAUNCH_HELP_PAGE); } @Test public void setupLearnMoreButton_validUrl_browserNotOnAnyUser_showsAdminPolicies() { ManagedDeviceActionDisabledByAdminController controller = createController( URL, /* isUserForeground= */ false, /* preferredUserHandle= */ MANAGED_USER, /* userContainingBrowser= */ null); controller.setupLearnMoreButton(mContext); Loading Loading @@ -110,13 +157,33 @@ public class ManagedDeviceActionDisabledByAdminControllerTest { } private ManagedDeviceActionDisabledByAdminController createController() { return createController(/* url= */ null); return createController( /* url= */ null, /* foregroundUserChecker= */ true, mContext.getUser(), /* userContainingBrowser= */ null); } private ManagedDeviceActionDisabledByAdminController createController(String url) { return createController( url, /* foregroundUserChecker= */ true, mContext.getUser(), /* userContainingBrowser= */ null); } private ManagedDeviceActionDisabledByAdminController createController( String url, boolean isUserForeground, UserHandle preferredUserHandle, UserHandle userContainingBrowser) { ManagedDeviceActionDisabledByAdminController controller = new ManagedDeviceActionDisabledByAdminController( new FakeDeviceAdminStringProvider(url), mContext.getUser()); new FakeDeviceAdminStringProvider(url), preferredUserHandle, /* foregroundUserChecker= */ (context, userHandle) -> isUserForeground, /* resolveActivityChecker= */ (packageManager, __, userHandle) -> userHandle.equals(userContainingBrowser)); controller.initialize(mTestUtils.createLearnMoreButtonLauncher()); controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID); return controller; Loading