Make the order of tabs same as profile creation

Currently the order is hardcoded to be Primary, Work, Private.
But SettingsLib in frameworks uses the order returned by UserManager's
getProfiles API. This causes inconsistency across various Settings
pages.

Changing the order to be the one returned by UserManager.

Bug: 304366476
Test: manual
Test: atest ProfileSelectFragmentTest
Change-Id: I9953f211bfbeab129aa6b7c7ec0fe294e91dda26
This commit is contained in:
Manish Singh
2023-11-09 20:59:03 +00:00
parent d503f25692
commit 9ee5f6edc9
2 changed files with 83 additions and 70 deletions

View File

@@ -57,6 +57,7 @@ import com.google.android.material.tabs.TabLayoutMediator;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
/**
* Base fragment class for profile settings.
@@ -295,8 +296,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
personalFragmentConstructor,
workFragmentConstructor,
privateFragmentConstructor,
new PrivateSpaceInfoProvider() {},
new ManagedProfileInfoProvider() {});
new PrivateSpaceInfoProvider() {});
}
/**
@@ -309,36 +309,35 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
FragmentConstructor personalFragmentConstructor,
FragmentConstructor workFragmentConstructor,
FragmentConstructor privateFragmentConstructor,
PrivateSpaceInfoProvider privateSpaceInfoProvider,
ManagedProfileInfoProvider managedProfileInfoProvider) {
PrivateSpaceInfoProvider privateSpaceInfoProvider) {
Fragment[] result = new Fragment[0];
ArrayList<Fragment> fragments = new ArrayList<>();
try {
final Bundle personalOnly = bundle != null ? bundle : new Bundle();
personalOnly.putInt(EXTRA_PROFILE, ProfileType.PERSONAL);
final Fragment personalFragment =
personalFragmentConstructor.constructAndGetFragment();
personalFragment.setArguments(personalOnly);
fragments.add(personalFragment);
UserManager userManager = context.getSystemService(UserManager.class);
List<UserInfo> userInfos = userManager.getProfiles(UserHandle.myUserId());
if (managedProfileInfoProvider.isManagedProfilePresent(context)) {
final Bundle workOnly = bundle != null ? bundle.deepCopy() : new Bundle();
workOnly.putInt(EXTRA_PROFILE, ProfileType.WORK);
final Fragment workFragment =
workFragmentConstructor.constructAndGetFragment();
workFragment.setArguments(workOnly);
fragments.add(workFragment);
for (UserInfo userInfo : userInfos) {
if (userInfo.getUserHandle().isSystem()) {
fragments.add(createAndGetFragment(
ProfileType.PERSONAL,
bundle != null ? bundle : new Bundle(),
personalFragmentConstructor));
} else if (userInfo.isManagedProfile()) {
fragments.add(createAndGetFragment(
ProfileType.WORK,
bundle != null ? bundle.deepCopy() : new Bundle(),
workFragmentConstructor));
} else if (Flags.allowPrivateProfile() && userInfo.isPrivateProfile()) {
if (!privateSpaceInfoProvider.isPrivateSpaceLocked(context)) {
fragments.add(createAndGetFragment(
ProfileType.PRIVATE,
bundle != null ? bundle.deepCopy() : new Bundle(),
privateFragmentConstructor));
}
} else {
Log.d(TAG, "Not showing tab for unsupported user");
}
if (Flags.allowPrivateProfile()
&& !privateSpaceInfoProvider.isPrivateSpaceLocked(context)) {
final Bundle privateOnly = bundle != null ? bundle.deepCopy() : new Bundle();
privateOnly.putInt(EXTRA_PROFILE, ProfileType.PRIVATE);
final Fragment privateFragment =
privateFragmentConstructor.constructAndGetFragment();
privateFragment.setArguments(privateOnly);
fragments.add(privateFragment);
}
result = new Fragment[fragments.size()];
@@ -350,6 +349,14 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
return result;
}
private static Fragment createAndGetFragment(
@ProfileType int profileType, Bundle bundle, FragmentConstructor fragmentConstructor) {
bundle.putInt(EXTRA_PROFILE, profileType);
final Fragment fragment = fragmentConstructor.constructAndGetFragment();
fragment.setArguments(bundle);
return fragment;
}
interface FragmentConstructor {
Fragment constructAndGetFragment();
}
@@ -360,13 +367,6 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
}
}
interface ManagedProfileInfoProvider {
default boolean isManagedProfilePresent(Context context) {
return Utils.doesProfileOfTypeExists(
context.getSystemService(UserManager.class), ProfileType.WORK);
}
}
static class ViewPagerAdapter extends FragmentStateAdapter {
private final Fragment[] mChildFragments;

View File

@@ -17,6 +17,9 @@
package com.android.settings.dashboard.profileselector;
import static android.content.Intent.EXTRA_USER_ID;
import static android.os.UserManager.USER_TYPE_FULL_SYSTEM;
import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED;
import static android.os.UserManager.USER_TYPE_PROFILE_PRIVATE;
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.EXTRA_PROFILE;
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.PERSONAL_TAB;
@@ -30,9 +33,11 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.Bundle;
import android.os.Flags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.util.ArraySet;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
@@ -51,7 +56,9 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@RunWith(RobolectricTestRunner.class)
@@ -60,6 +67,9 @@ import java.util.Set;
com.android.settings.testutils.shadow.ShadowFragment.class,
})
public class ProfileSelectFragmentTest {
private static final String PRIMARY_USER_NAME = "primary";
private static final String MANAGED_USER_NAME = "managed";
private static final String PRIVATE_USER_NAME = "private";
private Context mContext;
private TestProfileSelectFragment mFragment;
@@ -151,6 +161,8 @@ public class ProfileSelectFragmentTest {
@Test
public void testGetFragments_whenOnlyPersonal_returnsOneFragment() {
mSetFlagsRule.disableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE);
mUserManager.addProfile(
new UserInfo(0, PRIMARY_USER_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
Fragment[] fragments = ProfileSelectFragment.getFragments(
mContext,
null /* bundle */,
@@ -162,6 +174,10 @@ public class ProfileSelectFragmentTest {
@Test
public void testGetFragments_whenPrivateDisabled_returnsOneFragment() {
mUserManager.addProfile(
new UserInfo(0, PRIMARY_USER_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
mUserManager.addProfile(
new UserInfo(11, PRIVATE_USER_NAME, null, 0, USER_TYPE_PROFILE_PRIVATE));
Fragment[] fragments = ProfileSelectFragment.getFragments(
mContext,
null /* bundle */,
@@ -173,12 +189,6 @@ public class ProfileSelectFragmentTest {
public boolean isPrivateSpaceLocked(Context context) {
return true;
}
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
public boolean isManagedProfilePresent(Context context) {
return false;
}
});
assertThat(fragments).hasLength(1);
}
@@ -186,6 +196,10 @@ public class ProfileSelectFragmentTest {
@Test
public void testGetFragments_whenPrivateEnabled_returnsTwoFragments() {
mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE);
mUserManager.addProfile(
new UserInfo(0, PRIMARY_USER_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
mUserManager.addProfile(
new UserInfo(11, PRIVATE_USER_NAME, null, 0, USER_TYPE_PROFILE_PRIVATE));
Fragment[] fragments = ProfileSelectFragment.getFragments(
mContext,
null /* bundle */,
@@ -197,12 +211,6 @@ public class ProfileSelectFragmentTest {
public boolean isPrivateSpaceLocked(Context context) {
return false;
}
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
public boolean isManagedProfilePresent(Context context) {
return false;
}
});
assertThat(fragments).hasLength(2);
}
@@ -210,6 +218,10 @@ public class ProfileSelectFragmentTest {
@Test
public void testGetFragments_whenManagedProfile_returnsTwoFragments() {
mSetFlagsRule.disableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE);
mUserManager.addProfile(
new UserInfo(0, PRIMARY_USER_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
mUserManager.addProfile(
new UserInfo(10, MANAGED_USER_NAME, null, 0, USER_TYPE_PROFILE_MANAGED));
Fragment[] fragments = ProfileSelectFragment.getFragments(
mContext,
null /* bundle */,
@@ -221,12 +233,6 @@ public class ProfileSelectFragmentTest {
public boolean isPrivateSpaceLocked(Context context) {
return false;
}
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
public boolean isManagedProfilePresent(Context context) {
return true;
}
});
assertThat(fragments).hasLength(2);
}
@@ -234,6 +240,12 @@ public class ProfileSelectFragmentTest {
@Test
public void testGetFragments_whenAllProfiles_returnsThreeFragments() {
mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE);
mUserManager.addProfile(
new UserInfo(0, PRIMARY_USER_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
mUserManager.addProfile(
new UserInfo(10, MANAGED_USER_NAME, null, 0, USER_TYPE_PROFILE_MANAGED));
mUserManager.addProfile(
new UserInfo(11, PRIVATE_USER_NAME, null, 0, USER_TYPE_PROFILE_PRIVATE));
Fragment[] fragments = ProfileSelectFragment.getFragments(
mContext,
null /* bundle */,
@@ -245,12 +257,6 @@ public class ProfileSelectFragmentTest {
public boolean isPrivateSpaceLocked(Context context) {
return false;
}
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
public boolean isManagedProfilePresent(Context context) {
return true;
}
});
assertThat(fragments).hasLength(3);
}
@@ -258,6 +264,12 @@ public class ProfileSelectFragmentTest {
@Test
public void testGetFragments_whenAvailableBundle_returnsFragmentsWithCorrectBundles() {
mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE);
mUserManager.addProfile(
new UserInfo(0, PRIMARY_USER_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
mUserManager.addProfile(
new UserInfo(10, MANAGED_USER_NAME, null, 0, USER_TYPE_PROFILE_MANAGED));
mUserManager.addProfile(
new UserInfo(11, PRIVATE_USER_NAME, null, 0, USER_TYPE_PROFILE_PRIVATE));
Bundle bundle = new Bundle();
Fragment[] fragments = ProfileSelectFragment.getFragments(
mContext,
@@ -270,20 +282,21 @@ public class ProfileSelectFragmentTest {
public boolean isPrivateSpaceLocked(Context context) {
return false;
}
},
new ProfileSelectFragment.ManagedProfileInfoProvider() {
@Override
public boolean isManagedProfilePresent(Context context) {
return true;
}
});
assertThat(fragments).hasLength(3);
assertThat(fragments[0].getArguments().getInt(EXTRA_PROFILE))
.isEqualTo(ProfileSelectFragment.ProfileType.PERSONAL);
assertThat(fragments[1].getArguments().getInt(EXTRA_PROFILE))
.isEqualTo(ProfileSelectFragment.ProfileType.WORK);
assertThat(fragments[2].getArguments().getInt(EXTRA_PROFILE))
.isEqualTo(ProfileSelectFragment.ProfileType.PRIVATE);
List<Integer> foundProfileTypesList = new ArrayList<>();
for (Fragment fragment : fragments) {
foundProfileTypesList.add(fragment.getArguments().getInt(EXTRA_PROFILE));
}
assertThat(foundProfileTypesList).hasSize(3);
Set<Integer> foundProfileTypes = new ArraySet<>(foundProfileTypesList);
assertThat(foundProfileTypes).containsExactly(
ProfileSelectFragment.ProfileType.PERSONAL,
ProfileSelectFragment.ProfileType.WORK,
ProfileSelectFragment.ProfileType.PRIVATE);
}
public static class TestProfileSelectFragment extends ProfileSelectFragment {