| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.inputmethodservice; |
| 18 |
|
| 19 |
import android.annotation.AnyThread; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.os.Bundle; |
| 24 |
import android.os.Handler; |
| 25 |
import android.util.Log; |
| 26 |
import android.view.KeyEvent; |
| 27 |
import android.view.inputmethod.CompletionInfo; |
| 28 |
import android.view.inputmethod.CorrectionInfo; |
| 29 |
import android.view.inputmethod.ExtractedText; |
| 30 |
import android.view.inputmethod.ExtractedTextRequest; |
| 31 |
import android.view.inputmethod.InputConnection; |
| 32 |
import android.view.inputmethod.InputContentInfo; |
| 33 |
import android.view.inputmethod.SurroundingText; |
| 34 |
import android.view.inputmethod.TextAttribute; |
| 35 |
|
| 36 |
import com.android.internal.inputmethod.CancellationGroup; |
| 37 |
import com.android.internal.inputmethod.CompletableFutureUtil; |
| 38 |
import com.android.internal.inputmethod.IInputContextInvoker; |
| 39 |
import com.android.internal.inputmethod.ImeTracing; |
| 40 |
import com.android.internal.inputmethod.InputConnectionProtoDumper; |
| 41 |
import com.android.internal.view.IInputContext; |
| 42 |
import com.android.internal.view.IInputMethod; |
| 43 |
|
| 44 |
import java.lang.ref.WeakReference; |
| 45 |
import java.util.concurrent.CompletableFuture; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
final class RemoteInputConnection implements InputConnection { |
| 58 |
private static final String TAG = "RemoteInputConnection"; |
| 59 |
|
| 60 |
private static final int MAX_WAIT_TIME_MILLIS = 2000; |
| 61 |
|
| 62 |
@NonNull |
| 63 |
private final IInputContextInvoker mInvoker; |
| 64 |
|
| 65 |
private static final class InputMethodServiceInternalHolder { |
| 66 |
@NonNull |
| 67 |
private final WeakReference<InputMethodServiceInternal> mServiceRef; |
| 68 |
|
| 69 |
private InputMethodServiceInternalHolder( |
| 70 |
@NonNull WeakReference<InputMethodServiceInternal> ims) { |
| 71 |
mServiceRef = ims; |
| 72 |
} |
| 73 |
|
| 74 |
@AnyThread |
| 75 |
@Nullable |
| 76 |
public InputMethodServiceInternal getAndWarnIfNull() { |
| 77 |
final InputMethodServiceInternal ims = mServiceRef.get(); |
| 78 |
if (ims == null) { |
| 79 |
Log.e(TAG, "InputMethodService is already destroyed. InputConnection instances" |
| 80 |
+ " cannot be used beyond InputMethodService lifetime.", new Throwable()); |
| 81 |
} |
| 82 |
return ims; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@NonNull |
| 87 |
private final InputMethodServiceInternalHolder mImsInternal; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@NonNull |
| 96 |
private final CancellationGroup mCancellationGroup; |
| 97 |
|
| 98 |
RemoteInputConnection( |
| 99 |
@NonNull WeakReference<InputMethodServiceInternal> inputMethodService, |
| 100 |
IInputContext inputContext, @NonNull CancellationGroup cancellationGroup) { |
| 101 |
mImsInternal = new InputMethodServiceInternalHolder(inputMethodService); |
| 102 |
mInvoker = IInputContextInvoker.create(inputContext); |
| 103 |
mCancellationGroup = cancellationGroup; |
| 104 |
} |
| 105 |
|
| 106 |
@AnyThread |
| 107 |
public boolean isSameConnection(@NonNull IInputContext inputContext) { |
| 108 |
return mInvoker.isSameConnection(inputContext); |
| 109 |
} |
| 110 |
|
| 111 |
RemoteInputConnection(@NonNull RemoteInputConnection original, int sessionId) { |
| 112 |
mImsInternal = original.mImsInternal; |
| 113 |
mInvoker = original.mInvoker.cloneWithSessionId(sessionId); |
| 114 |
mCancellationGroup = original.mCancellationGroup; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
@Nullable |
| 121 |
@AnyThread |
| 122 |
public CharSequence getTextAfterCursor(@IntRange(from = 0) int length, int flags) { |
| 123 |
if (length < 0) { |
| 124 |
|
| 125 |
Log.e(TAG, "length=" + length + " is invalid and always results in null result."); |
| 126 |
} |
| 127 |
if (mCancellationGroup.isCanceled()) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
final CompletableFuture<CharSequence> value = mInvoker.getTextAfterCursor(length, flags); |
| 132 |
final CharSequence result = CompletableFutureUtil.getResultOrNull( |
| 133 |
value, TAG, "getTextAfterCursor()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 134 |
|
| 135 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 136 |
if (imsInternal != null && ImeTracing.getInstance().isEnabled()) { |
| 137 |
final byte[] icProto = InputConnectionProtoDumper.buildGetTextAfterCursorProto(length, |
| 138 |
flags, result); |
| 139 |
imsInternal.triggerServiceDump(TAG + "#getTextAfterCursor", icProto); |
| 140 |
} |
| 141 |
|
| 142 |
return result; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
@Nullable |
| 149 |
@AnyThread |
| 150 |
public CharSequence getTextBeforeCursor(@IntRange(from = 0) int length, int flags) { |
| 151 |
if (length < 0) { |
| 152 |
|
| 153 |
Log.e(TAG, "length=" + length + " is invalid and always results in null result."); |
| 154 |
} |
| 155 |
if (mCancellationGroup.isCanceled()) { |
| 156 |
return null; |
| 157 |
} |
| 158 |
|
| 159 |
final CompletableFuture<CharSequence> value = mInvoker.getTextBeforeCursor(length, flags); |
| 160 |
final CharSequence result = CompletableFutureUtil.getResultOrNull( |
| 161 |
value, TAG, "getTextBeforeCursor()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 162 |
|
| 163 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 164 |
if (imsInternal != null && ImeTracing.getInstance().isEnabled()) { |
| 165 |
final byte[] icProto = InputConnectionProtoDumper.buildGetTextBeforeCursorProto(length, |
| 166 |
flags, result); |
| 167 |
imsInternal.triggerServiceDump(TAG + "#getTextBeforeCursor", icProto); |
| 168 |
} |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
@AnyThread |
| 174 |
public CharSequence getSelectedText(int flags) { |
| 175 |
if (mCancellationGroup.isCanceled()) { |
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
final CompletableFuture<CharSequence> value = mInvoker.getSelectedText(flags); |
| 180 |
final CharSequence result = CompletableFutureUtil.getResultOrNull( |
| 181 |
value, TAG, "getSelectedText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 182 |
|
| 183 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 184 |
if (imsInternal != null && ImeTracing.getInstance().isEnabled()) { |
| 185 |
final byte[] icProto = InputConnectionProtoDumper.buildGetSelectedTextProto(flags, |
| 186 |
result); |
| 187 |
imsInternal.triggerServiceDump(TAG + "#getSelectedText", icProto); |
| 188 |
} |
| 189 |
|
| 190 |
return result; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
@AnyThread |
| 206 |
public SurroundingText getSurroundingText( |
| 207 |
@IntRange(from = 0) int beforeLength, @IntRange(from = 0) int afterLength, int flags) { |
| 208 |
if (beforeLength < 0) { |
| 209 |
|
| 210 |
Log.e(TAG, "beforeLength=" + beforeLength |
| 211 |
+ " is invalid and always results in null result."); |
| 212 |
} |
| 213 |
if (afterLength < 0) { |
| 214 |
|
| 215 |
Log.e(TAG, "afterLength=" + afterLength |
| 216 |
+ " is invalid and always results in null result."); |
| 217 |
} |
| 218 |
if (mCancellationGroup.isCanceled()) { |
| 219 |
return null; |
| 220 |
} |
| 221 |
|
| 222 |
final CompletableFuture<SurroundingText> value = mInvoker.getSurroundingText(beforeLength, |
| 223 |
afterLength, flags); |
| 224 |
final SurroundingText result = CompletableFutureUtil.getResultOrNull( |
| 225 |
value, TAG, "getSurroundingText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 226 |
|
| 227 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 228 |
if (imsInternal != null && ImeTracing.getInstance().isEnabled()) { |
| 229 |
final byte[] icProto = InputConnectionProtoDumper.buildGetSurroundingTextProto( |
| 230 |
beforeLength, afterLength, flags, result); |
| 231 |
imsInternal.triggerServiceDump(TAG + "#getSurroundingText", icProto); |
| 232 |
} |
| 233 |
|
| 234 |
return result; |
| 235 |
} |
| 236 |
|
| 237 |
@AnyThread |
| 238 |
public int getCursorCapsMode(int reqModes) { |
| 239 |
if (mCancellationGroup.isCanceled()) { |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
final CompletableFuture<Integer> value = mInvoker.getCursorCapsMode(reqModes); |
| 244 |
final int result = CompletableFutureUtil.getResultOrZero( |
| 245 |
value, TAG, "getCursorCapsMode()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 246 |
|
| 247 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 248 |
if (imsInternal != null && ImeTracing.getInstance().isEnabled()) { |
| 249 |
final byte[] icProto = InputConnectionProtoDumper.buildGetCursorCapsModeProto( |
| 250 |
reqModes, result); |
| 251 |
imsInternal.triggerServiceDump(TAG + "#getCursorCapsMode", icProto); |
| 252 |
} |
| 253 |
|
| 254 |
return result; |
| 255 |
} |
| 256 |
|
| 257 |
@AnyThread |
| 258 |
public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { |
| 259 |
if (mCancellationGroup.isCanceled()) { |
| 260 |
return null; |
| 261 |
} |
| 262 |
|
| 263 |
final CompletableFuture<ExtractedText> value = mInvoker.getExtractedText(request, flags); |
| 264 |
final ExtractedText result = CompletableFutureUtil.getResultOrNull( |
| 265 |
value, TAG, "getExtractedText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 266 |
|
| 267 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 268 |
if (imsInternal != null && ImeTracing.getInstance().isEnabled()) { |
| 269 |
final byte[] icProto = InputConnectionProtoDumper.buildGetExtractedTextProto( |
| 270 |
request, flags, result); |
| 271 |
imsInternal.triggerServiceDump(TAG + "#getExtractedText", icProto); |
| 272 |
} |
| 273 |
|
| 274 |
return result; |
| 275 |
} |
| 276 |
|
| 277 |
@AnyThread |
| 278 |
public boolean commitText(CharSequence text, int newCursorPosition) { |
| 279 |
final boolean handled = mInvoker.commitText(text, newCursorPosition); |
| 280 |
if (handled) { |
| 281 |
notifyUserActionIfNecessary(); |
| 282 |
} |
| 283 |
return handled; |
| 284 |
} |
| 285 |
|
| 286 |
@AnyThread |
| 287 |
public boolean commitText(@NonNull CharSequence text, int newCursorPosition, |
| 288 |
@Nullable TextAttribute textAttribute) { |
| 289 |
final boolean handled = |
| 290 |
mInvoker.commitText(text, newCursorPosition, textAttribute); |
| 291 |
if (handled) { |
| 292 |
notifyUserActionIfNecessary(); |
| 293 |
} |
| 294 |
return handled; |
| 295 |
} |
| 296 |
|
| 297 |
@AnyThread |
| 298 |
private void notifyUserActionIfNecessary() { |
| 299 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 300 |
if (imsInternal == null) { |
| 301 |
return; |
| 302 |
} |
| 303 |
imsInternal.notifyUserActionIfNecessary(); |
| 304 |
} |
| 305 |
|
| 306 |
@AnyThread |
| 307 |
public boolean commitCompletion(CompletionInfo text) { |
| 308 |
return mInvoker.commitCompletion(text); |
| 309 |
} |
| 310 |
|
| 311 |
@AnyThread |
| 312 |
public boolean commitCorrection(CorrectionInfo correctionInfo) { |
| 313 |
return mInvoker.commitCorrection(correctionInfo); |
| 314 |
} |
| 315 |
|
| 316 |
@AnyThread |
| 317 |
public boolean setSelection(int start, int end) { |
| 318 |
return mInvoker.setSelection(start, end); |
| 319 |
} |
| 320 |
|
| 321 |
@AnyThread |
| 322 |
public boolean performEditorAction(int actionCode) { |
| 323 |
return mInvoker.performEditorAction(actionCode); |
| 324 |
} |
| 325 |
|
| 326 |
@AnyThread |
| 327 |
public boolean performContextMenuAction(int id) { |
| 328 |
return mInvoker.performContextMenuAction(id); |
| 329 |
} |
| 330 |
|
| 331 |
@AnyThread |
| 332 |
public boolean setComposingRegion(int start, int end) { |
| 333 |
return mInvoker.setComposingRegion(start, end); |
| 334 |
} |
| 335 |
|
| 336 |
@AnyThread |
| 337 |
public boolean setComposingRegion(int start, int end, @Nullable TextAttribute textAttribute) { |
| 338 |
return mInvoker.setComposingRegion(start, end, textAttribute); |
| 339 |
} |
| 340 |
|
| 341 |
@AnyThread |
| 342 |
public boolean setComposingText(CharSequence text, int newCursorPosition) { |
| 343 |
final boolean handled = mInvoker.setComposingText(text, newCursorPosition); |
| 344 |
if (handled) { |
| 345 |
notifyUserActionIfNecessary(); |
| 346 |
} |
| 347 |
return handled; |
| 348 |
} |
| 349 |
|
| 350 |
@AnyThread |
| 351 |
public boolean setComposingText(CharSequence text, int newCursorPosition, |
| 352 |
@Nullable TextAttribute textAttribute) { |
| 353 |
final boolean handled = mInvoker.setComposingText(text, newCursorPosition, textAttribute); |
| 354 |
if (handled) { |
| 355 |
notifyUserActionIfNecessary(); |
| 356 |
} |
| 357 |
return handled; |
| 358 |
} |
| 359 |
|
| 360 |
@AnyThread |
| 361 |
public boolean finishComposingText() { |
| 362 |
return mInvoker.finishComposingText(); |
| 363 |
} |
| 364 |
|
| 365 |
@AnyThread |
| 366 |
public boolean beginBatchEdit() { |
| 367 |
return mInvoker.beginBatchEdit(); |
| 368 |
} |
| 369 |
|
| 370 |
@AnyThread |
| 371 |
public boolean endBatchEdit() { |
| 372 |
return mInvoker.endBatchEdit(); |
| 373 |
} |
| 374 |
|
| 375 |
@AnyThread |
| 376 |
public boolean sendKeyEvent(KeyEvent event) { |
| 377 |
final boolean handled = mInvoker.sendKeyEvent(event); |
| 378 |
if (handled) { |
| 379 |
notifyUserActionIfNecessary(); |
| 380 |
} |
| 381 |
return handled; |
| 382 |
} |
| 383 |
|
| 384 |
@AnyThread |
| 385 |
public boolean clearMetaKeyStates(int states) { |
| 386 |
return mInvoker.clearMetaKeyStates(states); |
| 387 |
} |
| 388 |
|
| 389 |
@AnyThread |
| 390 |
public boolean deleteSurroundingText(int beforeLength, int afterLength) { |
| 391 |
return mInvoker.deleteSurroundingText(beforeLength, afterLength); |
| 392 |
} |
| 393 |
|
| 394 |
@AnyThread |
| 395 |
public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) { |
| 396 |
return mInvoker.deleteSurroundingTextInCodePoints(beforeLength, afterLength); |
| 397 |
} |
| 398 |
|
| 399 |
@AnyThread |
| 400 |
public boolean reportFullscreenMode(boolean enabled) { |
| 401 |
|
| 402 |
return false; |
| 403 |
} |
| 404 |
|
| 405 |
@AnyThread |
| 406 |
public boolean performSpellCheck() { |
| 407 |
return mInvoker.performSpellCheck(); |
| 408 |
} |
| 409 |
|
| 410 |
@AnyThread |
| 411 |
public boolean performPrivateCommand(String action, Bundle data) { |
| 412 |
return mInvoker.performPrivateCommand(action, data); |
| 413 |
} |
| 414 |
|
| 415 |
@AnyThread |
| 416 |
public boolean requestCursorUpdates(int cursorUpdateMode) { |
| 417 |
if (mCancellationGroup.isCanceled()) { |
| 418 |
return false; |
| 419 |
} |
| 420 |
|
| 421 |
final InputMethodServiceInternal ims = mImsInternal.getAndWarnIfNull(); |
| 422 |
if (ims == null) { |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
final int displayId = ims.getContext().getDisplayId(); |
| 427 |
final CompletableFuture<Boolean> value = |
| 428 |
mInvoker.requestCursorUpdates(cursorUpdateMode, displayId); |
| 429 |
return CompletableFutureUtil.getResultOrFalse(value, TAG, "requestCursorUpdates()", |
| 430 |
mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 431 |
} |
| 432 |
|
| 433 |
@Override |
| 434 |
@AnyThread |
| 435 |
public boolean requestCursorUpdates(@CursorUpdateMode int cursorUpdateMode, |
| 436 |
@CursorUpdateFilter int cursorUpdateFilter) { |
| 437 |
if (mCancellationGroup.isCanceled()) { |
| 438 |
return false; |
| 439 |
} |
| 440 |
|
| 441 |
final InputMethodServiceInternal ims = mImsInternal.getAndWarnIfNull(); |
| 442 |
if (ims == null) { |
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
final int displayId = ims.getContext().getDisplayId(); |
| 447 |
final CompletableFuture<Boolean> value = |
| 448 |
mInvoker.requestCursorUpdates(cursorUpdateMode, cursorUpdateFilter, displayId); |
| 449 |
return CompletableFutureUtil.getResultOrFalse(value, TAG, "requestCursorUpdates()", |
| 450 |
mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 451 |
} |
| 452 |
|
| 453 |
@AnyThread |
| 454 |
public Handler getHandler() { |
| 455 |
|
| 456 |
return null; |
| 457 |
} |
| 458 |
|
| 459 |
@AnyThread |
| 460 |
public void closeConnection() { |
| 461 |
|
| 462 |
} |
| 463 |
|
| 464 |
@AnyThread |
| 465 |
public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) { |
| 466 |
if (mCancellationGroup.isCanceled()) { |
| 467 |
return false; |
| 468 |
} |
| 469 |
|
| 470 |
if ((flags & InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) { |
| 471 |
final InputMethodServiceInternal imsInternal = mImsInternal.getAndWarnIfNull(); |
| 472 |
if (imsInternal == null) { |
| 473 |
return false; |
| 474 |
} |
| 475 |
imsInternal.exposeContent(inputContentInfo, this); |
| 476 |
} |
| 477 |
|
| 478 |
final CompletableFuture<Boolean> value = |
| 479 |
mInvoker.commitContent(inputContentInfo, flags, opts); |
| 480 |
return CompletableFutureUtil.getResultOrFalse( |
| 481 |
value, TAG, "commitContent()", mCancellationGroup, MAX_WAIT_TIME_MILLIS); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
@AnyThread |
| 488 |
public boolean setImeConsumesInput(boolean imeConsumesInput) { |
| 489 |
return mInvoker.setImeConsumesInput(imeConsumesInput); |
| 490 |
} |
| 491 |
|
| 492 |
@AnyThread |
| 493 |
@Override |
| 494 |
public String toString() { |
| 495 |
return "RemoteInputConnection{idHash=#" |
| 496 |
+ Integer.toHexString(System.identityHashCode(this)) + "}"; |
| 497 |
} |
| 498 |
} |