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

Commit 777bbee0 authored by Ikram Gabiyev's avatar Ikram Gabiyev
Browse files

Implement early animation cancel thru merge

Upon receiving a merge signal on expand PiP,
end the animation early and finish the current
transition.

Also make sure to clear mFinishCallback cache
before calling onTransitionFinish(). Otherwise,
we might have a merge candidate come in and cache its own
finishCallback before we clear it.

This should fix issues we have been having with PinnedStackTests,
where some transitions would be left unfinished causing breaks.

Bug: 379390590
Flag: com.android.wm.shell.enable_pip2
Test: atest PinnedStackTests
Change-Id: I14fe9f845c8ee5e039213fb823dbf07f1663afe1
parent 17262154
Loading
Loading
Loading
Loading
+33 −9
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static com.android.wm.shell.transition.Transitions.TRANSIT_EXIT_PIP;
import static com.android.wm.shell.transition.Transitions.TRANSIT_REMOVE_PIP;
import static com.android.wm.shell.transition.Transitions.TRANSIT_RESIZE_PIP;

import android.animation.ValueAnimator;
import android.annotation.NonNull;
import android.app.ActivityManager;
import android.app.PictureInPictureParams;
@@ -120,6 +121,8 @@ public class PipTransition extends PipTransitionController implements
    @Nullable
    private Transitions.TransitionFinishCallback mFinishCallback;

    private ValueAnimator mTransitionAnimator;

    public PipTransition(
            Context context,
            @NonNull ShellInit shellInit,
@@ -209,7 +212,12 @@ public class PipTransition extends PipTransitionController implements
    @Override
    public void mergeAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
            @NonNull SurfaceControl.Transaction t, @NonNull IBinder mergeTarget,
            @NonNull Transitions.TransitionFinishCallback finishCallback) {}
            @NonNull Transitions.TransitionFinishCallback finishCallback) {
        // Just jump-cut the current animation if any, but do not merge.
        if (info.getType() == TRANSIT_EXIT_PIP) {
            end();
        }
    }

    @Override
    public void onTransitionConsumed(@NonNull IBinder transition, boolean aborted,
@@ -271,6 +279,14 @@ public class PipTransition extends PipTransitionController implements
        return false;
    }

    @Override
    public void end() {
        if (mTransitionAnimator != null && mTransitionAnimator.isRunning()) {
            mTransitionAnimator.end();
            mTransitionAnimator = null;
        }
    }

    //
    // Animation schedulers and entry points
    //
@@ -438,7 +454,7 @@ public class PipTransition extends PipTransitionController implements
            }
            finishTransition();
        });
        animator.start();
        cacheAndStartTransitionAnimator(animator);
        return true;
    }

@@ -538,7 +554,7 @@ public class PipTransition extends PipTransitionController implements
                PipAlphaAnimator.FADE_IN);
        // This should update the pip transition state accordingly after we stop playing.
        animator.setAnimationEndCallback(this::finishTransition);
        animator.start();
        cacheAndStartTransitionAnimator(animator);
        return true;
    }

@@ -608,7 +624,7 @@ public class PipTransition extends PipTransitionController implements
            }
            finishTransition();
        });
        animator.start();
        cacheAndStartTransitionAnimator(animator);
        return true;
    }

@@ -834,17 +850,17 @@ public class PipTransition extends PipTransitionController implements
        return leash;
    }

    void cacheAndStartTransitionAnimator(@NonNull ValueAnimator animator) {
        mTransitionAnimator = animator;
        mTransitionAnimator.start();
    }

    //
    // Miscellaneous callbacks and listeners
    //

    @Override
    public void finishTransition() {
        if (mFinishCallback != null) {
            mFinishCallback.onTransitionFinished(null /* finishWct */);
            mFinishCallback = null;
        }

        final int currentState = mPipTransitionState.getState();
        int nextState = PipTransitionState.UNDEFINED;
        switch (currentState) {
@@ -859,6 +875,14 @@ public class PipTransition extends PipTransitionController implements
                break;
        }
        mPipTransitionState.setState(nextState);

        if (mFinishCallback != null) {
            // Need to unset mFinishCallback first because onTransitionFinished can re-enter this
            // handler if there is a pending PiP animation.
            final Transitions.TransitionFinishCallback finishCallback = mFinishCallback;
            mFinishCallback = null;
            finishCallback.onTransitionFinished(null /* finishWct */);
        }
    }

    @Override