Merge "Update AppListRepository getSystemPackageNames" into udc-dev

This commit is contained in:
Chaohui Wang
2023-05-04 09:43:28 +00:00
committed by Android (Google) Code Review
3 changed files with 20 additions and 43 deletions

View File

@@ -49,7 +49,7 @@ internal interface AppListRepository {
): Flow<(app: ApplicationInfo) -> Boolean> ): Flow<(app: ApplicationInfo) -> Boolean>
/** Gets the system app package names. */ /** Gets the system app package names. */
fun getSystemPackageNamesBlocking(userId: Int, showInstantApps: Boolean): Set<String> fun getSystemPackageNamesBlocking(userId: Int): Set<String>
} }
/** /**
@@ -58,12 +58,8 @@ internal interface AppListRepository {
object AppListRepositoryUtil { object AppListRepositoryUtil {
/** Gets the system app package names. */ /** Gets the system app package names. */
@JvmStatic @JvmStatic
fun getSystemPackageNames( fun getSystemPackageNames(context: Context, userId: Int): Set<String> =
context: Context, AppListRepositoryImpl(context).getSystemPackageNamesBlocking(userId)
userId: Int,
showInstantApps: Boolean,
): Set<String> =
AppListRepositoryImpl(context).getSystemPackageNamesBlocking(userId, showInstantApps)
} }
internal class AppListRepositoryImpl(private val context: Context) : AppListRepository { internal class AppListRepositoryImpl(private val context: Context) : AppListRepository {
@@ -140,12 +136,12 @@ internal class AppListRepositoryImpl(private val context: Context) : AppListRepo
): Flow<(app: ApplicationInfo) -> Boolean> = ): Flow<(app: ApplicationInfo) -> Boolean> =
userIdFlow.combine(showSystemFlow, ::showSystemPredicate) userIdFlow.combine(showSystemFlow, ::showSystemPredicate)
override fun getSystemPackageNamesBlocking(userId: Int, showInstantApps: Boolean) = override fun getSystemPackageNamesBlocking(userId: Int) =
runBlocking { getSystemPackageNames(userId, showInstantApps) } runBlocking { getSystemPackageNames(userId) }
private suspend fun getSystemPackageNames(userId: Int, showInstantApps: Boolean): Set<String> = private suspend fun getSystemPackageNames(userId: Int): Set<String> =
coroutineScope { coroutineScope {
val loadAppsDeferred = async { loadApps(userId, showInstantApps) } val loadAppsDeferred = async { loadApps(userId) }
val homeOrLauncherPackages = loadHomeOrLauncherPackages(userId) val homeOrLauncherPackages = loadHomeOrLauncherPackages(userId)
val showSystemPredicate = val showSystemPredicate =
{ app: ApplicationInfo -> isSystemApp(app, homeOrLauncherPackages) } { app: ApplicationInfo -> isSystemApp(app, homeOrLauncherPackages) }
@@ -184,10 +180,8 @@ internal class AppListRepositoryImpl(private val context: Context) : AppListRepo
} }
} }
private fun isSystemApp(app: ApplicationInfo, homeOrLauncherPackages: Set<String>): Boolean { private fun isSystemApp(app: ApplicationInfo, homeOrLauncherPackages: Set<String>): Boolean =
return !app.isUpdatedSystemApp && app.isSystemApp && app.isSystemApp && !app.isUpdatedSystemApp && app.packageName !in homeOrLauncherPackages
!(app.packageName in homeOrLauncherPackages)
}
companion object { companion object {
private fun ApplicationInfo.isInAppList( private fun ApplicationInfo.isInAppList(

View File

@@ -78,9 +78,15 @@ class AppListRepositoryTest {
whenever(context.packageManager).thenReturn(packageManager) whenever(context.packageManager).thenReturn(packageManager)
whenever(context.userManager).thenReturn(userManager) whenever(context.userManager).thenReturn(userManager)
whenever(packageManager.getInstalledModules(anyInt())).thenReturn(emptyList()) whenever(packageManager.getInstalledModules(anyInt())).thenReturn(emptyList())
whenever(packageManager.getHomeActivities(any())).thenAnswer {
@Suppress("UNCHECKED_CAST")
val resolveInfos = it.arguments[0] as MutableList<ResolveInfo>
resolveInfos += resolveInfoOf(packageName = HOME_APP.packageName)
null
}
whenever( whenever(
packageManager.queryIntentActivitiesAsUser(any(), any<ResolveInfoFlags>(), anyInt()) packageManager.queryIntentActivitiesAsUser(any(), any<ResolveInfoFlags>(), anyInt())
).thenReturn(emptyList()) ).thenReturn(listOf(resolveInfoOf(packageName = IN_LAUNCHER_APP.packageName)))
whenever(userManager.getUserInfo(ADMIN_USER_ID)).thenReturn(UserInfo().apply { whenever(userManager.getUserInfo(ADMIN_USER_ID)).thenReturn(UserInfo().apply {
flags = UserInfo.FLAG_ADMIN flags = UserInfo.FLAG_ADMIN
}) })
@@ -290,35 +296,16 @@ class AppListRepositoryTest {
@Test @Test
fun showSystemPredicate_isHome() = runTest { fun showSystemPredicate_isHome() = runTest {
val app = HOME_APP
whenever(packageManager.getHomeActivities(any())).thenAnswer {
@Suppress("UNCHECKED_CAST")
val resolveInfos = it.arguments[0] as MutableList<ResolveInfo>
resolveInfos.add(resolveInfoOf(packageName = app.packageName))
null
}
val showSystemPredicate = getShowSystemPredicate(showSystem = false) val showSystemPredicate = getShowSystemPredicate(showSystem = false)
assertThat(showSystemPredicate(app)).isTrue() assertThat(showSystemPredicate(HOME_APP)).isTrue()
} }
@Test @Test
fun showSystemPredicate_appInLauncher() = runTest { fun showSystemPredicate_appInLauncher() = runTest {
val app = IN_LAUNCHER_APP
whenever(
packageManager.queryIntentActivitiesAsUser(
any(),
any<ResolveInfoFlags>(),
eq(ADMIN_USER_ID)
)
).thenReturn(listOf(resolveInfoOf(packageName = app.packageName)))
val showSystemPredicate = getShowSystemPredicate(showSystem = false) val showSystemPredicate = getShowSystemPredicate(showSystem = false)
assertThat(showSystemPredicate(app)).isTrue() assertThat(showSystemPredicate(IN_LAUNCHER_APP)).isTrue()
} }
@Test @Test
@@ -333,10 +320,9 @@ class AppListRepositoryTest {
val systemPackageNames = AppListRepositoryUtil.getSystemPackageNames( val systemPackageNames = AppListRepositoryUtil.getSystemPackageNames(
context = context, context = context,
userId = ADMIN_USER_ID, userId = ADMIN_USER_ID,
showInstantApps = false,
) )
assertThat(systemPackageNames).containsExactly("system.app", "home.app", "app.in.launcher") assertThat(systemPackageNames).containsExactly(SYSTEM_APP.packageName)
} }
private suspend fun getShowSystemPredicate(showSystem: Boolean) = private suspend fun getShowSystemPredicate(showSystem: Boolean) =

View File

@@ -96,10 +96,7 @@ class AppListViewModelTest {
showSystemFlow: Flow<Boolean>, showSystemFlow: Flow<Boolean>,
): Flow<(app: ApplicationInfo) -> Boolean> = flowOf { true } ): Flow<(app: ApplicationInfo) -> Boolean> = flowOf { true }
override fun getSystemPackageNamesBlocking( override fun getSystemPackageNamesBlocking(userId: Int): Set<String> = emptySet()
userId: Int,
showInstantApps: Boolean,
): Set<String> = emptySet()
} }
private object FakeAppRepository : AppRepository { private object FakeAppRepository : AppRepository {