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

Commit e678ab68 authored by George Mount's avatar George Mount
Browse files

Wait 2 frames before hiding shared elements.

Bug 15991516

Because of double and triple buffering, we can't guarantee
that the called Activity will show shared elements prior to
the calling activity hiding them. This forces the called
Activity to wait 2 frames prior to telling the calling activity
to hide its shared elements.

Change-Id: Ia49e71637ea04f8d7f93093abf6f47b00dcc7c06
parent a5736291
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ class EnterTransitionCoordinator extends ActivityTransitionCoordinator {
    private static final String TAG = "EnterTransitionCoordinator";

    private static final long MAX_WAIT_MS = 1000;
    private static final int MIN_ANIMATION_FRAMES = 2;

    private boolean mSharedElementTransitionStarted;
    private Activity mActivity;
@@ -280,10 +281,22 @@ class EnterTransitionCoordinator extends ActivityTransitionCoordinator {
        setOriginalImageViewState(originalImageViewState);

        if (mResultReceiver != null) {
            // We can't trust that the view will disappear on the same frame that the shared
            // element appears here. Assure that we get at least 2 frames for double-buffering.
            getDecor().postOnAnimation(new Runnable() {
                int mAnimations;
                @Override
                public void run() {
                    if (mAnimations++ < MIN_ANIMATION_FRAMES) {
                        getDecor().postOnAnimation(this);
                    } else {
                        mResultReceiver.send(MSG_HIDE_SHARED_ELEMENTS, null);
        }
                        mResultReceiver = null; // all done sending messages.
                    }
                }
            });
        }
    }

    @Override
    protected void stripOffscreenViews() {
+7 −29
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.transition.Transition;
import android.transition.TransitionManager;
import android.util.ArrayMap;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroupOverlay;
import android.view.ViewTreeObserver;

@@ -65,8 +66,6 @@ class ExitTransitionCoordinator extends ActivityTransitionCoordinator {

    private Bundle mExitSharedElementBundle;

    private ArrayList<View> mSharedElementSnapshots;

    public ExitTransitionCoordinator(Activity activity, ArrayList<String> names,
            ArrayList<String> accepted, ArrayList<View> mapped, boolean isReturning) {
        super(activity.getWindow(), names, getListener(activity, isReturning),
@@ -131,17 +130,14 @@ class ExitTransitionCoordinator extends ActivityTransitionCoordinator {
        Transition transition = getSharedElementExitTransition();
        final ArrayList<View> sharedElementSnapshots = createSnapshots(mExitSharedElementBundle,
                mSharedElementNames);
        mSharedElementSnapshots = createSnapshots(mExitSharedElementBundle, mSharedElementNames);
        transition.addListener(new Transition.TransitionListenerAdapter() {
            @Override
            public void onTransitionEnd(Transition transition) {
                transition.removeListener(this);
                setViewVisibility(mSharedElements, View.INVISIBLE);
                ViewGroupOverlay overlay = getDecor().getOverlay();
                if (mSharedElementSnapshots != null) {
                    for (int i = 0; i < mSharedElementSnapshots.size(); i++) {
                        overlay.add(mSharedElementSnapshots.get(i));
                    }
                int count = mSharedElements.size();
                for (int i = 0; i < count; i++) {
                    View sharedElement = mSharedElements.get(i);
                    ((ViewGroup)sharedElement.getParent()).suppressLayout(true);
                }
            }
        });
@@ -158,28 +154,10 @@ class ExitTransitionCoordinator extends ActivityTransitionCoordinator {
        getDecor().invalidate();
    }

    private static ArrayList<View> copySnapshots(ArrayList<View> snapshots) {
        ArrayList<View> copy = new ArrayList<View>(snapshots.size());
        for (int i = 0; i < snapshots.size(); i++) {
            View view = snapshots.get(i);
            View viewCopy = new View(view.getContext());
            viewCopy.setBackground(view.getBackground());
            copy.add(viewCopy);
        }
        return copy;
    }

    private void hideSharedElements() {
        if (!mIsHidden) {
            setViewVisibility(mSharedElements, View.INVISIBLE);
        }
        if (mSharedElementSnapshots != null) {
            ViewGroupOverlay overlay = getDecor().getOverlay();
            for (int i = 0; i < mSharedElementSnapshots.size(); i++) {
                overlay.remove(mSharedElementSnapshots.get(i));
            }
            mSharedElementSnapshots = null;
        }
        finishIfNecessary();
    }

@@ -360,7 +338,8 @@ class ExitTransitionCoordinator extends ActivityTransitionCoordinator {
    }

    private void finishIfNecessary() {
        if (mIsReturning && mExitNotified && mActivity != null && mSharedElementSnapshots == null) {
        if (mIsReturning && mExitNotified && mActivity != null && (mSharedElements.isEmpty() ||
                mSharedElements.get(0).getVisibility() == View.INVISIBLE)) {
            finish();
        }
        if (!mIsReturning && mExitNotified) {
@@ -382,7 +361,6 @@ class ExitTransitionCoordinator extends ActivityTransitionCoordinator {
            mBackgroundAnimator = null;
        }
        mExitSharedElementBundle = null;
        mSharedElementSnapshots = null;
        clearState();
    }