diff --git a/core/api/current.txt b/core/api/current.txt index 0f9554d2ac10f..e2a7f8761a115 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -57959,7 +57959,7 @@ package android.window { } public interface OnBackInvokedDispatcher { - method public void registerOnBackInvokedCallback(@NonNull android.window.OnBackInvokedCallback, @IntRange(from=0) int); + method public void registerOnBackInvokedCallback(@IntRange(from=0) int, @NonNull android.window.OnBackInvokedCallback); method public void unregisterOnBackInvokedCallback(@NonNull android.window.OnBackInvokedCallback); field public static final int PRIORITY_DEFAULT = 0; // 0x0 field public static final int PRIORITY_OVERLAY = 1000000; // 0xf4240 diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java index 82ff42b417994..a763b1464b6de 100644 --- a/core/java/android/app/Dialog.java +++ b/core/java/android/app/Dialog.java @@ -465,7 +465,8 @@ public class Dialog implements DialogInterface, Window.Callback, onBackPressed(); } }; - getOnBackInvokedDispatcher().registerSystemOnBackInvokedCallback(mDefaultBackCallback); + getOnBackInvokedDispatcher().registerOnBackInvokedCallback( + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mDefaultBackCallback); mDefaultBackCallback = null; } } diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index b72725ad2c32d..3879ce0b5ce68 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -10923,7 +10923,7 @@ public final class ViewRootImpl implements ViewParent, } }; mOnBackInvokedDispatcher.registerOnBackInvokedCallback( - mCompatOnBackInvokedCallback, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCompatOnBackInvokedCallback); } private void unregisterCompatOnBackInvokedCallback() { diff --git a/core/java/android/window/OnBackInvokedCallback.java b/core/java/android/window/OnBackInvokedCallback.java index dcd80fd76e09a..400a56f2c4857 100644 --- a/core/java/android/window/OnBackInvokedCallback.java +++ b/core/java/android/window/OnBackInvokedCallback.java @@ -33,7 +33,7 @@ import android.view.View; * within the same priority. Between different pirorities, callbacks with higher priority * are invoked first. * - * See {@link OnBackInvokedDispatcher#registerOnBackInvokedCallback(OnBackInvokedCallback, int)} + * See {@link OnBackInvokedDispatcher#registerOnBackInvokedCallback(int, OnBackInvokedCallback)} * for specifying callback priority. */ public interface OnBackInvokedCallback { diff --git a/core/java/android/window/OnBackInvokedDispatcher.java b/core/java/android/window/OnBackInvokedDispatcher.java index 63e6d30c8c0b5..5eed8cde8c7cc 100644 --- a/core/java/android/window/OnBackInvokedDispatcher.java +++ b/core/java/android/window/OnBackInvokedDispatcher.java @@ -94,15 +94,15 @@ public interface OnBackInvokedDispatcher { * Within the same priority level, callbacks are invoked in the reverse order in which * they are registered. Higher priority callbacks are invoked before lower priority ones. * + * @param priority The priority of the callback. * @param callback The callback to be registered. If the callback instance has been already * registered, the existing instance (no matter its priority) will be * unregistered and registered again. - * @param priority The priority of the callback. * @throws {@link IllegalArgumentException} if the priority is negative. */ - @SuppressLint({"SamShouldBeLast", "ExecutorRegistration"}) + @SuppressLint({"ExecutorRegistration"}) void registerOnBackInvokedCallback( - @NonNull OnBackInvokedCallback callback, @Priority @IntRange(from = 0) int priority); + @Priority @IntRange(from = 0) int priority, @NonNull OnBackInvokedCallback callback); /** * Unregisters a {@link OnBackInvokedCallback}. diff --git a/core/java/android/window/ProxyOnBackInvokedDispatcher.java b/core/java/android/window/ProxyOnBackInvokedDispatcher.java index cf17a2116aacb..2b2f5e9457106 100644 --- a/core/java/android/window/ProxyOnBackInvokedDispatcher.java +++ b/core/java/android/window/ProxyOnBackInvokedDispatcher.java @@ -44,7 +44,7 @@ public class ProxyOnBackInvokedDispatcher implements OnBackInvokedDispatcher { /** * List of pair representing an {@link OnBackInvokedCallback} and its associated priority. * - * @see OnBackInvokedDispatcher#registerOnBackInvokedCallback(OnBackInvokedCallback, int) + * @see OnBackInvokedDispatcher#registerOnBackInvokedCallback(int, OnBackInvokedCallback) */ private final List> mCallbacks = new ArrayList<>(); private final Object mLock = new Object(); @@ -52,7 +52,7 @@ public class ProxyOnBackInvokedDispatcher implements OnBackInvokedDispatcher { @Override public void registerOnBackInvokedCallback( - @NonNull OnBackInvokedCallback callback, int priority) { + int priority, @NonNull OnBackInvokedCallback callback) { if (DEBUG) { Log.v(TAG, String.format("Pending register %s. Actual=%s", callback, mActualDispatcherOwner)); @@ -91,7 +91,7 @@ public class ProxyOnBackInvokedDispatcher implements OnBackInvokedDispatcher { mCallbacks.add(Pair.create(callback, priority)); if (mActualDispatcherOwner != null) { mActualDispatcherOwner.getOnBackInvokedDispatcher().registerOnBackInvokedCallback( - callback, priority); + priority, callback); } } } @@ -115,7 +115,7 @@ public class ProxyOnBackInvokedDispatcher implements OnBackInvokedDispatcher { for (Pair callbackPair : mCallbacks) { int priority = callbackPair.second; if (priority >= 0) { - dispatcher.registerOnBackInvokedCallback(callbackPair.first, priority); + dispatcher.registerOnBackInvokedCallback(priority, callbackPair.first); } else { dispatcher.registerSystemOnBackInvokedCallback(callbackPair.first); } diff --git a/core/java/android/window/WindowOnBackInvokedDispatcher.java b/core/java/android/window/WindowOnBackInvokedDispatcher.java index d046cefee5f7c..97573c291340f 100644 --- a/core/java/android/window/WindowOnBackInvokedDispatcher.java +++ b/core/java/android/window/WindowOnBackInvokedDispatcher.java @@ -83,7 +83,7 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher { // TODO: Take an Executor for the callback to run on. @Override public void registerOnBackInvokedCallback( - @NonNull OnBackInvokedCallback callback, @Priority int priority) { + @Priority int priority, @NonNull OnBackInvokedCallback callback) { if (priority < 0) { throw new IllegalArgumentException("Application registered OnBackInvokedCallback " + "cannot have negative priority. Priority: " + priority); diff --git a/core/tests/coretests/src/android/window/BackNavigationTest.java b/core/tests/coretests/src/android/window/BackNavigationTest.java index 8fa48ef5494d7..94a149b09d54f 100644 --- a/core/tests/coretests/src/android/window/BackNavigationTest.java +++ b/core/tests/coretests/src/android/window/BackNavigationTest.java @@ -111,12 +111,12 @@ public class BackNavigationTest { CountDownLatch backRegisteredLatch = new CountDownLatch(1); mScenario.onActivity(activity -> { activity.getOnBackInvokedDispatcher().registerOnBackInvokedCallback( - new OnBackInvokedCallback() { + 0, new OnBackInvokedCallback() { @Override public void onBackInvoked() { backInvokedLatch.countDown(); } - }, 0 + } ); backRegisteredLatch.countDown(); }); diff --git a/core/tests/coretests/src/android/window/WindowOnBackInvokedDispatcherTest.java b/core/tests/coretests/src/android/window/WindowOnBackInvokedDispatcherTest.java index f8c994416f460..212f4ed92b8ca 100644 --- a/core/tests/coretests/src/android/window/WindowOnBackInvokedDispatcherTest.java +++ b/core/tests/coretests/src/android/window/WindowOnBackInvokedDispatcherTest.java @@ -77,9 +77,9 @@ public class WindowOnBackInvokedDispatcherTest { ArgumentCaptor.forClass(IOnBackInvokedCallback.class); mDispatcher.registerOnBackInvokedCallback( - mCallback1, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback1); mDispatcher.registerOnBackInvokedCallback( - mCallback2, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback2); verify(mWindowSession, times(2)).setOnBackInvokedCallback( Mockito.eq(mWindow), @@ -102,9 +102,9 @@ public class WindowOnBackInvokedDispatcherTest { ArgumentCaptor.forClass(IOnBackInvokedCallback.class); mDispatcher.registerOnBackInvokedCallback( - mCallback1, OnBackInvokedDispatcher.PRIORITY_OVERLAY); + OnBackInvokedDispatcher.PRIORITY_OVERLAY, mCallback1); mDispatcher.registerOnBackInvokedCallback( - mCallback2, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback2); verify(mWindowSession).setOnBackInvokedCallback( Mockito.eq(mWindow), captor.capture(), @@ -118,9 +118,9 @@ public class WindowOnBackInvokedDispatcherTest { @Test public void propagatesTopCallback_withRemoval() throws RemoteException { mDispatcher.registerOnBackInvokedCallback( - mCallback1, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback1); mDispatcher.registerOnBackInvokedCallback( - mCallback2, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback2); reset(mWindowSession); mDispatcher.unregisterOnBackInvokedCallback(mCallback1); @@ -139,16 +139,17 @@ public class WindowOnBackInvokedDispatcherTest { ArgumentCaptor captor = ArgumentCaptor.forClass(IOnBackInvokedCallback.class); - mDispatcher.registerOnBackInvokedCallback(mCallback1, - OnBackInvokedDispatcher.PRIORITY_OVERLAY); + mDispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_OVERLAY, + mCallback1 + ); mDispatcher.registerOnBackInvokedCallback( - mCallback2, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback2); mDispatcher.registerOnBackInvokedCallback( - mCallback1, OnBackInvokedDispatcher.PRIORITY_DEFAULT); + OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCallback1); reset(mWindowSession); mDispatcher.registerOnBackInvokedCallback( - mCallback2, OnBackInvokedDispatcher.PRIORITY_OVERLAY); + OnBackInvokedDispatcher.PRIORITY_OVERLAY, mCallback2); verify(mWindowSession).setOnBackInvokedCallback( Mockito.eq(mWindow), captor.capture(),