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

Commit c059123b authored by Yasin Kilicdere's avatar Yasin Kilicdere Committed by Android Build Coastguard Worker
Browse files

Make UserSwitchObserver.onBeforeUserSwitching oneway but still blocking.

Bug: 371536480
Test: atest UserControllerTest
Test: atest UserTrackerImplTest
Flag: EXEMPT bugfix
(cherry picked from commit a3bd1e2b)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:f7efa779da5c59085b38cb73da61ef0d83b672b6)
Merged-In: I6fd04b00ab768533df01a3eca613f388bf70e42a
Change-Id: I6fd04b00ab768533df01a3eca613f388bf70e42a
parent 8c290a4d
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 {
+8 −1
Original line number Diff line number Diff line
@@ -72,10 +72,17 @@ interface UserTracker : UserContentResolverProvider, UserContextProvider {
    @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
@@ -192,8 +192,9 @@ open class UserTrackerImpl 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?) {
@@ -228,8 +229,7 @@ open class UserTrackerImpl internal constructor(
        setUserIdInternal(newUserId)

        notifySubscribers { callback, resultCallback ->
            callback.onBeforeUserSwitching(newUserId)
            resultCallback.run()
            callback.onBeforeUserSwitching(newUserId, resultCallback)
        }.await()
    }

+16 −3
Original line number Diff line number Diff line
@@ -80,6 +80,9 @@ class UserTrackerImplTest : SysuiTestCase() {
    @Mock
    private lateinit var iActivityManager: IActivityManager

    @Mock
    private lateinit var beforeUserSwitchingReply: IRemoteCallback

    @Mock
    private lateinit var userSwitchingReply: IRemoteCallback

@@ -216,9 +219,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)
@@ -343,10 +347,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 +400,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()

@@ -449,8 +454,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)

@@ -465,6 +472,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    private class TestCallback : UserTracker.Callback {
        var calledOnBeforeUserChanging = 0
        var calledOnUserChanging = 0
        var calledOnUserChanged = 0
        var calledOnProfilesChanged = 0
@@ -472,6 +480,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