Merge "Revert "Revert "Revert "Revert "Launch admin policies settings screen if not possible to""" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
da685b239c
@@ -892,4 +892,26 @@
|
|||||||
column="59"/>
|
column="59"/>
|
||||||
</issue>
|
</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>
|
||||||
|
|
||||||
|
<issue
|
||||||
|
id="NewApi"
|
||||||
|
message="Call requires API level 31 (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>
|
</issues>
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ package com.android.settingslib.enterprise;
|
|||||||
|
|
||||||
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
|
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.app.admin.DevicePolicyManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.biometrics.BiometricAuthenticator;
|
import android.hardware.biometrics.BiometricAuthenticator;
|
||||||
@@ -43,7 +46,11 @@ public final class ActionDisabledByAdminControllerFactory {
|
|||||||
} else if (isFinancedDevice(context)) {
|
} else if (isFinancedDevice(context)) {
|
||||||
return new FinancedDeviceActionDisabledByAdminController(stringProvider);
|
return new FinancedDeviceActionDisabledByAdminController(stringProvider);
|
||||||
} else {
|
} else {
|
||||||
return new ManagedDeviceActionDisabledByAdminController(stringProvider, userHandle);
|
return new ManagedDeviceActionDisabledByAdminController(
|
||||||
|
stringProvider,
|
||||||
|
userHandle,
|
||||||
|
DEFAULT_FOREGROUND_USER_CHECKER,
|
||||||
|
DEFAULT_RESOLVE_ACTIVITY_CHECKER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import android.app.admin.DevicePolicyManager;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
@@ -34,6 +35,17 @@ import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
|||||||
*/
|
*/
|
||||||
public abstract class ActionDisabledLearnMoreButtonLauncher {
|
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
|
* Sets up a "learn more" button which shows a screen with device policy settings
|
||||||
*/
|
*/
|
||||||
@@ -111,6 +123,14 @@ public abstract class ActionDisabledLearnMoreButtonLauncher {
|
|||||||
finishSelf();
|
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) {
|
private void showAdminPolicies(Context context, EnforcedAdmin enforcedAdmin) {
|
||||||
if (enforcedAdmin.component != null) {
|
if (enforcedAdmin.component != null) {
|
||||||
launchShowAdminPolicies(context, enforcedAdmin.user, enforcedAdmin.component);
|
launchShowAdminPolicies(context, enforcedAdmin.user, enforcedAdmin.component);
|
||||||
|
|||||||
@@ -20,13 +20,14 @@ import static java.util.Objects.requireNonNull;
|
|||||||
|
|
||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import java.util.Objects;
|
import com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.ResolveActivityChecker;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,17 +36,37 @@ import java.util.Objects;
|
|||||||
final class ManagedDeviceActionDisabledByAdminController
|
final class ManagedDeviceActionDisabledByAdminController
|
||||||
extends BaseActionDisabledByAdminController {
|
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}
|
* 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(
|
ManagedDeviceActionDisabledByAdminController(
|
||||||
DeviceAdminStringProvider stringProvider,
|
DeviceAdminStringProvider stringProvider,
|
||||||
UserHandle userHandle) {
|
UserHandle preferredUserHandle,
|
||||||
|
ForegroundUserChecker foregroundUserChecker,
|
||||||
|
ResolveActivityChecker resolveActivityChecker) {
|
||||||
super(stringProvider);
|
super(stringProvider);
|
||||||
mUserHandle = requireNonNull(userHandle);
|
mPreferredUserHandle = requireNonNull(preferredUserHandle);
|
||||||
|
mForegroundUserChecker = requireNonNull(foregroundUserChecker);
|
||||||
|
mResolveActivityChecker = requireNonNull(resolveActivityChecker);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,14 +74,52 @@ final class ManagedDeviceActionDisabledByAdminController
|
|||||||
assertInitialized();
|
assertInitialized();
|
||||||
|
|
||||||
String url = mStringProvider.getLearnMoreHelpPageUrl();
|
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,
|
mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId,
|
||||||
mEnforcedAdmin);
|
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
|
@Override
|
||||||
public String getAdminSupportTitle(@Nullable String restriction) {
|
public String getAdminSupportTitle(@Nullable String restriction) {
|
||||||
if (restriction == null) {
|
if (restriction == null) {
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
@@ -45,9 +47,11 @@ import org.robolectric.android.controller.ActivityController;
|
|||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class ManagedDeviceActionDisabledByAdminControllerTest {
|
public class ManagedDeviceActionDisabledByAdminControllerTest {
|
||||||
|
|
||||||
|
private static UserHandle MANAGED_USER = UserHandle.of(123);
|
||||||
private static final String RESTRICTION = UserManager.DISALLOW_ADJUST_VOLUME;
|
private static final String RESTRICTION = UserManager.DISALLOW_ADJUST_VOLUME;
|
||||||
private static final String EMPTY_URL = "";
|
private static final String EMPTY_URL = "";
|
||||||
private static final String SUPPORT_TITLE_FOR_RESTRICTION = DISALLOW_ADJUST_VOLUME_TITLE;
|
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 Context mContext = ApplicationProvider.getApplicationContext();
|
||||||
private final Activity mActivity = ActivityController.of(new Activity()).get();
|
private final Activity mActivity = ActivityController.of(new Activity()).get();
|
||||||
@@ -60,8 +64,21 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setupLearnMoreButton_validUrl_negativeButtonSet() {
|
public void setupLearnMoreButton_noUrl_negativeButtonSet() {
|
||||||
ManagedDeviceActionDisabledByAdminController controller = createController(URL);
|
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);
|
controller.setupLearnMoreButton(mContext);
|
||||||
|
|
||||||
@@ -69,8 +86,38 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setupLearnMoreButton_noUrl_negativeButtonSet() {
|
public void setupLearnMoreButton_validUrl_browserInPreferredUser_notForeground_showsAdminPolicies() {
|
||||||
ManagedDeviceActionDisabledByAdminController controller = createController(EMPTY_URL);
|
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);
|
controller.setupLearnMoreButton(mContext);
|
||||||
|
|
||||||
@@ -110,13 +157,33 @@ public class ManagedDeviceActionDisabledByAdminControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ManagedDeviceActionDisabledByAdminController createController() {
|
private ManagedDeviceActionDisabledByAdminController createController() {
|
||||||
return createController(/* url= */ null);
|
return createController(
|
||||||
|
/* url= */ null,
|
||||||
|
/* foregroundUserChecker= */ true,
|
||||||
|
mContext.getUser(),
|
||||||
|
/* userContainingBrowser= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ManagedDeviceActionDisabledByAdminController createController(String url) {
|
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 =
|
ManagedDeviceActionDisabledByAdminController controller =
|
||||||
new ManagedDeviceActionDisabledByAdminController(
|
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.initialize(mTestUtils.createLearnMoreButtonLauncher());
|
||||||
controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
|
controller.updateEnforcedAdmin(ENFORCED_ADMIN, ENFORCEMENT_ADMIN_USER_ID);
|
||||||
return controller;
|
return controller;
|
||||||
|
|||||||
Reference in New Issue
Block a user