| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.inputmethod; |
| 18 |
|
| 19 |
import static android.view.ContentInfo.SOURCE_INPUT_METHOD; |
| 20 |
|
| 21 |
import android.annotation.CallSuper; |
| 22 |
import android.annotation.IntRange; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.content.ClipData; |
| 25 |
import android.content.ClipDescription; |
| 26 |
import android.content.Context; |
| 27 |
import android.content.res.TypedArray; |
| 28 |
import android.os.Bundle; |
| 29 |
import android.os.Handler; |
| 30 |
import android.os.SystemClock; |
| 31 |
import android.text.Editable; |
| 32 |
import android.text.NoCopySpan; |
| 33 |
import android.text.Selection; |
| 34 |
import android.text.Spannable; |
| 35 |
import android.text.SpannableStringBuilder; |
| 36 |
import android.text.Spanned; |
| 37 |
import android.text.TextUtils; |
| 38 |
import android.text.method.MetaKeyKeyListener; |
| 39 |
import android.util.Log; |
| 40 |
import android.util.LogPrinter; |
| 41 |
import android.view.ContentInfo; |
| 42 |
import android.view.KeyCharacterMap; |
| 43 |
import android.view.KeyEvent; |
| 44 |
import android.view.View; |
| 45 |
|
| 46 |
import com.android.internal.util.Preconditions; |
| 47 |
|
| 48 |
class ComposingText implements NoCopySpan { |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public class BaseInputConnection implements InputConnection { |
| 59 |
private static final boolean DEBUG = false; |
| 60 |
private static final String TAG = "BaseInputConnection"; |
| 61 |
static final Object COMPOSING = new ComposingText(); |
| 62 |
|
| 63 |
|
| 64 |
protected final InputMethodManager mIMM; |
| 65 |
final View mTargetView; |
| 66 |
final boolean mFallbackMode; |
| 67 |
|
| 68 |
private Object[] mDefaultComposingSpans; |
| 69 |
|
| 70 |
Editable mEditable; |
| 71 |
KeyCharacterMap mKeyCharacterMap; |
| 72 |
|
| 73 |
BaseInputConnection(InputMethodManager mgr, boolean fullEditor) { |
| 74 |
mIMM = mgr; |
| 75 |
mTargetView = null; |
| 76 |
mFallbackMode = !fullEditor; |
| 77 |
} |
| 78 |
|
| 79 |
public BaseInputConnection(View targetView, boolean fullEditor) { |
| 80 |
mIMM = (InputMethodManager)targetView.getContext().getSystemService( |
| 81 |
Context.INPUT_METHOD_SERVICE); |
| 82 |
mTargetView = targetView; |
| 83 |
mFallbackMode = !fullEditor; |
| 84 |
} |
| 85 |
|
| 86 |
public static final void removeComposingSpans(Spannable text) { |
| 87 |
text.removeSpan(COMPOSING); |
| 88 |
Object[] sps = text.getSpans(0, text.length(), Object.class); |
| 89 |
if (sps != null) { |
| 90 |
for (int i=sps.length-1; i>=0; i--) { |
| 91 |
Object o = sps[i]; |
| 92 |
if ((text.getSpanFlags(o)&Spanned.SPAN_COMPOSING) != 0) { |
| 93 |
text.removeSpan(o); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public static void setComposingSpans(Spannable text) { |
| 100 |
setComposingSpans(text, 0, text.length()); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
public static void setComposingSpans(Spannable text, int start, int end) { |
| 105 |
final Object[] sps = text.getSpans(start, end, Object.class); |
| 106 |
if (sps != null) { |
| 107 |
for (int i=sps.length-1; i>=0; i--) { |
| 108 |
final Object o = sps[i]; |
| 109 |
if (o == COMPOSING) { |
| 110 |
text.removeSpan(o); |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
final int fl = text.getSpanFlags(o); |
| 115 |
if ((fl & (Spanned.SPAN_COMPOSING | Spanned.SPAN_POINT_MARK_MASK)) |
| 116 |
!= (Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)) { |
| 117 |
text.setSpan(o, text.getSpanStart(o), text.getSpanEnd(o), |
| 118 |
(fl & ~Spanned.SPAN_POINT_MARK_MASK) |
| 119 |
| Spanned.SPAN_COMPOSING |
| 120 |
| Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
text.setSpan(COMPOSING, start, end, |
| 126 |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); |
| 127 |
} |
| 128 |
|
| 129 |
public static int getComposingSpanStart(Spannable text) { |
| 130 |
return text.getSpanStart(COMPOSING); |
| 131 |
} |
| 132 |
|
| 133 |
public static int getComposingSpanEnd(Spannable text) { |
| 134 |
return text.getSpanEnd(COMPOSING); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public Editable getEditable() { |
| 144 |
if (mEditable == null) { |
| 145 |
mEditable = Editable.Factory.getInstance().newEditable(""); |
| 146 |
Selection.setSelection(mEditable, 0); |
| 147 |
} |
| 148 |
return mEditable; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public boolean beginBatchEdit() { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public boolean endBatchEdit() { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public void endComposingRegionEditInternal() { |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@CallSuper |
| 181 |
public void closeConnection() { |
| 182 |
finishComposingText(); |
| 183 |
setImeConsumesInput(false); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public boolean clearMetaKeyStates(int states) { |
| 192 |
final Editable content = getEditable(); |
| 193 |
if (content == null) return false; |
| 194 |
MetaKeyKeyListener.clearMetaKeyState(content, states); |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public boolean commitCompletion(CompletionInfo text) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public boolean commitCorrection(CorrectionInfo correctionInfo) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public boolean commitText(CharSequence text, int newCursorPosition) { |
| 218 |
if (DEBUG) Log.v(TAG, "commitText " + text); |
| 219 |
replaceText(text, newCursorPosition, false); |
| 220 |
sendCurrentText(); |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public boolean deleteSurroundingText(int beforeLength, int afterLength) { |
| 242 |
if (DEBUG) Log.v(TAG, "deleteSurroundingText " + beforeLength |
| 243 |
+ " / " + afterLength); |
| 244 |
final Editable content = getEditable(); |
| 245 |
if (content == null) return false; |
| 246 |
|
| 247 |
beginBatchEdit(); |
| 248 |
|
| 249 |
int a = Selection.getSelectionStart(content); |
| 250 |
int b = Selection.getSelectionEnd(content); |
| 251 |
|
| 252 |
if (a > b) { |
| 253 |
int tmp = a; |
| 254 |
a = b; |
| 255 |
b = tmp; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
if (a == -1 || b == -1) { |
| 260 |
endBatchEdit(); |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
int ca = getComposingSpanStart(content); |
| 266 |
int cb = getComposingSpanEnd(content); |
| 267 |
if (cb < ca) { |
| 268 |
int tmp = ca; |
| 269 |
ca = cb; |
| 270 |
cb = tmp; |
| 271 |
} |
| 272 |
if (ca != -1 && cb != -1) { |
| 273 |
if (ca < a) a = ca; |
| 274 |
if (cb > b) b = cb; |
| 275 |
} |
| 276 |
|
| 277 |
int deleted = 0; |
| 278 |
|
| 279 |
if (beforeLength > 0) { |
| 280 |
int start = a - beforeLength; |
| 281 |
if (start < 0) start = 0; |
| 282 |
|
| 283 |
final int numDeleteBefore = a - start; |
| 284 |
if (a >= 0 && numDeleteBefore > 0) { |
| 285 |
content.delete(start, a); |
| 286 |
deleted = numDeleteBefore; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (afterLength > 0) { |
| 291 |
b = b - deleted; |
| 292 |
|
| 293 |
int end = b + afterLength; |
| 294 |
if (end > content.length()) end = content.length(); |
| 295 |
|
| 296 |
final int numDeleteAfter = end - b; |
| 297 |
if (b >= 0 && numDeleteAfter > 0) { |
| 298 |
content.delete(b, end); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
endBatchEdit(); |
| 303 |
|
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
private static int INVALID_INDEX = -1; |
| 308 |
private static int findIndexBackward(final CharSequence cs, final int from, |
| 309 |
final int numCodePoints) { |
| 310 |
int currentIndex = from; |
| 311 |
boolean waitingHighSurrogate = false; |
| 312 |
final int N = cs.length(); |
| 313 |
if (currentIndex < 0 || N < currentIndex) { |
| 314 |
return INVALID_INDEX; |
| 315 |
} |
| 316 |
if (numCodePoints < 0) { |
| 317 |
return INVALID_INDEX; |
| 318 |
} |
| 319 |
int remainingCodePoints = numCodePoints; |
| 320 |
while (true) { |
| 321 |
if (remainingCodePoints == 0) { |
| 322 |
return currentIndex; |
| 323 |
} |
| 324 |
|
| 325 |
--currentIndex; |
| 326 |
if (currentIndex < 0) { |
| 327 |
if (waitingHighSurrogate) { |
| 328 |
return INVALID_INDEX; |
| 329 |
} |
| 330 |
return 0; |
| 331 |
} |
| 332 |
final char c = cs.charAt(currentIndex); |
| 333 |
if (waitingHighSurrogate) { |
| 334 |
if (!java.lang.Character.isHighSurrogate(c)) { |
| 335 |
return INVALID_INDEX; |
| 336 |
} |
| 337 |
waitingHighSurrogate = false; |
| 338 |
--remainingCodePoints; |
| 339 |
continue; |
| 340 |
} |
| 341 |
if (!java.lang.Character.isSurrogate(c)) { |
| 342 |
--remainingCodePoints; |
| 343 |
continue; |
| 344 |
} |
| 345 |
if (java.lang.Character.isHighSurrogate(c)) { |
| 346 |
return INVALID_INDEX; |
| 347 |
} |
| 348 |
waitingHighSurrogate = true; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
private static int findIndexForward(final CharSequence cs, final int from, |
| 353 |
final int numCodePoints) { |
| 354 |
int currentIndex = from; |
| 355 |
boolean waitingLowSurrogate = false; |
| 356 |
final int N = cs.length(); |
| 357 |
if (currentIndex < 0 || N < currentIndex) { |
| 358 |
return INVALID_INDEX; |
| 359 |
} |
| 360 |
if (numCodePoints < 0) { |
| 361 |
return INVALID_INDEX; |
| 362 |
} |
| 363 |
int remainingCodePoints = numCodePoints; |
| 364 |
|
| 365 |
while (true) { |
| 366 |
if (remainingCodePoints == 0) { |
| 367 |
return currentIndex; |
| 368 |
} |
| 369 |
|
| 370 |
if (currentIndex >= N) { |
| 371 |
if (waitingLowSurrogate) { |
| 372 |
return INVALID_INDEX; |
| 373 |
} |
| 374 |
return N; |
| 375 |
} |
| 376 |
final char c = cs.charAt(currentIndex); |
| 377 |
if (waitingLowSurrogate) { |
| 378 |
if (!java.lang.Character.isLowSurrogate(c)) { |
| 379 |
return INVALID_INDEX; |
| 380 |
} |
| 381 |
--remainingCodePoints; |
| 382 |
waitingLowSurrogate = false; |
| 383 |
++currentIndex; |
| 384 |
continue; |
| 385 |
} |
| 386 |
if (!java.lang.Character.isSurrogate(c)) { |
| 387 |
--remainingCodePoints; |
| 388 |
++currentIndex; |
| 389 |
continue; |
| 390 |
} |
| 391 |
if (java.lang.Character.isLowSurrogate(c)) { |
| 392 |
return INVALID_INDEX; |
| 393 |
} |
| 394 |
waitingLowSurrogate = true; |
| 395 |
++currentIndex; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { |
| 412 |
if (DEBUG) Log.v(TAG, "deleteSurroundingText " + beforeLength |
| 413 |
+ " / " + afterLength); |
| 414 |
final Editable content = getEditable(); |
| 415 |
if (content == null) return false; |
| 416 |
|
| 417 |
beginBatchEdit(); |
| 418 |
|
| 419 |
int a = Selection.getSelectionStart(content); |
| 420 |
int b = Selection.getSelectionEnd(content); |
| 421 |
|
| 422 |
if (a > b) { |
| 423 |
int tmp = a; |
| 424 |
a = b; |
| 425 |
b = tmp; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
int ca = getComposingSpanStart(content); |
| 430 |
int cb = getComposingSpanEnd(content); |
| 431 |
if (cb < ca) { |
| 432 |
int tmp = ca; |
| 433 |
ca = cb; |
| 434 |
cb = tmp; |
| 435 |
} |
| 436 |
if (ca != -1 && cb != -1) { |
| 437 |
if (ca < a) a = ca; |
| 438 |
if (cb > b) b = cb; |
| 439 |
} |
| 440 |
|
| 441 |
if (a >= 0 && b >= 0) { |
| 442 |
final int start = findIndexBackward(content, a, Math.max(beforeLength, 0)); |
| 443 |
if (start != INVALID_INDEX) { |
| 444 |
final int end = findIndexForward(content, b, Math.max(afterLength, 0)); |
| 445 |
if (end != INVALID_INDEX) { |
| 446 |
final int numDeleteBefore = a - start; |
| 447 |
if (numDeleteBefore > 0) { |
| 448 |
content.delete(start, a); |
| 449 |
} |
| 450 |
final int numDeleteAfter = end - b; |
| 451 |
if (numDeleteAfter > 0) { |
| 452 |
content.delete(b - numDeleteBefore, end - numDeleteBefore); |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
endBatchEdit(); |
| 464 |
|
| 465 |
return true; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public boolean finishComposingText() { |
| 474 |
if (DEBUG) Log.v(TAG, "finishComposingText"); |
| 475 |
final Editable content = getEditable(); |
| 476 |
if (content != null) { |
| 477 |
beginBatchEdit(); |
| 478 |
removeComposingSpans(content); |
| 479 |
|
| 480 |
sendCurrentText(); |
| 481 |
endBatchEdit(); |
| 482 |
endComposingRegionEditInternal(); |
| 483 |
} |
| 484 |
return true; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public int getCursorCapsMode(int reqModes) { |
| 493 |
if (mFallbackMode) return 0; |
| 494 |
|
| 495 |
final Editable content = getEditable(); |
| 496 |
if (content == null) return 0; |
| 497 |
|
| 498 |
int a = Selection.getSelectionStart(content); |
| 499 |
int b = Selection.getSelectionEnd(content); |
| 500 |
|
| 501 |
if (a > b) { |
| 502 |
int tmp = a; |
| 503 |
a = b; |
| 504 |
b = tmp; |
| 505 |
} |
| 506 |
|
| 507 |
return TextUtils.getCapsMode(content, a, reqModes); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { |
| 514 |
return null; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
@Nullable |
| 522 |
public CharSequence getTextBeforeCursor(@IntRange(from = 0) int length, int flags) { |
| 523 |
Preconditions.checkArgumentNonnegative(length); |
| 524 |
|
| 525 |
final Editable content = getEditable(); |
| 526 |
if (content == null) return null; |
| 527 |
|
| 528 |
int a = Selection.getSelectionStart(content); |
| 529 |
int b = Selection.getSelectionEnd(content); |
| 530 |
|
| 531 |
if (a > b) { |
| 532 |
int tmp = a; |
| 533 |
a = b; |
| 534 |
b = tmp; |
| 535 |
} |
| 536 |
|
| 537 |
if (a <= 0) { |
| 538 |
return ""; |
| 539 |
} |
| 540 |
|
| 541 |
if (length > a) { |
| 542 |
length = a; |
| 543 |
} |
| 544 |
|
| 545 |
if ((flags&GET_TEXT_WITH_STYLES) != 0) { |
| 546 |
return content.subSequence(a - length, a); |
| 547 |
} |
| 548 |
return TextUtils.substring(content, a - length, a); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public CharSequence getSelectedText(int flags) { |
| 556 |
final Editable content = getEditable(); |
| 557 |
if (content == null) return null; |
| 558 |
|
| 559 |
int a = Selection.getSelectionStart(content); |
| 560 |
int b = Selection.getSelectionEnd(content); |
| 561 |
|
| 562 |
if (a > b) { |
| 563 |
int tmp = a; |
| 564 |
a = b; |
| 565 |
b = tmp; |
| 566 |
} |
| 567 |
|
| 568 |
if (a == b || a < 0) return null; |
| 569 |
|
| 570 |
if ((flags&GET_TEXT_WITH_STYLES) != 0) { |
| 571 |
return content.subSequence(a, b); |
| 572 |
} |
| 573 |
return TextUtils.substring(content, a, b); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
@Nullable |
| 581 |
public CharSequence getTextAfterCursor(@IntRange(from = 0) int length, int flags) { |
| 582 |
Preconditions.checkArgumentNonnegative(length); |
| 583 |
|
| 584 |
final Editable content = getEditable(); |
| 585 |
if (content == null) return null; |
| 586 |
|
| 587 |
int a = Selection.getSelectionStart(content); |
| 588 |
int b = Selection.getSelectionEnd(content); |
| 589 |
|
| 590 |
if (a > b) { |
| 591 |
int tmp = a; |
| 592 |
a = b; |
| 593 |
b = tmp; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
if (b < 0) { |
| 598 |
b = 0; |
| 599 |
} |
| 600 |
|
| 601 |
if (b + length > content.length()) { |
| 602 |
length = content.length() - b; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
if ((flags&GET_TEXT_WITH_STYLES) != 0) { |
| 607 |
return content.subSequence(b, b + length); |
| 608 |
} |
| 609 |
return TextUtils.substring(content, b, b + length); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
@Nullable |
| 617 |
public SurroundingText getSurroundingText( |
| 618 |
@IntRange(from = 0) int beforeLength, @IntRange(from = 0) int afterLength, int flags) { |
| 619 |
Preconditions.checkArgumentNonnegative(beforeLength); |
| 620 |
Preconditions.checkArgumentNonnegative(afterLength); |
| 621 |
|
| 622 |
final Editable content = getEditable(); |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
if (content == null || mEditable == content) { |
| 627 |
return InputConnection.super.getSurroundingText(beforeLength, afterLength, flags); |
| 628 |
} |
| 629 |
|
| 630 |
int selStart = Selection.getSelectionStart(content); |
| 631 |
int selEnd = Selection.getSelectionEnd(content); |
| 632 |
|
| 633 |
|
| 634 |
if (selStart < 0 || selEnd < 0) { |
| 635 |
return null; |
| 636 |
} |
| 637 |
|
| 638 |
if (selStart > selEnd) { |
| 639 |
int tmp = selStart; |
| 640 |
selStart = selEnd; |
| 641 |
selEnd = tmp; |
| 642 |
} |
| 643 |
|
| 644 |
int contentLength = content.length(); |
| 645 |
int startPos = selStart - beforeLength; |
| 646 |
int endPos = selEnd + afterLength; |
| 647 |
|
| 648 |
|
| 649 |
startPos = Math.max(0, startPos); |
| 650 |
endPos = Math.min(contentLength, endPos); |
| 651 |
|
| 652 |
CharSequence surroundingText; |
| 653 |
if ((flags & GET_TEXT_WITH_STYLES) != 0) { |
| 654 |
surroundingText = content.subSequence(startPos, endPos); |
| 655 |
} else { |
| 656 |
surroundingText = TextUtils.substring(content, startPos, endPos); |
| 657 |
} |
| 658 |
return new SurroundingText( |
| 659 |
surroundingText, selStart - startPos, selEnd - startPos, startPos); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public boolean performEditorAction(int actionCode) { |
| 666 |
long eventTime = SystemClock.uptimeMillis(); |
| 667 |
sendKeyEvent(new KeyEvent(eventTime, eventTime, |
| 668 |
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0, |
| 669 |
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
| 670 |
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE |
| 671 |
| KeyEvent.FLAG_EDITOR_ACTION)); |
| 672 |
sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, |
| 673 |
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, 0, |
| 674 |
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, |
| 675 |
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE |
| 676 |
| KeyEvent.FLAG_EDITOR_ACTION)); |
| 677 |
return true; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
public boolean performContextMenuAction(int id) { |
| 684 |
return false; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public boolean performPrivateCommand(String action, Bundle data) { |
| 691 |
return false; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
public boolean requestCursorUpdates(int cursorUpdateMode) { |
| 698 |
return false; |
| 699 |
} |
| 700 |
|
| 701 |
public Handler getHandler() { |
| 702 |
return null; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
public boolean setComposingText(CharSequence text, int newCursorPosition) { |
| 711 |
if (DEBUG) Log.v(TAG, "setComposingText " + text); |
| 712 |
replaceText(text, newCursorPosition, true); |
| 713 |
return true; |
| 714 |
} |
| 715 |
|
| 716 |
public boolean setComposingRegion(int start, int end) { |
| 717 |
final Editable content = getEditable(); |
| 718 |
if (content != null) { |
| 719 |
beginBatchEdit(); |
| 720 |
removeComposingSpans(content); |
| 721 |
int a = start; |
| 722 |
int b = end; |
| 723 |
if (a > b) { |
| 724 |
int tmp = a; |
| 725 |
a = b; |
| 726 |
b = tmp; |
| 727 |
} |
| 728 |
|
| 729 |
final int length = content.length(); |
| 730 |
if (a < 0) a = 0; |
| 731 |
if (b < 0) b = 0; |
| 732 |
if (a > length) a = length; |
| 733 |
if (b > length) b = length; |
| 734 |
|
| 735 |
ensureDefaultComposingSpans(); |
| 736 |
if (mDefaultComposingSpans != null) { |
| 737 |
for (int i = 0; i < mDefaultComposingSpans.length; ++i) { |
| 738 |
content.setSpan(mDefaultComposingSpans[i], a, b, |
| 739 |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
content.setSpan(COMPOSING, a, b, |
| 744 |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); |
| 745 |
|
| 746 |
|
| 747 |
sendCurrentText(); |
| 748 |
endBatchEdit(); |
| 749 |
endComposingRegionEditInternal(); |
| 750 |
} |
| 751 |
return true; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
public boolean setSelection(int start, int end) { |
| 759 |
if (DEBUG) Log.v(TAG, "setSelection " + start + ", " + end); |
| 760 |
final Editable content = getEditable(); |
| 761 |
if (content == null) return false; |
| 762 |
int len = content.length(); |
| 763 |
if (start > len || end > len || start < 0 || end < 0) { |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
return true; |
| 769 |
} |
| 770 |
if (start == end && MetaKeyKeyListener.getMetaState(content, |
| 771 |
MetaKeyKeyListener.META_SELECTING) != 0) { |
| 772 |
|
| 773 |
|
| 774 |
Selection.extendSelection(content, start); |
| 775 |
} else { |
| 776 |
Selection.setSelection(content, start, end); |
| 777 |
} |
| 778 |
return true; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
public boolean sendKeyEvent(KeyEvent event) { |
| 786 |
mIMM.dispatchKeyEventFromInputMethod(mTargetView, event); |
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
public boolean reportFullscreenMode(boolean enabled) { |
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
private void sendCurrentText() { |
| 798 |
if (!mFallbackMode) { |
| 799 |
return; |
| 800 |
} |
| 801 |
|
| 802 |
Editable content = getEditable(); |
| 803 |
if (content != null) { |
| 804 |
final int N = content.length(); |
| 805 |
if (N == 0) { |
| 806 |
return; |
| 807 |
} |
| 808 |
if (N == 1) { |
| 809 |
|
| 810 |
|
| 811 |
if (mKeyCharacterMap == null) { |
| 812 |
mKeyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); |
| 813 |
} |
| 814 |
char[] chars = new char[1]; |
| 815 |
content.getChars(0, 1, chars, 0); |
| 816 |
KeyEvent[] events = mKeyCharacterMap.getEvents(chars); |
| 817 |
if (events != null) { |
| 818 |
for (int i=0; i<events.length; i++) { |
| 819 |
if (DEBUG) Log.v(TAG, "Sending: " + events[i]); |
| 820 |
sendKeyEvent(events[i]); |
| 821 |
} |
| 822 |
content.clear(); |
| 823 |
return; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), |
| 830 |
content.toString(), KeyCharacterMap.VIRTUAL_KEYBOARD, 0); |
| 831 |
sendKeyEvent(event); |
| 832 |
content.clear(); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
private void ensureDefaultComposingSpans() { |
| 837 |
if (mDefaultComposingSpans == null) { |
| 838 |
Context context; |
| 839 |
if (mTargetView != null) { |
| 840 |
context = mTargetView.getContext(); |
| 841 |
} else if (mIMM.mCurRootView != null) { |
| 842 |
final View servedView = mIMM.mCurRootView.getImeFocusController().getServedView(); |
| 843 |
context = servedView != null ? servedView.getContext() : null; |
| 844 |
} else { |
| 845 |
context = null; |
| 846 |
} |
| 847 |
if (context != null) { |
| 848 |
TypedArray ta = context.getTheme() |
| 849 |
.obtainStyledAttributes(new int[] { |
| 850 |
com.android.internal.R.attr.candidatesTextStyleSpans |
| 851 |
}); |
| 852 |
CharSequence style = ta.getText(0); |
| 853 |
ta.recycle(); |
| 854 |
if (style != null && style instanceof Spanned) { |
| 855 |
mDefaultComposingSpans = ((Spanned)style).getSpans( |
| 856 |
0, style.length(), Object.class); |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
private void replaceText(CharSequence text, int newCursorPosition, |
| 863 |
boolean composing) { |
| 864 |
final Editable content = getEditable(); |
| 865 |
if (content == null) { |
| 866 |
return; |
| 867 |
} |
| 868 |
|
| 869 |
beginBatchEdit(); |
| 870 |
|
| 871 |
|
| 872 |
int a = getComposingSpanStart(content); |
| 873 |
int b = getComposingSpanEnd(content); |
| 874 |
|
| 875 |
if (DEBUG) Log.v(TAG, "Composing span: " + a + " to " + b); |
| 876 |
|
| 877 |
if (b < a) { |
| 878 |
int tmp = a; |
| 879 |
a = b; |
| 880 |
b = tmp; |
| 881 |
} |
| 882 |
|
| 883 |
if (a != -1 && b != -1) { |
| 884 |
removeComposingSpans(content); |
| 885 |
} else { |
| 886 |
a = Selection.getSelectionStart(content); |
| 887 |
b = Selection.getSelectionEnd(content); |
| 888 |
if (a < 0) a = 0; |
| 889 |
if (b < 0) b = 0; |
| 890 |
if (b < a) { |
| 891 |
int tmp = a; |
| 892 |
a = b; |
| 893 |
b = tmp; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
if (composing) { |
| 898 |
Spannable sp = null; |
| 899 |
if (!(text instanceof Spannable)) { |
| 900 |
sp = new SpannableStringBuilder(text); |
| 901 |
text = sp; |
| 902 |
ensureDefaultComposingSpans(); |
| 903 |
if (mDefaultComposingSpans != null) { |
| 904 |
for (int i = 0; i < mDefaultComposingSpans.length; ++i) { |
| 905 |
sp.setSpan(mDefaultComposingSpans[i], 0, sp.length(), |
| 906 |
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING); |
| 907 |
} |
| 908 |
} |
| 909 |
} else { |
| 910 |
sp = (Spannable)text; |
| 911 |
} |
| 912 |
setComposingSpans(sp); |
| 913 |
} |
| 914 |
|
| 915 |
if (DEBUG) Log.v(TAG, "Replacing from " + a + " to " + b + " with \"" |
| 916 |
+ text + "\", composing=" + composing |
| 917 |
+ ", type=" + text.getClass().getCanonicalName()); |
| 918 |
|
| 919 |
if (DEBUG) { |
| 920 |
LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG); |
| 921 |
lp.println("Current text:"); |
| 922 |
TextUtils.dumpSpans(content, lp, " "); |
| 923 |
lp.println("Composing text:"); |
| 924 |
TextUtils.dumpSpans(text, lp, " "); |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
if (newCursorPosition > 0) { |
| 932 |
newCursorPosition += b - 1; |
| 933 |
} else { |
| 934 |
newCursorPosition += a; |
| 935 |
} |
| 936 |
if (newCursorPosition < 0) newCursorPosition = 0; |
| 937 |
if (newCursorPosition > content.length()) |
| 938 |
newCursorPosition = content.length(); |
| 939 |
Selection.setSelection(content, newCursorPosition); |
| 940 |
|
| 941 |
content.replace(a, b, text); |
| 942 |
|
| 943 |
if (DEBUG) { |
| 944 |
LogPrinter lp = new LogPrinter(Log.VERBOSE, TAG); |
| 945 |
lp.println("Final text:"); |
| 946 |
TextUtils.dumpSpans(content, lp, " "); |
| 947 |
} |
| 948 |
|
| 949 |
endBatchEdit(); |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) { |
| 958 |
ClipDescription description = inputContentInfo.getDescription(); |
| 959 |
if (mTargetView.getReceiveContentMimeTypes() == null) { |
| 960 |
if (DEBUG) { |
| 961 |
Log.d(TAG, "Can't insert content from IME: content=" + description); |
| 962 |
} |
| 963 |
return false; |
| 964 |
} |
| 965 |
if ((flags & InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) { |
| 966 |
try { |
| 967 |
inputContentInfo.requestPermission(); |
| 968 |
} catch (Exception e) { |
| 969 |
Log.w(TAG, "Can't insert content from IME; requestPermission() failed", e); |
| 970 |
return false; |
| 971 |
} |
| 972 |
} |
| 973 |
final ClipData clip = new ClipData(inputContentInfo.getDescription(), |
| 974 |
new ClipData.Item(inputContentInfo.getContentUri())); |
| 975 |
final ContentInfo payload = new ContentInfo.Builder(clip, SOURCE_INPUT_METHOD) |
| 976 |
.setLinkUri(inputContentInfo.getLinkUri()) |
| 977 |
.setExtras(opts) |
| 978 |
.setInputContentInfo(inputContentInfo) |
| 979 |
.build(); |
| 980 |
return mTargetView.performReceiveContent(payload) == null; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
@Nullable |
| 990 |
@Override |
| 991 |
public TextSnapshot takeSnapshot() { |
| 992 |
final Editable content = getEditable(); |
| 993 |
if (content == null) { |
| 994 |
return null; |
| 995 |
} |
| 996 |
int composingStart = getComposingSpanStart(content); |
| 997 |
int composingEnd = getComposingSpanEnd(content); |
| 998 |
if (composingEnd < composingStart) { |
| 999 |
final int tmp = composingStart; |
| 1000 |
composingStart = composingEnd; |
| 1001 |
composingEnd = tmp; |
| 1002 |
} |
| 1003 |
|
| 1004 |
final SurroundingText surroundingText = getSurroundingText( |
| 1005 |
EditorInfo.MEMORY_EFFICIENT_TEXT_LENGTH / 2, |
| 1006 |
EditorInfo.MEMORY_EFFICIENT_TEXT_LENGTH / 2, GET_TEXT_WITH_STYLES); |
| 1007 |
if (surroundingText == null) { |
| 1008 |
return null; |
| 1009 |
} |
| 1010 |
|
| 1011 |
final int cursorCapsMode = getCursorCapsMode(TextUtils.CAP_MODE_CHARACTERS |
| 1012 |
| TextUtils.CAP_MODE_WORDS | TextUtils.CAP_MODE_SENTENCES); |
| 1013 |
|
| 1014 |
return new TextSnapshot(surroundingText, composingStart, composingEnd, cursorCapsMode); |
| 1015 |
} |
| 1016 |
} |