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

Commit 0d3e3352 authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

Code Refactor: UserTrackerImpl.UserTracker.Callback calls.

Remove code duplication by putting recurring code into
UserTrackerImpl.notifySubscribers method.

This CL doesn't make any behavioral changes, only does code refactor.

Bug: 326068767
Test: None
Flag: None
Change-Id: I91af59e72fb468928976523c71a3f599bc1cdbfa
parent 6b45c741
Loading
Loading
Loading
Loading
+22 −40
Original line number Original line Diff line number Diff line
@@ -227,22 +227,10 @@ open class UserTrackerImpl internal constructor(
    protected open fun handleBeforeUserSwitching(newUserId: Int) {
    protected open fun handleBeforeUserSwitching(newUserId: Int) {
        setUserIdInternal(newUserId)
        setUserIdInternal(newUserId)


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


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


        val list = synchronized(callbacks) {
        notifySubscribers { callback, resultCallback ->
            callbacks.toList()
            callback.onUserChanging(newUserId, userContext, resultCallback)
        }
        }.await()
        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()
    }
    }


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


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


@@ -308,8 +284,8 @@ open class UserTrackerImpl internal constructor(
        synchronized(mutex) {
        synchronized(mutex) {
            userProfiles = profiles.map { UserInfo(it) } // save a "deep" copy
            userProfiles = profiles.map { UserInfo(it) } // save a "deep" copy
        }
        }
        notifySubscribers {
        notifySubscribers { callback, _ ->
            onProfilesChanged(profiles)
            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) {
        val list = synchronized(callbacks) {
            callbacks.toList()
            callbacks.toList()
        }
        }

        val latch = CountDownLatch(list.size)
        list.forEach {
        list.forEach {
            if (it.callback.get() != null) {
            val callback = it.callback.get()
            if (callback != null) {
                it.executor.execute {
                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>) {
    override fun dump(pw: PrintWriter, args: Array<out String>) {