Skip to content

Commit a2c2130

Browse files
George MountAndroid (Google) Code Review
authored andcommitted
Merge "Limit when auto-complete popups attach to WebView." into jb-dev
2 parents 531d164 + 57bd517 commit a2c2130

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

core/java/android/webkit/AutoCompletePopup.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
import android.widget.Filterable;
2929
import android.widget.ListAdapter;
3030
import android.widget.ListPopupWindow;
31+
import android.widget.PopupWindow.OnDismissListener;
3132

32-
class AutoCompletePopup implements OnItemClickListener, Filter.FilterListener {
33+
class AutoCompletePopup implements OnItemClickListener, Filter.FilterListener,
34+
OnDismissListener{
3335
private static class AnchorView extends View {
3436
AnchorView(Context context) {
3537
super(context);
3638
setFocusable(false);
39+
setVisibility(INVISIBLE);
3740
}
3841
}
3942
private static final int AUTOFILL_FORM = 100;
@@ -48,17 +51,10 @@ private static class AnchorView extends View {
4851
private WebViewClassic.WebViewInputConnection mInputConnection;
4952
private WebViewClassic mWebView;
5053

51-
public AutoCompletePopup(Context context,
52-
WebViewClassic webView,
54+
public AutoCompletePopup(WebViewClassic webView,
5355
WebViewClassic.WebViewInputConnection inputConnection) {
5456
mInputConnection = inputConnection;
5557
mWebView = webView;
56-
mPopup = new ListPopupWindow(context);
57-
mAnchor = new AnchorView(context);
58-
mWebView.getWebView().addView(mAnchor);
59-
mPopup.setOnItemClickListener(this);
60-
mPopup.setAnchorView(mAnchor);
61-
mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
6258
mHandler = new Handler() {
6359
@Override
6460
public void handleMessage(Message msg) {
@@ -72,6 +68,9 @@ public void handleMessage(Message msg) {
7268
}
7369

7470
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
71+
if (mPopup == null) {
72+
return false;
73+
}
7574
if (keyCode == KeyEvent.KEYCODE_BACK && mPopup.isShowing()) {
7675
// special case for the back key, we do not even try to send it
7776
// to the drop down list but instead, consume it immediately
@@ -112,11 +111,14 @@ public void setAutoFillQueryId(int queryId) {
112111
public void clearAdapter() {
113112
mAdapter = null;
114113
mFilter = null;
115-
mPopup.dismiss();
116-
mPopup.setAdapter(null);
114+
if (mPopup != null) {
115+
mPopup.dismiss();
116+
mPopup.setAdapter(null);
117+
}
117118
}
118119

119120
public <T extends ListAdapter & Filterable> void setAdapter(T adapter) {
121+
ensurePopup();
120122
mPopup.setAdapter(adapter);
121123
mAdapter = adapter;
122124
if (adapter != null) {
@@ -129,6 +131,7 @@ public <T extends ListAdapter & Filterable> void setAdapter(T adapter) {
129131
}
130132

131133
public void resetRect() {
134+
ensurePopup();
132135
int left = mWebView.contentToViewX(mWebView.mEditTextContentBounds.left);
133136
int right = mWebView.contentToViewX(mWebView.mEditTextContentBounds.right);
134137
int width = right - left;
@@ -164,6 +167,9 @@ public void resetRect() {
164167
// AdapterView.OnItemClickListener implementation
165168
@Override
166169
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
170+
if (mPopup == null) {
171+
return;
172+
}
167173
if (id == 0 && position == 0 && mInputConnection.getIsAutoFillable()) {
168174
mText = "";
169175
pushTextToInputConnection();
@@ -206,6 +212,7 @@ private void pushTextToInputConnection() {
206212

207213
@Override
208214
public void onFilterComplete(int count) {
215+
ensurePopup();
209216
boolean showDropDown = (count > 0) &&
210217
(mInputConnection.getIsAutoFillable() || mText.length() > 0);
211218
if (showDropDown) {
@@ -219,5 +226,23 @@ public void onFilterComplete(int count) {
219226
mPopup.dismiss();
220227
}
221228
}
229+
230+
@Override
231+
public void onDismiss() {
232+
mWebView.getWebView().removeView(mAnchor);
233+
}
234+
235+
private void ensurePopup() {
236+
if (mPopup == null) {
237+
mPopup = new ListPopupWindow(mWebView.getContext());
238+
mAnchor = new AnchorView(mWebView.getContext());
239+
mWebView.getWebView().addView(mAnchor);
240+
mPopup.setOnItemClickListener(this);
241+
mPopup.setAnchorView(mAnchor);
242+
mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
243+
} else if (mWebView.getWebView().indexOfChild(mAnchor) < 0) {
244+
mWebView.getWebView().addView(mAnchor);
245+
}
246+
}
222247
}
223248

core/java/android/webkit/WebViewClassic.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4696,8 +4696,7 @@ void switchOutDrawHistory() {
46964696
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
46974697
if (mInputConnection == null) {
46984698
mInputConnection = new WebViewInputConnection();
4699-
mAutoCompletePopup = new AutoCompletePopup(mContext, this,
4700-
mInputConnection);
4699+
mAutoCompletePopup = new AutoCompletePopup(this, mInputConnection);
47014700
}
47024701
mInputConnection.setupEditorInfo(outAttrs);
47034702
return mInputConnection;

0 commit comments

Comments
 (0)