Debian Codegraph @

core/java/android/inputmethodservice/RemoteInputConnection.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Takes care of remote method invocations of {@link InputConnection} in the IME side.
49 *
50 * <p>This class works as a proxy to forward API calls on {@link InputConnection} to
51 * {@link com.android.internal.inputmethod.RemoteInputConnectionImpl} running on the IME client
52 * (editor app) process then waits replies as needed.</p>
53 *
54 * <p>See also {@link IInputContext} for the actual {@link android.os.Binder} IPC protocols under
55 * the hood.</p>
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 * Signaled when the system decided to take away IME focus from the target app.
91 *
92 * <p>This is expected to be signaled immediately when the IME process receives
93 * {@link IInputMethod#unbindInput()}.</p>
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 * See {@link InputConnection#getTextAfterCursor(int, int)}.
119 */
120 @Nullable
121 @AnyThread
122 public CharSequence getTextAfterCursor(@IntRange(from = 0) int length, int flags) {
123 if (length < 0) {
124 // TODO: Should we throw an InvalidParameterException() based on targetSdkVersion?
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 * See {@link InputConnection#getTextBeforeCursor(int, int)}.
147 */
148 @Nullable
149 @AnyThread
150 public CharSequence getTextBeforeCursor(@IntRange(from = 0) int length, int flags) {
151 if (length < 0) {
152 // TODO: Should we throw an InvalidParameterException() based on targetSdkVersion?
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 * Get {@link SurroundingText} around the current cursor, with <var>beforeLength</var>
195 * characters of text before the cursor, <var>afterLength</var> characters of text after the
196 * cursor, and all of the selected text.
197 * @param beforeLength The expected length of the text before the cursor
198 * @param afterLength The expected length of the text after the cursor
199 * @param flags Supplies additional options controlling how the text is returned. May be either
200 * 0 or {@link #GET_TEXT_WITH_STYLES}.
201 * @return the surrounding text around the cursor position; the length of the returned text
202 * might be less than requested. It could also be {@code null} when the editor or system could
203 * not support this protocol.
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 // TODO: Should we throw an InvalidParameterException() based on targetSdkVersion?
210 Log.e(TAG, "beforeLength=" + beforeLength
211 + " is invalid and always results in null result.");
212 }
213 if (afterLength < 0) {
214 // TODO: Should we throw an InvalidParameterException() based on targetSdkVersion?
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 // Nothing should happen when called from input method.
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 // Nothing should happen when called from input method.
456 return null;
457 }
458
459 @AnyThread
460 public void closeConnection() {
461 // Nothing should happen when called from input method.
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 * See {@link InputConnection#setImeConsumesInput(boolean)}.
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 }