Fix Settings getProfileIdsWithDisabled usage
In two places, Settings uses getProfileIdsWithDisabled intending to
restrict itself to *managed* profiles, but actually allows for any type
of profile.
Since the declared intent is to only deal with managed profiles, we
update the code to only consider managed profiles.
On devices that only have managed profiles (currently almost all
devices), this cl is a no-op.
Bug: 230495929
Bug: 230534572
Bug: 170249807
Test: com.android.settings.UtilsTest
Test: make RunSettingsRoboTests -j
Change-Id: Id04d45839ef61080b00ca2f91525718cb3a85120
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 2988ddc..990667e 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -85,7 +85,6 @@
import android.text.format.DateUtils;
import android.text.style.TtsSpan;
import android.util.ArraySet;
-import android.util.FeatureFlagUtils;
import android.util.IconDrawableFactory;
import android.util.Log;
import android.view.LayoutInflater;
@@ -435,18 +434,20 @@
* {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
*/
public static UserHandle getManagedProfileWithDisabled(UserManager userManager) {
- // TODO: Call getManagedProfileId from here once Robolectric supports
- // API level 24 and UserManager.getProfileIdsWithDisabled can be Mocked (to avoid having
- // yet another implementation that loops over user profiles in this method). In the meantime
- // we need to use UserManager.getProfiles that is available on API 23 (the one currently
- // used for Settings Robolectric tests).
- final int myUserId = UserHandle.myUserId();
- final List<UserInfo> profiles = userManager.getProfiles(myUserId);
+ return getManagedProfileWithDisabled(userManager, UserHandle.myUserId());
+ }
+
+ /**
+ * Returns the managed profile of the given user or {@code null} if none is found. Unlike
+ * {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
+ */
+ private static UserHandle getManagedProfileWithDisabled(UserManager um, int parentUserId) {
+ final List<UserInfo> profiles = um.getProfiles(parentUserId);
final int count = profiles.size();
for (int i = 0; i < count; i++) {
final UserInfo profile = profiles.get(i);
if (profile.isManagedProfile()
- && profile.getUserHandle().getIdentifier() != myUserId) {
+ && profile.getUserHandle().getIdentifier() != parentUserId) {
return profile.getUserHandle();
}
}
@@ -455,15 +456,14 @@
/**
* Retrieves the id for the given user's managed profile.
+ * Unlike {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
*
* @return the managed profile id or UserHandle.USER_NULL if there is none.
*/
public static int getManagedProfileId(UserManager um, int parentUserId) {
- final int[] profileIds = um.getProfileIdsWithDisabled(parentUserId);
- for (int profileId : profileIds) {
- if (profileId != parentUserId) {
- return profileId;
- }
+ final UserHandle profile = getManagedProfileWithDisabled(um, parentUserId);
+ if (profile != null) {
+ return profile.getIdentifier();
}
return UserHandle.USER_NULL;
}