Merge "Migrate AutoCompleteTextView to use OnBackInvokedDispatcher." into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
d1263fb5ce
@@ -42,6 +42,9 @@ import android.view.inputmethod.CompletionInfo;
|
|||||||
import android.view.inputmethod.EditorInfo;
|
import android.view.inputmethod.EditorInfo;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.view.inspector.InspectableProperty;
|
import android.view.inspector.InspectableProperty;
|
||||||
|
import android.window.OnBackInvokedCallback;
|
||||||
|
import android.window.OnBackInvokedDispatcher;
|
||||||
|
import android.window.WindowOnBackInvokedDispatcher;
|
||||||
|
|
||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
|
|
||||||
@@ -143,6 +146,14 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
|
|||||||
@UnsupportedAppUsage
|
@UnsupportedAppUsage
|
||||||
private PopupDataSetObserver mObserver;
|
private PopupDataSetObserver mObserver;
|
||||||
|
|
||||||
|
private boolean mBackCallbackRegistered;
|
||||||
|
/** Handles back invocation */
|
||||||
|
private final OnBackInvokedCallback mBackCallback = () -> {
|
||||||
|
if (isPopupShowing()) {
|
||||||
|
dismissDropDown();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new auto-complete text view with the given context's theme.
|
* Constructs a new auto-complete text view with the given context's theme.
|
||||||
*
|
*
|
||||||
@@ -272,6 +283,9 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
|
|||||||
}
|
}
|
||||||
|
|
||||||
mPopup = new ListPopupWindow(mPopupContext, attrs, defStyleAttr, defStyleRes);
|
mPopup = new ListPopupWindow(mPopupContext, attrs, defStyleAttr, defStyleRes);
|
||||||
|
mPopup.setOnDismissListener(() -> {
|
||||||
|
unregisterOnBackInvokedCallback();
|
||||||
|
});
|
||||||
mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
|
mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
|
||||||
mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
|
mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
|
||||||
mPopup.setListSelector(popupListSelector);
|
mPopup.setListSelector(popupListSelector);
|
||||||
@@ -734,6 +748,7 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
|
|||||||
wrappedListener = new PopupWindow.OnDismissListener() {
|
wrappedListener = new PopupWindow.OnDismissListener() {
|
||||||
@Override public void onDismiss() {
|
@Override public void onDismiss() {
|
||||||
dismissListener.onDismiss();
|
dismissListener.onDismiss();
|
||||||
|
unregisterOnBackInvokedCallback();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -1315,6 +1330,9 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
|
|||||||
mPopup.setListItemExpandMax(EXPAND_MAX);
|
mPopup.setListItemExpandMax(EXPAND_MAX);
|
||||||
}
|
}
|
||||||
mPopup.show();
|
mPopup.show();
|
||||||
|
if (!mPopup.isDropDownAlwaysVisible()) {
|
||||||
|
registerOnBackInvokedCallback();
|
||||||
|
}
|
||||||
mPopup.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
|
mPopup.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1414,6 +1432,35 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
|
|||||||
return AutoCompleteTextView.class.getName();
|
return AutoCompleteTextView.class.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void unregisterOnBackInvokedCallback() {
|
||||||
|
if (!mBackCallbackRegistered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
|
||||||
|
if (dispatcher == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mPopupContext)) {
|
||||||
|
dispatcher.unregisterOnBackInvokedCallback(mBackCallback);
|
||||||
|
}
|
||||||
|
mBackCallbackRegistered = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerOnBackInvokedCallback() {
|
||||||
|
if (mBackCallbackRegistered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
|
||||||
|
if (dispatcher == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mPopupContext)) {
|
||||||
|
dispatcher.registerOnBackInvokedCallback(
|
||||||
|
OnBackInvokedDispatcher.PRIORITY_OVERLAY, mBackCallback);
|
||||||
|
}
|
||||||
|
mBackCallbackRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
private class DropDownItemClickListener implements AdapterView.OnItemClickListener {
|
private class DropDownItemClickListener implements AdapterView.OnItemClickListener {
|
||||||
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
||||||
performCompletion(v, position, id);
|
performCompletion(v, position, id);
|
||||||
|
|||||||
Reference in New Issue
Block a user