Merge "Check purely for the presence of a managed profile" into main

This commit is contained in:
Manish Singh
2023-11-30 20:51:06 +00:00
committed by Android (Google) Code Review
3 changed files with 30 additions and 14 deletions

View File

@@ -409,6 +409,22 @@ public final class Utils extends com.android.settingslib.Utils {
return null;
}
/**
* Returns true if a profile of specified userType exists. Note that it considers all profiles,
* including the disabled profiles and the parent user itself.
*/
public static boolean doesProfileOfTypeExists(
@NonNull UserManager userManager, @ProfileType int userType) {
final List<UserInfo> userProfiles = userManager.getProfiles(UserHandle.myUserId());
String umUserType = getUmUserType(userType);
for (UserInfo profile : userProfiles) {
if (Objects.equals(umUserType, profile.userType)) {
return true;
}
}
return false;
}
private static String getUmUserType(@ProfileType int userType) throws IllegalArgumentException {
if (userType == ProfileType.WORK) {
return USER_TYPE_PROFILE_MANAGED;

View File

@@ -322,7 +322,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
personalFragment.setArguments(personalOnly);
fragments.add(personalFragment);
if (managedProfileInfoProvider.getManagedProfile(context) != null) {
if (managedProfileInfoProvider.isManagedProfilePresent(context)) {
final Bundle workOnly = bundle != null ? bundle.deepCopy() : new Bundle();
workOnly.putInt(EXTRA_PROFILE, ProfileType.WORK);
final Fragment workFragment =
@@ -361,8 +361,9 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
}
interface ManagedProfileInfoProvider {
default UserHandle getManagedProfile(Context context) {
return Utils.getManagedProfile(context.getSystemService(UserManager.class));
default boolean isManagedProfilePresent(Context context) {
return Utils.doesProfileOfTypeExists(
context.getSystemService(UserManager.class), ProfileType.WORK);
}
}