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

Commit f5ed8da1 authored by Bryce Lee's avatar Bryce Lee
Browse files

Reland "Ensure mDreamToken is only accessed on main thread."

Test: atest DreamServiceTest
Fixes: 382609257
Flag: EXEMPT bugfix
This reverts commit c20be361.

Change-Id: I550a7467355a30a43238db9a28c4dc4f95581cba
parent de78dfa6
Loading
Loading
Loading
Loading
+58 −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,11 @@ public class DreamService extends Service implements Window.Callback {
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    public void stopDozing() {
        mHandler.post(() -> {
            if (mDreamToken == null) {
                return;
            }

            if (mDozing) {
                mDozing = false;
                try {
@@ -974,6 +984,7 @@ public class DreamService extends Service implements Window.Callback {
                    // system server died
                }
            }
        });
    }

    /**
@@ -1201,7 +1212,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 +1310,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 +1372,7 @@ public class DreamService extends Service implements Window.Callback {
     * </p>
     */
    public final void wakeUp() {
        wakeUp(false);
        mHandler.post(()-> wakeUp(false));
    }

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

        mDreamToken = null;
@@ -1719,7 +1734,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;