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

Commit 17bab3e3 authored by Yasin Kilicdere's avatar Yasin Kilicdere Committed by Android (Google) Code Review
Browse files

Merge "Code Refactor: UserTrackerImpl.UserTracker.Callback calls." into main

parents f8746dfe 0d3e3352
Loading
Loading
Loading
Loading
+22 −40
Original line number Diff line number Diff line
@@ -227,22 +227,10 @@ open class UserTrackerImpl internal constructor(
    protected open fun handleBeforeUserSwitching(newUserId: Int) {
        setUserIdInternal(newUserId)

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

    @WorkerThread
@@ -250,21 +238,9 @@ open class UserTrackerImpl internal constructor(
        Assert.isNotMainThread()
        Log.i(TAG, "Switching to user $newUserId")

        val list = synchronized(callbacks) {
            callbacks.toList()
        }
        val latch = CountDownLatch(list.size)
        list.forEach {
            val callback = it.callback.get()
            if (callback != null) {
                it.executor.execute {
                    callback.onUserChanging(userId, userContext) { latch.countDown() }
                }
            } else {
                latch.countDown()
            }
        }
        latch.await()
        notifySubscribers { callback, resultCallback ->
            callback.onUserChanging(newUserId, userContext, resultCallback)
        }.await()
    }

    @WorkerThread
@@ -294,9 +270,9 @@ open class UserTrackerImpl internal constructor(
        Assert.isNotMainThread()
        Log.i(TAG, "Switched to user $newUserId")

        notifySubscribers {
            onUserChanged(newUserId, userContext)
            onProfilesChanged(userProfiles)
        notifySubscribers { callback, _ ->
            callback.onUserChanged(newUserId, userContext)
            callback.onProfilesChanged(userProfiles)
        }
    }

@@ -308,8 +284,8 @@ open class UserTrackerImpl internal constructor(
        synchronized(mutex) {
            userProfiles = profiles.map { UserInfo(it) } // save a "deep" copy
        }
        notifySubscribers {
            onProfilesChanged(profiles)
        notifySubscribers { callback, _ ->
            callback.onProfilesChanged(profiles)
        }
    }

@@ -325,18 +301,24 @@ open class UserTrackerImpl internal constructor(
        }
    }

    private inline fun notifySubscribers(crossinline action: UserTracker.Callback.() -> Unit) {
    private inline fun notifySubscribers(
            crossinline action: (UserTracker.Callback, resultCallback: Runnable) -> Unit
    ): CountDownLatch {
        val list = synchronized(callbacks) {
            callbacks.toList()
        }

        val latch = CountDownLatch(list.size)
        list.forEach {
            if (it.callback.get() != null) {
            val callback = it.callback.get()
            if (callback != null) {
                it.executor.execute {
                    it.callback.get()?.action()
                    action(callback) { latch.countDown() }
                }
            } else {
                latch.countDown()
            }
        }
        return latch
    }

    override fun dump(pw: PrintWriter, args: Array<out String>) {