2828import android .widget .Filterable ;
2929import android .widget .ListAdapter ;
3030import 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
0 commit comments