Merge "Add AppListRepository.loadAndFilterApps()" into udc-qpr-dev

This commit is contained in:
Chaohui Wang
2023-05-25 09:38:34 +00:00
committed by Android (Google) Code Review
3 changed files with 38 additions and 17 deletions

View File

@@ -34,11 +34,11 @@ import kotlinx.coroutines.runBlocking
/**
* The repository to load the App List data.
*/
internal interface AppListRepository {
interface AppListRepository {
/** Loads the list of [ApplicationInfo]. */
suspend fun loadApps(
userId: Int,
showInstantApps: Boolean = false,
loadInstantApps: Boolean = false,
matchAnyUserForAdmin: Boolean = false,
): List<ApplicationInfo>
@@ -50,6 +50,9 @@ internal interface AppListRepository {
/** Gets the system app package names. */
fun getSystemPackageNamesBlocking(userId: Int): Set<String>
/** Loads the list of [ApplicationInfo], and filter base on `isSystemApp`. */
suspend fun loadAndFilterApps(userId: Int, isSystemApp: Boolean): List<ApplicationInfo>
}
/**
@@ -62,13 +65,13 @@ object AppListRepositoryUtil {
AppListRepositoryImpl(context).getSystemPackageNamesBlocking(userId)
}
internal class AppListRepositoryImpl(private val context: Context) : AppListRepository {
class AppListRepositoryImpl(private val context: Context) : AppListRepository {
private val packageManager = context.packageManager
private val userManager = context.userManager
override suspend fun loadApps(
userId: Int,
showInstantApps: Boolean,
loadInstantApps: Boolean,
matchAnyUserForAdmin: Boolean,
): List<ApplicationInfo> = coroutineScope {
val hiddenSystemModulesDeferred = async {
@@ -86,7 +89,7 @@ internal class AppListRepositoryImpl(private val context: Context) : AppListRepo
val hiddenSystemModules = hiddenSystemModulesDeferred.await()
val hideWhenDisabledPackages = hideWhenDisabledPackagesDeferred.await()
installedApplicationsAsUser.filter { app ->
app.isInAppList(showInstantApps, hiddenSystemModules, hideWhenDisabledPackages)
app.isInAppList(loadInstantApps, hiddenSystemModules, hideWhenDisabledPackages)
}
}
@@ -136,17 +139,17 @@ internal class AppListRepositoryImpl(private val context: Context) : AppListRepo
): Flow<(app: ApplicationInfo) -> Boolean> =
userIdFlow.combine(showSystemFlow, ::showSystemPredicate)
override fun getSystemPackageNamesBlocking(userId: Int) =
runBlocking { getSystemPackageNames(userId) }
override fun getSystemPackageNamesBlocking(userId: Int) = runBlocking {
loadAndFilterApps(userId = userId, isSystemApp = true).map { it.packageName }.toSet()
}
private suspend fun getSystemPackageNames(userId: Int): Set<String> =
coroutineScope {
val loadAppsDeferred = async { loadApps(userId) }
val homeOrLauncherPackages = loadHomeOrLauncherPackages(userId)
val showSystemPredicate =
{ app: ApplicationInfo -> isSystemApp(app, homeOrLauncherPackages) }
loadAppsDeferred.await().filter(showSystemPredicate).map { it.packageName }.toSet()
override suspend fun loadAndFilterApps(userId: Int, isSystemApp: Boolean) = coroutineScope {
val loadAppsDeferred = async { loadApps(userId) }
val homeOrLauncherPackages = loadHomeOrLauncherPackages(userId)
loadAppsDeferred.await().filter { app ->
isSystemApp(app, homeOrLauncherPackages) == isSystemApp
}
}
private suspend fun showSystemPredicate(
userId: Int,

View File

@@ -108,7 +108,7 @@ class AppListRepositoryTest {
val appList = repository.loadApps(
userId = ADMIN_USER_ID,
showInstantApps = false,
loadInstantApps = false,
)
assertThat(appList).containsExactly(NORMAL_APP)
@@ -120,7 +120,7 @@ class AppListRepositoryTest {
val appList = repository.loadApps(
userId = ADMIN_USER_ID,
showInstantApps = true,
loadInstantApps = true,
)
assertThat(appList).containsExactly(NORMAL_APP, INSTANT_APP)
@@ -325,6 +325,21 @@ class AppListRepositoryTest {
assertThat(systemPackageNames).containsExactly(SYSTEM_APP.packageName)
}
@Test
fun loadAndFilterApps_loadNonSystemApp_returnExpectedValues() = runTest {
mockInstalledApplications(
apps = listOf(
NORMAL_APP, INSTANT_APP, SYSTEM_APP, UPDATED_SYSTEM_APP, HOME_APP, IN_LAUNCHER_APP
),
userId = ADMIN_USER_ID,
)
val appList = repository.loadAndFilterApps(userId = ADMIN_USER_ID, isSystemApp = false)
assertThat(appList)
.containsExactly(NORMAL_APP, UPDATED_SYSTEM_APP, HOME_APP, IN_LAUNCHER_APP)
}
private suspend fun getShowSystemPredicate(showSystem: Boolean) =
repository.showSystemPredicate(
userIdFlow = flowOf(ADMIN_USER_ID),

View File

@@ -87,7 +87,7 @@ class AppListViewModelTest {
private object FakeAppListRepository : AppListRepository {
override suspend fun loadApps(
userId: Int,
showInstantApps: Boolean,
loadInstantApps: Boolean,
matchAnyUserForAdmin: Boolean,
) = listOf(APP)
@@ -97,6 +97,9 @@ class AppListViewModelTest {
): Flow<(app: ApplicationInfo) -> Boolean> = flowOf { true }
override fun getSystemPackageNamesBlocking(userId: Int): Set<String> = emptySet()
override suspend fun loadAndFilterApps(userId: Int, isSystemApp: Boolean) =
emptyList<ApplicationInfo>()
}
private object FakeAppRepository : AppRepository {