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

Commit 181c1789 authored by petsjonkin's avatar petsjonkin
Browse files

DreamService should execute runnables in place if already in main thread

Bug: b/394274826
Test: atest DreamServiceTest , manual testing
Flag: EXEMPT bugfix
Change-Id: Ic302b534509d0eeabea567d95a2607a03801ee4d
parent 3d4617fd
Loading
Loading
Loading
Loading
+13 −11
Original line number Original line Diff line number Diff line
@@ -341,7 +341,7 @@ public class DreamService extends Service implements Window.Callback {
     */
     */
    public interface WakefulHandler {
    public interface WakefulHandler {
        /** Posts a {@link Runnable} to be ran on the underlying {@link Handler}. */
        /** Posts a {@link Runnable} to be ran on the underlying {@link Handler}. */
        void post(Runnable r);
        void postIfNeeded(Runnable r);


        /**
        /**
         * Returns the underlying {@link Handler}. Should only be used for passing the handler into
         * Returns the underlying {@link Handler}. Should only be used for passing the handler into
@@ -386,8 +386,10 @@ public class DreamService extends Service implements Window.Callback {
        }
        }


        @Override
        @Override
        public void post(Runnable r) {
        public void postIfNeeded(Runnable r) {
            if (mWakeLock != null) {
            if (mHandler.getLooper().isCurrentThread()) {
                r.run();
            } else if (mWakeLock != null) {
                mHandler.post(mWakeLock.wrap(r));
                mHandler.post(mWakeLock.wrap(r));
            } else {
            } else {
                mHandler.post(r);
                mHandler.post(r);
@@ -995,17 +997,17 @@ public class DreamService extends Service implements Window.Callback {
        }
        }
    }
    }


    private void post(Runnable runnable) {
    private void postIfNeeded(Runnable runnable) {
        // The handler is based on the populated context is not ready at construction time.
        // The handler is based on the populated context is not ready at construction time.
        // therefore we fetch on demand.
        // therefore we fetch on demand.
        mInjector.getWakefulHandler().post(runnable);
        mInjector.getWakefulHandler().postIfNeeded(runnable);
    }
    }


    /**
    /**
     * Updates doze state. Note that this must be called on the mHandler.
     * Updates doze state. Note that this must be called on the mHandler.
     */
     */
    private void updateDoze() {
    private void updateDoze() {
        post(() -> {
        postIfNeeded(() -> {
            if (mDreamToken == null) {
            if (mDreamToken == null) {
                Slog.w(mTag, "Updating doze without a dream token.");
                Slog.w(mTag, "Updating doze without a dream token.");
                return;
                return;
@@ -1047,7 +1049,7 @@ public class DreamService extends Service implements Window.Callback {
     */
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    public void stopDozing() {
    public void stopDozing() {
        post(() -> {
        postIfNeeded(() -> {
            if (mDreamToken == null) {
            if (mDreamToken == null) {
                return;
                return;
            }
            }
@@ -1290,7 +1292,7 @@ public class DreamService extends Service implements Window.Callback {
                final long token = Binder.clearCallingIdentity();
                final long token = Binder.clearCallingIdentity();
                try {
                try {
                    // Simply finish dream when exit is requested.
                    // Simply finish dream when exit is requested.
                    post(() -> finishInternal());
                    postIfNeeded(() -> finishInternal());
                } finally {
                } finally {
                    Binder.restoreCallingIdentity(token);
                    Binder.restoreCallingIdentity(token);
                }
                }
@@ -1396,7 +1398,7 @@ public class DreamService extends Service implements Window.Callback {
     * </p>
     * </p>
     */
     */
    public final void finish() {
    public final void finish() {
        post(this::finishInternal);
        postIfNeeded(this::finishInternal);
    }
    }


    private void finishInternal() {
    private void finishInternal() {
@@ -1458,7 +1460,7 @@ public class DreamService extends Service implements Window.Callback {
     * </p>
     * </p>
     */
     */
    public final void wakeUp() {
    public final void wakeUp() {
        post(()-> wakeUp(false));
        postIfNeeded(()-> wakeUp(false));
    }
    }


    /**
    /**
@@ -1964,7 +1966,7 @@ public class DreamService extends Service implements Window.Callback {
                return;
                return;
            }
            }


            service.post(() -> consumer.accept(service));
            service.postIfNeeded(() -> consumer.accept(service));
        }
        }


        @Override
        @Override
+1 −1
Original line number Original line Diff line number Diff line
@@ -201,7 +201,7 @@ public class TestDreamEnvironment {
            doAnswer(invocation -> {
            doAnswer(invocation -> {
                ((Runnable) invocation.getArgument(0)).run();
                ((Runnable) invocation.getArgument(0)).run();
                return null;
                return null;
            }).when(mWakefulHandler).post(any());
            }).when(mWakefulHandler).postIfNeeded(any());
        }
        }
        @Override
        @Override
        public void init(Context context) {
        public void init(Context context) {