Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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.DurationMillisLong;
21 import android.annotation.IntRange;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.view.KeyEvent;
25 import android.view.inputmethod.SurroundingText;
26 import android.view.inputmethod.TextAttribute;
27
28 import com.android.internal.infra.AndroidFuture;
29 import com.android.internal.view.IInputMethod;
30
31 import java.util.concurrent.CompletableFuture;
32
33 /**
34 * A wrapper object for A11y IME.
35 *
36 * <p>This needs to be public to be referenced from {@link android.app.UiAutomation}.</p>
37 */
38 public final class RemoteAccessibilityInputConnection {
39 private static final String TAG = "RemoteA11yInputConnection";
40
41 @DurationMillisLong
42 private static final int MAX_WAIT_TIME_MILLIS = 2000;
43
44 @NonNull
45 IRemoteAccessibilityInputConnectionInvoker mInvoker;
46
47 /**
48 * Signaled when the system decided to take away IME focus from the target app.
49 *
50 * <p>This is expected to be signaled immediately when the IME process receives
51 * {@link IInputMethod#unbindInput()}.</p>
52 */
53 @NonNull
54 private final CancellationGroup mCancellationGroup;
55
56 public RemoteAccessibilityInputConnection(
57 @NonNull IRemoteAccessibilityInputConnection connection,
58 @NonNull CancellationGroup cancellationGroup) {
59 mInvoker = IRemoteAccessibilityInputConnectionInvoker.create(connection);
60 mCancellationGroup = cancellationGroup;
61 }
62
63 public RemoteAccessibilityInputConnection(@NonNull RemoteAccessibilityInputConnection original,
64 int sessionId) {
65 mInvoker = original.mInvoker.cloneWithSessionId(sessionId);
66 mCancellationGroup = original.mCancellationGroup;
67 }
68
69 /**
70 * Test if this object holds the given {@link IRemoteAccessibilityInputConnection} or not.
71 *
72 * @param connection {@link IRemoteAccessibilityInputConnection} to be tested.
73 * @return {@code true} if this object holds the same object.
74 */
75 @AnyThread
76 public boolean isSameConnection(@NonNull IRemoteAccessibilityInputConnection connection) {
77 return mInvoker.isSameConnection(connection);
78 }
79
80 /**
81 * Invokes {@link IRemoteAccessibilityInputConnection#commitText(InputConnectionCommandHeader,
82 * CharSequence, int, TextAttribute)}.
83 *
84 * @param text The {@code "text"} parameter to be passed.
85 * @param newCursorPosition The {@code "newCursorPosition"} parameter to be passed.
86 * @param textAttribute The {@code "textAttribute"} parameter to be passed.
87 */
88 @AnyThread
89 public void commitText(@NonNull CharSequence text, int newCursorPosition,
90 @Nullable TextAttribute textAttribute) {
91 mInvoker.commitText(text, newCursorPosition, textAttribute);
92 }
93
94 /**
95 * Invokes {@link IRemoteAccessibilityInputConnection#setSelection(InputConnectionCommandHeader,
96 * int, int)}.
97 *
98 * @param start The {@code "start"} parameter to be passed.
99 * @param end The {@code "end"} parameter to be passed.
100 */
101 @AnyThread
102 public void setSelection(int start, int end) {
103 mInvoker.setSelection(start, end);
104 }
105
106 /**
107 * Invokes {@link IRemoteAccessibilityInputConnection#getSurroundingText(
108 * InputConnectionCommandHeader, int, int, int, AndroidFuture)}.
109 *
110 * @param beforeLength The {@code "beforeLength"} parameter to be passed.
111 * @param afterLength The {@code "afterLength"} parameter to be passed.
112 * @param flags The {@code "flags"} parameter to be passed.
113 * @return The {@link SurroundingText} object returned from the target application.
114 */
115 @AnyThread
116 public SurroundingText getSurroundingText(
117 @IntRange(from = 0) int beforeLength, @IntRange(from = 0) int afterLength, int flags) {
118 if (mCancellationGroup.isCanceled()) {
119 return null;
120 }
121
122 final CompletableFuture<SurroundingText> value = mInvoker.getSurroundingText(beforeLength,
123 afterLength, flags);
124 return CompletableFutureUtil.getResultOrNull(
125 value, TAG, "getSurroundingText()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
126 }
127
128 /**
129 * Invokes {@link IRemoteAccessibilityInputConnection#deleteSurroundingText(
130 * InputConnectionCommandHeader, int, int)}.
131 *
132 * @param beforeLength The {@code "beforeLength"} parameter to be passed.
133 * @param afterLength The {@code "afterLength"} parameter to be passed.
134 */
135 @AnyThread
136 public void deleteSurroundingText(int beforeLength, int afterLength) {
137 mInvoker.deleteSurroundingText(beforeLength, afterLength);
138 }
139
140 /**
141 * Invokes {@link IRemoteAccessibilityInputConnection#sendKeyEvent(InputConnectionCommandHeader,
142 * KeyEvent)}.
143 *
144 * @param event The {@code "event"} parameter to be passed.
145 */
146 @AnyThread
147 public void sendKeyEvent(KeyEvent event) {
148 mInvoker.sendKeyEvent(event);
149 }
150
151 /**
152 * Invokes {@link IRemoteAccessibilityInputConnection#performEditorAction(
153 * InputConnectionCommandHeader, int)}.
154 *
155 * @param actionCode The {@code "actionCode"} parameter to be passed.
156 */
157 @AnyThread
158 public void performEditorAction(int actionCode) {
159 mInvoker.performEditorAction(actionCode);
160 }
161
162 /**
163 * Invokes {@link IRemoteAccessibilityInputConnection#performContextMenuAction(
164 * InputConnectionCommandHeader, int)}.
165 *
166 * @param id The {@code "id"} parameter to be passed.
167 */
168 @AnyThread
169 public void performContextMenuAction(int id) {
170 mInvoker.performContextMenuAction(id);
171 }
172
173 /**
174 * Invokes {@link IRemoteAccessibilityInputConnection#getCursorCapsMode(
175 * InputConnectionCommandHeader, int, AndroidFuture)}.
176 *
177 * @param reqModes The {@code "reqModes"} parameter to be passed.
178 * @return integer result returned from the target application.
179 */
180 @AnyThread
181 public int getCursorCapsMode(int reqModes) {
182 if (mCancellationGroup.isCanceled()) {
183 return 0;
184 }
185
186 final CompletableFuture<Integer> value = mInvoker.getCursorCapsMode(reqModes);
187
188 return CompletableFutureUtil.getResultOrZero(
189 value, TAG, "getCursorCapsMode()", mCancellationGroup, MAX_WAIT_TIME_MILLIS);
190 }
191
192 /**
193 * Invokes {@link IRemoteAccessibilityInputConnection#clearMetaKeyStates(
194 * InputConnectionCommandHeader, int)}.
195 *
196 * @param states The {@code "states"} parameter to be passed.
197 */
198 @AnyThread
199 public void clearMetaKeyStates(int states) {
200 mInvoker.clearMetaKeyStates(states);
201 }
202 }