summaryrefslogtreecommitdiff
path: root/core/java/android/window/WindowOnBackInvokedDispatcher.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/java/android/window/WindowOnBackInvokedDispatcher.java')
-rw-r--r--core/java/android/window/WindowOnBackInvokedDispatcher.java63
1 files changed, 58 insertions, 5 deletions
diff --git a/core/java/android/window/WindowOnBackInvokedDispatcher.java b/core/java/android/window/WindowOnBackInvokedDispatcher.java
index 1d0bc5a6f0ba..d147524d3b3d 100644
--- a/core/java/android/window/WindowOnBackInvokedDispatcher.java
+++ b/core/java/android/window/WindowOnBackInvokedDispatcher.java
@@ -30,6 +30,7 @@ import android.view.IWindowSession;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Objects;
import java.util.TreeMap;
/**
@@ -62,6 +63,11 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
/** Holds all callbacks by priorities. */
private final TreeMap<Integer, ArrayList<OnBackInvokedCallback>>
mOnBackInvokedCallbacks = new TreeMap<>();
+ private final Checker mChecker;
+
+ public WindowOnBackInvokedDispatcher(boolean applicationCallBackEnabled) {
+ mChecker = new Checker(applicationCallBackEnabled);
+ }
/**
* Sends the pending top callback (if one exists) to WM when the view root
@@ -86,14 +92,16 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
@Override
public void registerOnBackInvokedCallback(
@Priority int priority, @NonNull OnBackInvokedCallback callback) {
- if (priority < 0) {
- throw new IllegalArgumentException("Application registered OnBackInvokedCallback "
- + "cannot have negative priority. Priority: " + priority);
+ if (mChecker.checkApplicationCallbackRegistration(priority, callback)) {
+ registerOnBackInvokedCallbackUnchecked(callback, priority);
}
- registerOnBackInvokedCallbackUnchecked(callback, priority);
}
- private void registerOnBackInvokedCallbackUnchecked(
+ /**
+ * Register a callback bypassing platform checks. This is used to register compatibility
+ * callbacks.
+ */
+ public void registerOnBackInvokedCallbackUnchecked(
@NonNull OnBackInvokedCallback callback, @Priority int priority) {
if (mImeDispatcher != null) {
mImeDispatcher.registerOnBackInvokedCallback(priority, callback);
@@ -203,6 +211,14 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
return null;
}
+ /**
+ * Returns the checker used to check whether a callback can be registered
+ */
+ @NonNull
+ public Checker getChecker() {
+ return mChecker;
+ }
+
static class OnBackInvokedCallbackWrapper extends IOnBackInvokedCallback.Stub {
private final WeakReference<OnBackInvokedCallback> mCallback;
@@ -289,4 +305,41 @@ public class WindowOnBackInvokedDispatcher implements OnBackInvokedDispatcher {
@NonNull ImeOnBackInvokedDispatcher imeDispatcher) {
mImeDispatcher = imeDispatcher;
}
+
+
+ /**
+ * Class used to check whether a callback can be registered or not. This is meant to be
+ * shared with {@link ProxyOnBackInvokedDispatcher} which needs to do the same checks.
+ */
+ public static class Checker {
+
+ private final boolean mApplicationCallBackEnabled;
+
+ public Checker(boolean applicationCallBackEnabled) {
+ mApplicationCallBackEnabled = applicationCallBackEnabled;
+ }
+
+ /**
+ * Checks whether the given callback can be registered with the given priority.
+ * @return true if the callback can be added.
+ * @throws IllegalArgumentException if the priority is negative.
+ */
+ public boolean checkApplicationCallbackRegistration(int priority,
+ OnBackInvokedCallback callback) {
+ if (!mApplicationCallBackEnabled
+ && !(callback instanceof CompatOnBackInvokedCallback)) {
+ Log.w("OnBackInvokedCallback",
+ "OnBackInvokedCallback is not enabled for the application."
+ + "\nSet 'android:enableOnBackInvokedCallback=\"true\"' in the"
+ + " application manifest.");
+ return false;
+ }
+ if (priority < 0) {
+ throw new IllegalArgumentException("Application registered OnBackInvokedCallback "
+ + "cannot have negative priority. Priority: " + priority);
+ }
+ Objects.requireNonNull(callback);
+ return true;
+ }
+ }
}