Check purely for the presence of a managed profile
including the parent user, as here we care only about the existence of
the managed profile.
This maintains the behaviour prior to private space changes.
Bug: 313128792
Test: manual
Test: atest ProfileSelectFragmentTest
Change-Id: I1d253f95ca534b48acfdb4c1c8b84368d731dded
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 2113b5d..a4466fb 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -467,6 +467,22 @@
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;
diff --git a/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java b/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java
index dc1b792..a70d7d8 100644
--- a/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java
+++ b/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java
@@ -322,7 +322,7 @@
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 @@
}
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);
}
}
diff --git a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java
index 0f0de56..302c8f3 100644
--- a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java
@@ -32,7 +32,6 @@
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;
@@ -177,8 +176,8 @@
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
- public UserHandle getManagedProfile(Context context) {
- return null;
+ public boolean isManagedProfilePresent(Context context) {
+ return false;
}
});
assertThat(fragments).hasLength(1);
@@ -201,8 +200,8 @@
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
- public UserHandle getManagedProfile(Context context) {
- return null;
+ public boolean isManagedProfilePresent(Context context) {
+ return false;
}
});
assertThat(fragments).hasLength(2);
@@ -225,8 +224,8 @@
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
- public UserHandle getManagedProfile(Context context) {
- return new UserHandle(123);
+ public boolean isManagedProfilePresent(Context context) {
+ return true;
}
});
assertThat(fragments).hasLength(2);
@@ -249,8 +248,8 @@
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
- public UserHandle getManagedProfile(Context context) {
- return new UserHandle(123);
+ public boolean isManagedProfilePresent(Context context) {
+ return true;
}
});
assertThat(fragments).hasLength(3);
@@ -274,8 +273,8 @@
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
- public UserHandle getManagedProfile(Context context) {
- return new UserHandle(123);
+ public boolean isManagedProfilePresent(Context context) {
+ return true;
}
});
assertThat(fragments).hasLength(3);