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

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

Move onBeforeUserSwitching call to the beginning of the user switch.

Bug: 331853529
Bug: 360838273
Test: atest FrameworksServicesTests:UserControllerTest
Flag: EXEMPT bugfix
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:bd1399244a3cef79f5d8e3b07b2e29452685ff0a)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:975974c906a9447e0009a41d3ed37b35a7f7ddc8)
Merged-In: I03e3756194ea0565ea5ce1b5ac383beffae36839
Change-Id: I03e3756194ea0565ea5ce1b5ac383beffae36839
parent a09b6451
Loading
Loading
Loading
Loading
+20 −11
Original line number Diff line number Diff line
@@ -1925,6 +1925,7 @@ class UserController implements Handler.Callback {
                // it should be moved outside, but for now it's not as there are many calls to
                // external components here afterwards
                updateProfileRelatedCaches();
                dispatchOnBeforeUserSwitching(userId);
                mInjector.getWindowManager().setCurrentUser(userId);
                mInjector.reportCurWakefulnessUsageEvent();
                // Once the internal notion of the active user has switched, we lock the device
@@ -2229,6 +2230,25 @@ class UserController implements Handler.Callback {
        mUserSwitchObservers.finishBroadcast();
    }

    private void dispatchOnBeforeUserSwitching(@UserIdInt int newUserId) {
        final TimingsTraceAndSlog t = new TimingsTraceAndSlog();
        t.traceBegin("dispatchOnBeforeUserSwitching-" + newUserId);
        final int observerCount = mUserSwitchObservers.beginBroadcast();
        for (int i = 0; i < observerCount; i++) {
            final String name = "#" + i + " " + mUserSwitchObservers.getBroadcastCookie(i);
            t.traceBegin("onBeforeUserSwitching-" + name);
            try {
                mUserSwitchObservers.getBroadcastItem(i).onBeforeUserSwitching(newUserId);
            } catch (RemoteException e) {
                // Ignore
            } finally {
                t.traceEnd();
            }
        }
        mUserSwitchObservers.finishBroadcast();
        t.traceEnd();
    }

    /** Called on handler thread */
    @VisibleForTesting
    void dispatchUserSwitchComplete(@UserIdInt int oldUserId, @UserIdInt int newUserId) {
@@ -2411,17 +2431,6 @@ class UserController implements Handler.Callback {

        final int observerCount = mUserSwitchObservers.beginBroadcast();
        if (observerCount > 0) {
            for (int i = 0; i < observerCount; i++) {
                final String name = "#" + i + " " + mUserSwitchObservers.getBroadcastCookie(i);
                t.traceBegin("onBeforeUserSwitching-" + name);
                try {
                    mUserSwitchObservers.getBroadcastItem(i).onBeforeUserSwitching(newUserId);
                } catch (RemoteException e) {
                    // Ignore
                } finally {
                    t.traceEnd();
                }
            }
            final ArraySet<String> curWaitingUserSwitchCallbacks = new ArraySet<>();
            synchronized (mLock) {
                uss.switching = true;
+2 −2
Original line number Diff line number Diff line
@@ -418,6 +418,7 @@ public class UserControllerTest {
        mUserController.registerUserSwitchObserver(observer, "mock");
        // Start user -- this will update state of mUserController
        mUserController.startUser(TEST_USER_ID, USER_START_MODE_FOREGROUND);
        verify(observer, times(1)).onBeforeUserSwitching(eq(TEST_USER_ID));
        Message reportMsg = mInjector.mHandler.getMessageForCode(REPORT_USER_SWITCH_MSG);
        assertNotNull(reportMsg);
        UserState userState = (UserState) reportMsg.obj;
@@ -426,7 +427,6 @@ public class UserControllerTest {
        // Call dispatchUserSwitch and verify that observer was called only once
        mInjector.mHandler.clearAllRecordedMessages();
        mUserController.dispatchUserSwitch(userState, oldUserId, newUserId);
        verify(observer, times(1)).onBeforeUserSwitching(eq(TEST_USER_ID));
        verify(observer, times(1)).onUserSwitching(eq(TEST_USER_ID), any());
        Set<Integer> expectedCodes = Collections.singleton(CONTINUE_USER_SWITCH_MSG);
        Set<Integer> actualCodes = mInjector.mHandler.getMessageCodes();
@@ -449,6 +449,7 @@ public class UserControllerTest {
        mUserController.registerUserSwitchObserver(observer, "mock");
        // Start user -- this will update state of mUserController
        mUserController.startUser(TEST_USER_ID, USER_START_MODE_FOREGROUND);
        verify(observer, times(1)).onBeforeUserSwitching(eq(TEST_USER_ID));
        Message reportMsg = mInjector.mHandler.getMessageForCode(REPORT_USER_SWITCH_MSG);
        assertNotNull(reportMsg);
        UserState userState = (UserState) reportMsg.obj;
@@ -457,7 +458,6 @@ public class UserControllerTest {
        // Call dispatchUserSwitch and verify that observer was called only once
        mInjector.mHandler.clearAllRecordedMessages();
        mUserController.dispatchUserSwitch(userState, oldUserId, newUserId);
        verify(observer, times(1)).onBeforeUserSwitching(eq(TEST_USER_ID));
        verify(observer, times(1)).onUserSwitching(eq(TEST_USER_ID), any());
        // Verify that CONTINUE_USER_SWITCH_MSG is not sent (triggers timeout)
        Set<Integer> actualCodes = mInjector.mHandler.getMessageCodes();