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

Commit 4acc4d35 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Call onBeforeUserSwitching event in callbacks' executor." into main

parents e2732d11 6b45c741
Loading
Loading
Loading
Loading
+12 −11
Original line number Diff line number Diff line
@@ -121,7 +121,6 @@ open class UserTrackerImpl internal constructor(
    @GuardedBy("callbacks")
    private val callbacks: MutableList<DataItem> = ArrayList()

    private var beforeUserSwitchingJob: Job? = null
    private var userSwitchingJob: Job? = null
    private var afterUserSwitchingJob: Job? = null

@@ -194,15 +193,8 @@ open class UserTrackerImpl internal constructor(
    private fun registerUserSwitchObserver() {
        iActivityManager.registerUserSwitchObserver(object : UserSwitchObserver() {
            override fun onBeforeUserSwitching(newUserId: Int) {
                if (isBackgroundUserSwitchEnabled) {
                    beforeUserSwitchingJob?.cancel()
                    beforeUserSwitchingJob = appScope.launch(backgroundContext) {
                        handleBeforeUserSwitching(newUserId)
                    }
                } else {
                handleBeforeUserSwitching(newUserId)
            }
            }

            override fun onUserSwitching(newUserId: Int, reply: IRemoteCallback?) {
                if (isBackgroundUserSwitchEnabled) {
@@ -233,16 +225,25 @@ open class UserTrackerImpl internal constructor(

    @WorkerThread
    protected open fun handleBeforeUserSwitching(newUserId: Int) {
        Assert.isNotMainThread()
        setUserIdInternal(newUserId)

        val list = synchronized(callbacks) {
            callbacks.toList()
        }
        val latch = CountDownLatch(list.size)
        list.forEach {
            it.callback.get()?.onBeforeUserSwitching(newUserId)
            val callback = it.callback.get()
            if (callback != null) {
                it.executor.execute {
                    callback.onBeforeUserSwitching(newUserId)
                    latch.countDown()
                }
            } else {
                latch.countDown()
            }
        }
        latch.await()
    }

    @WorkerThread
    protected open fun handleUserSwitching(newUserId: Int) {
+0 −1
Original line number Diff line number Diff line
@@ -371,7 +371,6 @@ class UserTrackerImplTest : SysuiTestCase() {

        val captor = ArgumentCaptor.forClass(IUserSwitchObserver::class.java)
        verify(iActivityManager).registerUserSwitchObserver(capture(captor), anyString())
        captor.value.onBeforeUserSwitching(newID)
        captor.value.onUserSwitching(newID, userSwitchingReply)

        assertThat(callback.calledOnUserChanging).isEqualTo(0)