summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorVadim Caen <caen@google.com>2022-03-29 13:32:12 +0200
committerVadim Caen <caen@google.com>2022-04-01 11:15:30 +0200
commitb23c8c8e87972380cdf77e6bacbf9ae608ebcf40 (patch)
treee15f4956cdf88464efb5cbc7e72a247cd7ce6cfc /core/java
parent924b02bb035c18a79966e4f35177547ab31b9942 (diff)
Add View.findOnBackInvokedDispatcher
To make it easier for developer to migrate away from KEYCODE_BACK without having to walk up the view hierachy in order to find an OnBackDispatcher, introduce findOnBackInvokedDispatcher. Bug: 227301455 Test: android.view.cts.OnBackInvokedDispatcherTest#testGetDispatcherOnView Change-Id: I7f3e76df596d306fa26d72df8115d5b15d7ac564
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/view/View.java18
-rw-r--r--core/java/android/view/ViewGroup.java22
-rw-r--r--core/java/android/view/ViewParent.java17
-rw-r--r--core/java/android/view/ViewRootImpl.java7
4 files changed, 64 insertions, 0 deletions
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 7d823b1c100d..0fc365784e49 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -162,6 +162,7 @@ import android.view.translation.ViewTranslationResponse;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ScrollBarDrawable;
+import android.window.OnBackInvokedDispatcher;
import com.android.internal.R;
import com.android.internal.util.ArrayUtils;
@@ -12098,6 +12099,23 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
return null;
}
+
+ /**
+ * Walk up the View hierarchy to find the nearest {@link OnBackInvokedDispatcher}.
+ *
+ * @return The {@link OnBackInvokedDispatcher} from this or the nearest
+ * ancestor, or null if this view is both not attached and have no ancestor providing an
+ * {@link OnBackInvokedDispatcher}.
+ */
+ @Nullable
+ public final OnBackInvokedDispatcher findOnBackInvokedDispatcher() {
+ ViewParent parent = getParent();
+ if (parent != null) {
+ return parent.findOnBackInvokedDispatcherForChild(this, this);
+ }
+ return null;
+ }
+
/**
* @hide Compute the insets that should be consumed by this view and the ones
* that should propagate to those under it.
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
index fe672327346a..9f0ad1169a8e 100644
--- a/core/java/android/view/ViewGroup.java
+++ b/core/java/android/view/ViewGroup.java
@@ -72,6 +72,7 @@ import android.view.inspector.InspectableProperty.EnumEntry;
import android.view.translation.TranslationCapability;
import android.view.translation.TranslationSpec.DataFormat;
import android.view.translation.ViewTranslationRequest;
+import android.window.OnBackInvokedDispatcher;
import com.android.internal.R;
@@ -9296,4 +9297,25 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
requests);
}
}
+
+ /**
+ * Walk up the View hierarchy to find the nearest {@link OnBackInvokedDispatcher}.
+ *
+ * @return The {@link OnBackInvokedDispatcher} from this or the nearest
+ * ancestor, or null if the view is both not attached and have no ancestor providing an
+ * {@link OnBackInvokedDispatcher}.
+ *
+ * @param child The direct child of this view for which to find a dispatcher.
+ * @param requester The requester that will use the dispatcher. Can be the same as child.
+ */
+ @Nullable
+ @Override
+ public OnBackInvokedDispatcher findOnBackInvokedDispatcherForChild(@NonNull View child,
+ @NonNull View requester) {
+ ViewParent parent = getParent();
+ if (parent != null) {
+ return parent.findOnBackInvokedDispatcherForChild(this, requester);
+ }
+ return null;
+ }
}
diff --git a/core/java/android/view/ViewParent.java b/core/java/android/view/ViewParent.java
index 128da314e907..1020d2ef02be 100644
--- a/core/java/android/view/ViewParent.java
+++ b/core/java/android/view/ViewParent.java
@@ -22,6 +22,7 @@ import android.graphics.Rect;
import android.graphics.Region;
import android.os.Bundle;
import android.view.accessibility.AccessibilityEvent;
+import android.window.OnBackInvokedDispatcher;
/**
* Defines the responsibilities for a class that will be a parent of a View.
@@ -697,4 +698,20 @@ public interface ViewParent {
getParent().onDescendantUnbufferedRequested();
}
}
+
+ /**
+ * Walk up the View hierarchy to find the nearest {@link OnBackInvokedDispatcher}.
+ *
+ * @return The {@link OnBackInvokedDispatcher} from this or the nearest
+ * ancestor, or null if the view is both not attached and have no ancestor providing an
+ * {@link OnBackInvokedDispatcher}.
+ *
+ * @param child The direct child of this view for which to find a dispatcher.
+ * @param requester The requester that will use the dispatcher. Can be the same as child.
+ */
+ @Nullable
+ default OnBackInvokedDispatcher findOnBackInvokedDispatcherForChild(@NonNull View child,
+ @NonNull View requester) {
+ return null;
+ }
}
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 931ae2748785..9edf29d6b8c7 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -10736,6 +10736,13 @@ public final class ViewRootImpl implements ViewParent,
return mOnBackInvokedDispatcher;
}
+ @NonNull
+ @Override
+ public OnBackInvokedDispatcher findOnBackInvokedDispatcherForChild(
+ @NonNull View child, @NonNull View requester) {
+ return getOnBackInvokedDispatcher();
+ }
+
/**
* When this ViewRootImpl is added to the window manager, transfers the first
* {@link OnBackInvokedCallback} to be called to the server.