Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 70b857ad authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Update AppListRepository getSystemPackageNames

Since Instant Apps never system, no need for param showInstantApps.

Also correct the test result, since "home app" and "app in launcher"
will not be returned from this function.

Bug: 280280596
Test: Unit Tests
Change-Id: I2ae17ecaa24780fb2f0c896e84176fc16ccf7de0
parent 23c3902e
Loading
Loading
Loading
Loading
+9 −15
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ internal interface AppListRepository {
    ): Flow<(app: ApplicationInfo) -> Boolean>

    /** 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 {
    /** Gets the system app package names. */
    @JvmStatic
    fun getSystemPackageNames(
        context: Context,
        userId: Int,
        showInstantApps: Boolean,
    ): Set<String> =
        AppListRepositoryImpl(context).getSystemPackageNamesBlocking(userId, showInstantApps)
    fun getSystemPackageNames(context: Context, userId: Int): Set<String> =
        AppListRepositoryImpl(context).getSystemPackageNamesBlocking(userId)
}

internal class AppListRepositoryImpl(private val context: Context) : AppListRepository {
@@ -140,12 +136,12 @@ internal class AppListRepositoryImpl(private val context: Context) : AppListRepo
    ): Flow<(app: ApplicationInfo) -> Boolean> =
        userIdFlow.combine(showSystemFlow, ::showSystemPredicate)

    override fun getSystemPackageNamesBlocking(userId: Int, showInstantApps: Boolean) =
        runBlocking { getSystemPackageNames(userId, showInstantApps) }
    override fun getSystemPackageNamesBlocking(userId: Int) =
        runBlocking { getSystemPackageNames(userId) }

    private suspend fun getSystemPackageNames(userId: Int, showInstantApps: Boolean): Set<String> =
    private suspend fun getSystemPackageNames(userId: Int): Set<String> =
        coroutineScope {
            val loadAppsDeferred = async { loadApps(userId, showInstantApps) }
            val loadAppsDeferred = async { loadApps(userId) }
            val homeOrLauncherPackages = loadHomeOrLauncherPackages(userId)
            val showSystemPredicate =
                { 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 {
        return !app.isUpdatedSystemApp && app.isSystemApp &&
            !(app.packageName in homeOrLauncherPackages)
    }
    private fun isSystemApp(app: ApplicationInfo, homeOrLauncherPackages: Set<String>): Boolean =
        app.isSystemApp && !app.isUpdatedSystemApp && app.packageName !in homeOrLauncherPackages

    companion object {
        private fun ApplicationInfo.isInAppList(
+10 −24
Original line number Diff line number Diff line
@@ -78,9 +78,15 @@ class AppListRepositoryTest {
        whenever(context.packageManager).thenReturn(packageManager)
        whenever(context.userManager).thenReturn(userManager)
        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(
            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 {
            flags = UserInfo.FLAG_ADMIN
        })
@@ -290,35 +296,16 @@ class AppListRepositoryTest {

    @Test
    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)

        assertThat(showSystemPredicate(app)).isTrue()
        assertThat(showSystemPredicate(HOME_APP)).isTrue()
    }

    @Test
    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)

        assertThat(showSystemPredicate(app)).isTrue()
        assertThat(showSystemPredicate(IN_LAUNCHER_APP)).isTrue()
    }

    @Test
@@ -333,10 +320,9 @@ class AppListRepositoryTest {
        val systemPackageNames = AppListRepositoryUtil.getSystemPackageNames(
            context = context,
            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) =
+1 −4
Original line number Diff line number Diff line
@@ -96,10 +96,7 @@ class AppListViewModelTest {
            showSystemFlow: Flow<Boolean>,
        ): Flow<(app: ApplicationInfo) -> Boolean> = flowOf { true }

        override fun getSystemPackageNamesBlocking(
            userId: Int,
            showInstantApps: Boolean,
        ): Set<String> = emptySet()
        override fun getSystemPackageNamesBlocking(userId: Int): Set<String> = emptySet()
    }

    private object FakeAppRepository : AppRepository {