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

Commit f149add9 authored by Valentin Iftime's avatar Valentin Iftime
Browse files

Remove ToastOutAnimatorListener on animation end

 Cleanup animation listeners to prevent memory leak.

Test: atest SystemUITests:com.android.systemui.toast.ToastUITest#testShowToast_afterShowToast_correctCleanup
Bug: 296995785
Change-Id: I4c6abc2db28c3d9bb9d7662f5bf037da72aa0fc4
parent 9118f780
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ public class ToastUI implements CoreStartable, CommandQueue.Callbacks {
    private final ToastLogger mToastLogger;
    @Nullable private ToastPresenter mPresenter;
    @Nullable private ITransientNotificationCallback mCallback;
    private ToastOutAnimatorListener mToastOutAnimatorListener;
    @VisibleForTesting ToastOutAnimatorListener mToastOutAnimatorListener;

    @VisibleForTesting SystemUIToast mToast;
    private int mOrientation = ORIENTATION_PORTRAIT;
@@ -172,7 +172,7 @@ public class ToastUI implements CoreStartable, CommandQueue.Callbacks {
        if (mToast.getOutAnimation() != null) {
            Animator animator = mToast.getOutAnimation();
            mToastOutAnimatorListener = new ToastOutAnimatorListener(mPresenter, mCallback,
                    runnable);
                    runnable, animator);
            animator.addListener(mToastOutAnimatorListener);
            animator.start();
        } else {
@@ -211,14 +211,17 @@ public class ToastUI implements CoreStartable, CommandQueue.Callbacks {
        final ToastPresenter mPrevPresenter;
        final ITransientNotificationCallback mPrevCallback;
        @Nullable Runnable mShowNextToastRunnable;
        @NonNull private final Animator mAnimator;

        ToastOutAnimatorListener(
                @NonNull ToastPresenter presenter,
                @NonNull ITransientNotificationCallback callback,
                @Nullable Runnable runnable) {
                @Nullable Runnable runnable,
                @NonNull Animator animator) {
            mPrevPresenter = presenter;
            mPrevCallback = callback;
            mShowNextToastRunnable = runnable;
            mAnimator = animator;
        }

        void setShowNextToastRunnable(Runnable runnable) {
@@ -231,6 +234,8 @@ public class ToastUI implements CoreStartable, CommandQueue.Callbacks {
            if (mShowNextToastRunnable != null) {
                mShowNextToastRunnable.run();
            }
            mAnimator.removeListener(this);
            mShowNextToastRunnable = null;
            mToastOutAnimatorListener = null;
        }
    }
+25 −0
Original line number Diff line number Diff line
@@ -341,6 +341,31 @@ public class ToastUITest extends SysuiTestCase {
        verify(mCallback).onToastHidden();
    }

    @Test
    public void testShowToast_afterShowToast_animationListenerCleanup() throws RemoteException {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,
                mCallback, Display.DEFAULT_DISPLAY);
        final SystemUIToast toast = mToastUI.mToast;

        View view = verifyWmAddViewAndAttachToParent();
        mToastUI.showToast(UID_2, PACKAGE_NAME_2, TOKEN_2, TEXT, WINDOW_TOKEN_2, Toast.LENGTH_LONG,
                null, Display.DEFAULT_DISPLAY);

        if (toast.getOutAnimation() != null) {
            assertThat(mToastUI.mToastOutAnimatorListener).isNotNull();
            assertThat(toast.getOutAnimation().getListeners()
                .contains(mToastUI.mToastOutAnimatorListener)).isTrue();
            assertThat(toast.getOutAnimation().isRunning()).isTrue();
            toast.getOutAnimation().cancel(); // end early if applicable
            assertThat(toast.getOutAnimation().getListeners()).isNull();
        }

        verify(mWindowManager).removeViewImmediate(view);
        verify(mNotificationManager).finishToken(PACKAGE_NAME_1, TOKEN_1);
        verify(mCallback).onToastHidden();
        assertThat(mToastUI.mToastOutAnimatorListener).isNull();
    }

    @Test
    public void testShowToast_logs() {
        mToastUI.showToast(UID_1, PACKAGE_NAME_1, TOKEN_1, TEXT, WINDOW_TOKEN_1, Toast.LENGTH_LONG,