@@ -378,6 +378,7 @@ private class WebViewInputConnection extends BaseInputConnection {
378378 private int mInputType ;
379379 private int mImeOptions ;
380380 private String mHint ;
381+ private int mMaxLength ;
381382
382383 public WebViewInputConnection () {
383384 super (WebView .this , true );
@@ -412,13 +413,9 @@ public void setTextAndKeepSelection(CharSequence text) {
412413 Editable editable = getEditable ();
413414 int selectionStart = Selection .getSelectionStart (editable );
414415 int selectionEnd = Selection .getSelectionEnd (editable );
416+ text = limitReplaceTextByMaxLength (text , editable .length ());
415417 editable .replace (0 , editable .length (), text );
416- InputMethodManager imm = InputMethodManager .peekInstance ();
417- if (imm != null ) {
418- // Since the text has changed, do not allow the IME to replace the
419- // existing text as though it were a completion.
420- imm .restartInput (WebView .this );
421- }
418+ restartInput ();
422419 // Keep the previous selection.
423420 selectionStart = Math .min (selectionStart , editable .length ());
424421 selectionEnd = Math .min (selectionEnd , editable .length ());
@@ -429,14 +426,10 @@ public void replaceSelection(CharSequence text) {
429426 Editable editable = getEditable ();
430427 int selectionStart = Selection .getSelectionStart (editable );
431428 int selectionEnd = Selection .getSelectionEnd (editable );
429+ text = limitReplaceTextByMaxLength (text , selectionEnd - selectionStart );
432430 setNewText (selectionStart , selectionEnd , text );
433431 editable .replace (selectionStart , selectionEnd , text );
434- InputMethodManager imm = InputMethodManager .peekInstance ();
435- if (imm != null ) {
436- // Since the text has changed, do not allow the IME to replace the
437- // existing text as though it were a completion.
438- imm .restartInput (WebView .this );
439- }
432+ restartInput ();
440433 // Move caret to the end of the new text
441434 int newCaret = selectionStart + text .length ();
442435 setSelection (newCaret , newCaret );
@@ -456,8 +449,19 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {
456449 end = start ;
457450 start = temp ;
458451 }
459- setNewText (start , end , text );
460- return super .setComposingText (text , newCursorPosition );
452+ CharSequence limitedText = limitReplaceTextByMaxLength (text , end - start );
453+ setNewText (start , end , limitedText );
454+ if (limitedText != text ) {
455+ newCursorPosition -= text .length () - limitedText .length ();
456+ }
457+ super .setComposingText (limitedText , newCursorPosition );
458+ if (limitedText != text ) {
459+ restartInput ();
460+ int lastCaret = start + limitedText .length ();
461+ finishComposingText ();
462+ setSelection (lastCaret , lastCaret );
463+ }
464+ return true ;
461465 }
462466
463467 @ Override
@@ -573,6 +577,7 @@ public void initEditorInfo(WebViewCore.TextFieldInitData initData) {
573577 mHint = initData .mLabel ;
574578 mInputType = inputType ;
575579 mImeOptions = imeOptions ;
580+ mMaxLength = initData .mMaxLength ;
576581 }
577582
578583 public void setupEditorInfo (EditorInfo outAttrs ) {
@@ -660,6 +665,29 @@ private void sendKey(int keyCode) {
660665 KeyCharacterMap .VIRTUAL_KEYBOARD , 0 ,
661666 KeyEvent .FLAG_SOFT_KEYBOARD ));
662667 }
668+
669+ private CharSequence limitReplaceTextByMaxLength (CharSequence text ,
670+ int numReplaced ) {
671+ if (mMaxLength > 0 ) {
672+ Editable editable = getEditable ();
673+ int maxReplace = mMaxLength - editable .length () + numReplaced ;
674+ if (maxReplace < text .length ()) {
675+ maxReplace = Math .max (maxReplace , 0 );
676+ // New length is greater than the maximum. trim it down.
677+ text = text .subSequence (0 , maxReplace );
678+ }
679+ }
680+ return text ;
681+ }
682+
683+ private void restartInput () {
684+ InputMethodManager imm = InputMethodManager .peekInstance ();
685+ if (imm != null ) {
686+ // Since the text has changed, do not allow the IME to replace the
687+ // existing text as though it were a completion.
688+ imm .restartInput (WebView .this );
689+ }
690+ }
663691 }
664692
665693 private class PastePopupWindow extends PopupWindow implements OnClickListener {
0 commit comments