Fix a BadTokenException in DreamOverlayService.

This exception occurs when adding the DreamOverlayService's DecorView to
WindowManager (presumably because the service has already been
destroyed).

Test: atest DreamOverlayServiceTest
Bug: 221872102
Change-Id: I9eb40c518dc24a92ea06949a0701295883c4f150
This commit is contained in:
Will
2022-03-07 11:29:23 -08:00
parent 2cb1f3534f
commit 95419ca1c5
2 changed files with 31 additions and 0 deletions

View File

@@ -65,6 +65,9 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
// A reference to the {@link Window} used to hold the dream overlay.
private Window mWindow;
// True if the service has been destroyed.
private boolean mDestroyed;
private final Complication.Host mHost = new Complication.Host() {
@Override
public void requestExitDream() {
@@ -134,6 +137,7 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
mPreviewComplication.setDreamLabel(null);
mStateController.removeComplication(mPreviewComplication);
mStateController.setPreviewMode(false);
mDestroyed = true;
super.onDestroy();
}
@@ -141,6 +145,11 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ
public void onStartDream(@NonNull WindowManager.LayoutParams layoutParams) {
setCurrentState(Lifecycle.State.STARTED);
mExecutor.execute(() -> {
if (mDestroyed) {
// The task could still be executed after the service has been destroyed. Bail if
// that is the case.
return;
}
mStateController.setShouldShowComplications(shouldShowComplications());
mStateController.setPreviewMode(isPreviewMode());
if (isPreviewMode()) {

View File

@@ -19,6 +19,7 @@ package com.android.systemui.dreams;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -222,4 +223,25 @@ public class DreamOverlayServiceTest extends SysuiTestCase {
verify(mLifecycleRegistry).setCurrentState(Lifecycle.State.DESTROYED);
verify(mStateController).setOverlayActive(false);
}
@Test
public void testDecorViewNotAddedToWindowAfterDestroy() throws Exception {
when(mDreamOverlayContainerView.getParent())
.thenReturn(mDreamOverlayContainerViewParent)
.thenReturn(null);
final IBinder proxy = mService.onBind(new Intent());
final IDreamOverlay overlay = IDreamOverlay.Stub.asInterface(proxy);
// Inform the overlay service of dream starting.
overlay.startDream(mWindowParams, mDreamOverlayCallback);
// Destroy the service.
mService.onDestroy();
// Run executor tasks.
mMainExecutor.runAllReady();
verify(mWindowManager, never()).addView(any(), any());
}
}