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

Commit a3bd1e2b authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

Make UserSwitchObserver.onBeforeUserSwitching oneway but still blocking.

Bug: 371536480
Test: atest UserControllerTest
Test: atest UserTrackerImplTest
Flag: EXEMPT bugfix
Change-Id: I6fd04b00ab768533df01a3eca613f388bf70e42a
parent 6b27ad03
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -19,10 +19,10 @@ package android.app;
import android.os.IRemoteCallback;

/** {@hide} */
interface IUserSwitchObserver {
    void onBeforeUserSwitching(int newUserId);
    oneway void onUserSwitching(int newUserId, IRemoteCallback reply);
    oneway void onUserSwitchComplete(int newUserId);
    oneway void onForegroundProfileSwitch(int newProfileId);
    oneway void onLockedBootComplete(int newUserId);
oneway interface IUserSwitchObserver {
    void onBeforeUserSwitching(int newUserId, IRemoteCallback reply);
    void onUserSwitching(int newUserId, IRemoteCallback reply);
    void onUserSwitchComplete(int newUserId);
    void onForegroundProfileSwitch(int newProfileId);
    void onLockedBootComplete(int newUserId);
}
+5 −1
Original line number Diff line number Diff line
@@ -30,7 +30,11 @@ public class UserSwitchObserver extends IUserSwitchObserver.Stub {
    }

    @Override
    public void onBeforeUserSwitching(int newUserId) throws RemoteException {}
    public void onBeforeUserSwitching(int newUserId, IRemoteCallback reply) throws RemoteException {
        if (reply != null) {
            reply.sendResult(null);
        }
    }

    @Override
    public void onUserSwitching(int newUserId, IRemoteCallback reply) throws RemoteException {
+10 −1
Original line number Diff line number Diff line
@@ -61,9 +61,18 @@ interface UserTracker : UserContentResolverProvider, UserContextProvider {
    /** Callback for notifying of changes. */
    @WeaklyReferencedCallback
    interface Callback {
        /** Notifies that the current user will be changed. */
        /**
         * Same as {@link onBeforeUserSwitching(Int, Runnable)} but the callback will be called
         * automatically after the completion of this method.
         */
        fun onBeforeUserSwitching(newUser: Int) {}

        /** Notifies that the current user will be changed. */
        fun onBeforeUserSwitching(newUser: Int, resultCallback: Runnable) {
            onBeforeUserSwitching(newUser)
            resultCallback.run()
        }

        /**
         * Same as {@link onUserChanging(Int, Context, Runnable)} but the callback will be called
         * automatically after the completion of this method.
+3 −3
Original line number Diff line number Diff line
@@ -196,8 +196,9 @@ internal constructor(
    private fun registerUserSwitchObserver() {
        iActivityManager.registerUserSwitchObserver(
            object : UserSwitchObserver() {
                override fun onBeforeUserSwitching(newUserId: Int) {
                override fun onBeforeUserSwitching(newUserId: Int, reply: IRemoteCallback?) {
                    handleBeforeUserSwitching(newUserId)
                    reply?.sendResult(null)
                }

                override fun onUserSwitching(newUserId: Int, reply: IRemoteCallback?) {
@@ -236,8 +237,7 @@ internal constructor(
        setUserIdInternal(newUserId)

        notifySubscribers { callback, resultCallback ->
                callback.onBeforeUserSwitching(newUserId)
                resultCallback.run()
                callback.onBeforeUserSwitching(newUserId, resultCallback)
            }
            .await()
    }
+15 −3
Original line number Diff line number Diff line
@@ -76,6 +76,8 @@ class UserTrackerImplTest : SysuiTestCase() {

    @Mock private lateinit var iActivityManager: IActivityManager

    @Mock private lateinit var beforeUserSwitchingReply: IRemoteCallback

    @Mock private lateinit var userSwitchingReply: IRemoteCallback

    @Mock(stubOnly = true) private lateinit var dumpManager: DumpManager
@@ -199,9 +201,10 @@ class UserTrackerImplTest : SysuiTestCase() {

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

            verify(userManager).getProfiles(newID)
@@ -341,10 +344,11 @@ class UserTrackerImplTest : SysuiTestCase() {

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

            verify(beforeUserSwitchingReply).sendResult(any())
            verify(userSwitchingReply).sendResult(any())
            assertThat(callback.calledOnUserChanging).isEqualTo(1)
            assertThat(callback.lastUser).isEqualTo(newID)
@@ -395,7 +399,7 @@ class UserTrackerImplTest : SysuiTestCase() {

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

@@ -453,8 +457,10 @@ class UserTrackerImplTest : SysuiTestCase() {

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

@@ -488,6 +494,7 @@ class UserTrackerImplTest : SysuiTestCase() {
        }

    private class TestCallback : UserTracker.Callback {
        var calledOnBeforeUserChanging = 0
        var calledOnUserChanging = 0
        var calledOnUserChanged = 0
        var calledOnProfilesChanged = 0
@@ -495,6 +502,11 @@ class UserTrackerImplTest : SysuiTestCase() {
        var lastUserContext: Context? = null
        var lastUserProfiles = emptyList<UserInfo>()

        override fun onBeforeUserSwitching(newUser: Int) {
            calledOnBeforeUserChanging++
            lastUser = newUser
        }

        override fun onUserChanging(newUser: Int, userContext: Context) {
            calledOnUserChanging++
            lastUser = newUser
Loading