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

Commit 19fcc80f authored by Bryce Lee's avatar Bryce Lee
Browse files

Ensure mDreamToken is only accessed on main thread.

This changelist updates access to mDreamToken to only happen on the
main thread. Most calls already happen this way for view and
lifecycle callbacks. However, a number of calls happen from other
invocations such as binder calls or invocations from DreamService
implementations. In such cases, the logic should be marshalled
onto the handler thread.

Fixes: 382609257
Flag: EXEMPT bugfix
Test: atest DreamServiceTest
Change-Id: Ifa142368838e7330ae042480d34bed0c23184894
parent 6c8bada5
Loading
Loading
Loading
Loading
+54 −43
Original line number Diff line number Diff line
@@ -455,7 +455,7 @@ public class DreamService extends Service implements Window.Callback {

                        // Simply wake up in the case the device is not locked.
                        if (!keyguardManager.isKeyguardLocked()) {
                            wakeUp();
                            wakeUp(false);
                            return true;
                        }

@@ -477,11 +477,11 @@ public class DreamService extends Service implements Window.Callback {

        if (!mInteractive) {
            if (mDebug) Slog.v(mTag, "Waking up on keyEvent");
            wakeUp();
            wakeUp(false);
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mDebug) Slog.v(mTag, "Waking up on back key");
            wakeUp();
            wakeUp(false);
            return true;
        }
        return mWindow.superDispatchKeyEvent(event);
@@ -492,7 +492,7 @@ public class DreamService extends Service implements Window.Callback {
    public boolean dispatchKeyShortcutEvent(KeyEvent event) {
        if (!mInteractive) {
            if (mDebug) Slog.v(mTag, "Waking up on keyShortcutEvent");
            wakeUp();
            wakeUp(false);
            return true;
        }
        return mWindow.superDispatchKeyShortcutEvent(event);
@@ -505,7 +505,7 @@ public class DreamService extends Service implements Window.Callback {
        // but finish()es on any other kind of activity
        if (!mInteractive && event.getActionMasked() == MotionEvent.ACTION_UP) {
            if (mDebug) Slog.v(mTag, "Waking up on touchEvent");
            wakeUp();
            wakeUp(false);
            return true;
        }
        return mWindow.superDispatchTouchEvent(event);
@@ -516,7 +516,7 @@ public class DreamService extends Service implements Window.Callback {
    public boolean dispatchTrackballEvent(MotionEvent event) {
        if (!mInteractive) {
            if (mDebug) Slog.v(mTag, "Waking up on trackballEvent");
            wakeUp();
            wakeUp(false);
            return true;
        }
        return mWindow.superDispatchTrackballEvent(event);
@@ -527,7 +527,7 @@ public class DreamService extends Service implements Window.Callback {
    public boolean dispatchGenericMotionEvent(MotionEvent event) {
        if (!mInteractive) {
            if (mDebug) Slog.v(mTag, "Waking up on genericMotionEvent");
            wakeUp();
            wakeUp(false);
            return true;
        }
        return mWindow.superDispatchGenericMotionEvent(event);
@@ -925,7 +925,11 @@ public class DreamService extends Service implements Window.Callback {
        }
    }

    private synchronized void updateDoze() {
    /**
     * Updates doze state. Note that this must be called on the mHandler.
     */
    private void updateDoze() {
        mHandler.post(() -> {
            if (mDreamToken == null) {
                Slog.w(mTag, "Updating doze without a dream token.");
                return;
@@ -951,6 +955,7 @@ public class DreamService extends Service implements Window.Callback {
                    // system server died
                }
            }
        });
    }

    /**
@@ -966,6 +971,7 @@ public class DreamService extends Service implements Window.Callback {
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    public void stopDozing() {
        mHandler.post(() -> {
            if (mDozing) {
                mDozing = false;
                try {
@@ -974,6 +980,7 @@ public class DreamService extends Service implements Window.Callback {
                    // system server died
                }
            }
        });
    }

    /**
@@ -1201,7 +1208,7 @@ public class DreamService extends Service implements Window.Callback {
            @Override
            public void onExitRequested() {
                // Simply finish dream when exit is requested.
                mHandler.post(() -> finish());
                mHandler.post(() -> finishInternal());
            }

            @Override
@@ -1299,9 +1306,13 @@ public class DreamService extends Service implements Window.Callback {
     * </p>
     */
    public final void finish() {
        mHandler.post(this::finishInternal);
    }

    private void finishInternal() {
        // If there is an active overlay connection, signal that the dream is ending before
        // continuing. Note that the overlay cannot rely on the unbound state, since another dream
        // might have bound to it in the meantime.
        // continuing. Note that the overlay cannot rely on the unbound state, since another
        // dream might have bound to it in the meantime.
        if (mOverlayConnection != null) {
            mOverlayConnection.addConsumer(overlay -> {
                try {
@@ -1357,7 +1368,7 @@ public class DreamService extends Service implements Window.Callback {
     * </p>
     */
    public final void wakeUp() {
        wakeUp(false);
        mHandler.post(()-> wakeUp(false));
    }

    /**
@@ -1559,7 +1570,7 @@ public class DreamService extends Service implements Window.Callback {
        if (mActivity != null && !mActivity.isFinishing()) {
            mActivity.finishAndRemoveTask();
        } else {
            finish();
            finishInternal();
        }

        mDreamToken = null;
@@ -1719,7 +1730,7 @@ public class DreamService extends Service implements Window.Callback {
                            // the window reference in order to fully release the DreamActivity.
                            mWindow = null;
                            mActivity = null;
                            finish();
                            finishInternal();
                        }

                        if (mOverlayConnection != null && mDreamStartOverlayConsumer != null) {
+1 −0
Original line number Diff line number Diff line
@@ -328,6 +328,7 @@ public class TestDreamEnvironment {
                case DREAM_STATE_STARTED -> startDream();
                case DREAM_STATE_WOKEN -> wakeDream();
            }
            mTestableLooper.processAllMessages();
        } while (mCurrentDreamState < state);

        return true;