From b8a2a026a59175f175580e35c13ec3abbca895a8 Mon Sep 17 00:00:00 2001 From: Winson Chung Date: Mon, 15 Aug 2022 23:44:42 +0000 Subject: [PATCH] Defer adding surface changed callback until view root is valid - It appears there are cases where the view root is not valid (in which case schedule() returns false and we don't wait to handle the callback) which causes an NPE, but in these cases we don't need to add the surface changed callback at all. Bug: 202776119 Bug: 242347940 Bug: 242897135 Test: Launch an app with sharesheet, swipe up and verify that screenshot callbacks Change-Id: Icc3afc604bd925c5d49c693b7de51755f5a8bd42 --- quickstep/src/com/android/quickstep/ViewUtils.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/quickstep/src/com/android/quickstep/ViewUtils.java b/quickstep/src/com/android/quickstep/ViewUtils.java index 1bb95b97f9..b1320674e6 100644 --- a/quickstep/src/com/android/quickstep/ViewUtils.java +++ b/quickstep/src/com/android/quickstep/ViewUtils.java @@ -53,13 +53,13 @@ public class ViewUtils { final Runnable mFinishCallback; final BooleanSupplier mCancelled; final Handler mHandler; + boolean mSurfaceCallbackRegistered = false; boolean mFinished; int mDeferFrameCount = 1; FrameHandler(View view, Runnable finishCallback, BooleanSupplier cancelled) { mViewRoot = view.getViewRootImpl(); - mViewRoot.addSurfaceChangedCallback(this); mFinishCallback = finishCallback; mCancelled = cancelled; mHandler = new Handler(); @@ -103,6 +103,10 @@ public class ViewUtils { private boolean schedule() { if (mViewRoot != null && mViewRoot.getView() != null) { + if (!mSurfaceCallbackRegistered) { + mSurfaceCallbackRegistered = true; + mViewRoot.addSurfaceChangedCallback(this); + } mViewRoot.registerRtFrameCallback(this); mViewRoot.getView().invalidate(); return true; @@ -119,7 +123,10 @@ public class ViewUtils { if (mFinishCallback != null) { mFinishCallback.run(); } - mViewRoot.removeSurfaceChangedCallback(this); + if (mViewRoot != null) { + mViewRoot.removeSurfaceChangedCallback(this); + mSurfaceCallbackRegistered = false; + } } } }