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
diff --git a/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java b/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java
index a70d7d8..3321d50 100644
--- a/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java
+++ b/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java
@@ -57,6 +57,7 @@
 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 @@
                 personalFragmentConstructor,
                 workFragmentConstructor,
                 privateFragmentConstructor,
-                new PrivateSpaceInfoProvider() {},
-                new ManagedProfileInfoProvider() {});
+                new PrivateSpaceInfoProvider() {});
     }
 
     /**
@@ -309,36 +309,35 @@
             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);
-            }
-
-            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);
+            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");
+                }
             }
 
             result = new Fragment[fragments.size()];
@@ -350,6 +349,14 @@
         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 @@
         }
     }
 
-    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;
diff --git a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java
index 302c8f3..3df6449 100644
--- a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java
@@ -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 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.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 @@
         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 @@
     @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 @@
 
     @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 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 @@
     @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 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 @@
     @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 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 @@
     @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 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 @@
     @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 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 {