From 2843c5bf634b02f0e634659f9c88a5de9a309ac8 Mon Sep 17 00:00:00 2001 From: Victor Truong Date: Wed, 8 Feb 2023 09:42:14 -0500 Subject: [PATCH] Add overlayfinishing state. There was an error due to unbind during an async function causing the connection to be set to null before unbind is called. Add overlayfinishing state to prevent overlay finishing consumer to be added twice causing a NullPointerException. Bug: 267335683 Test: Manually tested to see that the error can no longer be repro'd. Change-Id: I1cfb5bd68c6568e304bb55064794aa0e09075d56 --- core/java/android/service/dreams/DreamService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java index 6a4710f9475a5..cfbc75ab377b1 100644 --- a/core/java/android/service/dreams/DreamService.java +++ b/core/java/android/service/dreams/DreamService.java @@ -234,6 +234,7 @@ public class DreamService extends Service implements Window.Callback { private boolean mCanDoze; private boolean mDozing; private boolean mWindowless; + private boolean mOverlayFinishing; private int mDozeScreenState = Display.STATE_UNKNOWN; private int mDozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT; @@ -1051,6 +1052,7 @@ public class DreamService extends Service implements Window.Callback { // We must unbind from any overlay connection if we are unbound before finishing. if (mOverlayConnection != null) { mOverlayConnection.unbind(); + mOverlayConnection = null; } return super.onUnbind(intent); @@ -1067,7 +1069,9 @@ public class DreamService extends Service implements Window.Callback { // If there is an active overlay connection, signal that the dream is ending before // continuing. Note that the overlay cannot rely on the unbound state, since another dream // might have bound to it in the meantime. - if (mOverlayConnection != null) { + if (mOverlayConnection != null && !mOverlayFinishing) { + // Set mOverlayFinish to true to only allow this consumer to be added once. + mOverlayFinishing = true; mOverlayConnection.addConsumer(overlay -> { try { overlay.endDream();