From c34a64d044d5e916c9f09e9b1ea34d7ffe3f6fd0 Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Wed, 27 Apr 2022 00:02:32 +0000 Subject: [PATCH] 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 --- .../android/service/dreams/DreamService.java | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java index 6a9afdb84e188..eca83e5badcc4 100644 --- a/core/java/android/service/dreams/DreamService.java +++ b/core/java/android/service/dreams/DreamService.java @@ -251,6 +251,8 @@ public class DreamService extends Service implements Window.Callback { // A Queue of pending requests to execute on the overlay. private final ArrayDeque> mRequests; + private Handler mHandler = new Handler(Looper.getMainLooper()); + private boolean mBound; OverlayConnection() { @@ -259,34 +261,40 @@ public class DreamService extends Service implements Window.Callback { public void bind(Context context, @Nullable ComponentName overlayService, ComponentName dreamService) { - if (overlayService == null) { - return; - } + mHandler.post(() -> { + if (overlayService == null) { + return; + } - final ServiceInfo serviceInfo = fetchServiceInfo(context, dreamService); + final ServiceInfo serviceInfo = fetchServiceInfo(context, dreamService); - final Intent overlayIntent = new Intent(); - overlayIntent.setComponent(overlayService); - overlayIntent.putExtra(EXTRA_SHOW_COMPLICATIONS, - fetchShouldShowComplications(context, serviceInfo)); + final Intent overlayIntent = new Intent(); + overlayIntent.setComponent(overlayService); + overlayIntent.putExtra(EXTRA_SHOW_COMPLICATIONS, + fetchShouldShowComplications(context, serviceInfo)); - context.bindService(overlayIntent, - this, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE); - mBound = true; + context.bindService(overlayIntent, + this, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE); + mBound = true; + }); } public void unbind(Context context) { - if (!mBound) { - return; - } + mHandler.post(() -> { + if (!mBound) { + return; + } - context.unbindService(this); - mBound = false; + context.unbindService(this); + mBound = false; + }); } public void request(Consumer request) { - mRequests.push(request); - evaluate(); + mHandler.post(() -> { + mRequests.push(request); + evaluate(); + }); } private void evaluate() { @@ -304,15 +312,19 @@ public class DreamService extends Service implements Window.Callback { @Override public void onServiceConnected(ComponentName name, IBinder service) { - // Store Overlay and execute pending requests. - mOverlay = IDreamOverlay.Stub.asInterface(service); - evaluate(); + mHandler.post(() -> { + // Store Overlay and execute pending requests. + mOverlay = IDreamOverlay.Stub.asInterface(service); + evaluate(); + }); } @Override public void onServiceDisconnected(ComponentName name) { - // Clear Overlay binder to prevent further request processing. - mOverlay = null; + mHandler.post(() -> { + // Clear Overlay binder to prevent further request processing. + mOverlay = null; + }); } }