Merge "Migrate PopupWindow to use OnBackInvokedDispatcher" into tm-dev am: 0ba48fea68

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17105464

Change-Id: I3237ccdd548ff3f0641a031f60561ebaa5eca9a6
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Vadim Caen
2022-04-11 18:16:20 +00:00
committed by Automerger Merge Worker

View File

@@ -56,6 +56,9 @@ import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
import android.view.WindowManagerGlobal;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
import android.window.WindowOnBackInvokedDispatcher;
import com.android.internal.R;
@@ -277,6 +280,8 @@ public class PopupWindow {
private boolean mPopupViewInitialLayoutDirectionInherited;
private OnBackInvokedCallback mBackCallback;
/**
* <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
*
@@ -2028,6 +2033,8 @@ public class PopupWindow {
final PopupDecorView decorView = mDecorView;
final View contentView = mContentView;
unregisterBackCallback(decorView.findOnBackInvokedDispatcher());
final ViewGroup contentHolder;
final ViewParent contentParent = contentView.getParent();
if (contentParent instanceof ViewGroup) {
@@ -2082,6 +2089,15 @@ public class PopupWindow {
}
}
private void unregisterBackCallback(@Nullable OnBackInvokedDispatcher onBackInvokedDispatcher) {
OnBackInvokedCallback backCallback = mBackCallback;
mBackCallback = null;
if (onBackInvokedDispatcher != null && backCallback != null) {
onBackInvokedDispatcher.unregisterOnBackInvokedCallback(
backCallback);
}
}
/**
* Returns the window-relative epicenter bounds to be used by enter and
* exit transitions.
@@ -2725,6 +2741,30 @@ public class PopupWindow {
}
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mContext)) {
return;
}
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
if (dispatcher == null) {
return;
}
mBackCallback = PopupWindow.this::dismiss;
dispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT,
mBackCallback);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
unregisterBackCallback(findOnBackInvokedDispatcher());
}
}
private class PopupBackgroundView extends FrameLayout {