Add config flag in Settings to control whether restricted profiles are offered.

This adds a config resource to specify whether restricted profiles
should be offered as an option when a new user is added. This replaces
the previous check if a device is voice capable, and will be defaulted
to false.

Bug: 202854971
Test: make RunSettingsRoboTests -j40 ROBOTEST_FILTER="com.android.settings.users.UserCapabilitiesTest"
Change-Id: If090fe8d902d6521acfde8c26e801aa4fc4f5ff4
This commit is contained in:
Oli Lan
2022-01-11 12:45:28 +00:00
parent 6649b9b0eb
commit 8aec687b29
4 changed files with 81 additions and 8 deletions

View File

@@ -560,4 +560,7 @@
<!-- Whether to aggregate for network selection list-->
<bool name="config_network_selection_list_aggregation_enabled">false</bool>
<!-- Whether to give option to add restricted profiles -->
<bool name="config_offer_restricted_profiles">false</bool>
</resources>

View File

@@ -23,6 +23,7 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
@@ -30,7 +31,7 @@ import com.android.settingslib.RestrictedLockUtilsInternal;
public class UserCapabilities {
boolean mEnabled = true;
boolean mCanAddUser = true;
boolean mCanAddRestrictedProfile = true;
boolean mCanAddRestrictedProfile;
boolean mIsAdmin;
boolean mIsGuest;
boolean mUserSwitcherEnabled;
@@ -57,12 +58,13 @@ public class UserCapabilities {
caps.mIsAdmin = myUserInfo.isAdmin();
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
// No restricted profiles for tablets with a device owner, or phones.
if (dpm.isDeviceManaged()
|| Utils.isVoiceCapable(context)
|| !userManager.isUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED)) {
caps.mCanAddRestrictedProfile = false;
}
boolean offerRestricted =
context.getResources().getBoolean(R.bool.config_offer_restricted_profiles);
caps.mCanAddRestrictedProfile =
offerRestricted && !dpm.isDeviceManaged() && userManager.isUserTypeEnabled(
UserManager.USER_TYPE_FULL_RESTRICTED);
caps.updateAddUserCapabilities(context);
return caps;
}

View File

@@ -49,6 +49,7 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
private final List<UserInfo> mUserProfileInfos = new ArrayList<>();
private final Set<Integer> mManagedProfiles = new HashSet<>();
private final Set<String> mEnabledTypes = new HashSet<>();
private boolean mIsQuietModeEnabled = false;
private int[] profileIdsForUser = new int[0];
private boolean mUserSwitchEnabled;
@@ -105,6 +106,11 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
mGuestRestrictions.add(restriction);
}
@Implementation
protected boolean hasUserRestriction(String restrictionKey) {
return hasUserRestriction(restrictionKey, UserHandle.of(UserHandle.myUserId()));
}
public static ShadowUserManager getShadow() {
return (ShadowUserManager) Shadow.extract(
RuntimeEnvironment.application.getSystemService(UserManager.class));
@@ -199,4 +205,17 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
public void setSwitchabilityStatus(@UserManager.UserSwitchabilityResult int newStatus) {
mSwitchabilityStatus = newStatus;
}
@Implementation
protected boolean isUserTypeEnabled(String userType) {
return mEnabledTypes.contains(userType);
}
public void setUserTypeEnabled(String type, boolean enabled) {
if (enabled) {
mEnabledTypes.add(type);
} else {
mEnabledTypes.remove(type);
}
}
}

View File

@@ -18,12 +18,17 @@ package com.android.settings.users;
import static com.google.common.truth.Truth.assertThat;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.R;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -32,16 +37,24 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowUserManager.class})
@Config(shadows = {ShadowUserManager.class, ShadowDevicePolicyManager.class,
SettingsShadowResources.class})
public class UserCapabilitiesTest {
private Context mContext;
private ShadowUserManager mUserManager;
private ShadowDevicePolicyManager mDpm;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mUserManager = ShadowUserManager.getShadow();
mDpm = ShadowDevicePolicyManager.getShadow();
}
@After
public void tearDown() {
SettingsShadowResources.reset();
}
@Test
@@ -85,4 +98,40 @@ public class UserCapabilitiesTest {
assertThat(userCapabilities.mUserSwitcherEnabled).isTrue();
}
@Test
public void restrictedProfile_enabled() {
mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED, true);
mDpm.setDeviceOwner(null);
SettingsShadowResources.overrideResource(R.bool.config_offer_restricted_profiles, true);
final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
assertThat(userCapabilities.mCanAddRestrictedProfile).isTrue();
}
@Test
public void restrictedProfile_configNotSet() {
mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED, true);
mDpm.setDeviceOwner(null);
SettingsShadowResources.overrideResource(R.bool.config_offer_restricted_profiles, false);
final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
assertThat(userCapabilities.mCanAddRestrictedProfile).isFalse();
}
@Test
public void restrictedProfile_deviceIsManaged() {
mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED, true);
mDpm.setDeviceOwner(new ComponentName("test", "test"));
SettingsShadowResources.overrideResource(R.bool.config_offer_restricted_profiles, true);
final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
assertThat(userCapabilities.mCanAddRestrictedProfile).isFalse();
}
@Test
public void restrictedProfile_typeNotEnabled() {
mUserManager.setUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED, false);
mDpm.setDeviceOwner(null);
SettingsShadowResources.overrideResource(R.bool.config_offer_restricted_profiles, true);
final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
assertThat(userCapabilities.mCanAddRestrictedProfile).isFalse();
}
}