| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.content.browser.input; | |
| 6 | |
| 7 import android.text.Editable; | |
| 8 import android.text.InputType; | |
| 9 import android.text.Selection; | |
| 10 import android.util.StringBuilderPrinter; | |
| 11 import android.view.KeyCharacterMap; | |
| 12 import android.view.KeyEvent; | |
| 13 import android.view.View; | |
| 14 import android.view.inputmethod.BaseInputConnection; | |
| 15 import android.view.inputmethod.EditorInfo; | |
| 16 import android.view.inputmethod.ExtractedText; | |
| 17 import android.view.inputmethod.ExtractedTextRequest; | |
| 18 | |
| 19 import org.chromium.base.Log; | |
| 20 import org.chromium.base.VisibleForTesting; | |
| 21 import org.chromium.blink_public.web.WebTextInputFlags; | |
| 22 import org.chromium.ui.base.ime.TextInputType; | |
| 23 | |
| 24 import java.util.Locale; | |
| 25 | |
| 26 /** | |
| 27 * InputConnection is created by ContentView.onCreateInputConnection. | |
| 28 * It then adapts android's IME to chrome's RenderWidgetHostView using the | |
| 29 * native ImeAdapterAndroid via the class ImeAdapter. | |
| 30 */ | |
| 31 public class AdapterInputConnection extends BaseInputConnection { | |
| 32 private static final String TAG = "cr_Ime"; | |
| 33 /** | |
| 34 * Selection value should be -1 if not known. See EditorInfo.java for detail
s. | |
| 35 */ | |
| 36 public static final int INVALID_SELECTION = -1; | |
| 37 public static final int INVALID_COMPOSITION = -1; | |
| 38 | |
| 39 private final ImeAdapter mImeAdapter; | |
| 40 | |
| 41 private boolean mSingleLine; | |
| 42 private int mNumNestedBatchEdits = 0; | |
| 43 private int mPendingAccent; | |
| 44 | |
| 45 private int mLastUpdateSelectionStart = INVALID_SELECTION; | |
| 46 private int mLastUpdateSelectionEnd = INVALID_SELECTION; | |
| 47 private int mLastUpdateCompositionStart = INVALID_COMPOSITION; | |
| 48 private int mLastUpdateCompositionEnd = INVALID_COMPOSITION; | |
| 49 | |
| 50 @VisibleForTesting | |
| 51 AdapterInputConnection(View view, ImeAdapter imeAdapter, int initialSelStart
, int initialSelEnd, | |
| 52 EditorInfo outAttrs) { | |
| 53 super(view, true); | |
| 54 mImeAdapter = imeAdapter; | |
| 55 mImeAdapter.setInputConnection(this); | |
| 56 | |
| 57 mSingleLine = true; | |
| 58 outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN | |
| 59 | EditorInfo.IME_FLAG_NO_EXTRACT_UI; | |
| 60 outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT | |
| 61 | EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT; | |
| 62 | |
| 63 int inputType = imeAdapter.getTextInputType(); | |
| 64 int inputFlags = imeAdapter.getTextInputFlags(); | |
| 65 if ((inputFlags & WebTextInputFlags.AutocompleteOff) != 0) { | |
| 66 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS; | |
| 67 } | |
| 68 | |
| 69 if (inputType == TextInputType.TEXT) { | |
| 70 // Normal text field | |
| 71 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
| 72 if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) { | |
| 73 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; | |
| 74 } | |
| 75 } else if (inputType == TextInputType.TEXT_AREA | |
| 76 || inputType == TextInputType.CONTENT_EDITABLE) { | |
| 77 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; | |
| 78 if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) { | |
| 79 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; | |
| 80 } | |
| 81 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NONE; | |
| 82 mSingleLine = false; | |
| 83 } else if (inputType == TextInputType.PASSWORD) { | |
| 84 // Password | |
| 85 outAttrs.inputType = InputType.TYPE_CLASS_TEXT | |
| 86 | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD; | |
| 87 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
| 88 } else if (inputType == TextInputType.SEARCH) { | |
| 89 // Search | |
| 90 outAttrs.imeOptions |= EditorInfo.IME_ACTION_SEARCH; | |
| 91 } else if (inputType == TextInputType.URL) { | |
| 92 // Url | |
| 93 outAttrs.inputType = InputType.TYPE_CLASS_TEXT | |
| 94 | InputType.TYPE_TEXT_VARIATION_URI; | |
| 95 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
| 96 } else if (inputType == TextInputType.EMAIL) { | |
| 97 // Email | |
| 98 outAttrs.inputType = InputType.TYPE_CLASS_TEXT | |
| 99 | InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; | |
| 100 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
| 101 } else if (inputType == TextInputType.TELEPHONE) { | |
| 102 // Telephone | |
| 103 // Number and telephone do not have both a Tab key and an | |
| 104 // action in default OSK, so set the action to NEXT | |
| 105 outAttrs.inputType = InputType.TYPE_CLASS_PHONE; | |
| 106 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; | |
| 107 } else if (inputType == TextInputType.NUMBER) { | |
| 108 // Number | |
| 109 outAttrs.inputType = InputType.TYPE_CLASS_NUMBER | |
| 110 | InputType.TYPE_NUMBER_VARIATION_NORMAL | |
| 111 | InputType.TYPE_NUMBER_FLAG_DECIMAL; | |
| 112 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; | |
| 113 } | |
| 114 | |
| 115 // Handling of autocapitalize. Blink will send the flag taking into acco
unt the element's | |
| 116 // type. This is not using AutocapitalizeNone because Android does not a
utocapitalize by | |
| 117 // default and there is no way to express no capitalization. | |
| 118 // Autocapitalize is meant as a hint to the virtual keyboard. | |
| 119 if ((inputFlags & WebTextInputFlags.AutocapitalizeCharacters) != 0) { | |
| 120 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; | |
| 121 } else if ((inputFlags & WebTextInputFlags.AutocapitalizeWords) != 0) { | |
| 122 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_WORDS; | |
| 123 } else if ((inputFlags & WebTextInputFlags.AutocapitalizeSentences) != 0
) { | |
| 124 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
| 125 } | |
| 126 // Content editable doesn't use autocapitalize so we need to set it manu
ally. | |
| 127 if (inputType == TextInputType.CONTENT_EDITABLE) { | |
| 128 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
| 129 } | |
| 130 | |
| 131 outAttrs.initialSelStart = initialSelStart; | |
| 132 outAttrs.initialSelEnd = initialSelEnd; | |
| 133 mLastUpdateSelectionStart = outAttrs.initialSelStart; | |
| 134 mLastUpdateSelectionEnd = outAttrs.initialSelEnd; | |
| 135 Log.d(TAG, "Constructor called with outAttrs: %s", dumpEditorInfo(outAtt
rs)); | |
| 136 } | |
| 137 | |
| 138 private static String dumpEditorInfo(EditorInfo editorInfo) { | |
| 139 StringBuilder builder = new StringBuilder(); | |
| 140 StringBuilderPrinter printer = new StringBuilderPrinter(builder); | |
| 141 editorInfo.dump(printer, ""); | |
| 142 return builder.toString(); | |
| 143 } | |
| 144 | |
| 145 private static String dumpEditable(Editable editable) { | |
| 146 return String.format(Locale.US, "Editable {[%s] SEL[%d %d] COM[%d %d]}", | |
| 147 editable.toString(), | |
| 148 Selection.getSelectionStart(editable), | |
| 149 Selection.getSelectionEnd(editable), | |
| 150 getComposingSpanStart(editable), | |
| 151 getComposingSpanEnd(editable)); | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * Updates the AdapterInputConnection's internal representation of the text
being edited and | |
| 156 * its selection and composition properties. The resulting Editable is acces
sible through the | |
| 157 * getEditable() method. If the text has not changed, this also calls update
Selection on the | |
| 158 * InputMethodManager. | |
| 159 * | |
| 160 * @param text The String contents of the field being edited. | |
| 161 * @param selectionStart The character offset of the selection start, or the
caret position if | |
| 162 * there is no selection. | |
| 163 * @param selectionEnd The character offset of the selection end, or the car
et position if there | |
| 164 * is no selection. | |
| 165 * @param compositionStart The character offset of the composition start, or
-1 if there is no | |
| 166 * composition. | |
| 167 * @param compositionEnd The character offset of the composition end, or -1
if there is no | |
| 168 * selection. | |
| 169 * @param isNonImeChange True when the update was caused by non-IME (e.g. Ja
vascript). | |
| 170 */ | |
| 171 @VisibleForTesting | |
| 172 public void updateState(String text, int selectionStart, int selectionEnd, i
nt compositionStart, | |
| 173 int compositionEnd, boolean isNonImeChange) { | |
| 174 Log.d(TAG, "updateState [%s] [%s %s] [%s %s] [%b]", text, selectionStart
, | |
| 175 selectionEnd, compositionStart, compositionEnd, isNonImeChange); | |
| 176 // If this update is from the IME, no further state modification is nece
ssary because the | |
| 177 // state should have been updated already by the IM framework directly. | |
| 178 if (!isNonImeChange) return; | |
| 179 | |
| 180 // Non-breaking spaces can cause the IME to get confused. Replace with n
ormal spaces. | |
| 181 text = text.replace('\u00A0', ' '); | |
| 182 | |
| 183 selectionStart = Math.min(selectionStart, text.length()); | |
| 184 selectionEnd = Math.min(selectionEnd, text.length()); | |
| 185 compositionStart = Math.min(compositionStart, text.length()); | |
| 186 compositionEnd = Math.min(compositionEnd, text.length()); | |
| 187 | |
| 188 Editable editable = getEditableInternal(); | |
| 189 | |
| 190 String prevText = editable.toString(); | |
| 191 boolean textUnchanged = prevText.equals(text); | |
| 192 | |
| 193 if (!textUnchanged) { | |
| 194 editable.replace(0, editable.length(), text); | |
| 195 } | |
| 196 | |
| 197 Selection.setSelection(editable, selectionStart, selectionEnd); | |
| 198 | |
| 199 if (compositionStart == compositionEnd) { | |
| 200 removeComposingSpans(editable); | |
| 201 } else { | |
| 202 super.setComposingRegion(compositionStart, compositionEnd); | |
| 203 } | |
| 204 updateSelectionIfRequired(); | |
| 205 } | |
| 206 | |
| 207 /** | |
| 208 * @see BaseInputConnection#getEditable() | |
| 209 */ | |
| 210 @Override | |
| 211 public Editable getEditable() { | |
| 212 Editable editable = getEditableInternal(); | |
| 213 Log.d(TAG, "getEditable: %s", dumpEditable(editable)); | |
| 214 return editable; | |
| 215 } | |
| 216 | |
| 217 private Editable getEditableInternal() { | |
| 218 return mImeAdapter.getEditable(); | |
| 219 } | |
| 220 | |
| 221 /** | |
| 222 * Sends selection update to the InputMethodManager unless we are currently
in a batch edit or | |
| 223 * if the exact same selection and composition update was sent already. | |
| 224 */ | |
| 225 private void updateSelectionIfRequired() { | |
| 226 if (mNumNestedBatchEdits != 0) return; | |
| 227 Editable editable = getEditableInternal(); | |
| 228 int selectionStart = Selection.getSelectionStart(editable); | |
| 229 int selectionEnd = Selection.getSelectionEnd(editable); | |
| 230 int compositionStart = getComposingSpanStart(editable); | |
| 231 int compositionEnd = getComposingSpanEnd(editable); | |
| 232 // Avoid sending update if we sent an exact update already previously. | |
| 233 if (mLastUpdateSelectionStart == selectionStart | |
| 234 && mLastUpdateSelectionEnd == selectionEnd | |
| 235 && mLastUpdateCompositionStart == compositionStart | |
| 236 && mLastUpdateCompositionEnd == compositionEnd) { | |
| 237 return; | |
| 238 } | |
| 239 Log.d(TAG, "updateSelectionIfRequired [%d %d] [%d %d]", selectionStart,
selectionEnd, | |
| 240 compositionStart, compositionEnd); | |
| 241 // updateSelection should be called every time the selection or composit
ion changes | |
| 242 // if it happens not within a batch edit, or at the end of each top leve
l batch edit. | |
| 243 mImeAdapter.updateSelection(selectionStart, selectionEnd, compositionSta
rt, compositionEnd); | |
| 244 mLastUpdateSelectionStart = selectionStart; | |
| 245 mLastUpdateSelectionEnd = selectionEnd; | |
| 246 mLastUpdateCompositionStart = compositionStart; | |
| 247 mLastUpdateCompositionEnd = compositionEnd; | |
| 248 } | |
| 249 | |
| 250 /** | |
| 251 * @see BaseInputConnection#setComposingText(java.lang.CharSequence, int) | |
| 252 */ | |
| 253 @Override | |
| 254 public boolean setComposingText(CharSequence text, int newCursorPosition) { | |
| 255 Log.d(TAG, "setComposingText [%s] [%d]", text, newCursorPosition); | |
| 256 mPendingAccent = 0; | |
| 257 super.setComposingText(text, newCursorPosition); | |
| 258 updateSelectionIfRequired(); | |
| 259 return mImeAdapter.sendCompositionToNative(text, newCursorPosition, fals
e); | |
| 260 } | |
| 261 | |
| 262 /** | |
| 263 * @see BaseInputConnection#commitText(java.lang.CharSequence, int) | |
| 264 */ | |
| 265 @Override | |
| 266 public boolean commitText(CharSequence text, int newCursorPosition) { | |
| 267 Log.d(TAG, "commitText [%s] [%d]", text, newCursorPosition); | |
| 268 mPendingAccent = 0; | |
| 269 super.commitText(text, newCursorPosition); | |
| 270 updateSelectionIfRequired(); | |
| 271 return mImeAdapter.sendCompositionToNative(text, newCursorPosition, text
.length() > 0); | |
| 272 } | |
| 273 | |
| 274 /** | |
| 275 * @see BaseInputConnection#performEditorAction(int) | |
| 276 */ | |
| 277 @Override | |
| 278 public boolean performEditorAction(int actionCode) { | |
| 279 Log.d(TAG, "performEditorAction [%d]", actionCode); | |
| 280 return mImeAdapter.performEditorAction(actionCode); | |
| 281 } | |
| 282 | |
| 283 /** | |
| 284 * @see BaseInputConnection#performContextMenuAction(int) | |
| 285 */ | |
| 286 @Override | |
| 287 public boolean performContextMenuAction(int id) { | |
| 288 Log.d(TAG, "performContextMenuAction [%d]", id); | |
| 289 return mImeAdapter.performContextMenuAction(id); | |
| 290 } | |
| 291 | |
| 292 /** | |
| 293 * @see BaseInputConnection#getExtractedText(android.view.inputmethod.Extrac
tedTextRequest, | |
| 294 * int) | |
| 295 */ | |
| 296 @Override | |
| 297 public ExtractedText getExtractedText(ExtractedTextRequest request, int flag
s) { | |
| 298 Log.d(TAG, "getExtractedText"); | |
| 299 Editable editable = getEditableInternal(); | |
| 300 ExtractedText et = new ExtractedText(); | |
| 301 et.text = editable.toString(); | |
| 302 et.partialEndOffset = editable.length(); | |
| 303 et.selectionStart = Selection.getSelectionStart(editable); | |
| 304 et.selectionEnd = Selection.getSelectionEnd(editable); | |
| 305 et.flags = mSingleLine ? ExtractedText.FLAG_SINGLE_LINE : 0; | |
| 306 return et; | |
| 307 } | |
| 308 | |
| 309 /** | |
| 310 * @see BaseInputConnection#beginBatchEdit() | |
| 311 */ | |
| 312 @Override | |
| 313 public boolean beginBatchEdit() { | |
| 314 Log.d(TAG, "beginBatchEdit [%b]", (mNumNestedBatchEdits == 0)); | |
| 315 mNumNestedBatchEdits++; | |
| 316 return true; | |
| 317 } | |
| 318 | |
| 319 /** | |
| 320 * @see BaseInputConnection#endBatchEdit() | |
| 321 */ | |
| 322 @Override | |
| 323 public boolean endBatchEdit() { | |
| 324 if (mNumNestedBatchEdits == 0) return false; | |
| 325 --mNumNestedBatchEdits; | |
| 326 Log.d(TAG, "endBatchEdit [%b]", (mNumNestedBatchEdits == 0)); | |
| 327 if (mNumNestedBatchEdits == 0) updateSelectionIfRequired(); | |
| 328 return mNumNestedBatchEdits != 0; | |
| 329 } | |
| 330 | |
| 331 /** | |
| 332 * @see BaseInputConnection#deleteSurroundingText(int, int) | |
| 333 */ | |
| 334 @Override | |
| 335 public boolean deleteSurroundingText(int beforeLength, int afterLength) { | |
| 336 return deleteSurroundingTextImpl(beforeLength, afterLength, false); | |
| 337 } | |
| 338 | |
| 339 /** | |
| 340 * Check if the given {@code index} is between UTF-16 surrogate pair. | |
| 341 * @param str The String. | |
| 342 * @param index The index | |
| 343 * @return True if the index is between UTF-16 surrogate pair, false otherwi
se. | |
| 344 */ | |
| 345 @VisibleForTesting | |
| 346 static boolean isIndexBetweenUtf16SurrogatePair(CharSequence str, int index)
{ | |
| 347 return index > 0 && index < str.length() && Character.isHighSurrogate(st
r.charAt(index - 1)) | |
| 348 && Character.isLowSurrogate(str.charAt(index)); | |
| 349 } | |
| 350 | |
| 351 private boolean deleteSurroundingTextImpl( | |
| 352 int beforeLength, int afterLength, boolean fromPhysicalKey) { | |
| 353 Log.d(TAG, "deleteSurroundingText [%d %d %b]", beforeLength, afterLength
, fromPhysicalKey); | |
| 354 | |
| 355 if (mPendingAccent != 0) { | |
| 356 finishComposingText(); | |
| 357 } | |
| 358 | |
| 359 Editable editable = getEditableInternal(); | |
| 360 int selectionStart = Selection.getSelectionStart(editable); | |
| 361 int selectionEnd = Selection.getSelectionEnd(editable); | |
| 362 int availableBefore = selectionStart; | |
| 363 int availableAfter = editable.length() - selectionEnd; | |
| 364 beforeLength = Math.min(beforeLength, availableBefore); | |
| 365 afterLength = Math.min(afterLength, availableAfter); | |
| 366 | |
| 367 // Adjust these values even before calling super.deleteSurroundingText()
to be consistent | |
| 368 // with the super class. | |
| 369 if (isIndexBetweenUtf16SurrogatePair(editable, selectionStart - beforeLe
ngth)) { | |
| 370 beforeLength += 1; | |
| 371 } | |
| 372 if (isIndexBetweenUtf16SurrogatePair(editable, selectionEnd + afterLengt
h)) { | |
| 373 afterLength += 1; | |
| 374 } | |
| 375 | |
| 376 super.deleteSurroundingText(beforeLength, afterLength); | |
| 377 updateSelectionIfRequired(); | |
| 378 | |
| 379 // If this was called due to a physical key, no need to generate a key e
vent here as | |
| 380 // the caller will take care of forwarding the original. | |
| 381 if (fromPhysicalKey) { | |
| 382 return true; | |
| 383 } | |
| 384 | |
| 385 return mImeAdapter.deleteSurroundingText(beforeLength, afterLength); | |
| 386 } | |
| 387 | |
| 388 /** | |
| 389 * @see BaseInputConnection#sendKeyEvent(android.view.KeyEvent) | |
| 390 */ | |
| 391 @Override | |
| 392 public boolean sendKeyEvent(KeyEvent event) { | |
| 393 Log.d(TAG, "sendKeyEvent [%d] [%d] [%d]", event.getAction(), event.getKe
yCode(), | |
| 394 event.getUnicodeChar()); | |
| 395 | |
| 396 int action = event.getAction(); | |
| 397 int keycode = event.getKeyCode(); | |
| 398 int unicodeChar = event.getUnicodeChar(); | |
| 399 | |
| 400 // If this isn't a KeyDown event, no need to update composition state; j
ust pass the key | |
| 401 // event through and return. But note that some keys, such as enter, may
actually be | |
| 402 // handled on ACTION_UP in Blink. | |
| 403 if (action != KeyEvent.ACTION_DOWN) { | |
| 404 mImeAdapter.sendKeyEvent(event); | |
| 405 return true; | |
| 406 } | |
| 407 | |
| 408 // If this is backspace/del or if the key has a character representation
, | |
| 409 // need to update the underlying Editable (i.e. the local representation
of the text | |
| 410 // being edited). Some IMEs like Jellybean stock IME and Samsung IME mi
x in delete | |
| 411 // KeyPress events instead of calling deleteSurroundingText. | |
| 412 if (keycode == KeyEvent.KEYCODE_DEL) { | |
| 413 deleteSurroundingTextImpl(1, 0, true); | |
| 414 } else if (keycode == KeyEvent.KEYCODE_FORWARD_DEL) { | |
| 415 deleteSurroundingTextImpl(0, 1, true); | |
| 416 } else if (keycode == KeyEvent.KEYCODE_ENTER) { | |
| 417 // Finish text composition when pressing enter, as that may submit a
form field. | |
| 418 // TODO(aurimas): remove this workaround when crbug.com/278584 is fi
xed. | |
| 419 finishComposingText(); | |
| 420 } else if ((unicodeChar & KeyCharacterMap.COMBINING_ACCENT) != 0) { | |
| 421 // Store a pending accent character and make it the current composit
ion. | |
| 422 int pendingAccent = unicodeChar & KeyCharacterMap.COMBINING_ACCENT_M
ASK; | |
| 423 StringBuilder builder = new StringBuilder(); | |
| 424 builder.appendCodePoint(pendingAccent); | |
| 425 setComposingText(builder.toString(), 1); | |
| 426 mPendingAccent = pendingAccent; | |
| 427 return true; | |
| 428 } else if (mPendingAccent != 0 && unicodeChar != 0) { | |
| 429 int combined = KeyEvent.getDeadChar(mPendingAccent, unicodeChar); | |
| 430 if (combined != 0) { | |
| 431 StringBuilder builder = new StringBuilder(); | |
| 432 builder.appendCodePoint(combined); | |
| 433 commitText(builder.toString(), 1); | |
| 434 return true; | |
| 435 } | |
| 436 // Noncombinable character; commit the accent character and fall thr
ough to sending | |
| 437 // the key event for the character afterwards. | |
| 438 finishComposingText(); | |
| 439 } | |
| 440 replaceSelectionWithUnicodeChar(unicodeChar); | |
| 441 mImeAdapter.sendKeyEvent(event); | |
| 442 return true; | |
| 443 } | |
| 444 | |
| 445 /** | |
| 446 * Update the Editable to reflect what Blink will do in response to the KeyD
own for a | |
| 447 * unicode-mapped key event. | |
| 448 * @param unicodeChar The Unicode character to update selection with. | |
| 449 */ | |
| 450 private void replaceSelectionWithUnicodeChar(int unicodeChar) { | |
| 451 if (unicodeChar == 0) return; | |
| 452 Editable editable = getEditableInternal(); | |
| 453 int selectionStart = Selection.getSelectionStart(editable); | |
| 454 int selectionEnd = Selection.getSelectionEnd(editable); | |
| 455 if (selectionStart > selectionEnd) { | |
| 456 int temp = selectionStart; | |
| 457 selectionStart = selectionEnd; | |
| 458 selectionEnd = temp; | |
| 459 } | |
| 460 editable.replace(selectionStart, selectionEnd, Character.toString((char)
unicodeChar)); | |
| 461 updateSelectionIfRequired(); | |
| 462 } | |
| 463 | |
| 464 /** | |
| 465 * @see BaseInputConnection#finishComposingText() | |
| 466 */ | |
| 467 @Override | |
| 468 public boolean finishComposingText() { | |
| 469 Log.d(TAG, "finishComposingText"); | |
| 470 mPendingAccent = 0; | |
| 471 | |
| 472 if (getComposingSpanStart(getEditableInternal()) | |
| 473 == getComposingSpanEnd(getEditableInternal())) { | |
| 474 return true; | |
| 475 } | |
| 476 | |
| 477 super.finishComposingText(); | |
| 478 updateSelectionIfRequired(); | |
| 479 mImeAdapter.finishComposingText(); | |
| 480 | |
| 481 return true; | |
| 482 } | |
| 483 | |
| 484 /** | |
| 485 * @see BaseInputConnection#setSelection(int, int) | |
| 486 */ | |
| 487 @Override | |
| 488 public boolean setSelection(int start, int end) { | |
| 489 Log.d(TAG, "setSelection [%d %d]", start, end); | |
| 490 int textLength = getEditableInternal().length(); | |
| 491 if (start < 0 || end < 0 || start > textLength || end > textLength) retu
rn true; | |
| 492 super.setSelection(start, end); | |
| 493 updateSelectionIfRequired(); | |
| 494 return mImeAdapter.setEditableSelectionOffsets(start, end); | |
| 495 } | |
| 496 | |
| 497 /** | |
| 498 * Call this when restartInput() is called. | |
| 499 */ | |
| 500 void onRestartInput() { | |
| 501 Log.d(TAG, "onRestartInput"); | |
| 502 mNumNestedBatchEdits = 0; | |
| 503 mPendingAccent = 0; | |
| 504 } | |
| 505 | |
| 506 /** | |
| 507 * @see BaseInputConnection#setComposingRegion(int, int) | |
| 508 */ | |
| 509 @Override | |
| 510 public boolean setComposingRegion(int start, int end) { | |
| 511 Log.d(TAG, "setComposingRegion [%d %d]", start, end); | |
| 512 Editable editable = getEditableInternal(); | |
| 513 int textLength = editable.length(); | |
| 514 int a = Math.min(start, end); | |
| 515 int b = Math.max(start, end); | |
| 516 if (a < 0) a = 0; | |
| 517 if (b < 0) b = 0; | |
| 518 if (a > textLength) a = textLength; | |
| 519 if (b > textLength) b = textLength; | |
| 520 | |
| 521 if (a == b) { | |
| 522 removeComposingSpans(editable); | |
| 523 } else { | |
| 524 super.setComposingRegion(a, b); | |
| 525 } | |
| 526 updateSelectionIfRequired(); | |
| 527 | |
| 528 CharSequence regionText = null; | |
| 529 if (b > a) { | |
| 530 regionText = editable.subSequence(a, b); | |
| 531 } | |
| 532 return mImeAdapter.setComposingRegion(regionText, a, b); | |
| 533 } | |
| 534 | |
| 535 @VisibleForTesting | |
| 536 static class ImeState { | |
| 537 public final String text; | |
| 538 public final int selectionStart; | |
| 539 public final int selectionEnd; | |
| 540 public final int compositionStart; | |
| 541 public final int compositionEnd; | |
| 542 | |
| 543 public ImeState(String text, int selectionStart, int selectionEnd, | |
| 544 int compositionStart, int compositionEnd) { | |
| 545 this.text = text; | |
| 546 this.selectionStart = selectionStart; | |
| 547 this.selectionEnd = selectionEnd; | |
| 548 this.compositionStart = compositionStart; | |
| 549 this.compositionEnd = compositionEnd; | |
| 550 } | |
| 551 } | |
| 552 | |
| 553 @VisibleForTesting | |
| 554 ImeState getImeStateForTesting() { | |
| 555 Editable editable = getEditableInternal(); | |
| 556 String text = editable.toString(); | |
| 557 int selectionStart = Selection.getSelectionStart(editable); | |
| 558 int selectionEnd = Selection.getSelectionEnd(editable); | |
| 559 int compositionStart = getComposingSpanStart(editable); | |
| 560 int compositionEnd = getComposingSpanEnd(editable); | |
| 561 return new ImeState(text, selectionStart, selectionEnd, compositionStart
, compositionEnd); | |
| 562 } | |
| 563 } | |
| OLD | NEW |