Debian Codegraph @

core/java/com/android/internal/inputmethod/IRemoteAccessibilityInputConnectionInvoker.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.NonNull;
21 import android.annotation.Nullable;
22 import android.os.RemoteException;
23 import android.view.KeyEvent;
24 import android.view.inputmethod.SurroundingText;
25 import android.view.inputmethod.TextAttribute;
26
27 import com.android.internal.infra.AndroidFuture;
28
29 import java.util.Objects;
30
31 final class IRemoteAccessibilityInputConnectionInvoker {
32 @NonNull
33 private final IRemoteAccessibilityInputConnection mConnection;
34 private final int mSessionId;
35
36 private IRemoteAccessibilityInputConnectionInvoker(
37 @NonNull IRemoteAccessibilityInputConnection inputContext, int sessionId) {
38 mConnection = inputContext;
39 mSessionId = sessionId;
40 }
41
42 /**
43 * Creates a new instance of {@link IRemoteAccessibilityInputConnectionInvoker} for the given
44 * {@link IRemoteAccessibilityInputConnection}.
45 *
46 * @param connection {@link IRemoteAccessibilityInputConnection} to be wrapped.
47 * @return A new instance of {@link IRemoteAccessibilityInputConnectionInvoker}.
48 */
49 public static IRemoteAccessibilityInputConnectionInvoker create(
50 @NonNull IRemoteAccessibilityInputConnection connection) {
51 Objects.requireNonNull(connection);
52 return new IRemoteAccessibilityInputConnectionInvoker(connection, 0);
53 }
54
55 /**
56 * Creates a new instance of {@link IRemoteAccessibilityInputConnectionInvoker} with the given
57 * {@code sessionId}.
58 *
59 * @param sessionId the new session ID to be used.
60 * @return A new instance of {@link IRemoteAccessibilityInputConnectionInvoker}.
61 */
62 @NonNull
63 public IRemoteAccessibilityInputConnectionInvoker cloneWithSessionId(int sessionId) {
64 return new IRemoteAccessibilityInputConnectionInvoker(mConnection, sessionId);
65 }
66
67 /**
68 * @param connection {@code IRemoteAccessibilityInputConnection} to be compared with
69 * @return {@code true} if the underlying {@code IRemoteAccessibilityInputConnection} is the
70 * same. {@code false} if {@code connection} is {@code null}.
71 */
72 @AnyThread
73 public boolean isSameConnection(@NonNull IRemoteAccessibilityInputConnection connection) {
74 if (connection == null) {
75 return false;
76 }
77 return mConnection.asBinder() == connection.asBinder();
78 }
79
80 @NonNull
81 InputConnectionCommandHeader createHeader() {
82 return new InputConnectionCommandHeader(mSessionId);
83 }
84
85 /**
86 * Invokes {@link IRemoteAccessibilityInputConnection#commitText(InputConnectionCommandHeader,
87 * int, CharSequence)}.
88 *
89 * @param text {@code text} parameter to be passed.
90 * @param newCursorPosition {@code newCursorPosition} parameter to be passed.
91 * @param textAttribute The extra information about the text.
92 */
93 @AnyThread
94 public void commitText(CharSequence text, int newCursorPosition,
95 @Nullable TextAttribute textAttribute) {
96 try {
97 mConnection.commitText(createHeader(), text, newCursorPosition, textAttribute);
98 } catch (RemoteException e) {
99 }
100 }
101
102 /**
103 * Invokes {@link IRemoteAccessibilityInputConnection#setSelection(InputConnectionCommandHeader,
104 * int, int)}.
105 *
106 * @param start {@code start} parameter to be passed.
107 * @param end {@code start} parameter to be passed.
108 */
109 @AnyThread
110 public void setSelection(int start, int end) {
111 try {
112 mConnection.setSelection(createHeader(), start, end);
113 } catch (RemoteException e) {
114 }
115 }
116
117 /**
118 * Invokes {@link IRemoteAccessibilityInputConnection#getSurroundingText(
119 * InputConnectionCommandHeader, int, int, int, AndroidFuture)}.
120 *
121 * @param beforeLength {@code beforeLength} parameter to be passed.
122 * @param afterLength {@code afterLength} parameter to be passed.
123 * @param flags {@code flags} parameter to be passed.
124 * @return {@link AndroidFuture< SurroundingText >} that can be used to retrieve the
125 * invocation result. {@link RemoteException} will be treated as an error.
126 */
127 @AnyThread
128 @NonNull
129 public AndroidFuture<SurroundingText> getSurroundingText(int beforeLength, int afterLength,
130 int flags) {
131 final AndroidFuture<SurroundingText> future = new AndroidFuture<>();
132 try {
133 mConnection.getSurroundingText(createHeader(), beforeLength, afterLength, flags,
134 future);
135 } catch (RemoteException e) {
136 future.completeExceptionally(e);
137 }
138 return future;
139 }
140
141 /**
142 * Invokes {@link IRemoteAccessibilityInputConnection#deleteSurroundingText(
143 * InputConnectionCommandHeader, int, int)}.
144 *
145 * @param beforeLength {@code beforeLength} parameter to be passed.
146 * @param afterLength {@code afterLength} parameter to be passed.
147 */
148 @AnyThread
149 public void deleteSurroundingText(int beforeLength, int afterLength) {
150 try {
151 mConnection.deleteSurroundingText(createHeader(), beforeLength, afterLength);
152 } catch (RemoteException e) {
153 }
154 }
155
156 /**
157 * Invokes {@link IRemoteAccessibilityInputConnection#sendKeyEvent(
158 * InputConnectionCommandHeader, KeyEvent)}.
159 *
160 * @param event {@code event} parameter to be passed.
161 */
162 @AnyThread
163 public void sendKeyEvent(KeyEvent event) {
164 try {
165 mConnection.sendKeyEvent(createHeader(), event);
166 } catch (RemoteException e) {
167 }
168 }
169
170 /**
171 * Invokes {@link IRemoteAccessibilityInputConnection#performEditorAction(
172 * InputConnectionCommandHeader, int)}.
173 *
174 * @param actionCode {@code start} parameter to be passed.
175 */
176 @AnyThread
177 public void performEditorAction(int actionCode) {
178 try {
179 mConnection.performEditorAction(createHeader(), actionCode);
180 } catch (RemoteException e) {
181 }
182 }
183
184 /**
185 * Invokes {@link IRemoteAccessibilityInputConnection#performContextMenuAction(
186 * InputConnectionCommandHeader, int)}.
187 *
188 * @param id {@code id} parameter to be passed.
189 */
190 @AnyThread
191 public void performContextMenuAction(int id) {
192 try {
193 mConnection.performContextMenuAction(createHeader(), id);
194 } catch (RemoteException e) {
195 }
196 }
197
198 /**
199 * Invokes {@link IRemoteAccessibilityInputConnection#getCursorCapsMode(
200 * InputConnectionCommandHeader, int, AndroidFuture)}.
201 *
202 * @param reqModes {@code reqModes} parameter to be passed.
203 * @return {@link AndroidFuture<Integer>} that can be used to retrieve the invocation
204 * result. {@link RemoteException} will be treated as an error.
205 */
206 @AnyThread
207 @NonNull
208 public AndroidFuture<Integer> getCursorCapsMode(int reqModes) {
209 final AndroidFuture<Integer> future = new AndroidFuture<>();
210 try {
211 mConnection.getCursorCapsMode(createHeader(), reqModes, future);
212 } catch (RemoteException e) {
213 future.completeExceptionally(e);
214 }
215 return future;
216 }
217
218 /**
219 * Invokes {@link IRemoteAccessibilityInputConnection#clearMetaKeyStates(
220 * InputConnectionCommandHeader, int)}.
221 *
222 * @param states {@code states} parameter to be passed.
223 */
224 @AnyThread
225 public void clearMetaKeyStates(int states) {
226 try {
227 mConnection.clearMetaKeyStates(createHeader(), states);
228 } catch (RemoteException e) {
229 }
230 }
231 }