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

Commit 85c8c448 authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

Make UserTrackerImpl.userInfo thread-safe.

The userInfo property was not synchronized, which was causing crashes
in some cases. In the previous implementation userProfiles could change
and not contain the userId, throwing NoSuchElementException. This CL
makes the property thread-safe.

Bug: 363466988
Change-Id: Ibc5f9ea8d50f729e9d24b4647999350b4ea577a4
Test: None
Flag: EXEMPT bugfix
parent db99fede
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -103,11 +103,8 @@ internal constructor(
    override val userContentResolver: ContentResolver
        get() = userContext.contentResolver

    override val userInfo: UserInfo
        get() {
            val user = userId
            return userProfiles.first { it.id == user }
        }
    override var userInfo: UserInfo by SynchronizedDelegate(UserInfo(context.userId, "", 0))
        protected set

    /**
     * Returns a [List<UserInfo>] of all profiles associated with the current user.
@@ -187,6 +184,7 @@ internal constructor(
            userHandle = handle
            userContext = ctx
            userProfiles = profiles.map { UserInfo(it) }
            userInfo = profiles.first { it.id == user }
        }
        return ctx to profiles
    }
+15 −15
Original line number Diff line number Diff line
@@ -79,21 +79,6 @@ class UserTrackerImplReceiveTest : SysuiTestCase() {

    @Test
    fun callsCallbackAndUpdatesProfilesWhenAnIntentReceived() = runTest {
        tracker =
            UserTrackerImpl(
                context,
                { fakeFeatures },
                userManager,
                iActivityManager,
                dumpManager,
                this,
                testDispatcher,
                handler
            )
        tracker.initialize(0)
        tracker.addCallback(callback, executor)
        val profileID = tracker.userId + 10

        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val id = invocation.getArgument<Int>(0)
            val info = UserInfo(id, "", UserInfo.FLAG_FULL)
@@ -109,6 +94,21 @@ class UserTrackerImplReceiveTest : SysuiTestCase() {
            listOf(info, infoProfile)
        }

        tracker =
            UserTrackerImpl(
                context,
                { fakeFeatures },
                userManager,
                iActivityManager,
                dumpManager,
                this,
                testDispatcher,
                handler
            )
        tracker.initialize(0)
        tracker.addCallback(callback, executor)
        val profileID = tracker.userId + 10

        tracker.onReceive(context, Intent(intentAction))

        verify(callback, times(0)).onUserChanged(anyInt(), any())