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

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

Fix race condition causing screenshot view to be added to window twice

In some cases, invoking multiple screenshots in quick succession causes
an error as the decor view is added to the window twice. This change
blocks attempting to attach the window between telling the window
manager to add the decor view and getting the callback that it has
been added.

Bug: 190249438
Fix: 190249438
Test: manual
Change-Id: I2bc3a1322651ab312e75bb9e9426909db368d84e
parent df2554e6
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import static java.util.Objects.requireNonNull;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.MainThread;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityOptions;
@@ -261,6 +262,7 @@ public class ScreenshotController {
    private Bitmap mScreenBitmap;
    private SaveImageInBackgroundTask mSaveInBgTask;
    private boolean mScreenshotTakenInPortrait;
    private boolean mBlockAttach;

    private Animator mScreenshotAnimation;
    private RequestCallback mCurrentRequestCallback;
@@ -731,6 +733,7 @@ public class ScreenshotController {
                    new ViewTreeObserver.OnWindowAttachListener() {
                        @Override
                        public void onWindowAttached() {
                            mBlockAttach = false;
                            decorView.getViewTreeObserver().removeOnWindowAttachListener(this);
                            action.run();
                        }
@@ -747,14 +750,16 @@ public class ScreenshotController {
        mWindow.setContentView(contentView);
    }

    @MainThread
    private void attachWindow() {
        View decorView = mWindow.getDecorView();
        if (decorView.isAttachedToWindow()) {
        if (decorView.isAttachedToWindow() || mBlockAttach) {
            return;
        }
        if (DEBUG_WINDOW) {
            Log.d(TAG, "attachWindow");
        }
        mBlockAttach = true;
        mWindowManager.addView(decorView, mWindowLayoutParams);
        decorView.requestApplyInsets();
    }