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

Commit 84326475 authored by Miranda Kephart's avatar Miranda Kephart
Browse files

Post screenshot removeWindow call if necessary

It's possible to hit the removeWindow call at a point where we've
requested that the screenshot view be attached to the window, but it
hasn't actually been added yet. In this case, the removeWindow call does
nothing (since there's no actual view to detach), but then a frame or
two later the view is attached as requested, and never removed.

This change checks in the removeWindow method whether we've requested
the view be attached, and if so waits for it to be attached and then
detaches it.

Bug: 329659738
Bug: 337229903
Test: manual, by taking many screenshots in succession + artificially
increasing the delay between requesting attachment and attaching the
view
Flag: NONE

Change-Id: I5e40e61994cde251d6c819961d88c5b0b77d9eff
parent 351d100b
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -221,7 +221,8 @@ public class ScreenshotController {
    private Bitmap mScreenBitmap;
    private SaveImageInBackgroundTask mSaveInBgTask;
    private boolean mScreenshotTakenInPortrait;
    private boolean mBlockAttach;
    private boolean mAttachRequested;
    private boolean mDetachRequested;
    private Animator mScreenshotAnimation;
    private RequestCallback mCurrentRequestCallback;
    private ScreenshotActionsProvider mActionsProvider;
@@ -674,7 +675,7 @@ public class ScreenshotController {
                    new ViewTreeObserver.OnWindowAttachListener() {
                        @Override
                        public void onWindowAttached() {
                            mBlockAttach = false;
                            mAttachRequested = false;
                            decorView.getViewTreeObserver().removeOnWindowAttachListener(this);
                            action.run();
                        }
@@ -690,13 +691,13 @@ public class ScreenshotController {
    @MainThread
    private void attachWindow() {
        View decorView = mWindow.getDecorView();
        if (decorView.isAttachedToWindow() || mBlockAttach) {
        if (decorView.isAttachedToWindow() || mAttachRequested) {
            return;
        }
        if (DEBUG_WINDOW) {
            Log.d(TAG, "attachWindow");
        }
        mBlockAttach = true;
        mAttachRequested = true;
        mWindowManager.addView(decorView, mWindowLayoutParams);
        decorView.requestApplyInsets();
    }
@@ -708,6 +709,11 @@ public class ScreenshotController {
                Log.d(TAG, "Removing screenshot window");
            }
            mWindowManager.removeViewImmediate(decorView);
            mDetachRequested = false;
        }
        if (mAttachRequested && !mDetachRequested) {
            mDetachRequested = true;
            withWindowAttached(this::removeWindow);
        }

        mViewProxy.stopInputListening();