Merge "Fix Settings getProfileIdsWithDisabled usage"

This commit is contained in:
TreeHugger Robot
2022-05-06 03:16:37 +00:00
committed by Android (Google) Code Review
7 changed files with 57 additions and 36 deletions

View File

@@ -434,18 +434,20 @@ public final class Utils extends com.android.settingslib.Utils {
* {@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();
}
}
@@ -454,15 +456,14 @@ public final class Utils extends com.android.settingslib.Utils {
/**
* 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;
}

View File

@@ -22,6 +22,7 @@ import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFIC
import android.app.KeyguardManager;
import android.content.Context;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.Looper;
@@ -42,6 +43,8 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import java.util.List;
/**
* The controller of the sensitive notifications.
*/
@@ -74,11 +77,13 @@ public class RedactNotificationPreferenceController extends TogglePreferenceCont
mKm = context.getSystemService(KeyguardManager.class);
mProfileUserId = UserHandle.myUserId();
final int[] profileIds = mUm.getProfileIdsWithDisabled(UserHandle.myUserId());
for (int profileId : profileIds) {
if (profileId != UserHandle.myUserId()) {
mProfileUserId = profileId;
final List<UserInfo> profiles = mUm.getProfiles(UserHandle.myUserId());
final int count = profiles.size();
for (int i = 0; i < count; i++) {
final UserInfo profile = profiles.get(i);
if (profile.isManagedProfile()
&& profile.getUserHandle().getIdentifier() != UserHandle.myUserId()) {
mProfileUserId = profile.getUserHandle().getIdentifier();
}
}
}