BaseInputConnection.java revision ac3e599069e1b87ea190f008aef60a506c8561c7
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package android.view.inputmethod; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.os.Bundle; 22import android.os.SystemClock; 23import android.text.Editable; 24import android.text.NoCopySpan; 25import android.text.Selection; 26import android.text.Spannable; 27import android.text.SpannableStringBuilder; 28import android.text.Spanned; 29import android.text.TextUtils; 30import android.text.method.MetaKeyKeyListener; 31import android.util.Log; 32import android.util.LogPrinter; 33import android.view.KeyCharacterMap; 34import android.view.KeyEvent; 35import android.view.View; 36import android.view.ViewRootImpl; 37 38class ComposingText implements NoCopySpan { 39} 40 41/** 42 * Base class for implementors of the InputConnection interface, taking care 43 * of most of the common behavior for providing a connection to an Editable. 44 * Implementors of this class will want to be sure to implement 45 * {@link #getEditable} to provide access to their own editable object, and 46 * to refer to the documentation in {@link InputConnection}. 47 */ 48public class BaseInputConnection implements InputConnection { 49 private static final boolean DEBUG = false; 50 private static final String TAG = "BaseInputConnection"; 51 static final Object COMPOSING = new ComposingText(); 52 53 /** @hide */ 54 protected final InputMethodManager mIMM; 55 final View mTargetView; 56 final boolean mDummyMode; 57 58 private Object[] mDefaultComposingSpans; 59 60 Editable mEditable; 61 KeyCharacterMap mKeyCharacterMap; 62 63 BaseInputConnection(InputMethodManager mgr, boolean fullEditor) { 64 mIMM = mgr; 65 mTargetView = null; 66 mDummyMode = !fullEditor; 67 } 68 69 public BaseInputConnection(View targetView, boolean fullEditor) { 70 mIMM = (InputMethodManager)targetView.getContext().getSystemService( 71 Context.INPUT_METHOD_SERVICE); 72 mTargetView = targetView; 73 mDummyMode = !fullEditor; 74 } 75 76 public static final void removeComposingSpans(Spannable text) { 77 text.removeSpan(COMPOSING); 78 Object[] sps = text.getSpans(0, text.length(), Object.class); 79 if (sps != null) { 80 for (int i=sps.length-1; i>=0; i--) { 81 Object o = sps[i]; 82 if ((text.getSpanFlags(o)&Spanned.SPAN_COMPOSING) != 0) { 83 text.removeSpan(o); 84 } 85 } 86 } 87 } 88 89 public static void setComposingSpans(Spannable text) { 90 setComposingSpans(text, 0, text.length()); 91 } 92 93 /** @hide */ 94 public static void setComposingSpans(Spannable text, int start, int end) { 95 final Object[] sps = text.getSpans(start, end, Object.class); 96 if (sps != null) { 97 for (int i=sps.length-1; i>=0; i--) { 98 final Object o = sps[i]; 99 if (o == COMPOSING) { 100 text.removeSpan(o); 101 continue; 102 } 103 104 final int fl = text.getSpanFlags(o); 105 if ((fl&(Spanned.SPAN_COMPOSING|Spanned.SPAN_POINT_MARK_MASK)) 106 != (Spanned.SPAN_COMPOSING|Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)) { 107 text.setSpan(o, text.getSpanStart(o), text.getSpanEnd(o), 108 (fl & ~Spanned.SPAN_POINT_MARK_MASK) 109 | Spanned.SPAN_COMPOSING 110 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 111 } 112 } 113 } 114 115 text.setSpan(COMPOSING, start, end, 116 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 117 } 118 119 public static int getComposingSpanStart(Spannable text) { 120 return text.getSpanStart(COMPOSING); 121 } 122 123 public static int getComposingSpanEnd(Spannable text) { 124 return text.getSpanEnd(COMPOSING); 125 } 126 127 /** 128 * Return the target of edit operations. The default implementation 129 * returns its own fake editable that is just used for composing text; 130 * subclasses that are real text editors should override this and 131 * supply their own. 132 */ 133 public Editable getEditable() { 134 if (mEditable == null) { 135 mEditable = Editable.Factory.getInstance().newEditable(""); 136 Selection.setSelection(mEditable, 0); 137 } 138 return mEditable; 139 } 140 141 /** 142 * Default implementation does nothing. 143 */ 144 public boolean beginBatchEdit() { 145 return false; 146 } 147 148 /** 149 * Default implementation does nothing. 150 */ 151 public boolean endBatchEdit() { 152 return false; 153 } 154 155 /** 156 * Called when this InputConnection is no longer used by the InputMethodManager. 157 * 158 * @hide 159 */ 160 protected void reportFinish() { 161 // Intentionaly empty 162 } 163 164 /** 165 * Default implementation uses 166 * {@link MetaKeyKeyListener#clearMetaKeyState(long, int) 167 * MetaKeyKeyListener.clearMetaKeyState(long, int)} to clear the state. 168 */ 169 public boolean clearMetaKeyStates(int states) { 170 final Editable content = getEditable(); 171 if (content == null) return false; 172 MetaKeyKeyListener.clearMetaKeyState(content, states); 173 return true; 174 } 175 176 /** 177 * Default implementation does nothing and returns false. 178 */ 179 public boolean commitCompletion(CompletionInfo text) { 180 return false; 181 } 182 183 /** 184 * Default implementation does nothing and returns false. 185 */ 186 public boolean commitCorrection(CorrectionInfo correctionInfo) { 187 return false; 188 } 189 190 /** 191 * Default implementation replaces any existing composing text with 192 * the given text. In addition, only if dummy mode, a key event is 193 * sent for the new text and the current editable buffer cleared. 194 */ 195 public boolean commitText(CharSequence text, int newCursorPosition) { 196 if (DEBUG) Log.v(TAG, "commitText " + text); 197 replaceText(text, newCursorPosition, false); 198 sendCurrentText(); 199 return true; 200 } 201 202 /** 203 * The default implementation performs the deletion around the current selection position of the 204 * editable text. 205 * 206 * @param beforeLength The number of characters before the cursor to be deleted, in code unit. 207 * If this is greater than the number of existing characters between the beginning of the 208 * text and the cursor, then this method does not fail but deletes all the characters in 209 * that range. 210 * @param afterLength The number of characters after the cursor to be deleted, in code unit. 211 * If this is greater than the number of existing characters between the cursor and 212 * the end of the text, then this method does not fail but deletes all the characters in 213 * that range. 214 */ 215 public boolean deleteSurroundingText(int beforeLength, int afterLength) { 216 if (DEBUG) Log.v(TAG, "deleteSurroundingText " + beforeLength 217 + " / " + afterLength); 218 final Editable content = getEditable(); 219 if (content == null) return false; 220 221 beginBatchEdit(); 222 223 int a = Selection.getSelectionStart(content); 224 int b = Selection.getSelectionEnd(content); 225 226 if (a > b) { 227 int tmp = a; 228 a = b; 229 b = tmp; 230 } 231 232 // Ignore the composing text. 233 int ca = getComposingSpanStart(content); 234 int cb = getComposingSpanEnd(content); 235 if (cb < ca) { 236 int tmp = ca; 237 ca = cb; 238 cb = tmp; 239 } 240 if (ca != -1 && cb != -1) { 241 if (ca < a) a = ca; 242 if (cb > b) b = cb; 243 } 244 245 int deleted = 0; 246 247 if (beforeLength > 0) { 248 int start = a - beforeLength; 249 if (start < 0) start = 0; 250 content.delete(start, a); 251 deleted = a - start; 252 } 253 254 if (afterLength > 0) { 255 b = b - deleted; 256 257 int end = b + afterLength; 258 if (end > content.length()) end = content.length(); 259 260 content.delete(b, end); 261 } 262 263 endBatchEdit(); 264 265 return true; 266 } 267 268 private static int INVALID_INDEX = -1; 269 private static int findIndexBackward(final CharSequence cs, final int from, 270 final int numCodePoints) { 271 int currentIndex = from; 272 boolean waitingHighSurrogate = false; 273 final int N = cs.length(); 274 if (currentIndex < 0 || N < currentIndex) { 275 return INVALID_INDEX; // The starting point is out of range. 276 } 277 if (numCodePoints < 0) { 278 return INVALID_INDEX; // Basically this should not happen. 279 } 280 int remainingCodePoints = numCodePoints; 281 while (true) { 282 if (remainingCodePoints == 0) { 283 return currentIndex; // Reached to the requested length in code points. 284 } 285 286 --currentIndex; 287 if (currentIndex < 0) { 288 if (waitingHighSurrogate) { 289 return INVALID_INDEX; // An invalid surrogate pair is found. 290 } 291 return 0; // Reached to the beginning of the text w/o any invalid surrogate pair. 292 } 293 final char c = cs.charAt(currentIndex); 294 if (waitingHighSurrogate) { 295 if (!java.lang.Character.isHighSurrogate(c)) { 296 return INVALID_INDEX; // An invalid surrogate pair is found. 297 } 298 waitingHighSurrogate = false; 299 --remainingCodePoints; 300 continue; 301 } 302 if (!java.lang.Character.isSurrogate(c)) { 303 --remainingCodePoints; 304 continue; 305 } 306 if (java.lang.Character.isHighSurrogate(c)) { 307 return INVALID_INDEX; // A invalid surrogate pair is found. 308 } 309 waitingHighSurrogate = true; 310 } 311 } 312 313 private static int findIndexForward(final CharSequence cs, final int from, 314 final int numCodePoints) { 315 int currentIndex = from; 316 boolean waitingLowSurrogate = false; 317 final int N = cs.length(); 318 if (currentIndex < 0 || N < currentIndex) { 319 return INVALID_INDEX; // The starting point is out of range. 320 } 321 if (numCodePoints < 0) { 322 return INVALID_INDEX; // Basically this should not happen. 323 } 324 int remainingCodePoints = numCodePoints; 325 326 while (true) { 327 if (remainingCodePoints == 0) { 328 return currentIndex; // Reached to the requested length in code points. 329 } 330 331 if (currentIndex >= N) { 332 if (waitingLowSurrogate) { 333 return INVALID_INDEX; // An invalid surrogate pair is found. 334 } 335 return N; // Reached to the end of the text w/o any invalid surrogate pair. 336 } 337 final char c = cs.charAt(currentIndex); 338 if (waitingLowSurrogate) { 339 if (!java.lang.Character.isLowSurrogate(c)) { 340 return INVALID_INDEX; // An invalid surrogate pair is found. 341 } 342 --remainingCodePoints; 343 waitingLowSurrogate = false; 344 ++currentIndex; 345 continue; 346 } 347 if (!java.lang.Character.isSurrogate(c)) { 348 --remainingCodePoints; 349 ++currentIndex; 350 continue; 351 } 352 if (java.lang.Character.isLowSurrogate(c)) { 353 return INVALID_INDEX; // A invalid surrogate pair is found. 354 } 355 waitingLowSurrogate = true; 356 ++currentIndex; 357 } 358 } 359 360 /** 361 * The default implementation performs the deletion around the current selection position of the 362 * editable text. 363 * @param beforeLength The number of characters before the cursor to be deleted, in code points. 364 * If this is greater than the number of existing characters between the beginning of the 365 * text and the cursor, then this method does not fail but deletes all the characters in 366 * that range. 367 * @param afterLength The number of characters after the cursor to be deleted, in code points. 368 * If this is greater than the number of existing characters between the cursor and 369 * the end of the text, then this method does not fail but deletes all the characters in 370 * that range. 371 */ 372 public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { 373 if (DEBUG) Log.v(TAG, "deleteSurroundingText " + beforeLength 374 + " / " + afterLength); 375 final Editable content = getEditable(); 376 if (content == null) return false; 377 378 beginBatchEdit(); 379 380 int a = Selection.getSelectionStart(content); 381 int b = Selection.getSelectionEnd(content); 382 383 if (a > b) { 384 int tmp = a; 385 a = b; 386 b = tmp; 387 } 388 389 // Ignore the composing text. 390 int ca = getComposingSpanStart(content); 391 int cb = getComposingSpanEnd(content); 392 if (cb < ca) { 393 int tmp = ca; 394 ca = cb; 395 cb = tmp; 396 } 397 if (ca != -1 && cb != -1) { 398 if (ca < a) a = ca; 399 if (cb > b) b = cb; 400 } 401 402 if (a >= 0 && b >= 0) { 403 final int start = findIndexBackward(content, a, Math.max(beforeLength, 0)); 404 if (start != INVALID_INDEX) { 405 final int end = findIndexForward(content, b, Math.max(afterLength, 0)); 406 if (end != INVALID_INDEX) { 407 final int numDeleteBefore = a - start; 408 if (numDeleteBefore > 0) { 409 content.delete(start, a); 410 } 411 final int numDeleteAfter = end - b; 412 if (numDeleteAfter > 0) { 413 content.delete(b - numDeleteBefore, end - numDeleteBefore); 414 } 415 } 416 } 417 // NOTE: You may think we should return false here if start and/or end is INVALID_INDEX, 418 // but the truth is that IInputConnectionWrapper running in the middle of IPC calls 419 // always returns true to the IME without waiting for the completion of this method as 420 // IInputConnectionWrapper#isAtive() returns true. This is actually why some methods 421 // including this method look like asynchronous calls from the IME. 422 } 423 424 endBatchEdit(); 425 426 return true; 427 } 428 429 /** 430 * The default implementation removes the composing state from the 431 * current editable text. In addition, only if dummy mode, a key event is 432 * sent for the new text and the current editable buffer cleared. 433 */ 434 public boolean finishComposingText() { 435 if (DEBUG) Log.v(TAG, "finishComposingText"); 436 final Editable content = getEditable(); 437 if (content != null) { 438 beginBatchEdit(); 439 removeComposingSpans(content); 440 // Note: sendCurrentText does nothing unless mDummyMode is set 441 sendCurrentText(); 442 endBatchEdit(); 443 } 444 return true; 445 } 446 447 /** 448 * The default implementation uses TextUtils.getCapsMode to get the 449 * cursor caps mode for the current selection position in the editable 450 * text, unless in dummy mode in which case 0 is always returned. 451 */ 452 public int getCursorCapsMode(int reqModes) { 453 if (mDummyMode) return 0; 454 455 final Editable content = getEditable(); 456 if (content == null) return 0; 457 458 int a = Selection.getSelectionStart(content); 459 int b = Selection.getSelectionEnd(content); 460 461 if (a > b) { 462 int tmp = a; 463 a = b; 464 b = tmp; 465 } 466 467 return TextUtils.getCapsMode(content, a, reqModes); 468 } 469 470 /** 471 * The default implementation always returns null. 472 */ 473 public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { 474 return null; 475 } 476 477 /** 478 * The default implementation returns the given amount of text from the 479 * current cursor position in the buffer. 480 */ 481 public CharSequence getTextBeforeCursor(int length, int flags) { 482 final Editable content = getEditable(); 483 if (content == null) return null; 484 485 int a = Selection.getSelectionStart(content); 486 int b = Selection.getSelectionEnd(content); 487 488 if (a > b) { 489 int tmp = a; 490 a = b; 491 b = tmp; 492 } 493 494 if (a <= 0) { 495 return ""; 496 } 497 498 if (length > a) { 499 length = a; 500 } 501 502 if ((flags&GET_TEXT_WITH_STYLES) != 0) { 503 return content.subSequence(a - length, a); 504 } 505 return TextUtils.substring(content, a - length, a); 506 } 507 508 /** 509 * The default implementation returns the text currently selected, or null if none is 510 * selected. 511 */ 512 public CharSequence getSelectedText(int flags) { 513 final Editable content = getEditable(); 514 if (content == null) return null; 515 516 int a = Selection.getSelectionStart(content); 517 int b = Selection.getSelectionEnd(content); 518 519 if (a > b) { 520 int tmp = a; 521 a = b; 522 b = tmp; 523 } 524 525 if (a == b) return null; 526 527 if ((flags&GET_TEXT_WITH_STYLES) != 0) { 528 return content.subSequence(a, b); 529 } 530 return TextUtils.substring(content, a, b); 531 } 532 533 /** 534 * The default implementation returns the given amount of text from the 535 * current cursor position in the buffer. 536 */ 537 public CharSequence getTextAfterCursor(int length, int flags) { 538 final Editable content = getEditable(); 539 if (content == null) return null; 540 541 int a = Selection.getSelectionStart(content); 542 int b = Selection.getSelectionEnd(content); 543 544 if (a > b) { 545 int tmp = a; 546 a = b; 547 b = tmp; 548 } 549 550 // Guard against the case where the cursor has not been positioned yet. 551 if (b < 0) { 552 b = 0; 553 } 554 555 if (b + length > content.length()) { 556 length = content.length() - b; 557 } 558 559 560 if ((flags&GET_TEXT_WITH_STYLES) != 0) { 561 return content.subSequence(b, b + length); 562 } 563 return TextUtils.substring(content, b, b + length); 564 } 565 566 /** 567 * The default implementation turns this into the enter key. 568 */ 569 public boolean performEditorAction(int actionCode) { 570 long eventTime = SystemClock.uptimeMillis(); 571 sendKeyEvent(new KeyEvent(eventTime, eventTime, 572 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0, 573 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 574 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE 575 | KeyEvent.FLAG_EDITOR_ACTION)); 576 sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, 577 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0, 578 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 579 KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE 580 | KeyEvent.FLAG_EDITOR_ACTION)); 581 return true; 582 } 583 584 /** 585 * The default implementation does nothing. 586 */ 587 public boolean performContextMenuAction(int id) { 588 return false; 589 } 590 591 /** 592 * The default implementation does nothing. 593 */ 594 public boolean performPrivateCommand(String action, Bundle data) { 595 return false; 596 } 597 598 /** 599 * The default implementation does nothing. 600 */ 601 public boolean requestCursorUpdates(int cursorUpdateMode) { 602 return false; 603 } 604 605 /** 606 * The default implementation places the given text into the editable, 607 * replacing any existing composing text. The new text is marked as 608 * in a composing state with the composing style. 609 */ 610 public boolean setComposingText(CharSequence text, int newCursorPosition) { 611 if (DEBUG) Log.v(TAG, "setComposingText " + text); 612 replaceText(text, newCursorPosition, true); 613 return true; 614 } 615 616 public boolean setComposingRegion(int start, int end) { 617 final Editable content = getEditable(); 618 if (content != null) { 619 beginBatchEdit(); 620 removeComposingSpans(content); 621 int a = start; 622 int b = end; 623 if (a > b) { 624 int tmp = a; 625 a = b; 626 b = tmp; 627 } 628 // Clip the end points to be within the content bounds. 629 final int length = content.length(); 630 if (a < 0) a = 0; 631 if (b < 0) b = 0; 632 if (a > length) a = length; 633 if (b > length) b = length; 634 635 ensureDefaultComposingSpans(); 636 if (mDefaultComposingSpans != null) { 637 for (int i = 0; i < mDefaultComposingSpans.length; ++i) { 638 content.setSpan(mDefaultComposingSpans[i], a, b, 639 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 640 } 641 } 642 643 content.setSpan(COMPOSING, a, b, 644 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 645 646 // Note: sendCurrentText does nothing unless mDummyMode is set 647 sendCurrentText(); 648 endBatchEdit(); 649 } 650 return true; 651 } 652 653 /** 654 * The default implementation changes the selection position in the 655 * current editable text. 656 */ 657 public boolean setSelection(int start, int end) { 658 if (DEBUG) Log.v(TAG, "setSelection " + start + ", " + end); 659 final Editable content = getEditable(); 660 if (content == null) return false; 661 int len = content.length(); 662 if (start > len || end > len || start < 0 || end < 0) { 663 // If the given selection is out of bounds, just ignore it. 664 // Most likely the text was changed out from under the IME, 665 // and the IME is going to have to update all of its state 666 // anyway. 667 return true; 668 } 669 if (start == end && MetaKeyKeyListener.getMetaState(content, 670 MetaKeyKeyListener.META_SELECTING) != 0) { 671 // If we are in selection mode, then we want to extend the 672 // selection instead of replacing it. 673 Selection.extendSelection(content, start); 674 } else { 675 Selection.setSelection(content, start, end); 676 } 677 return true; 678 } 679 680 /** 681 * Provides standard implementation for sending a key event to the window 682 * attached to the input connection's view. 683 */ 684 public boolean sendKeyEvent(KeyEvent event) { 685 mIMM.dispatchKeyEventFromInputMethod(mTargetView, event); 686 return false; 687 } 688 689 /** 690 * Updates InputMethodManager with the current fullscreen mode. 691 */ 692 public boolean reportFullscreenMode(boolean enabled) { 693 return true; 694 } 695 696 private void sendCurrentText() { 697 if (!mDummyMode) { 698 return; 699 } 700 701 Editable content = getEditable(); 702 if (content != null) { 703 final int N = content.length(); 704 if (N == 0) { 705 return; 706 } 707 if (N == 1) { 708 // If it's 1 character, we have a chance of being 709 // able to generate normal key events... 710 if (mKeyCharacterMap == null) { 711 mKeyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); 712 } 713 char[] chars = new char[1]; 714 content.getChars(0, 1, chars, 0); 715 KeyEvent[] events = mKeyCharacterMap.getEvents(chars); 716 if (events != null) { 717 for (int i=0; i<events.length; i++) { 718 if (DEBUG) Log.v(TAG, "Sending: " + events[i]); 719 sendKeyEvent(events[i]); 720 } 721 content.clear(); 722 return; 723 } 724 } 725 726 // Otherwise, revert to the special key event containing 727 // the actual characters. 728 KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), 729 content.toString(), KeyCharacterMap.VIRTUAL_KEYBOARD, 0); 730 sendKeyEvent(event); 731 content.clear(); 732 } 733 } 734 735 private void ensureDefaultComposingSpans() { 736 if (mDefaultComposingSpans == null) { 737 Context context; 738 if (mTargetView != null) { 739 context = mTargetView.getContext(); 740 } else if (mIMM.mServedView != null) { 741 context = mIMM.mServedView.getContext(); 742 } else { 743 context = null; 744 } 745 if (context != null) { 746 TypedArray ta = context.getTheme() 747 .obtainStyledAttributes(new int[] { 748 com.android.internal.R.attr.candidatesTextStyleSpans 749 }); 750 CharSequence style = ta.getText(0); 751 ta.recycle(); 752 if (style != null && style instanceof Spanned) { 753 mDefaultComposingSpans = ((Spanned)style).getSpans( 754 0, style.length(), Object.class); 755 } 756 } 757 } 758 } 759 760 private void replaceText(CharSequence text, int newCursorPosition, 761 boolean composing) { 762 final Editable content = getEditable(); 763 if (content == null) { 764 return; 765 } 766 767 beginBatchEdit(); 768 769 // delete composing text set previously. 770 int a = getComposingSpanStart(content); 771 int b = getComposingSpanEnd(content); 772 773 if (DEBUG) Log.v(TAG, "Composing span: " + a + " to " + b); 774 775 if (b < a) { 776 int tmp = a; 777 a = b; 778 b = tmp; 779 } 780 781 if (a != -1 && b != -1) { 782 removeComposingSpans(content); 783 } else { 784 a = Selection.getSelectionStart(content); 785 b = Selection.getSelectionEnd(content); 786 if (a < 0) a = 0; 787 if (b < 0) b = 0; 788 if (b < a) { 789 int tmp = a; 790 a = b; 791 b = tmp; 792 } 793 } 794 795 if (composing) { 796 Spannable sp = null; 797 if (!(text instanceof Spannable)) { 798 sp = new SpannableStringBuilder(text); 799 text = sp; 800 ensureDefaultComposingSpans(); 801 if (mDefaultComposingSpans != null) { 802 for (int i = 0; i < mDefaultComposingSpans.length; ++i) { 803 sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(), 804 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); 805 } 806 } 807 } else { 808 sp = (Spannable)text; 809 } 810 setComposingSpans(sp); 811 } 812 813 if (DEBUG) Log.v(TAG, "Replacing from " + a + " to " + b + " with \"" 814 + text + "\", composing=" + composing 815 + ", type=" + text.getClass().getCanonicalName()); 816 817 if (DEBUG) { 818 LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG); 819 lp.println("Current text:"); 820 TextUtils.dumpSpans(content, lp, " "); 821 lp.println("Composing text:"); 822 TextUtils.dumpSpans(text, lp, " "); 823 } 824 825 // Position the cursor appropriately, so that after replacing the 826 // desired range of text it will be located in the correct spot. 827 // This allows us to deal with filters performing edits on the text 828 // we are providing here. 829 if (newCursorPosition > 0) { 830 newCursorPosition += b - 1; 831 } else { 832 newCursorPosition += a; 833 } 834 if (newCursorPosition < 0) newCursorPosition = 0; 835 if (newCursorPosition > content.length()) 836 newCursorPosition = content.length(); 837 Selection.setSelection(content, newCursorPosition); 838 839 content.replace(a, b, text); 840 841 if (DEBUG) { 842 LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG); 843 lp.println("Final text:"); 844 TextUtils.dumpSpans(content, lp, " "); 845 } 846 847 endBatchEdit(); 848 } 849} 850