From ae4631e6a3f8e1e40233330d2c738eb78e9a2730 Mon Sep 17 00:00:00 2001 From: Himanshu Gupta Date: Sun, 21 Apr 2024 19:05:42 +0100 Subject: [PATCH] Move PreInstalled App List to UserCache. Bug: 333543006 Test: atest PrivateSpaceHeaderViewTest, AlphabeticalAppsListTest Flag: NA Change-Id: I84233c5a6406325d9e641b72ea8455911b46f65f --- .../allapps/AlphabeticalAppsList.java | 3 +- .../allapps/PrivateProfileManager.java | 13 +++----- src/com/android/launcher3/pm/UserCache.java | 27 ++++++++++++++++ .../allapps/AlphabeticalAppsListTest.java | 4 +-- .../allapps/PrivateSpaceHeaderViewTest.java | 31 +++++++++---------- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/com/android/launcher3/allapps/AlphabeticalAppsList.java b/src/com/android/launcher3/allapps/AlphabeticalAppsList.java index 60df7c529c..a8103317cd 100644 --- a/src/com/android/launcher3/allapps/AlphabeticalAppsList.java +++ b/src/com/android/launcher3/allapps/AlphabeticalAppsList.java @@ -22,7 +22,6 @@ import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCH import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_PRIVATE_SPACE_USER_INSTALLED_APPS_COUNT; import android.content.Context; -import android.graphics.drawable.Drawable; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ImageSpan; @@ -362,7 +361,7 @@ public class AlphabeticalAppsList implement // Split of private space apps into user-installed and system apps. Map> split = mPrivateApps.stream() .collect(Collectors.partitioningBy(mPrivateProviderManager - .splitIntoUserInstalledAndSystemApps())); + .splitIntoUserInstalledAndSystemApps(mActivityContext))); // TODO(b/329688630): switch to the pulled LayoutStaticSnapshot atom mActivityContext diff --git a/src/com/android/launcher3/allapps/PrivateProfileManager.java b/src/com/android/launcher3/allapps/PrivateProfileManager.java index 38fe138996..7e975df713 100644 --- a/src/com/android/launcher3/allapps/PrivateProfileManager.java +++ b/src/com/android/launcher3/allapps/PrivateProfileManager.java @@ -80,9 +80,7 @@ import com.android.launcher3.views.ActivityContext; import com.android.launcher3.views.RecyclerViewFastScroller; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.function.Predicate; /** @@ -105,7 +103,6 @@ public class PrivateProfileManager extends UserProfileManager { } } }; - private Set mPreInstalledSystemPackages = new HashSet<>(); private Intent mAppInstallerIntent = new Intent(); private PrivateAppsSectionDecorator mPrivateAppsSectionDecorator; private boolean mPrivateSpaceSettingsAvailable; @@ -264,8 +261,6 @@ public class PrivateProfileManager extends UserProfileManager { ApiWrapper apiWrapper = ApiWrapper.INSTANCE.get(appContext); UserHandle profileUser = getProfileUser(); if (profileUser != null) { - mPreInstalledSystemPackages = new HashSet<>( - apiWrapper.getPreInstalledSystemPackages(profileUser)); mAppInstallerIntent = apiWrapper .getAppMarketActivityIntent(BuildConfig.APPLICATION_ID, profileUser); } @@ -349,10 +344,12 @@ public class PrivateProfileManager extends UserProfileManager { * Splits private apps into user installed and system apps. * When the list of system apps is empty, all apps are treated as system. */ - public Predicate splitIntoUserInstalledAndSystemApps() { - return appInfo -> !mPreInstalledSystemPackages.isEmpty() + public Predicate splitIntoUserInstalledAndSystemApps(Context context) { + List preInstallApps = UserCache.getInstance(context) + .getPreInstallApps(getProfileUser()); + return appInfo -> !preInstallApps.isEmpty() && (appInfo.componentName == null - || !(mPreInstalledSystemPackages.contains(appInfo.componentName.getPackageName()))); + || !(preInstallApps.contains(appInfo.componentName.getPackageName()))); } /** Add Private Space Header view elements based upon {@link UserProfileState} */ diff --git a/src/com/android/launcher3/pm/UserCache.java b/src/com/android/launcher3/pm/UserCache.java index 185e0b55bd..b7b557dbd4 100644 --- a/src/com/android/launcher3/pm/UserCache.java +++ b/src/com/android/launcher3/pm/UserCache.java @@ -24,6 +24,7 @@ import android.content.Intent; import android.os.Process; import android.os.UserHandle; import android.os.UserManager; +import android.util.ArrayMap; import androidx.annotation.AnyThread; import androidx.annotation.NonNull; @@ -81,6 +82,9 @@ public class UserCache implements SafeCloseable { @NonNull private Map mUserToSerialMap; + @NonNull + private Map> mUserToPreInstallAppMap; + private UserCache(Context context) { mContext = context; mUserToSerialMap = Collections.emptyMap(); @@ -120,6 +124,20 @@ public class UserCache implements SafeCloseable { @WorkerThread private void updateCache() { mUserToSerialMap = ApiWrapper.INSTANCE.get(mContext).queryAllUsers(); + mUserToPreInstallAppMap = fetchPreInstallApps(); + } + + @WorkerThread + private Map> fetchPreInstallApps() { + Map> userToPreInstallApp = new ArrayMap<>(); + mUserToSerialMap.forEach((userHandle, userIconInfo) -> { + // Fetch only for private profile, as other profiles have no usages yet. + List preInstallApp = userIconInfo.isPrivate() + ? ApiWrapper.INSTANCE.get(mContext).getPreInstalledSystemPackages(userHandle) + : new ArrayList<>(); + userToPreInstallApp.put(userHandle, preInstallApp); + }); + return userToPreInstallApp; } /** @@ -171,6 +189,15 @@ public class UserCache implements SafeCloseable { return List.copyOf(mUserToSerialMap.keySet()); } + /** + * Returns the pre-installed apps for a user. + */ + @NonNull + public List getPreInstallApps(UserHandle user) { + List preInstallApp = mUserToPreInstallAppMap.get(user); + return preInstallApp == null ? new ArrayList<>() : preInstallApp; + } + /** * Get a non-themed {@link UserBadgeDrawable} based on the provided {@link UserHandle}. */ diff --git a/tests/src/com/android/launcher3/allapps/AlphabeticalAppsListTest.java b/tests/src/com/android/launcher3/allapps/AlphabeticalAppsListTest.java index 423ca2415f..d2238ffda4 100644 --- a/tests/src/com/android/launcher3/allapps/AlphabeticalAppsListTest.java +++ b/tests/src/com/android/launcher3/allapps/AlphabeticalAppsListTest.java @@ -98,7 +98,7 @@ public class AlphabeticalAppsListTest { when(mPrivateProfileManager.addPrivateSpaceHeader(any())) .thenAnswer(answer(this::addPrivateSpaceHeader)); when(mPrivateProfileManager.getCurrentState()).thenReturn(STATE_ENABLED); - when(mPrivateProfileManager.splitIntoUserInstalledAndSystemApps()) + when(mPrivateProfileManager.splitIntoUserInstalledAndSystemApps(any())) .thenReturn(iteminfo -> iteminfo.componentName == null || !iteminfo.componentName.getPackageName() .equals("com.android.launcher3.tests.camera")); @@ -127,7 +127,7 @@ public class AlphabeticalAppsListTest { when(mPrivateProfileManager.addSystemAppsDivider(any())) .thenAnswer(answer(this::addSystemAppsDivider)); when(mPrivateProfileManager.getCurrentState()).thenReturn(STATE_ENABLED); - when(mPrivateProfileManager.splitIntoUserInstalledAndSystemApps()) + when(mPrivateProfileManager.splitIntoUserInstalledAndSystemApps(mContext)) .thenReturn(iteminfo -> iteminfo.componentName == null || !iteminfo.componentName.getPackageName() .equals("com.android.launcher3.tests.camera")); diff --git a/tests/src/com/android/launcher3/allapps/PrivateSpaceHeaderViewTest.java b/tests/src/com/android/launcher3/allapps/PrivateSpaceHeaderViewTest.java index 9363ce81c6..512b2acc10 100644 --- a/tests/src/com/android/launcher3/allapps/PrivateSpaceHeaderViewTest.java +++ b/tests/src/com/android/launcher3/allapps/PrivateSpaceHeaderViewTest.java @@ -71,6 +71,7 @@ import org.mockito.MockitoAnnotations; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Predicate; @SmallTest @RunWith(AndroidJUnit4.class) @@ -279,10 +280,8 @@ public class PrivateSpaceHeaderViewTest { when(mAllAppsStore.getApps()).thenReturn(createAppInfoList()); PrivateProfileManager privateProfileManager = spy(mPrivateProfileManager); when(privateProfileManager.getCurrentState()).thenReturn(STATE_ENABLED); - when(privateProfileManager.splitIntoUserInstalledAndSystemApps()) - .thenReturn(iteminfo -> iteminfo.componentName == null - || !iteminfo.componentName.getPackageName() - .equals(CAMERA_PACKAGE_NAME)); + doReturn(splitIntoUserInstalledAndSystemApps()).when(privateProfileManager) + .splitIntoUserInstalledAndSystemApps(any()); doReturn(0).when(privateProfileManager).addPrivateSpaceHeader(any()); doAnswer(answer(this::addPrivateSpaceHeader)).when(privateProfileManager) .addPrivateSpaceHeader(any()); @@ -316,10 +315,8 @@ public class PrivateSpaceHeaderViewTest { when(mAllAppsStore.getApps()).thenReturn(createAppInfoList()); PrivateProfileManager privateProfileManager = spy(mPrivateProfileManager); when(privateProfileManager.getCurrentState()).thenReturn(STATE_ENABLED); - when(privateProfileManager.splitIntoUserInstalledAndSystemApps()) - .thenReturn(iteminfo -> iteminfo.componentName == null - || !iteminfo.componentName.getPackageName() - .equals(CAMERA_PACKAGE_NAME)); + doReturn(splitIntoUserInstalledAndSystemApps()).when(privateProfileManager) + .splitIntoUserInstalledAndSystemApps(any()); doReturn(0).when(privateProfileManager).addPrivateSpaceHeader(any()); doAnswer(answer(this::addPrivateSpaceHeader)).when(privateProfileManager) .addPrivateSpaceHeader(any()); @@ -353,10 +350,8 @@ public class PrivateSpaceHeaderViewTest { when(mAllAppsStore.getApps()).thenReturn(createAppInfoList()); PrivateProfileManager privateProfileManager = spy(mPrivateProfileManager); when(privateProfileManager.getCurrentState()).thenReturn(STATE_ENABLED); - when(privateProfileManager.splitIntoUserInstalledAndSystemApps()) - .thenReturn(iteminfo -> iteminfo.componentName == null - || !iteminfo.componentName.getPackageName() - .equals(CAMERA_PACKAGE_NAME)); + doReturn(splitIntoUserInstalledAndSystemApps()).when(privateProfileManager) + .splitIntoUserInstalledAndSystemApps(any()); doReturn(0).when(privateProfileManager).addPrivateSpaceHeader(any()); doAnswer(answer(this::addPrivateSpaceHeader)).when(privateProfileManager) .addPrivateSpaceHeader(any()); @@ -390,10 +385,8 @@ public class PrivateSpaceHeaderViewTest { when(mAllAppsStore.getApps()).thenReturn(createAppInfoList()); PrivateProfileManager privateProfileManager = spy(mPrivateProfileManager); when(privateProfileManager.getCurrentState()).thenReturn(STATE_ENABLED); - when(privateProfileManager.splitIntoUserInstalledAndSystemApps()) - .thenReturn(iteminfo -> iteminfo.componentName == null - || !iteminfo.componentName.getPackageName() - .equals(CAMERA_PACKAGE_NAME)); + doReturn(splitIntoUserInstalledAndSystemApps()).when(privateProfileManager) + .splitIntoUserInstalledAndSystemApps(any()); doReturn(0).when(privateProfileManager).addPrivateSpaceHeader(any()); doNothing().when(privateProfileManager).addPrivateSpaceInstallAppButton(any()); doReturn(0).when(privateProfileManager).addSystemAppsDivider(any()); @@ -465,4 +458,10 @@ public class PrivateSpaceHeaderViewTest { } return appInfos.toArray(AppInfo[]::new); } + + private Predicate splitIntoUserInstalledAndSystemApps() { + return iteminfo -> iteminfo.componentName == null + || !iteminfo.componentName.getPackageName() + .equals(CAMERA_PACKAGE_NAME); + } }