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
This commit is contained in:
Vadim Caen
2022-03-29 13:32:12 +02:00
parent 924b02bb03
commit b23c8c8e87
5 changed files with 66 additions and 0 deletions

View File

@@ -49642,6 +49642,7 @@ package android.view {
method @CallSuper public void drawableHotspotChanged(float, float);
method @CallSuper protected void drawableStateChanged();
method public android.view.View findFocus();
method @Nullable public final android.window.OnBackInvokedDispatcher findOnBackInvokedDispatcher();
method public final <T extends android.view.View> T findViewById(@IdRes int);
method public final <T extends android.view.View> T findViewWithTag(Object);
method public void findViewsWithText(java.util.ArrayList<android.view.View>, CharSequence, int);
@@ -50819,6 +50820,7 @@ package android.view {
method public void childHasTransientStateChanged(@NonNull android.view.View, boolean);
method public void clearChildFocus(android.view.View);
method public void createContextMenu(android.view.ContextMenu);
method @Nullable public default android.window.OnBackInvokedDispatcher findOnBackInvokedDispatcherForChild(@NonNull android.view.View, @NonNull android.view.View);
method public android.view.View focusSearch(android.view.View, int);
method public void focusableViewAvailable(android.view.View);
method public boolean getChildVisibleRect(android.view.View, android.graphics.Rect, android.graphics.Point);

View File

@@ -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.

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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.