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

Commit b353af03 authored by Shan Huang's avatar Shan Huang
Browse files

Create OnBackInvokedCallbackInfo to wrap callback and its priority.

Main motivation is to store a back callback's exact priority value in WM. This is required by the IME migration (ag/17076160) to compare the priority levels of IME window callback and focused window callback in BackNavigationController.

This also consolidates the WindowState#mSystemOnBackInvokedCallback and WindowState#mApplicationOnBackInvokedCallback fields into one field, as tracking two fields for one callback was error prone. We had to remember to clear the application / system field when the other field is set, and failing to do so has resulted in bugs such as b/222675481.


Bug: 224856664
Test: atest BackNavigationControllerTest
Test: atest WindowOnBackInvokedDispatcherTest
Test: m -j and test back behavior throughout the system on apps that
opted in and out.

Change-Id: Ic57113610d934f33d2c9ca4cef59f39a9b87e832
parent d0923a21
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ import android.view.Surface;
import android.view.SurfaceControl;
import android.view.SurfaceControl.Transaction;
import android.window.ClientWindowFrames;
import android.window.IOnBackInvokedCallback;
import android.window.OnBackInvokedCallbackInfo;

import java.util.List;

@@ -371,14 +371,14 @@ interface IWindowSession {
            in String hashAlgorithm, in RemoteCallback callback);

    /**
     * Sets the {@link IOnBackInvokedCallback} to be invoked for a window when back is triggered.
     * Sets the {@link OnBackInvokedCallbackInfo} containing the callback to be invoked for
     * a window when back is triggered.
     *
     * @param window The token for the window to set the callback to.
     * @param callback The {@link IOnBackInvokedCallback} to set.
     * @param priority The priority of the callback.
     * @param callbackInfo The {@link OnBackInvokedCallbackInfo} to set.
     */
    oneway void setOnBackInvokedCallback(
            IWindow window, IOnBackInvokedCallback callback, int priority);
    oneway void setOnBackInvokedCallbackInfo(
            IWindow window, in OnBackInvokedCallbackInfo callbackInfo);

    /**
     * Clears a touchable region set by {@link #setInsets}.
+3 −3
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import android.os.RemoteException;
import android.util.Log;
import android.util.MergedConfiguration;
import android.window.ClientWindowFrames;
import android.window.IOnBackInvokedCallback;
import android.window.OnBackInvokedCallbackInfo;

import java.util.HashMap;
import java.util.List;
@@ -529,8 +529,8 @@ public class WindowlessWindowManager implements IWindowSession {
    }

    @Override
    public void setOnBackInvokedCallback(IWindow iWindow,
            IOnBackInvokedCallback iOnBackInvokedCallback, int priority) throws RemoteException { }
    public void setOnBackInvokedCallbackInfo(IWindow iWindow,
            OnBackInvokedCallbackInfo callbackInfo) throws RemoteException { }

    @Override
    public boolean dropForAccessibility(IWindow window, int x, int y) {
+21 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License

 */

package android.window;

/** @hide */
parcelable OnBackInvokedCallbackInfo;
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.window;

import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * Data object to hold an {@link IOnBackInvokedCallback} and its priority.
 * @hide
 */
public final class OnBackInvokedCallbackInfo implements Parcelable {
    @NonNull
    private final IOnBackInvokedCallback mCallback;
    private @OnBackInvokedDispatcher.Priority int mPriority;

    public OnBackInvokedCallbackInfo(@NonNull IOnBackInvokedCallback callback, int priority) {
        mCallback = callback;
        mPriority = priority;
    }

    private OnBackInvokedCallbackInfo(@NonNull Parcel in) {
        mCallback = IOnBackInvokedCallback.Stub.asInterface(in.readStrongBinder());
        mPriority = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeStrongInterface(mCallback);
        dest.writeInt(mPriority);
    }

    public static final Creator<OnBackInvokedCallbackInfo> CREATOR =
            new Creator<OnBackInvokedCallbackInfo>() {
                @Override
                public OnBackInvokedCallbackInfo createFromParcel(Parcel in) {
                    return new OnBackInvokedCallbackInfo(in);
                }

                @Override
                public OnBackInvokedCallbackInfo[] newArray(int size) {
                    return new OnBackInvokedCallbackInfo[size];
                }
            };

    public boolean isSystemCallback() {
        return mPriority == OnBackInvokedDispatcher.PRIORITY_SYSTEM;
    }

    @NonNull
    public IOnBackInvokedCallback getCallback() {
        return mCallback;
    }

    @OnBackInvokedDispatcher.Priority
    public int getPriority() {
        return mPriority;
    }

    @Override
    public String toString() {
        return "OnBackInvokedCallbackInfo{"
                + "mCallback=" + mCallback + ", mPriority=" + mPriority + '}';
    }
}
+4 −3
Original line number Diff line number Diff line
@@ -160,11 +160,12 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
        }
        try {
            if (callback == null) {
                mWindowSession.setOnBackInvokedCallback(mWindow, null, PRIORITY_DEFAULT);
                mWindowSession.setOnBackInvokedCallbackInfo(mWindow, null);
            } else {
                int priority = mAllCallbacks.get(callback);
                mWindowSession.setOnBackInvokedCallback(
                        mWindow, new OnBackInvokedCallbackWrapper(callback), priority);
                mWindowSession.setOnBackInvokedCallbackInfo(
                        mWindow, new OnBackInvokedCallbackInfo(
                                new OnBackInvokedCallbackWrapper(callback), priority));
            }
            if (DEBUG && callback == null) {
                Log.d(TAG, TextUtils.formatSimple("setTopOnBackInvokedCallback(null) Callers:%s",
Loading