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
This commit is contained in:
Bryce Lee
2021-11-04 00:28:10 -07:00
parent bfc555ea4a
commit 16b5913cb4

View File

@@ -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<Consumer<IDreamOverlay>> 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<IDreamOverlay> 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