Merge "Make the order of tabs same as profile creation" into main

This commit is contained in:
Manish Singh
2023-12-07 13:26:19 +00:00
committed by Android (Google) Code Review
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.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* Base fragment class for profile settings. * Base fragment class for profile settings.
@@ -295,8 +296,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
personalFragmentConstructor, personalFragmentConstructor,
workFragmentConstructor, workFragmentConstructor,
privateFragmentConstructor, privateFragmentConstructor,
new PrivateSpaceInfoProvider() {}, new PrivateSpaceInfoProvider() {});
new ManagedProfileInfoProvider() {});
} }
/** /**
@@ -309,36 +309,35 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
FragmentConstructor personalFragmentConstructor, FragmentConstructor personalFragmentConstructor,
FragmentConstructor workFragmentConstructor, FragmentConstructor workFragmentConstructor,
FragmentConstructor privateFragmentConstructor, FragmentConstructor privateFragmentConstructor,
PrivateSpaceInfoProvider privateSpaceInfoProvider, PrivateSpaceInfoProvider privateSpaceInfoProvider) {
ManagedProfileInfoProvider managedProfileInfoProvider) {
Fragment[] result = new Fragment[0]; Fragment[] result = new Fragment[0];
ArrayList<Fragment> fragments = new ArrayList<>(); ArrayList<Fragment> fragments = new ArrayList<>();
try { try {
final Bundle personalOnly = bundle != null ? bundle : new Bundle(); UserManager userManager = context.getSystemService(UserManager.class);
personalOnly.putInt(EXTRA_PROFILE, ProfileType.PERSONAL); List<UserInfo> userInfos = userManager.getProfiles(UserHandle.myUserId());
final Fragment personalFragment =
personalFragmentConstructor.constructAndGetFragment();
personalFragment.setArguments(personalOnly);
fragments.add(personalFragment);
if (managedProfileInfoProvider.isManagedProfilePresent(context)) { for (UserInfo userInfo : userInfos) {
final Bundle workOnly = bundle != null ? bundle.deepCopy() : new Bundle(); if (userInfo.getUserHandle().isSystem()) {
workOnly.putInt(EXTRA_PROFILE, ProfileType.WORK); fragments.add(createAndGetFragment(
final Fragment workFragment = ProfileType.PERSONAL,
workFragmentConstructor.constructAndGetFragment(); bundle != null ? bundle : new Bundle(),
workFragment.setArguments(workOnly); personalFragmentConstructor));
fragments.add(workFragment); } 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()]; result = new Fragment[fragments.size()];
@@ -350,6 +349,14 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
return result; 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 { interface FragmentConstructor {
Fragment constructAndGetFragment(); 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 { static class ViewPagerAdapter extends FragmentStateAdapter {
private final Fragment[] mChildFragments; private final Fragment[] mChildFragments;

View File

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