From 16b5913cb425455cba2c079b8442f06bbc777c72 Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Thu, 4 Nov 2021 00:28:10 -0700 Subject: [PATCH] Consolidate dream overlay binding logic. This changelist moves the binding logic to the dream overlay service into the OverlayConnection class within DreamService. This helps better track the binding lifecycle. Test: manual Test: atest DreamOverlayTest Bug: 205077932 Change-Id: I92959a9ecb53533d998abd711f960ef4fe0628d0 --- .../android/service/dreams/DreamService.java | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java index 9e351691310d1..3ab6907557da4 100644 --- a/core/java/android/service/dreams/DreamService.java +++ b/core/java/android/service/dreams/DreamService.java @@ -203,7 +203,6 @@ public class DreamService extends Service implements Window.Callback { private boolean mCanDoze; private boolean mDozing; private boolean mWindowless; - private boolean mOverlayServiceBound; private int mDozeScreenState = Display.STATE_UNKNOWN; private int mDozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT; @@ -220,10 +219,34 @@ public class DreamService extends Service implements Window.Callback { // A Queue of pending requests to execute on the overlay. private ArrayDeque> mRequests; + private boolean mBound; + OverlayConnection() { mRequests = new ArrayDeque<>(); } + public void bind(Context context, @Nullable ComponentName overlayService) { + if (overlayService == null) { + return; + } + + final Intent overlayIntent = new Intent(); + overlayIntent.setComponent(overlayService); + + context.bindService(overlayIntent, + this, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE); + mBound = true; + } + + public void unbind(Context context) { + if (!mBound) { + return; + } + + context.unbindService(this); + mBound = false; + } + public void request(Consumer request) { mRequests.push(request); evaluate(); @@ -930,14 +953,8 @@ public class DreamService extends Service implements Window.Callback { mDreamServiceWrapper = new DreamServiceWrapper(); // Connect to the overlay service if present. - final ComponentName overlayComponent = - intent.getParcelableExtra(EXTRA_DREAM_OVERLAY_COMPONENT); - if (overlayComponent != null && !mWindowless) { - final Intent overlayIntent = new Intent(); - overlayIntent.setComponent(overlayComponent); - - mOverlayServiceBound = getApplicationContext().bindService(overlayIntent, - mOverlayConnection, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE); + if (!mWindowless) { + mOverlayConnection.bind(this, intent.getParcelableExtra(EXTRA_DREAM_OVERLAY_COMPONENT)); } return mDreamServiceWrapper; @@ -973,10 +990,7 @@ public class DreamService extends Service implements Window.Callback { return; } - if (!mWindowless && mOverlayServiceBound) { - unbindService(mOverlayConnection); - mOverlayServiceBound = false; - } + mOverlayConnection.unbind(this); try { // finishSelf will unbind the dream controller from the dream service. This will