summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorVadim Caen <caen@google.com>2022-03-31 18:29:26 +0200
committerVadim Caen <caen@google.com>2022-04-01 20:09:26 +0000
commitc627777ddac2dd764dfb7ad7baf3706a9052c44b (patch)
tree23abd8c265d7359a3aa310a6aef22eebf15c7618 /core/java
parent924b02bb035c18a79966e4f35177547ab31b9942 (diff)
Split animation methods out of OnBackInvokedCallback
This makes OnBackInvokedCallback SAM compatible Bug: 227789359 Test: atest \ CtsWindowManagerDeviceTestCases:android.server.wm.BackNavigationLegacyTest \ CtsWindowManagerDeviceTestCases:android.server.wm.BackNavigationTests \ WmTests:com.android.server.wm.BackNavigationControllerTests \ FrameworksCoreTests:android.window.BackNavigationTest \ FrameworksCoreTests:android.window.WindowOnBackInvokedDispatcherTest \ CtsViewTestCases:android.view.cts.OnBackInvokedDispatcherTest Change-Id: Iab86b0488f1b3048eb02042191acaeb6a645a0bc
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/app/Activity.java7
-rw-r--r--core/java/android/app/Dialog.java7
-rw-r--r--core/java/android/view/ViewRootImpl.java5
-rw-r--r--core/java/android/widget/Editor.java7
-rw-r--r--core/java/android/widget/MediaController.java7
-rw-r--r--core/java/android/window/OnBackAnimationCallback.java61
-rw-r--r--core/java/android/window/OnBackInvokedCallback.java42
-rw-r--r--core/java/android/window/WindowOnBackInvokedDispatcher.java31
8 files changed, 93 insertions, 74 deletions
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index dff3c076727d..5537b075d42f 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -1663,12 +1663,7 @@ public class Activity extends ContextThemeWrapper
.isOnBackInvokedCallbackEnabled(this);
if (aheadOfTimeBack) {
// Add onBackPressed as default back behavior.
- mDefaultBackCallback = new OnBackInvokedCallback() {
- @Override
- public void onBackInvoked() {
- navigateBack();
- }
- };
+ mDefaultBackCallback = this::navigateBack;
getOnBackInvokedDispatcher().registerSystemOnBackInvokedCallback(mDefaultBackCallback);
}
}
diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java
index 72c7fe4c5b40..51b71f0c5d52 100644
--- a/core/java/android/app/Dialog.java
+++ b/core/java/android/app/Dialog.java
@@ -459,12 +459,7 @@ public class Dialog implements DialogInterface, Window.Callback,
if (mContext != null
&& WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) {
// Add onBackPressed as default back behavior.
- mDefaultBackCallback = new OnBackInvokedCallback() {
- @Override
- public void onBackInvoked() {
- onBackPressed();
- }
- };
+ mDefaultBackCallback = this::onBackPressed;
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 931ae2748785..900aa23d0347 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -10765,12 +10765,9 @@ public final class ViewRootImpl implements ViewParent,
}
private void registerCompatOnBackInvokedCallback() {
- mCompatOnBackInvokedCallback = new OnBackInvokedCallback() {
- @Override
- public void onBackInvoked() {
+ mCompatOnBackInvokedCallback = () -> {
sendBackKeyEvent(KeyEvent.ACTION_DOWN);
sendBackKeyEvent(KeyEvent.ACTION_UP);
- }
};
mOnBackInvokedDispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, mCompatOnBackInvokedCallback);
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index dbfcca9d632a..285a40779b90 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -240,12 +240,7 @@ public class Editor {
private final boolean mHapticTextHandleEnabled;
/** Handles OnBackInvokedCallback back dispatch */
- private final OnBackInvokedCallback mBackCallback = new OnBackInvokedCallback() {
- @Override
- public void onBackInvoked() {
- stopTextActionMode();
- }
- };
+ private final OnBackInvokedCallback mBackCallback = this::stopTextActionMode;
private boolean mBackCallbackRegistered;
@Nullable
diff --git a/core/java/android/widget/MediaController.java b/core/java/android/widget/MediaController.java
index f1dc5e7050fb..ab2d0056df6b 100644
--- a/core/java/android/widget/MediaController.java
+++ b/core/java/android/widget/MediaController.java
@@ -122,12 +122,7 @@ public class MediaController extends FrameLayout {
private final AccessibilityManager mAccessibilityManager;
private boolean mBackCallbackRegistered;
/** Handles back invocation */
- private final OnBackInvokedCallback mBackCallback = new OnBackInvokedCallback() {
- @Override
- public void onBackInvoked() {
- hide();
- }
- };
+ private final OnBackInvokedCallback mBackCallback = this::hide;
/** Handles decor view attach state change */
private final OnAttachStateChangeListener mAttachStateListener =
new OnAttachStateChangeListener() {
diff --git a/core/java/android/window/OnBackAnimationCallback.java b/core/java/android/window/OnBackAnimationCallback.java
new file mode 100644
index 000000000000..1a37e57df403
--- /dev/null
+++ b/core/java/android/window/OnBackAnimationCallback.java
@@ -0,0 +1,61 @@
+/*
+ * 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.app.Activity;
+import android.app.Dialog;
+import android.view.View;
+
+/**
+ * Interface for applications to register back animation callbacks along their custom back
+ * handling.
+ * <p>
+ * This allows the client to customize various back behaviors by overriding the corresponding
+ * callback methods.
+ * <p>
+ * Callback instances can be added to and removed from {@link OnBackInvokedDispatcher}, held
+ * by classes that implement {@link OnBackInvokedDispatcherOwner} (such as {@link Activity},
+ * {@link Dialog} and {@link View}).
+ * <p>
+ * When back is triggered, callbacks on the in-focus window are invoked in reverse order in which
+ * they are added within the same priority. Between different priorities, callbacks with higher
+ * priority are invoked first.
+ * <p>
+ * @see OnBackInvokedCallback
+ * @hide
+ */
+public interface OnBackAnimationCallback extends OnBackInvokedCallback {
+ /**
+ * Called when a back gesture has been started, or back button has been pressed down.
+ */
+ default void onBackStarted() { }
+
+ /**
+ * Called on back gesture progress.
+ *
+ * @param backEvent An {@link BackEvent} object describing the progress event.
+ *
+ * @see BackEvent
+ */
+ default void onBackProgressed(@NonNull BackEvent backEvent) { }
+
+ /**
+ * Called when a back gesture or back button press has been cancelled.
+ */
+ default void onBackCancelled() { }
+}
diff --git a/core/java/android/window/OnBackInvokedCallback.java b/core/java/android/window/OnBackInvokedCallback.java
index 400a56f2c485..7b8cc0ae6de7 100644
--- a/core/java/android/window/OnBackInvokedCallback.java
+++ b/core/java/android/window/OnBackInvokedCallback.java
@@ -21,49 +21,29 @@ import android.app.Dialog;
import android.view.View;
/**
- * Interface for applications to register back invocation callbacks. This allows the client
- * to customize various back behaviors by overriding the corresponding callback methods.
- *
+ * Callback allowing applications to handle back events in place of the system.
+ * <p>
* Callback instances can be added to and removed from {@link OnBackInvokedDispatcher}, held
* by classes that implement {@link OnBackInvokedDispatcherOwner} (such as {@link Activity},
* {@link Dialog} and {@link View}).
- *
+ * <p>
* Under the hood callbacks are registered at window level. When back is triggered,
* callbacks on the in-focus window are invoked in reverse order in which they are added
* within the same priority. Between different pirorities, callbacks with higher priority
* are invoked first.
+ * <p>
+ * This replaces {@link Activity#onBackPressed()}, {@link Dialog#onBackPressed()} and
+ * {@link android.view.KeyEvent#KEYCODE_BACK}
*
- * See {@link OnBackInvokedDispatcher#registerOnBackInvokedCallback(int, OnBackInvokedCallback)}
- * for specifying callback priority.
+ * @see OnBackInvokedDispatcher#registerOnBackInvokedCallback(int, OnBackInvokedCallback)
+ * registerOnBackInvokedCallback(priority, OnBackInvokedCallback)
+ * to specify callback priority.
*/
+@SuppressWarnings("deprecation")
public interface OnBackInvokedCallback {
- /**
- * Called when a back gesture has been started, or back button has been pressed down.
- *
- * @hide
- */
- default void onBackStarted() { };
-
- /**
- * Called on back gesture progress.
- *
- * @param backEvent An {@link android.window.BackEvent} object describing the progress event.
- *
- * @see android.window.BackEvent
- * @hide
- */
- default void onBackProgressed(BackEvent backEvent) { };
-
- /**
- * Called when a back gesture or back button press has been cancelled.
- *
- * @hide
- */
- default void onBackCancelled() { };
-
/**
* Called when a back gesture has been completed and committed, or back button pressed
* has been released and committed.
*/
- default void onBackInvoked() { };
+ void onBackInvoked();
}
diff --git a/core/java/android/window/WindowOnBackInvokedDispatcher.java b/core/java/android/window/WindowOnBackInvokedDispatcher.java
index 8811116b25ad..77031d93f943 100644
--- a/core/java/android/window/WindowOnBackInvokedDispatcher.java
+++ b/core/java/android/window/WindowOnBackInvokedDispatcher.java
@@ -199,36 +199,30 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
@Override
public void onBackStarted() {
Handler.getMain().post(() -> {
- final OnBackInvokedCallback callback = mCallback.get();
- if (callback == null) {
- return;
+ final OnBackAnimationCallback callback = getBackAnimationCallback();
+ if (callback != null) {
+ callback.onBackStarted();
}
-
- callback.onBackStarted();
});
}
@Override
public void onBackProgressed(BackEvent backEvent) {
Handler.getMain().post(() -> {
- final OnBackInvokedCallback callback = mCallback.get();
- if (callback == null) {
- return;
+ final OnBackAnimationCallback callback = getBackAnimationCallback();
+ if (callback != null) {
+ callback.onBackProgressed(backEvent);
}
-
- callback.onBackProgressed(backEvent);
});
}
@Override
public void onBackCancelled() {
Handler.getMain().post(() -> {
- final OnBackInvokedCallback callback = mCallback.get();
- if (callback == null) {
- return;
+ final OnBackAnimationCallback callback = getBackAnimationCallback();
+ if (callback != null) {
+ callback.onBackCancelled();
}
-
- callback.onBackCancelled();
});
}
@@ -243,6 +237,13 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
callback.onBackInvoked();
});
}
+
+ @Nullable
+ private OnBackAnimationCallback getBackAnimationCallback() {
+ OnBackInvokedCallback callback = mCallback.get();
+ return callback instanceof OnBackAnimationCallback ? (OnBackAnimationCallback) callback
+ : null;
+ }
}
/**