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

Commit b0270d89 authored by Sergey Volnov's avatar Sergey Volnov Committed by Automerger Merge Worker
Browse files

Merge "Revert "Revert "Revert "Launch admin policies settings screen if not...

Merge "Revert "Revert "Revert "Launch admin policies settings screen if not possible to"" into sc-dev am: a9949c86 am: 9d422053

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

Change-Id: Iedb2cf6da4b7c6b8ba290bd6b6ac47acae42750b
parents 9e0f4ac4 9d422053
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -892,15 +892,4 @@
            column="59"/>
    </issue>

    <issue
        id="NewApi"
        message="Call requires API level S (current min is 29): `android.os.UserManager#isUserForeground`"
        errorLine1="                .getSystemService(UserManager.class).isUserForeground();"
        errorLine2="                                                     ~~~~~~~~~~~~~~~~">
        <location
            file="frameworks/base/packages/SettingsLib/src/com/android/settingslib/enterprise/ManagedDeviceActionDisabledByAdminController.java"
            line="120"
            column="54"/>
    </issue>

</issues>
+1 −8
Original line number Diff line number Diff line
@@ -18,9 +18,6 @@ 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;
@@ -46,11 +43,7 @@ public final class ActionDisabledByAdminControllerFactory {
        } else if (isFinancedDevice(context)) {
            return new FinancedDeviceActionDisabledByAdminController(stringProvider);
        } else {
            return new ManagedDeviceActionDisabledByAdminController(
                    stringProvider,
                    userHandle,
                    DEFAULT_FOREGROUND_USER_CHECKER,
                    DEFAULT_RESOLVE_ACTIVITY_CHECKER);
            return new ManagedDeviceActionDisabledByAdminController(stringProvider, userHandle);
        }
    }

+0 −20
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ 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;
@@ -35,17 +34,6 @@ 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
     */
@@ -123,14 +111,6 @@ 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);
+8 −67
Original line number Diff line number Diff line
@@ -20,14 +20,13 @@ 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 com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.ResolveActivityChecker;
import java.util.Objects;


/**
@@ -36,37 +35,17 @@ import com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.
final class ManagedDeviceActionDisabledByAdminController
        extends BaseActionDisabledByAdminController {

    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;
    private final UserHandle mUserHandle;

    /**
     * Constructs a {@link ManagedDeviceActionDisabledByAdminController}
     * @param preferredUserHandle - user on which to launch the help web page, if necessary
     * @param userHandle - user on which to launch the help web page, if necessary
     */
    ManagedDeviceActionDisabledByAdminController(
            DeviceAdminStringProvider stringProvider,
            UserHandle preferredUserHandle,
            ForegroundUserChecker foregroundUserChecker,
            ResolveActivityChecker resolveActivityChecker) {
            UserHandle userHandle) {
        super(stringProvider);
        mPreferredUserHandle = requireNonNull(preferredUserHandle);
        mForegroundUserChecker = requireNonNull(foregroundUserChecker);
        mResolveActivityChecker = requireNonNull(resolveActivityChecker);
        mUserHandle = requireNonNull(userHandle);
    }

    @Override
@@ -74,52 +53,14 @@ final class ManagedDeviceActionDisabledByAdminController
        assertInitialized();

        String url = mStringProvider.getLearnMoreHelpPageUrl();

        if (!TextUtils.isEmpty(url)
                && canLaunchHelpPageInPreferredOrCurrentUser(context, url, mPreferredUserHandle)) {
            setupLearnMoreButtonToLaunchHelpPage(context, url, mPreferredUserHandle);
        } else {
        if (TextUtils.isEmpty(url)) {
            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) {
+6 −73
Original line number Diff line number Diff line
@@ -30,8 +30,6 @@ 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;
@@ -47,11 +45,9 @@ 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();
@@ -64,21 +60,8 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
    }

    @Test
    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);
    public void setupLearnMoreButton_validUrl_negativeButtonSet() {
        ManagedDeviceActionDisabledByAdminController controller = createController(URL);

        controller.setupLearnMoreButton(mContext);

@@ -86,38 +69,8 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
    }

    @Test
    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);
    public void setupLearnMoreButton_noUrl_negativeButtonSet() {
        ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL);

        controller.setupLearnMoreButton(mContext);

@@ -157,33 +110,13 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
    }

    private ManagedDeviceActionDisabledByAdminController createController() {
        return createController(
                /* url= */ null,
                /* foregroundUserChecker= */ true,
                mContext.getUser(),
                /* userContainingBrowser= */ null);
        return createController(/* url= */ 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),
                        preferredUserHandle,
                        /* foregroundUserChecker= */ (context, userHandle) -> isUserForeground,
                        /* resolveActivityChecker= */ (packageManager, __, userHandle) ->
                                userHandle.equals(userContainingBrowser));
                        new FakeDeviceAdminStringProvider(url), mContext.getUser());
        controller.initialize(mTestUtils.createLearnMoreButtonLauncher());
        controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
        return controller;