100100import android .webkit .WebViewCore .EventHub ;
101101import android .webkit .WebViewCore .TextFieldInitData ;
102102import android .webkit .WebViewCore .TextSelectionData ;
103- import android .webkit .WebViewCore .TouchHighlightData ;
104103import android .webkit .WebViewCore .WebKitHitTest ;
105104import android .widget .AbsoluteLayout ;
106105import android .widget .Adapter ;
@@ -274,6 +273,7 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {
274273 newCursorPosition -= text .length () - limitedText .length ();
275274 }
276275 super .setComposingText (limitedText , newCursorPosition );
276+ updateSelection ();
277277 if (limitedText != text ) {
278278 restartInput ();
279279 int lastCaret = start + limitedText .length ();
@@ -286,19 +286,44 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {
286286 @ Override
287287 public boolean commitText (CharSequence text , int newCursorPosition ) {
288288 setComposingText (text , newCursorPosition );
289- int cursorPosition = Selection .getSelectionEnd (getEditable ());
290- setComposingRegion (cursorPosition , cursorPosition );
289+ finishComposingText ();
291290 return true ;
292291 }
293292
294293 @ Override
295294 public boolean deleteSurroundingText (int leftLength , int rightLength ) {
296- Editable editable = getEditable ();
297- int cursorPosition = Selection .getSelectionEnd (editable );
298- int startDelete = Math .max (0 , cursorPosition - leftLength );
299- int endDelete = Math .min (editable .length (),
300- cursorPosition + rightLength );
301- setNewText (startDelete , endDelete , "" );
295+ // This code is from BaseInputConnection#deleteSurroundText.
296+ // We have to delete the same text in webkit.
297+ Editable content = getEditable ();
298+ int a = Selection .getSelectionStart (content );
299+ int b = Selection .getSelectionEnd (content );
300+
301+ if (a > b ) {
302+ int tmp = a ;
303+ a = b ;
304+ b = tmp ;
305+ }
306+
307+ int ca = getComposingSpanStart (content );
308+ int cb = getComposingSpanEnd (content );
309+ if (cb < ca ) {
310+ int tmp = ca ;
311+ ca = cb ;
312+ cb = tmp ;
313+ }
314+ if (ca != -1 && cb != -1 ) {
315+ if (ca < a ) a = ca ;
316+ if (cb > b ) b = cb ;
317+ }
318+
319+ int endDelete = Math .min (content .length (), b + rightLength );
320+ if (endDelete > b ) {
321+ setNewText (b , endDelete , "" );
322+ }
323+ int startDelete = Math .max (0 , a - leftLength );
324+ if (startDelete < a ) {
325+ setNewText (startDelete , a , "" );
326+ }
302327 return super .deleteSurroundingText (leftLength , rightLength );
303328 }
304329
@@ -411,6 +436,46 @@ public void setupEditorInfo(EditorInfo outAttrs) {
411436 outAttrs .imeOptions = mImeOptions ;
412437 outAttrs .hintText = mHint ;
413438 outAttrs .initialCapsMode = getCursorCapsMode (InputType .TYPE_CLASS_TEXT );
439+
440+ Editable editable = getEditable ();
441+ int selectionStart = Selection .getSelectionStart (editable );
442+ int selectionEnd = Selection .getSelectionEnd (editable );
443+ if (selectionStart < 0 || selectionEnd < 0 ) {
444+ selectionStart = editable .length ();
445+ selectionEnd = selectionStart ;
446+ }
447+ outAttrs .initialSelStart = selectionStart ;
448+ outAttrs .initialSelEnd = selectionEnd ;
449+ }
450+
451+ @ Override
452+ public boolean setSelection (int start , int end ) {
453+ boolean result = super .setSelection (start , end );
454+ updateSelection ();
455+ return result ;
456+ }
457+
458+ @ Override
459+ public boolean setComposingRegion (int start , int end ) {
460+ boolean result = super .setComposingRegion (start , end );
461+ updateSelection ();
462+ return result ;
463+ }
464+
465+ /**
466+ * Send the selection and composing spans to the IME.
467+ */
468+ private void updateSelection () {
469+ Editable editable = getEditable ();
470+ int selectionStart = Selection .getSelectionStart (editable );
471+ int selectionEnd = Selection .getSelectionEnd (editable );
472+ int composingStart = getComposingSpanStart (editable );
473+ int composingEnd = getComposingSpanEnd (editable );
474+ InputMethodManager imm = InputMethodManager .peekInstance ();
475+ if (imm != null ) {
476+ imm .updateSelection (mWebView , selectionStart , selectionEnd ,
477+ composingStart , composingEnd );
478+ }
414479 }
415480
416481 /**
0 commit comments