Skip to content

Commit b290caa

Browse files
George MountAndroid (Google) Code Review
authored andcommitted
Merge "Add support for maxlength text fields to WebViewInputConnection."
2 parents c383580 + 6012ede commit b290caa

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

core/java/android/webkit/WebView.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

core/java/android/webkit/WebViewCore.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,20 +923,22 @@ public String getPreviewString() {
923923
static class TextFieldInitData {
924924
public TextFieldInitData(int fieldPointer,
925925
String text, int type, boolean isSpellCheckEnabled,
926-
boolean isTextFieldNext, String label) {
926+
boolean isTextFieldNext, String label, int maxLength) {
927927
mFieldPointer = fieldPointer;
928928
mText = text;
929929
mType = type;
930930
mIsSpellCheckEnabled = isSpellCheckEnabled;
931931
mIsTextFieldNext = isTextFieldNext;
932932
mLabel = label;
933+
mMaxLength = maxLength;
933934
}
934935
int mFieldPointer;
935936
String mText;
936937
int mType;
937938
boolean mIsSpellCheckEnabled;
938939
boolean mIsTextFieldNext;
939940
String mLabel;
941+
int mMaxLength;
940942
}
941943

942944
// mAction of TouchEventData can be MotionEvent.getAction() which uses the
@@ -2826,12 +2828,13 @@ private void clearTextEntry() {
28262828
// called by JNI
28272829
private void initEditField(int pointer, String text, int inputType,
28282830
boolean isSpellCheckEnabled, boolean nextFieldIsText,
2829-
String label, int start, int end, int selectionPtr) {
2831+
String label, int start, int end, int selectionPtr, int maxLength) {
28302832
if (mWebView == null) {
28312833
return;
28322834
}
28332835
TextFieldInitData initData = new TextFieldInitData(pointer,
2834-
text, inputType, isSpellCheckEnabled, nextFieldIsText, label);
2836+
text, inputType, isSpellCheckEnabled, nextFieldIsText, label,
2837+
maxLength);
28352838
Message.obtain(mWebView.mPrivateHandler,
28362839
WebView.INIT_EDIT_FIELD, initData).sendToTarget();
28372840
Message.obtain(mWebView.mPrivateHandler,

0 commit comments

Comments
 (0)