Loading src/com/android/settings/Utils.java +16 −0 Original line number Diff line number Diff line Loading @@ -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; Loading src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java +4 −3 Original line number Diff line number Diff line Loading @@ -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 = Loading Loading @@ -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); } } Loading tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java +10 −11 Original line number Diff line number Diff line Loading @@ -32,7 +32,6 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Flags; import android.os.UserHandle; import android.platform.test.flag.junit.SetFlagsRule; import androidx.fragment.app.Fragment; Loading Loading @@ -177,8 +176,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return null; public boolean isManagedProfilePresent(Context context) { return false; } }); assertThat(fragments).hasLength(1); Loading @@ -201,8 +200,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return null; public boolean isManagedProfilePresent(Context context) { return false; } }); assertThat(fragments).hasLength(2); Loading @@ -225,8 +224,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return new UserHandle(123); public boolean isManagedProfilePresent(Context context) { return true; } }); assertThat(fragments).hasLength(2); Loading @@ -249,8 +248,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return new UserHandle(123); public boolean isManagedProfilePresent(Context context) { return true; } }); assertThat(fragments).hasLength(3); Loading @@ -274,8 +273,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return new UserHandle(123); public boolean isManagedProfilePresent(Context context) { return true; } }); assertThat(fragments).hasLength(3); Loading Loading
src/com/android/settings/Utils.java +16 −0 Original line number Diff line number Diff line Loading @@ -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; Loading
src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java +4 −3 Original line number Diff line number Diff line Loading @@ -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 = Loading Loading @@ -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); } } Loading
tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java +10 −11 Original line number Diff line number Diff line Loading @@ -32,7 +32,6 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Flags; import android.os.UserHandle; import android.platform.test.flag.junit.SetFlagsRule; import androidx.fragment.app.Fragment; Loading Loading @@ -177,8 +176,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return null; public boolean isManagedProfilePresent(Context context) { return false; } }); assertThat(fragments).hasLength(1); Loading @@ -201,8 +200,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return null; public boolean isManagedProfilePresent(Context context) { return false; } }); assertThat(fragments).hasLength(2); Loading @@ -225,8 +224,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return new UserHandle(123); public boolean isManagedProfilePresent(Context context) { return true; } }); assertThat(fragments).hasLength(2); Loading @@ -249,8 +248,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return new UserHandle(123); public boolean isManagedProfilePresent(Context context) { return true; } }); assertThat(fragments).hasLength(3); Loading @@ -274,8 +273,8 @@ public class ProfileSelectFragmentTest { }, new ProfileSelectFragment.ManagedProfileInfoProvider() { @Override public UserHandle getManagedProfile(Context context) { return new UserHandle(123); public boolean isManagedProfilePresent(Context context) { return true; } }); assertThat(fragments).hasLength(3); Loading