Debian Codegraph @

core/java/com/android/internal/inputmethod/IInputContextInvoker.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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 com.android.internal.inputmethod;
18
19 import android.annotation.AnyThread;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.os.Bundle;
23 import android.os.RemoteException;
24 import android.view.KeyEvent;
25 import android.view.inputmethod.CompletionInfo;
26 import android.view.inputmethod.CorrectionInfo;
27 import android.view.inputmethod.ExtractedText;
28 import android.view.inputmethod.ExtractedTextRequest;
29 import android.view.inputmethod.InputContentInfo;
30 import android.view.inputmethod.SurroundingText;
31 import android.view.inputmethod.TextAttribute;
32
33 import com.android.internal.infra.AndroidFuture;
34 import com.android.internal.view.IInputContext;
35
36 import java.util.Objects;
37
38 /**
39 * A stateless wrapper of {@link com.android.internal.view.IInputContext} to encapsulate boilerplate
40 * code around {@link AndroidFuture} and {@link RemoteException}.
41 */
42 public final class IInputContextInvoker {
43
44 @NonNull
45 private final IInputContext mIInputContext;
46 private final int mSessionId;
47
48 private IInputContextInvoker(@NonNull IInputContext inputContext, int sessionId) {
49 mIInputContext = inputContext;
50 mSessionId = sessionId;
51 }
52
53 /**
54 * Creates a new instance of {@link IInputContextInvoker} for the given {@link IInputContext}.
55 *
56 * @param inputContext {@link IInputContext} to be wrapped.
57 * @return A new instance of {@link IInputContextInvoker}.
58 */
59 public static IInputContextInvoker create(@NonNull IInputContext inputContext) {
60 Objects.requireNonNull(inputContext);
61 return new IInputContextInvoker(inputContext, 0);
62 }
63
64 /**
65 * Creates a new instance of {@link IInputContextInvoker} with the given {@code sessionId}.
66 *
67 * @param sessionId the new session ID to be used.
68 * @return A new instance of {@link IInputContextInvoker}.
69 */
70 @NonNull
71 public IInputContextInvoker cloneWithSessionId(int sessionId) {
72 return new IInputContextInvoker(mIInputContext, sessionId);
73 }
74
75 /**
76 * @param inputContext {@code IInputContext} to be compared with
77 * @return {@code true} if the underlying {@code IInputContext} is the same. {@code false} if
78 * {@code inputContext} is {@code null}.
79 */
80 @AnyThread
81 public boolean isSameConnection(@NonNull IInputContext inputContext) {
82 if (inputContext == null) {
83 return false;
84 }
85 return mIInputContext.asBinder() == inputContext.asBinder();
86 }
87
88 @NonNull
89 InputConnectionCommandHeader createHeader() {
90 return new InputConnectionCommandHeader(mSessionId);
91 }
92
93 /**
94 * Invokes {@link IInputContext#getTextAfterCursor(InputConnectionCommandHeader, int, int,
95 * AndroidFuture)}.
96 *
97 * @param length {@code length} parameter to be passed.
98 * @param flags {@code flags} parameter to be passed.
99 * @return {@link AndroidFuture<CharSequence>} that can be used to retrieve the invocation
100 * result. {@link RemoteException} will be treated as an error.
101 */
102 @AnyThread
103 @NonNull
104 public AndroidFuture<CharSequence> getTextAfterCursor(int length, int flags) {
105 final AndroidFuture<CharSequence> future = new AndroidFuture<>();
106 try {
107 mIInputContext.getTextAfterCursor(createHeader(), length, flags, future);
108 } catch (RemoteException e) {
109 future.completeExceptionally(e);
110 }
111 return future;
112 }
113
114 /**
115 * Invokes {@link IInputContext#getTextBeforeCursor(InputConnectionCommandHeader, int, int,
116 * AndroidFuture)}.
117 *
118 * @param length {@code length} parameter to be passed.
119 * @param flags {@code flags} parameter to be passed.
120 * @return {@link AndroidFuture<CharSequence>} that can be used to retrieve the invocation
121 * result. {@link RemoteException} will be treated as an error.
122 */
123 @AnyThread
124 @NonNull
125 public AndroidFuture<CharSequence> getTextBeforeCursor(int length, int flags) {
126 final AndroidFuture<CharSequence> future = new AndroidFuture<>();
127 try {
128 mIInputContext.getTextBeforeCursor(createHeader(), length, flags, future);
129 } catch (RemoteException e) {
130 future.completeExceptionally(e);
131 }
132 return future;
133 }
134
135 /**
136 * Invokes
137 * {@link IInputContext#getSelectedText(InputConnectionCommandHeader, int, AndroidFuture)}.
138 *
139 * @param flags {@code flags} parameter to be passed.
140 * @return {@link AndroidFuture<CharSequence>} that can be used to retrieve the invocation
141 * result. {@link RemoteException} will be treated as an error.
142 */
143 @AnyThread
144 @NonNull
145 public AndroidFuture<CharSequence> getSelectedText(int flags) {
146 final AndroidFuture<CharSequence> future = new AndroidFuture<>();
147 try {
148 mIInputContext.getSelectedText(createHeader(), flags, future);
149 } catch (RemoteException e) {
150 future.completeExceptionally(e);
151 }
152 return future;
153 }
154
155 /**
156 * Invokes
157 * {@link IInputContext#getSurroundingText(InputConnectionCommandHeader, int, int, int,
158 * AndroidFuture)}.
159 *
160 * @param beforeLength {@code beforeLength} parameter to be passed.
161 * @param afterLength {@code afterLength} parameter to be passed.
162 * @param flags {@code flags} parameter to be passed.
163 * @return {@link AndroidFuture<SurroundingText>} that can be used to retrieve the
164 * invocation result. {@link RemoteException} will be treated as an error.
165 */
166 @AnyThread
167 @NonNull
168 public AndroidFuture<SurroundingText> getSurroundingText(int beforeLength, int afterLength,
169 int flags) {
170 final AndroidFuture<SurroundingText> future = new AndroidFuture<>();
171 try {
172 mIInputContext.getSurroundingText(createHeader(), beforeLength, afterLength, flags,
173 future);
174 } catch (RemoteException e) {
175 future.completeExceptionally(e);
176 }
177 return future;
178 }
179
180 /**
181 * Invokes
182 * {@link IInputContext#getCursorCapsMode(InputConnectionCommandHeader, int, AndroidFuture)}.
183 *
184 * @param reqModes {@code reqModes} parameter to be passed.
185 * @return {@link AndroidFuture<Integer>} that can be used to retrieve the invocation
186 * result. {@link RemoteException} will be treated as an error.
187 */
188 @AnyThread
189 @NonNull
190 public AndroidFuture<Integer> getCursorCapsMode(int reqModes) {
191 final AndroidFuture<Integer> future = new AndroidFuture<>();
192 try {
193 mIInputContext.getCursorCapsMode(createHeader(), reqModes, future);
194 } catch (RemoteException e) {
195 future.completeExceptionally(e);
196 }
197 return future;
198 }
199
200 /**
201 * Invokes {@link IInputContext#getExtractedText(InputConnectionCommandHeader,
202 * ExtractedTextRequest, int, AndroidFuture)}.
203 *
204 * @param request {@code request} parameter to be passed.
205 * @param flags {@code flags} parameter to be passed.
206 * @return {@link AndroidFuture<ExtractedText>} that can be used to retrieve the invocation
207 * result. {@link RemoteException} will be treated as an error.
208 */
209 @AnyThread
210 @NonNull
211 public AndroidFuture<ExtractedText> getExtractedText(ExtractedTextRequest request,
212 int flags) {
213 final AndroidFuture<ExtractedText> future = new AndroidFuture<>();
214 try {
215 mIInputContext.getExtractedText(createHeader(), request, flags, future);
216 } catch (RemoteException e) {
217 future.completeExceptionally(e);
218 }
219 return future;
220 }
221
222 /**
223 * Invokes {@link IInputContext#commitText(InputConnectionCommandHeader, CharSequence, int)}.
224 *
225 * @param text {@code text} parameter to be passed.
226 * @param newCursorPosition {@code newCursorPosition} parameter to be passed.
227 * @return {@code true} if the invocation is completed without {@link RemoteException}.
228 * {@code false} otherwise.
229 */
230 @AnyThread
231 public boolean commitText(CharSequence text, int newCursorPosition) {
232 try {
233 mIInputContext.commitText(createHeader(), text, newCursorPosition);
234 return true;
235 } catch (RemoteException e) {
236 return false;
237 }
238 }
239
240 /**
241 * Invokes {@link IInputContext#commitTextWithTextAttribute(InputConnectionCommandHeader, int,
242 * CharSequence)}.
243 *
244 * @param text {@code text} parameter to be passed.
245 * @param newCursorPosition {@code newCursorPosition} parameter to be passed.
246 * @param textAttribute The extra information about the text.
247 * @return {@code true} if the invocation is completed without {@link RemoteException}.
248 * {@code false} otherwise.
249 */
250 @AnyThread
251 public boolean commitText(CharSequence text, int newCursorPosition,
252 @Nullable TextAttribute textAttribute) {
253 try {
254 mIInputContext.commitTextWithTextAttribute(
255 createHeader(), text, newCursorPosition, textAttribute);
256 return true;
257 } catch (RemoteException e) {
258 return false;
259 }
260 }
261
262 /**
263 * Invokes {@link IInputContext#commitCompletion(InputConnectionCommandHeader, CompletionInfo)}.
264 *
265 * @param text {@code text} parameter to be passed.
266 * @return {@code true} if the invocation is completed without {@link RemoteException}.
267 * {@code false} otherwise.
268 */
269 @AnyThread
270 public boolean commitCompletion(CompletionInfo text) {
271 try {
272 mIInputContext.commitCompletion(createHeader(), text);
273 return true;
274 } catch (RemoteException e) {
275 return false;
276 }
277 }
278
279 /**
280 * Invokes {@link IInputContext#commitCorrection(InputConnectionCommandHeader, CorrectionInfo)}.
281 *
282 * @param correctionInfo {@code correctionInfo} parameter to be passed.
283 * @return {@code true} if the invocation is completed without {@link RemoteException}.
284 * {@code false} otherwise.
285 */
286 @AnyThread
287 public boolean commitCorrection(CorrectionInfo correctionInfo) {
288 try {
289 mIInputContext.commitCorrection(createHeader(), correctionInfo);
290 return true;
291 } catch (RemoteException e) {
292 return false;
293 }
294 }
295
296 /**
297 * Invokes {@link IInputContext#setSelection(InputConnectionCommandHeader, int, int)}.
298 *
299 * @param start {@code start} parameter to be passed.
300 * @param end {@code start} parameter to be passed.
301 * @return {@code true} if the invocation is completed without {@link RemoteException}.
302 * {@code false} otherwise.
303 */
304 @AnyThread
305 public boolean setSelection(int start, int end) {
306 try {
307 mIInputContext.setSelection(createHeader(), start, end);
308 return true;
309 } catch (RemoteException e) {
310 return false;
311 }
312 }
313
314 /**
315 * Invokes {@link IInputContext#performEditorAction(InputConnectionCommandHeader, int)}.
316 *
317 * @param actionCode {@code start} parameter to be passed.
318 * @return {@code true} if the invocation is completed without {@link RemoteException}.
319 * {@code false} otherwise.
320 */
321 @AnyThread
322 public boolean performEditorAction(int actionCode) {
323 try {
324 mIInputContext.performEditorAction(createHeader(), actionCode);
325 return true;
326 } catch (RemoteException e) {
327 return false;
328 }
329 }
330
331 /**
332 * Invokes {@link IInputContext#performContextMenuAction(InputConnectionCommandHeader, int)}.
333 *
334 * @param id {@code id} parameter to be passed.
335 * @return {@code true} if the invocation is completed without {@link RemoteException}.
336 * {@code false} otherwise.
337 */
338 @AnyThread
339 public boolean performContextMenuAction(int id) {
340 try {
341 mIInputContext.performContextMenuAction(createHeader(), id);
342 return true;
343 } catch (RemoteException e) {
344 return false;
345 }
346 }
347
348 /**
349 * Invokes {@link IInputContext#setComposingRegion(InputConnectionCommandHeader, int, int)}.
350 *
351 * @param start {@code id} parameter to be passed.
352 * @param end {@code id} parameter to be passed.
353 * @return {@code true} if the invocation is completed without {@link RemoteException}.
354 * {@code false} otherwise.
355 */
356 @AnyThread
357 public boolean setComposingRegion(int start, int end) {
358 try {
359 mIInputContext.setComposingRegion(createHeader(), start, end);
360 return true;
361 } catch (RemoteException e) {
362 return false;
363 }
364 }
365
366 /**
367 * Invokes {@link IInputContext#setComposingRegionWithTextAttribute(
368 * InputConnectionCommandHeader, int, int, TextAttribute)}.
369 *
370 * @param start {@code id} parameter to be passed.
371 * @param end {@code id} parameter to be passed.
372 * @param textAttribute The extra information about the text.
373 * @return {@code true} if the invocation is completed without {@link RemoteException}.
374 * {@code false} otherwise.
375 */
376 @AnyThread
377 public boolean setComposingRegion(int start, int end, @Nullable TextAttribute textAttribute) {
378 try {
379 mIInputContext.setComposingRegionWithTextAttribute(
380 createHeader(), start, end, textAttribute);
381 return true;
382 } catch (RemoteException e) {
383 return false;
384 }
385 }
386
387 /**
388 * Invokes
389 * {@link IInputContext#setComposingText(InputConnectionCommandHeader, CharSequence, int)}.
390 *
391 * @param text {@code text} parameter to be passed.
392 * @param newCursorPosition {@code newCursorPosition} parameter to be passed.
393 * @return {@code true} if the invocation is completed without {@link RemoteException}.
394 * {@code false} otherwise.
395 */
396 @AnyThread
397 public boolean setComposingText(CharSequence text, int newCursorPosition) {
398 try {
399 mIInputContext.setComposingText(createHeader(), text, newCursorPosition);
400 return true;
401 } catch (RemoteException e) {
402 return false;
403 }
404 }
405
406 /**
407 * Invokes {@link IInputContext#setComposingTextWithTextAttribute(InputConnectionCommandHeader,
408 * CharSequence, int, TextAttribute)}.
409 *
410 * @param text {@code text} parameter to be passed.
411 * @param newCursorPosition {@code newCursorPosition} parameter to be passed.
412 * @param textAttribute The extra information about the text.
413 * @return {@code true} if the invocation is completed without {@link RemoteException}.
414 * {@code false} otherwise.
415 */
416 @AnyThread
417 public boolean setComposingText(CharSequence text, int newCursorPosition,
418 @Nullable TextAttribute textAttribute) {
419 try {
420 mIInputContext.setComposingTextWithTextAttribute(
421 createHeader(), text, newCursorPosition, textAttribute);
422 return true;
423 } catch (RemoteException e) {
424 return false;
425 }
426 }
427
428 /**
429 * Invokes {@link IInputContext#finishComposingText(InputConnectionCommandHeader)}.
430 *
431 * @return {@code true} if the invocation is completed without {@link RemoteException}.
432 * {@code false} otherwise.
433 */
434 @AnyThread
435 public boolean finishComposingText() {
436 try {
437 mIInputContext.finishComposingText(createHeader());
438 return true;
439 } catch (RemoteException e) {
440 return false;
441 }
442 }
443
444 /**
445 * Invokes {@link IInputContext#beginBatchEdit(InputConnectionCommandHeader)}.
446 *
447 * @return {@code true} if the invocation is completed without {@link RemoteException}.
448 * {@code false} otherwise.
449 */
450 @AnyThread
451 public boolean beginBatchEdit() {
452 try {
453 mIInputContext.beginBatchEdit(createHeader());
454 return true;
455 } catch (RemoteException e) {
456 return false;
457 }
458 }
459
460 /**
461 * Invokes {@link IInputContext#endBatchEdit(InputConnectionCommandHeader)}.
462 *
463 * @return {@code true} if the invocation is completed without {@link RemoteException}.
464 * {@code false} otherwise.
465 */
466 @AnyThread
467 public boolean endBatchEdit() {
468 try {
469 mIInputContext.endBatchEdit(createHeader());
470 return true;
471 } catch (RemoteException e) {
472 return false;
473 }
474 }
475
476 /**
477 * Invokes {@link IInputContext#sendKeyEvent(InputConnectionCommandHeader, KeyEvent)}.
478 *
479 * @param event {@code event} parameter to be passed.
480 * @return {@code true} if the invocation is completed without {@link RemoteException}.
481 * {@code false} otherwise.
482 */
483 @AnyThread
484 public boolean sendKeyEvent(KeyEvent event) {
485 try {
486 mIInputContext.sendKeyEvent(createHeader(), event);
487 return true;
488 } catch (RemoteException e) {
489 return false;
490 }
491 }
492
493 /**
494 * Invokes {@link IInputContext#clearMetaKeyStates(InputConnectionCommandHeader, int)}.
495 *
496 * @param states {@code states} parameter to be passed.
497 * @return {@code true} if the invocation is completed without {@link RemoteException}.
498 * {@code false} otherwise.
499 */
500 @AnyThread
501 public boolean clearMetaKeyStates(int states) {
502 try {
503 mIInputContext.clearMetaKeyStates(createHeader(), states);
504 return true;
505 } catch (RemoteException e) {
506 return false;
507 }
508 }
509
510 /**
511 * Invokes {@link IInputContext#deleteSurroundingText(InputConnectionCommandHeader, int, int)}.
512 *
513 * @param beforeLength {@code beforeLength} parameter to be passed.
514 * @param afterLength {@code afterLength} parameter to be passed.
515 * @return {@code true} if the invocation is completed without {@link RemoteException}.
516 * {@code false} otherwise.
517 */
518 @AnyThread
519 public boolean deleteSurroundingText(int beforeLength, int afterLength) {
520 try {
521 mIInputContext.deleteSurroundingText(createHeader(), beforeLength, afterLength);
522 return true;
523 } catch (RemoteException e) {
524 return false;
525 }
526 }
527
528 /**
529 * Invokes {@link IInputContext#deleteSurroundingTextInCodePoints(InputConnectionCommandHeader,
530 * int, int)}.
531 *
532 * @param beforeLength {@code beforeLength} parameter to be passed.
533 * @param afterLength {@code afterLength} parameter to be passed.
534 * @return {@code true} if the invocation is completed without {@link RemoteException}.
535 * {@code false} otherwise.
536 */
537 @AnyThread
538 public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
539 try {
540 mIInputContext.deleteSurroundingTextInCodePoints(createHeader(), beforeLength,
541 afterLength);
542 return true;
543 } catch (RemoteException e) {
544 return false;
545 }
546 }
547
548 /**
549 * Invokes {@link IInputContext#performSpellCheck(InputConnectionCommandHeader)}.
550 *
551 * @return {@code true} if the invocation is completed without {@link RemoteException}.
552 * {@code false} otherwise.
553 */
554 @AnyThread
555 public boolean performSpellCheck() {
556 try {
557 mIInputContext.performSpellCheck(createHeader());
558 return true;
559 } catch (RemoteException e) {
560 return false;
561 }
562 }
563
564 /**
565 * Invokes
566 * {@link IInputContext#performPrivateCommand(InputConnectionCommandHeader, String, Bundle)}.
567 *
568 * @param action {@code action} parameter to be passed.
569 * @param data {@code data} parameter to be passed.
570 * @return {@code true} if the invocation is completed without {@link RemoteException}.
571 * {@code false} otherwise.
572 */
573 @AnyThread
574 public boolean performPrivateCommand(String action, Bundle data) {
575 try {
576 mIInputContext.performPrivateCommand(createHeader(), action, data);
577 return true;
578 } catch (RemoteException e) {
579 return false;
580 }
581 }
582
583 /**
584 * Invokes {@link IInputContext#requestCursorUpdates(InputConnectionCommandHeader, int, int,
585 * AndroidFuture)}.
586 *
587 * @param cursorUpdateMode {@code cursorUpdateMode} parameter to be passed.
588 * @param imeDisplayId the display ID that is associated with the IME.
589 * @return {@link AndroidFuture<Boolean>} that can be used to retrieve the invocation
590 * result. {@link RemoteException} will be treated as an error.
591 */
592 @AnyThread
593 @NonNull
594 public AndroidFuture<Boolean> requestCursorUpdates(int cursorUpdateMode, int imeDisplayId) {
595 final AndroidFuture<Boolean> future = new AndroidFuture<>();
596 try {
597 mIInputContext.requestCursorUpdates(createHeader(), cursorUpdateMode, imeDisplayId,
598 future);
599 } catch (RemoteException e) {
600 future.completeExceptionally(e);
601 }
602 return future;
603 }
604
605 /**
606 * Invokes {@link IInputContext#requestCursorUpdatesWithFilter(InputConnectionCommandHeader,
607 * int, int, int, AndroidFuture)}.
608 *
609 * @param cursorUpdateMode {@code cursorUpdateMode} parameter to be passed.
610 * @param cursorUpdateFilter {@code cursorUpdateFilter} parameter to be passed.
611 * @param imeDisplayId the display ID that is associated with the IME.
612 * @return {@link AndroidFuture<Boolean>} that can be used to retrieve the invocation
613 * result. {@link RemoteException} will be treated as an error.
614 */
615 @AnyThread
616 @NonNull
617 public AndroidFuture<Boolean> requestCursorUpdates(int cursorUpdateMode, int cursorUpdateFilter,
618 int imeDisplayId) {
619 final AndroidFuture<Boolean> future = new AndroidFuture<>();
620 try {
621 mIInputContext.requestCursorUpdatesWithFilter(createHeader(), cursorUpdateMode,
622 cursorUpdateFilter, imeDisplayId, future);
623 } catch (RemoteException e) {
624 future.completeExceptionally(e);
625 }
626 return future;
627 }
628
629 /**
630 * Invokes {@link IInputContext#commitContent(InputConnectionCommandHeader, InputContentInfo,
631 * int, Bundle, AndroidFuture)}.
632 *
633 * @param inputContentInfo {@code inputContentInfo} parameter to be passed.
634 * @param flags {@code flags} parameter to be passed.
635 * @param opts {@code opts} parameter to be passed.
636 * @return {@link AndroidFuture<Boolean>} that can be used to retrieve the invocation
637 * result. {@link RemoteException} will be treated as an error.
638 */
639 @AnyThread
640 @NonNull
641 public AndroidFuture<Boolean> commitContent(InputContentInfo inputContentInfo, int flags,
642 Bundle opts) {
643 final AndroidFuture<Boolean> future = new AndroidFuture<>();
644 try {
645 mIInputContext.commitContent(createHeader(), inputContentInfo, flags, opts, future);
646 } catch (RemoteException e) {
647 future.completeExceptionally(e);
648 }
649 return future;
650 }
651
652 /**
653 * Invokes {@link IInputContext#setImeConsumesInput(InputConnectionCommandHeader, boolean)}.
654 *
655 * @param imeConsumesInput {@code imeConsumesInput} parameter to be passed.
656 * @return {@code true} if the invocation is completed without {@link RemoteException}.
657 * {@code false} otherwise.
658 */
659 @AnyThread
660 public boolean setImeConsumesInput(boolean imeConsumesInput) {
661 try {
662 mIInputContext.setImeConsumesInput(createHeader(), imeConsumesInput);
663 return true;
664 } catch (RemoteException e) {
665 return false;
666 }
667 }
668 }