Migrate PopupWindow to use OnBackInvokedDispatcher
Bug: 219968356 Test: In BackTestApp, show popup window and swipe back to close it. Test: CTS: android.widget.cts.BackInvokedOnWidgetsTest#popupWindowDismissedOnBackGesture Change-Id: Ic22c12e2ed9f1c961f6e0abfdaf3d2a9a7bd2934
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user