Merge "Include disabled managed profiles in Accounts page" into oc-dev
am: 92f78354f9
Change-Id: I3dc5fd6bb4c7466b204946db179b10d9a3c9ccdf
This commit is contained in:
@@ -603,7 +603,8 @@ public final class Utils extends com.android.settingslib.Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the managed profile of the current user or null if none found.
|
||||
* Returns the managed profile of the current user or {@code null} if none is found or a profile
|
||||
* exists but it is disabled.
|
||||
*/
|
||||
public static UserHandle getManagedProfile(UserManager userManager) {
|
||||
List<UserHandle> userProfiles = userManager.getUserProfiles();
|
||||
@@ -621,6 +622,29 @@ public final class Utils extends com.android.settingslib.Utils {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the managed profile of the current user or {@code null} if none is found. Unlike
|
||||
* {@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();
|
||||
List<UserInfo> profiles = userManager.getProfiles(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() != myUserId) {
|
||||
return profile.getUserHandle();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the id for the given user's managed profile.
|
||||
*
|
||||
|
@@ -27,7 +27,7 @@ public class AutoSyncWorkDataPreferenceController extends AutoSyncPersonalDataPr
|
||||
|
||||
public AutoSyncWorkDataPreferenceController(Context context, Fragment parent) {
|
||||
super(context, parent);
|
||||
mUserHandle = Utils.getManagedProfile(mUserManager);
|
||||
mUserHandle = Utils.getManagedProfileWithDisabled(mUserManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -21,6 +21,7 @@ import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Fragment;
|
||||
@@ -46,6 +47,8 @@ import java.util.List;
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AutoSyncWorkDataPreferenceControllerTest {
|
||||
|
||||
private static int MANAGED_PROFILE_ID = 10;
|
||||
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private UserManager mUserManager;
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
@@ -80,29 +83,30 @@ public class AutoSyncWorkDataPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void checkIsAvailable_singleUserProfile_shouldNotDisplay() {
|
||||
final List<UserInfo> infos = new ArrayList<>();
|
||||
infos.add(new UserInfo(1, "user 1", 0));
|
||||
when(mUserManager.isManagedProfile()).thenReturn(false);
|
||||
when(mUserManager.isLinkedUser()).thenReturn(false);
|
||||
when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
|
||||
|
||||
final List<UserInfo> infos = new ArrayList<>();
|
||||
infos.add(new UserInfo(UserHandle.USER_SYSTEM, "user 1", 0 /* flags */));
|
||||
when(mUserManager.getProfiles(eq(UserHandle.USER_SYSTEM))).thenReturn(infos);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleProfile_shouldInitWithWorkProfileUserHandle() {
|
||||
final int id1 = 1;
|
||||
final int id2 = 2;
|
||||
final UserInfo managedUser = new UserInfo(id2, "user 2", FLAG_MANAGED_PROFILE);
|
||||
final List<UserHandle> infos = new ArrayList<>();
|
||||
infos.add(new UserHandle(id1));
|
||||
infos.add(new UserHandle(id2));
|
||||
when(mUserManager.getUserProfiles()).thenReturn(infos);
|
||||
when(mUserManager.getUserHandle()).thenReturn(id1);
|
||||
when(mUserManager.getUserInfo(id2)).thenReturn(managedUser);
|
||||
when(mUserManager.isManagedProfile()).thenReturn(false);
|
||||
when(mUserManager.isLinkedUser()).thenReturn(false);
|
||||
|
||||
final List<UserInfo> infos = new ArrayList<>();
|
||||
infos.add(new UserInfo(UserHandle.USER_SYSTEM, "user 1", 0 /* flags */));
|
||||
infos.add(new UserInfo(
|
||||
MANAGED_PROFILE_ID, "work profile", UserInfo.FLAG_MANAGED_PROFILE));
|
||||
when(mUserManager.getProfiles(eq(UserHandle.USER_SYSTEM))).thenReturn(infos);
|
||||
|
||||
mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
|
||||
|
||||
assertThat(mController.mUserHandle.getIdentifier()).isEqualTo(id2);
|
||||
assertThat(mController.mUserHandle.getIdentifier()).isEqualTo(MANAGED_PROFILE_ID);
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user