Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b835d283 authored by Kean Mariotti's avatar Kean Mariotti
Browse files

viewcapture: fix NPE (WindowListener#mRoot)

Threads are scheduling accesses to WindowListener#mRoot to be performed
on the UI thread, but WindowListener#mRoot might become null by the time
the UI thread accesses it. This CL should cover such cases.

Bug: 375005884
Bug: 386904682
Flag: EXEMPT bugfix
Test: enable viewcapture on sysui windows and check the NPE \
        doesn't happen anymore on presubmit
Change-Id: I6f06fe1c6d92ea6606334fb65e5189f9a6dc4d51
parent 8f546b07
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -161,10 +161,13 @@ public abstract class ViewCapture {
    public void stopCapture(@NonNull View rootView) {
        mListeners.forEach(it -> {
            if (rootView == it.mRoot) {
                runOnUiThread(() -> it.mRoot.getViewTreeObserver().removeOnDrawListener(it),
                        it.mRoot);
                runOnUiThread(() -> {
                    if (it.mRoot != null) {
                        it.mRoot.getViewTreeObserver().removeOnDrawListener(it);
                        it.mRoot = null;
                    }
                }, it.mRoot);
            }
        });
    }

@@ -494,9 +497,11 @@ public abstract class ViewCapture {
        @AnyThread
        void detachFromRoot() {
            mIsActive = false;
            runOnUiThread(() -> {
                if (mRoot != null) {
                runOnUiThread(() -> mRoot.getViewTreeObserver().removeOnDrawListener(this), mRoot);
                    mRoot.getViewTreeObserver().removeOnDrawListener(this);
                }
            }, mRoot);
        }

        @UiThread