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

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

Marshal dream overlay connection logic with Handler.

This changelist ensures all activity on the dream overlay service
connection occurs on the same thread with the user of a Handler.

Fixed: 230269347
Test: atest DreamOverlayTest
Change-Id: If3485de7225a4220457d297527d6c358076368a4
parent 8276fac6
Loading
Loading
Loading
Loading
+35 −23
Original line number Original line Diff line number Diff line
@@ -251,6 +251,8 @@ public class DreamService extends Service implements Window.Callback {
        // A Queue of pending requests to execute on the overlay.
        // A Queue of pending requests to execute on the overlay.
        private final ArrayDeque<Consumer<IDreamOverlay>> mRequests;
        private final ArrayDeque<Consumer<IDreamOverlay>> mRequests;


        private Handler mHandler = new Handler(Looper.getMainLooper());

        private boolean mBound;
        private boolean mBound;


        OverlayConnection() {
        OverlayConnection() {
@@ -259,6 +261,7 @@ public class DreamService extends Service implements Window.Callback {


        public void bind(Context context, @Nullable ComponentName overlayService,
        public void bind(Context context, @Nullable ComponentName overlayService,
                ComponentName dreamService) {
                ComponentName dreamService) {
            mHandler.post(() -> {
                if (overlayService == null) {
                if (overlayService == null) {
                    return;
                    return;
                }
                }
@@ -273,20 +276,25 @@ public class DreamService extends Service implements Window.Callback {
                context.bindService(overlayIntent,
                context.bindService(overlayIntent,
                        this, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE);
                        this, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE);
                mBound = true;
                mBound = true;
            });
        }
        }


        public void unbind(Context context) {
        public void unbind(Context context) {
            mHandler.post(() -> {
                if (!mBound) {
                if (!mBound) {
                    return;
                    return;
                }
                }


                context.unbindService(this);
                context.unbindService(this);
                mBound = false;
                mBound = false;
            });
        }
        }


        public void request(Consumer<IDreamOverlay> request) {
        public void request(Consumer<IDreamOverlay> request) {
            mHandler.post(() -> {
                mRequests.push(request);
                mRequests.push(request);
                evaluate();
                evaluate();
            });
        }
        }


        private void evaluate() {
        private void evaluate() {
@@ -304,15 +312,19 @@ public class DreamService extends Service implements Window.Callback {


        @Override
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        public void onServiceConnected(ComponentName name, IBinder service) {
            mHandler.post(() -> {
                // Store Overlay and execute pending requests.
                // Store Overlay and execute pending requests.
                mOverlay = IDreamOverlay.Stub.asInterface(service);
                mOverlay = IDreamOverlay.Stub.asInterface(service);
                evaluate();
                evaluate();
            });
        }
        }


        @Override
        @Override
        public void onServiceDisconnected(ComponentName name) {
        public void onServiceDisconnected(ComponentName name) {
            mHandler.post(() -> {
                // Clear Overlay binder to prevent further request processing.
                // Clear Overlay binder to prevent further request processing.
                mOverlay = null;
                mOverlay = null;
            });
        }
        }
    }
    }