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

Commit 0973118d authored by Aaron Liu's avatar Aaron Liu
Browse files

Prevent Sysui crash when user is deleted.

When User is deleted, we need to make sure that we don't call
context#startServiceAsUser. We can do this by ensuring that the user is
in the list of alive users in Usermanager. I also added a test.

Fixes: 296305878
Test: Do repro steps described in bug. Make user and skip set up wizard
and remove user. Observe no crash.
Change-Id: I5b346f0ecc95b45b73b9cfdea4a46e7333b73aaf

Change-Id: Ib7af8ca3b5405e2d10eda7c52e4210ed9dfae024
parent 4129e187
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -644,6 +644,11 @@ constructor(
    }
    }


    private fun restartSecondaryService(@UserIdInt userId: Int) {
    private fun restartSecondaryService(@UserIdInt userId: Int) {
        // Do not start service for user that is marked for deletion.
        if (!manager.aliveUsers.map { it.id }.contains(userId)) {
            return
        }

        val intent = Intent(applicationContext, SystemUISecondaryUserService::class.java)
        val intent = Intent(applicationContext, SystemUISecondaryUserService::class.java)
        // Disconnect from the old secondary user's service
        // Disconnect from the old secondary user's service
        val secondaryUserId = repository.secondaryUserId
        val secondaryUserId = repository.secondaryUserId
@@ -657,6 +662,7 @@ constructor(


        // Connect to the new secondary user's service (purely to ensure that a persistent
        // Connect to the new secondary user's service (purely to ensure that a persistent
        // SystemUI application is created for that user)
        // SystemUI application is created for that user)

        if (userId != Process.myUserHandle().identifier) {
        if (userId != Process.myUserHandle().identifier) {
            applicationContext.startServiceAsUser(
            applicationContext.startServiceAsUser(
                intent,
                intent,
+12 −1
Original line number Original line Diff line number Diff line
@@ -155,6 +155,9 @@ class UserInteractorTest : SysuiTestCase() {


    @Test
    @Test
    fun createUserInteractor_nonProcessUser_startsSecondaryService() {
    fun createUserInteractor_nonProcessUser_startsSecondaryService() {
        val userId = Process.myUserHandle().identifier + 1
        whenever(manager.aliveUsers).thenReturn(listOf(createUserInfo(userId, "abc")))

        createUserInteractor(false /* startAsProcessUser */)
        createUserInteractor(false /* startAsProcessUser */)
        verify(spyContext).startServiceAsUser(any(), any())
        verify(spyContext).startServiceAsUser(any(), any())
    }
    }
@@ -655,9 +658,10 @@ class UserInteractorTest : SysuiTestCase() {


    @Test
    @Test
    fun userSwitchedBroadcast() {
    fun userSwitchedBroadcast() {
        createUserInteractor()
        testScope.runTest {
        testScope.runTest {
            val userInfos = createUserInfos(count = 2, includeGuest = false)
            val userInfos = createUserInfos(count = 2, includeGuest = false)
            whenever(manager.aliveUsers).thenReturn(userInfos)
            createUserInteractor()
            userRepository.setUserInfos(userInfos)
            userRepository.setUserInfos(userInfos)
            userRepository.setSelectedUserInfo(userInfos[0])
            userRepository.setSelectedUserInfo(userInfos[0])
            userRepository.setSettings(UserSwitcherSettingsModel(isUserSwitcherEnabled = true))
            userRepository.setSettings(UserSwitcherSettingsModel(isUserSwitcherEnabled = true))
@@ -985,6 +989,13 @@ class UserInteractorTest : SysuiTestCase() {
        }
        }
    }
    }


    @Test
    fun initWithNoAliveUsers() {
        whenever(manager.aliveUsers).thenReturn(listOf())
        createUserInteractor()
        verify(spyContext, never()).startServiceAsUser(any(), any())
    }

    private fun assertUsers(
    private fun assertUsers(
        models: List<UserModel>?,
        models: List<UserModel>?,
        count: Int,
        count: Int,