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

Commit 0d26dfb7 authored by Shan Huang's avatar Shan Huang
Browse files

Fix OnBackStarted being dispatched before the pointer is pilfered

Test: atest OnBackInvokedCallbackGestureTest
Test: atest BackAnimationControllerTest
Bug: 320838835

Change-Id: I5a8546e50079b22a348e53b03055ec364b120bf2
parent 6f38816b
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
    /** Tracks if we should start the back gesture on the next motion move event */
    private boolean mShouldStartOnNextMoveEvent = false;
    private boolean mOnBackStartDispatched = false;
    private boolean mPointerPilfered = false;

    private final FlingAnimationUtils mFlingAnimationUtils;

@@ -404,11 +405,12 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont

    @VisibleForTesting
    void onPilferPointers() {
        mPointerPilfered = true;
        // Dispatch onBackStarted, only to app callbacks.
        // System callbacks will receive onBackStarted when the remote animation starts.
        if (!shouldDispatchToAnimator() && mActiveCallback != null) {
            mCurrentTracker.updateStartLocation();
            tryDispatchAppOnBackStarted(mActiveCallback, mCurrentTracker.createStartEvent(null));
            tryDispatchOnBackStarted(mActiveCallback, mCurrentTracker.createStartEvent(null));
        }
    }

@@ -511,7 +513,7 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
            mActiveCallback = mBackNavigationInfo.getOnBackInvokedCallback();
            // App is handling back animation. Cancel system animation latency tracking.
            cancelLatencyTracking();
            tryDispatchAppOnBackStarted(mActiveCallback, touchTracker.createStartEvent(null));
            tryDispatchOnBackStarted(mActiveCallback, touchTracker.createStartEvent(null));
        }
    }

@@ -555,14 +557,13 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
                && mBackNavigationInfo.isPrepareRemoteAnimation();
    }

    private void tryDispatchAppOnBackStarted(
    private void tryDispatchOnBackStarted(
            IOnBackInvokedCallback callback,
            BackMotionEvent backEvent) {
        if (mOnBackStartDispatched && callback != null) {
        if (mOnBackStartDispatched || callback == null || !mPointerPilfered) {
            return;
        }
        dispatchOnBackStarted(callback, backEvent);
        mOnBackStartDispatched = true;
    }

    private void dispatchOnBackStarted(
@@ -573,6 +574,7 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
        }
        try {
            callback.onBackStarted(backEvent);
            mOnBackStartDispatched = true;
        } catch (RemoteException e) {
            Log.e(TAG, "dispatchOnBackStarted error: ", e);
        }
@@ -872,6 +874,7 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
        mActiveCallback = null;
        mShouldStartOnNextMoveEvent = false;
        mOnBackStartDispatched = false;
        mPointerPilfered = false;
        mShellBackAnimationRegistry.resetDefaultCrossActivity();
        cancelLatencyTracking();
        if (mBackNavigationInfo != null) {
@@ -957,15 +960,7 @@ public class BackAnimationController implements RemoteCallable<BackAnimationCont
                                        mCurrentTracker.updateStartLocation();
                                        BackMotionEvent startEvent =
                                                mCurrentTracker.createStartEvent(apps[0]);
                                        // {@code mActiveCallback} is the callback from
                                        // the BackAnimationRunners and not a real app-side
                                        // callback. We also dispatch to the app-side callback
                                        // (which should be a system callback with PRIORITY_SYSTEM)
                                        // to keep consistent with app registered callbacks.
                                        dispatchOnBackStarted(mActiveCallback, startEvent);
                                        tryDispatchAppOnBackStarted(
                                                mBackNavigationInfo.getOnBackInvokedCallback(),
                                                startEvent);
                                    }

                                    // Dispatch the first progress after animation start for