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

Commit 24c38549 authored by Matt Pietal's avatar Matt Pietal
Browse files

Delay handler messages until class is fully initialized

KeyguardViewMediator must wait for the both the #start() method on its
own class, as well as that of CentralSurfaces, since CentralSurfaces
needs to call #registerCentralSurfaces to complete the class
setup. Before this fix, the delay could cause an NPE when SystemUI was
restarted.

If this situation is detected, repost the same message but with a
small delay in order to check again.

Fixes: 319540806
Test: atest KeyguardViewMediatorTest
Flag: N/A
Change-Id: Ib821ff3742ac9bce67d05b6b29ae1ea9fcd42c5a
parent 6538db51
Loading
Loading
Loading
Loading
+24 −4
Original line number Diff line number Diff line
@@ -2507,8 +2507,18 @@ public class KeyguardViewMediator implements CoreStartable, Dumpable,
            String message = "";
            switch (msg.what) {
                case SHOW:
                    // There is a potential race condition when SysUI starts up. CentralSurfaces
                    // must invoke #registerCentralSurfaces on this class before any messages can be
                    // processed. If this happens, repost the message with a small delay and try
                    // again.
                    if (mCentralSurfaces == null) {
                        message = "DELAYING SHOW";
                        Message newMsg = mHandler.obtainMessage(SHOW, (Bundle) msg.obj);
                        mHandler.sendMessageDelayed(newMsg, 100);
                    } else {
                        message = "SHOW";
                        handleShow((Bundle) msg.obj);
                    }
                    break;
                case HIDE:
                    message = "HIDE";
@@ -2595,8 +2605,18 @@ public class KeyguardViewMediator implements CoreStartable, Dumpable,
                    Trace.endSection();
                    break;
                case SYSTEM_READY:
                    // There is a potential race condition when SysUI starts up. CentralSurfaces
                    // must invoke #registerCentralSurfaces on this class before any messages can be
                    // processed. If this happens, repost the message with a small delay and try
                    // again.
                    if (mCentralSurfaces == null) {
                        message = "DELAYING SYSTEM_READY";
                        Message newMsg = mHandler.obtainMessage(SYSTEM_READY);
                        mHandler.sendMessageDelayed(newMsg, 100);
                    } else {
                        message = "SYSTEM_READY";
                        handleSystemReady();
                    }
                    break;
            }
            Log.d(TAG, "KeyguardViewMediator queue processing message: " + message);
+29 −0
Original line number Diff line number Diff line
@@ -307,6 +307,28 @@ public class KeyguardViewMediatorTest extends SysuiTestCase {
        }
    }

    @Test
    @TestableLooper.RunWithLooper(setAsMainLooper = true)
    public void testRaceCondition_doNotRegisterCentralSurfacesImmediately() {
        create(false);

        // GIVEN central surfaces is not registered with KeyguardViewMediator, but a call to enable
        // keyguard comes in
        mViewMediator.onSystemReady();
        mViewMediator.setKeyguardEnabled(true);
        TestableLooper.get(this).processAllMessages();

        // If this step has been reached, then system ui has not crashed. Now register
        // CentralSurfaces
        assertFalse(mViewMediator.isShowingAndNotOccluded());
        register();
        TestableLooper.get(this).moveTimeForward(100);
        TestableLooper.get(this).processAllMessages();

        // THEN keyguard is shown
        assertTrue(mViewMediator.isShowingAndNotOccluded());
    }

    @Test
    @TestableLooper.RunWithLooper(setAsMainLooper = true)
    public void onLockdown_showKeyguard_evenIfKeyguardIsNotEnabledExternally() {
@@ -1139,6 +1161,11 @@ public class KeyguardViewMediatorTest extends SysuiTestCase {
    }

    private void createAndStartViewMediator(boolean orderUnlockAndWake) {
        create(orderUnlockAndWake);
        register();
    }

    private void create(boolean orderUnlockAndWake) {
        mContext.getOrCreateTestableResources().addOverride(
                com.android.internal.R.bool.config_orderUnlockAndWake, orderUnlockAndWake);

@@ -1189,7 +1216,9 @@ public class KeyguardViewMediatorTest extends SysuiTestCase {
                mSelectedUserInteractor,
                mKeyguardInteractor);
        mViewMediator.start();
    }

    private void register() {
        mViewMediator.registerCentralSurfaces(mCentralSurfaces, null, null, null, null, null);
    }