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

Commit 748d8d0d authored by Louis Chang's avatar Louis Chang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "revert-bubble-back" into main

* changes:
  Revert "Use customized back-invoked-callback when intercepting back"
  Revert "Intercept back for task root activity only"
parents daeb7668 92de5ff8
Loading
Loading
Loading
Loading
+8 −86
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
import static android.view.WindowManager.TRANSIT_CHANGE;
import static android.view.WindowManager.TRANSIT_PREPARE_BACK_NAVIGATION;
import static android.window.DesktopExperienceFlags.ENABLE_INDEPENDENT_BACK_IN_PROJECTED;
import static android.window.OnBackInvokedDispatcher.PRIORITY_DEFAULT;
import static android.window.SystemOverrideOnBackInvokedCallback.OVERRIDE_FINISH_AND_REMOVE_TASK;
import static android.window.SystemOverrideOnBackInvokedCallback.OVERRIDE_UNDEFINED;

@@ -61,11 +60,8 @@ import android.view.RemoteAnimationTarget;
import android.view.SurfaceControl;
import android.view.WindowInsets;
import android.window.BackAnimationAdapter;
import android.window.BackMotionEvent;
import android.window.BackNavigationInfo;
import android.window.IBackAnimationFinishedCallback;
import android.window.IBackAnimationHandoffHandler;
import android.window.IOnBackInvokedCallback;
import android.window.IWindowlessStartingSurfaceCallback;
import android.window.OnBackInvokedCallbackInfo;
import android.window.SystemOverrideOnBackInvokedCallback;
@@ -78,7 +74,6 @@ import com.android.internal.protolog.ProtoLog;
import com.android.window.flags.Flags;

import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
@@ -140,63 +135,6 @@ class BackNavigationController {
        }
    }

    class OnInterceptBackInvokedCallback extends IOnBackInvokedCallback.Stub {
        @NonNull
        final WeakReference<ActivityRecord> mActivityRecordRef;
        @Nullable
        final WeakReference<IOnBackInvokedCallback> mFallbackCallbackRef;

        OnInterceptBackInvokedCallback(@NonNull ActivityRecord r,
                @Nullable IOnBackInvokedCallback fallback) {
            mActivityRecordRef = new WeakReference<>(r);
            mFallbackCallbackRef = fallback != null ? new WeakReference<>(fallback) : null;
        }

        @Override
        public void onBackInvoked() {
            synchronized (mWindowManagerService.mGlobalLock) {
                final ActivityRecord r = mActivityRecordRef.get();
                if (r != null && mWindowManagerService.mAtmService.mTaskOrganizerController
                        .handleInterceptBackPressedOnTaskRoot(r)) {
                    // Handled by the controller, exit early.
                    return;
                }

                if (mFallbackCallbackRef == null) {
                    return;
                }

                // Try the fallback callback if the back event was not handled.
                final IOnBackInvokedCallback fallbackCallback = mFallbackCallbackRef.get();
                if (fallbackCallback != null) {
                    try {
                        fallbackCallback.onBackInvoked();
                    } catch (RemoteException ex) {
                        Slog.w(TAG, "Failed invoking fallback callback for back");
                    }
                }
            }
        }

        @Override
        public void onBackCancelled() {}

        @Override
        public void onBackProgressed(BackMotionEvent backMotionEvent) {
        }

        @Override
        public void onBackStarted(BackMotionEvent backEvent) {
        }

        @Override
        public void setTriggerBack(boolean triggerBack) {
        }

        @Override
        public void setHandoffHandler(IBackAnimationHandoffHandler unused) {
        }
    }
    /**
     * Set up the necessary leashes and build a {@link BackNavigationInfo} instance for an upcoming
     * back gesture animation.
@@ -304,9 +242,8 @@ class BackNavigationController {
                ProtoLog.d(WM_DEBUG_BACK_PREVIEW, "Focus window is closing.");
                return null;
            }

            final OnBackInvokedCallbackInfo callbackInfo = getOnBackInvokedCallbackInfo(
                    window, currentTask, currentActivity);
            // Now let's find if this window has a callback from the client side.
            final OnBackInvokedCallbackInfo callbackInfo = window.getOnBackInvokedCallbackInfo();
            if (callbackInfo == null) {
                Slog.e(TAG, "No callback registered, returning null.");
                return null;
@@ -529,27 +466,6 @@ class BackNavigationController {
        }
    }

    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    @Nullable OnBackInvokedCallbackInfo getOnBackInvokedCallbackInfo(@NonNull WindowState window,
            @Nullable Task task, @Nullable ActivityRecord activity) {
        final OnBackInvokedCallbackInfo info = window.getOnBackInvokedCallbackInfo();
        if (activity == null || task == null) {
            return info;
        }

        final ActivityRecord root = task.getRootActivity(
                false /*ignoreRelinquishIdentity*/, true /*setToBottomIfNone*/);
        if (activity != root || !task.mAtmService.mTaskOrganizerController
                        .shouldInterceptBackPressedOnRootTask(task.getRootTask())) {
            return info;
        }

        final IOnBackInvokedCallback callback = new OnInterceptBackInvokedCallback(activity,
                info != null ? info.getCallback() : null);
        return new OnBackInvokedCallbackInfo(callback, PRIORITY_DEFAULT, false,
                    OVERRIDE_UNDEFINED);
    }

    /**
     * Gets previous activities from currentActivity.
     *
@@ -559,6 +475,12 @@ class BackNavigationController {
    static boolean getAnimatablePrevActivities(@NonNull Task currentTask,
            @NonNull ActivityRecord currentActivity,
            @NonNull ArrayList<ActivityRecord> outPrevActivities) {
        if (currentActivity.mAtmService
                .mTaskOrganizerController.shouldInterceptBackPressedOnRootTask(
                        currentTask.getRootTask())) {
            // The task organizer will handle back pressed, don't play animation.
            return false;
        }
        final ActivityRecord root = currentTask.getRootActivity(false /*ignoreRelinquishIdentity*/,
                true /*setToBottomIfNone*/);
        if (root != null && ActivityClientController.shouldMoveTaskToBack(currentActivity, root)) {
+0 −67
Original line number Diff line number Diff line
@@ -552,58 +552,6 @@ public class BackNavigationControllerTests extends WindowTestsBase {
        assertThat(backNavigationInfo.getOnBackInvokedCallback()).isEqualTo(callback);
    }

    @Test
    public void testGetOnBackInvokedCallbackInfo_interceptBackDisabled_returnsCallbackInfo() {
        final Task topTask = createTopTaskWithActivity();
        final WindowState window = topTask.getTopVisibleAppMainWindow();
        final ActivityRecord r = window.getActivityRecord();
        final OnBackInvokedCallbackInfo callbackInfo = mock(OnBackInvokedCallbackInfo.class);
        window.setOnBackInvokedCallbackInfo(callbackInfo);
        spyOn(mAtm.mTaskOrganizerController);
        Mockito.doReturn(false).when(mAtm.mTaskOrganizerController)
                .shouldInterceptBackPressedOnRootTask(eq(topTask));

        final OnBackInvokedCallbackInfo info =
                mBackNavigationController.getOnBackInvokedCallbackInfo(window, topTask, r);

        assertThat(info).isEqualTo(callbackInfo);
    }

    @Test
    public void testGetOnBackInvokedCallbackInfo_interceptBackRootActivity_returnsNewCallback() {
        final Task topTask = createTopTaskWithActivity();
        final WindowState window = topTask.getTopVisibleAppMainWindow();
        final ActivityRecord root = window.getActivityRecord();
        final OnBackInvokedCallbackInfo callbackInfo = mock(OnBackInvokedCallbackInfo.class);
        window.setOnBackInvokedCallbackInfo(callbackInfo);
        spyOn(mAtm.mTaskOrganizerController);
        Mockito.doReturn(true).when(mAtm.mTaskOrganizerController)
                .shouldInterceptBackPressedOnRootTask(eq(topTask));

        final OnBackInvokedCallbackInfo info =
                mBackNavigationController.getOnBackInvokedCallbackInfo(window, topTask, root);

        assertThat(info).isNotNull();
        assertThat(info).isNotEqualTo(callbackInfo);
    }

    @Test
    public void testGetOnBackInvokedCallbackInfo_interceptBackChildActivity_returnsCallbackInfo() {
        final Task topTask = createTopTaskWithActivity();
        final WindowState window = topTask.getTopVisibleAppMainWindow();
        final ActivityRecord child = createActivityRecord(topTask);
        final OnBackInvokedCallbackInfo callbackInfo = mock(OnBackInvokedCallbackInfo.class);
        window.setOnBackInvokedCallbackInfo(callbackInfo);
        spyOn(mAtm.mTaskOrganizerController);
        Mockito.doReturn(true).when(mAtm.mTaskOrganizerController)
                .shouldInterceptBackPressedOnRootTask(eq(topTask));

        final OnBackInvokedCallbackInfo info =
                mBackNavigationController.getOnBackInvokedCallbackInfo(window, topTask, child);

        assertThat(info).isEqualTo(callbackInfo);
    }

    @Test
    public void preparesForBackToHome() {
        final Task topTask = createTopTaskWithActivity();
@@ -635,21 +583,6 @@ public class BackNavigationControllerTests extends WindowTestsBase {
        assertThat(backNavigationInfo.getOnBackInvokedCallback()).isEqualTo(appCallback);
    }

    @Test
    public void backTypeCallbackWhenInterceptBackPressedOnRootTask() {
        Task task = createTopTaskWithActivity();
        IOnBackInvokedCallback appCallback = withAppCallback(task);

        spyOn(mAtm.mTaskOrganizerController);
        Mockito.doReturn(true).when(
                mAtm.mTaskOrganizerController).shouldInterceptBackPressedOnRootTask(eq(task));
        BackNavigationInfo backNavigationInfo = startBackNavigation();
        assertThat(typeToString(backNavigationInfo.getType()))
                .isEqualTo(typeToString(BackNavigationInfo.TYPE_CALLBACK));
        // The callback should be replaced.
        assertThat(backNavigationInfo.getOnBackInvokedCallback()).isNotEqualTo(appCallback);
    }

    // TODO (b/259427810) Remove this test when we figure out new API
    @Test
    public void backAnimationSkipSharedElementTransition() {