1 /* 2 * Copyright (C) 2007-2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package android.view.inputmethod; 18 19 import android.annotation.MainThread; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SdkConstant; 23 import android.annotation.SdkConstant.SdkConstantType; 24 import android.inputmethodservice.InputMethodService; 25 import android.os.IBinder; 26 import android.os.RemoteException; 27 import android.os.ResultReceiver; 28 import android.util.Log; 29 import android.view.View; 30 31 import com.android.internal.inputmethod.IInputMethodPrivilegedOperations; 32 import com.android.internal.view.IInlineSuggestionsRequestCallback; 33 import com.android.internal.view.InlineSuggestionsRequestInfo; 34 35 /** 36 * The InputMethod interface represents an input method which can generate key 37 * events and text, such as digital, email addresses, CJK characters, other 38 * language characters, and etc., while handling various input events, and send 39 * the text back to the application that requests text input. See 40 * {@link InputMethodManager} for more general information about the 41 * architecture. 42 * 43 * <p>Applications will not normally use this interface themselves, instead 44 * relying on the standard interaction provided by 45 * {@link android.widget.TextView} and {@link android.widget.EditText}. 46 * 47 * <p>Those implementing input methods should normally do so by deriving from 48 * {@link InputMethodService} or one of its subclasses. When implementing 49 * an input method, the service component containing it must also supply 50 * a {@link #SERVICE_META_DATA} meta-data field, referencing an XML resource 51 * providing details about the input method. All input methods also must 52 * require that clients hold the 53 * {@link android.Manifest.permission#BIND_INPUT_METHOD} in order to interact 54 * with the service; if this is not required, the system will not use that 55 * input method, because it can not trust that it is not compromised. 56 * 57 * <p>The InputMethod interface is actually split into two parts: the interface 58 * here is the top-level interface to the input method, providing all 59 * access to it, which only the system can access (due to the BIND_INPUT_METHOD 60 * permission requirement). In addition its method 61 * {@link #createSession(android.view.inputmethod.InputMethod.SessionCallback)} 62 * can be called to instantate a secondary {@link InputMethodSession} interface 63 * which is what clients use to communicate with the input method. 64 */ 65 public interface InputMethod { 66 /** @hide **/ 67 public static final String TAG = "InputMethod"; 68 /** 69 * This is the interface name that a service implementing an input 70 * method should say that it supports -- that is, this is the action it 71 * uses for its intent filter. 72 * To be supported, the service must also require the 73 * {@link android.Manifest.permission#BIND_INPUT_METHOD} permission so 74 * that other applications can not abuse it. 75 */ 76 @SdkConstant(SdkConstantType.SERVICE_ACTION) 77 public static final String SERVICE_INTERFACE = "android.view.InputMethod"; 78 79 /** 80 * Name under which an InputMethod service component publishes information 81 * about itself. This meta-data must reference an XML resource containing 82 * an 83 * <code><{@link android.R.styleable#InputMethod input-method}></code> 84 * tag. 85 */ 86 public static final String SERVICE_META_DATA = "android.view.im"; 87 88 public interface SessionCallback { sessionCreated(InputMethodSession session)89 public void sessionCreated(InputMethodSession session); 90 } 91 92 /** 93 * Called first thing after an input method is created, this supplies a 94 * unique token for the session it has with the system service as well as 95 * IPC endpoint to do some other privileged operations. 96 * 97 * @param token special token for the system to identify 98 * {@link InputMethodService} 99 * @param displayId The id of the display that current IME shown. 100 * Used for {{@link #updateInputMethodDisplay(int)}} 101 * @param privilegedOperations IPC endpoint to do some privileged 102 * operations that are allowed only to the 103 * current IME. 104 * @hide 105 */ 106 @MainThread initializeInternal(IBinder token, int displayId, IInputMethodPrivilegedOperations privilegedOperations)107 default void initializeInternal(IBinder token, int displayId, 108 IInputMethodPrivilegedOperations privilegedOperations) { 109 updateInputMethodDisplay(displayId); 110 attachToken(token); 111 } 112 113 /** 114 * Called to notify the IME that Autofill Frameworks requested an inline suggestions request. 115 * 116 * @param requestInfo information needed to create an {@link InlineSuggestionsRequest}. 117 * @param cb {@link IInlineSuggestionsRequestCallback} used to pass back the request object. 118 * 119 * @hide 120 */ onCreateInlineSuggestionsRequest(InlineSuggestionsRequestInfo requestInfo, IInlineSuggestionsRequestCallback cb)121 default void onCreateInlineSuggestionsRequest(InlineSuggestionsRequestInfo requestInfo, 122 IInlineSuggestionsRequestCallback cb) { 123 try { 124 cb.onInlineSuggestionsUnsupported(); 125 } catch (RemoteException e) { 126 Log.w(TAG, "Failed to call onInlineSuggestionsUnsupported.", e); 127 } 128 } 129 130 /** 131 * Called first thing after an input method is created, this supplies a 132 * unique token for the session it has with the system service. It is 133 * needed to identify itself with the service to validate its operations. 134 * This token <strong>must not</strong> be passed to applications, since 135 * it grants special priviledges that should not be given to applications. 136 * 137 * <p>The system guarantees that this method is called back between 138 * {@link InputMethodService#onCreate()} and {@link InputMethodService#onDestroy()} 139 * at most once. 140 */ 141 @MainThread attachToken(IBinder token)142 public void attachToken(IBinder token); 143 144 /** 145 * Update context display according to given displayId. 146 * 147 * @param displayId The id of the display that need to update for context. 148 * @hide 149 */ 150 @MainThread updateInputMethodDisplay(int displayId)151 default void updateInputMethodDisplay(int displayId) { 152 } 153 154 /** 155 * Bind a new application environment in to the input method, so that it 156 * can later start and stop input processing. 157 * Typically this method is called when this input method is enabled in an 158 * application for the first time. 159 * 160 * @param binding Information about the application window that is binding 161 * to the input method. 162 * 163 * @see InputBinding 164 * @see #unbindInput() 165 */ 166 @MainThread bindInput(InputBinding binding)167 public void bindInput(InputBinding binding); 168 169 /** 170 * Unbind an application environment, called when the information previously 171 * set by {@link #bindInput} is no longer valid for this input method. 172 * 173 * <p> 174 * Typically this method is called when the application changes to be 175 * non-foreground. 176 */ 177 @MainThread unbindInput()178 public void unbindInput(); 179 180 /** 181 * This method is called when the application starts to receive text and it 182 * is ready for this input method to process received events and send result 183 * text back to the application. 184 * 185 * @param inputConnection Optional specific input connection for 186 * communicating with the text box; if null, you should use the generic 187 * bound input connection. 188 * @param info Information about the text box (typically, an EditText) 189 * that requests input. 190 * 191 * @see EditorInfo 192 */ 193 @MainThread startInput(InputConnection inputConnection, EditorInfo info)194 public void startInput(InputConnection inputConnection, EditorInfo info); 195 196 /** 197 * This method is called when the state of this input method needs to be 198 * reset. 199 * 200 * <p> 201 * Typically, this method is called when the input focus is moved from one 202 * text box to another. 203 * 204 * @param inputConnection Optional specific input connection for 205 * communicating with the text box; if null, you should use the generic 206 * bound input connection. 207 * @param attribute The attribute of the text box (typically, a EditText) 208 * that requests input. 209 * 210 * @see EditorInfo 211 */ 212 @MainThread restartInput(InputConnection inputConnection, EditorInfo attribute)213 public void restartInput(InputConnection inputConnection, EditorInfo attribute); 214 215 /** 216 * This method is called when {@code {@link #startInput(InputConnection, EditorInfo)} or 217 * {@code {@link #restartInput(InputConnection, EditorInfo)} needs to be dispatched. 218 * 219 * <p>Note: This method is hidden because the {@code startInputToken} that this method is 220 * dealing with is one of internal details, which should not be exposed to the IME developers. 221 * If you override this method, you are responsible for not breaking existing IMEs that expect 222 * {@link #startInput(InputConnection, EditorInfo)} to be still called back.</p> 223 * 224 * @param inputConnection optional specific input connection for communicating with the text 225 * box; if {@code null}, you should use the generic bound input 226 * connection 227 * @param editorInfo information about the text box (typically, an EditText) that requests input 228 * @param restarting {@code false} if this corresponds to 229 * {@link #startInput(InputConnection, EditorInfo)}. Otherwise this 230 * corresponds to {@link #restartInput(InputConnection, EditorInfo)}. 231 * @param startInputToken a token that identifies a logical session that starts with this method 232 * call. Some internal IPCs such as {@link 233 * InputMethodManager#setImeWindowStatus(IBinder, IBinder, int, int)} 234 * require this token to work, and you have to keep the token alive until 235 * the next {@link #startInput(InputConnection, EditorInfo, IBinder)} as 236 * long as your implementation of {@link InputMethod} relies on such 237 * IPCs 238 * @see #startInput(InputConnection, EditorInfo) 239 * @see #restartInput(InputConnection, EditorInfo) 240 * @see EditorInfo 241 * @hide 242 */ 243 @MainThread dispatchStartInputWithToken(@ullable InputConnection inputConnection, @NonNull EditorInfo editorInfo, boolean restarting, @NonNull IBinder startInputToken, boolean shouldPreRenderIme)244 default void dispatchStartInputWithToken(@Nullable InputConnection inputConnection, 245 @NonNull EditorInfo editorInfo, boolean restarting, 246 @NonNull IBinder startInputToken, boolean shouldPreRenderIme) { 247 if (restarting) { 248 restartInput(inputConnection, editorInfo); 249 } else { 250 startInput(inputConnection, editorInfo); 251 } 252 } 253 254 /** 255 * Create a new {@link InputMethodSession} that can be handed to client 256 * applications for interacting with the input method. You can later 257 * use {@link #revokeSession(InputMethodSession)} to destroy the session 258 * so that it can no longer be used by any clients. 259 * 260 * @param callback Interface that is called with the newly created session. 261 */ 262 @MainThread createSession(SessionCallback callback)263 public void createSession(SessionCallback callback); 264 265 /** 266 * Control whether a particular input method session is active. 267 * 268 * @param session The {@link InputMethodSession} previously provided through 269 * SessionCallback.sessionCreated() that is to be changed. 270 */ 271 @MainThread setSessionEnabled(InputMethodSession session, boolean enabled)272 public void setSessionEnabled(InputMethodSession session, boolean enabled); 273 274 /** 275 * Disable and destroy a session that was previously created with 276 * {@link #createSession(android.view.inputmethod.InputMethod.SessionCallback)}. 277 * After this call, the given session interface is no longer active and 278 * calls on it will fail. 279 * 280 * @param session The {@link InputMethodSession} previously provided through 281 * SessionCallback.sessionCreated() that is to be revoked. 282 */ 283 @MainThread revokeSession(InputMethodSession session)284 public void revokeSession(InputMethodSession session); 285 286 /** 287 * Flag for {@link #showSoftInput}: this show has been explicitly 288 * requested by the user. If not set, the system has decided it may be 289 * a good idea to show the input method based on a navigation operation 290 * in the UI. 291 */ 292 public static final int SHOW_EXPLICIT = 0x00001; 293 294 /** 295 * Flag for {@link #showSoftInput}: this show has been forced to 296 * happen by the user. If set, the input method should remain visible 297 * until deliberated dismissed by the user in its UI. 298 */ 299 public static final int SHOW_FORCED = 0x00002; 300 301 /** 302 * Request that any soft input part of the input method be shown to the user. 303 * 304 * @param flags Provides additional information about the show request. 305 * Currently may be 0 or have the bit {@link #SHOW_EXPLICIT} set. 306 * @param resultReceiver The client requesting the show may wish to 307 * be told the impact of their request, which should be supplied here. 308 * The result code should be 309 * {@link InputMethodManager#RESULT_UNCHANGED_SHOWN InputMethodManager.RESULT_UNCHANGED_SHOWN}, 310 * {@link InputMethodManager#RESULT_UNCHANGED_HIDDEN InputMethodManager.RESULT_UNCHANGED_HIDDEN}, 311 * {@link InputMethodManager#RESULT_SHOWN InputMethodManager.RESULT_SHOWN}, or 312 * {@link InputMethodManager#RESULT_HIDDEN InputMethodManager.RESULT_HIDDEN}. 313 * @param showInputToken an opaque {@link android.os.Binder} token to identify which API call 314 * of {@link InputMethodManager#showSoftInput(View, int)} is associated with 315 * this callback. 316 * @hide 317 */ 318 @MainThread showSoftInputWithToken(int flags, ResultReceiver resultReceiver, IBinder showInputToken)319 default public void showSoftInputWithToken(int flags, ResultReceiver resultReceiver, 320 IBinder showInputToken) { 321 showSoftInput(flags, resultReceiver); 322 } 323 324 /** 325 * Request that any soft input part of the input method be shown to the user. 326 * 327 * @param flags Provides additional information about the show request. 328 * Currently may be 0 or have the bit {@link #SHOW_EXPLICIT} set. 329 * @param resultReceiver The client requesting the show may wish to 330 * be told the impact of their request, which should be supplied here. 331 * The result code should be 332 * {@link InputMethodManager#RESULT_UNCHANGED_SHOWN InputMethodManager.RESULT_UNCHANGED_SHOWN}, 333 * {@link InputMethodManager#RESULT_UNCHANGED_HIDDEN InputMethodManager.RESULT_UNCHANGED_HIDDEN}, 334 * {@link InputMethodManager#RESULT_SHOWN InputMethodManager.RESULT_SHOWN}, or 335 * {@link InputMethodManager#RESULT_HIDDEN InputMethodManager.RESULT_HIDDEN}. 336 */ 337 @MainThread showSoftInput(int flags, ResultReceiver resultReceiver)338 public void showSoftInput(int flags, ResultReceiver resultReceiver); 339 340 /** 341 * Request that any soft input part of the input method be hidden from the user. 342 * @param flags Provides additional information about the show request. 343 * Currently always 0. 344 * @param resultReceiver The client requesting the show may wish to 345 * be told the impact of their request, which should be supplied here. 346 * The result code should be 347 * {@link InputMethodManager#RESULT_UNCHANGED_SHOWN InputMethodManager.RESULT_UNCHANGED_SHOWN}, 348 * {@link InputMethodManager#RESULT_UNCHANGED_HIDDEN InputMethodManager.RESULT_UNCHANGED_HIDDEN}, 349 * {@link InputMethodManager#RESULT_SHOWN InputMethodManager.RESULT_SHOWN}, or 350 * {@link InputMethodManager#RESULT_HIDDEN InputMethodManager.RESULT_HIDDEN}. 351 * @param hideInputToken an opaque {@link android.os.Binder} token to identify which API call 352 * of {@link InputMethodManager#hideSoftInputFromWindow(IBinder, int)}} is associated 353 * with this callback. 354 * @hide 355 */ 356 @MainThread hideSoftInputWithToken(int flags, ResultReceiver resultReceiver, IBinder hideInputToken)357 public void hideSoftInputWithToken(int flags, ResultReceiver resultReceiver, 358 IBinder hideInputToken); 359 360 /** 361 * Request that any soft input part of the input method be hidden from the user. 362 * @param flags Provides additional information about the show request. 363 * Currently always 0. 364 * @param resultReceiver The client requesting the show may wish to 365 * be told the impact of their request, which should be supplied here. 366 * The result code should be 367 * {@link InputMethodManager#RESULT_UNCHANGED_SHOWN InputMethodManager.RESULT_UNCHANGED_SHOWN}, 368 * {@link InputMethodManager#RESULT_UNCHANGED_HIDDEN 369 * InputMethodManager.RESULT_UNCHANGED_HIDDEN}, 370 * {@link InputMethodManager#RESULT_SHOWN InputMethodManager.RESULT_SHOWN}, or 371 * {@link InputMethodManager#RESULT_HIDDEN InputMethodManager.RESULT_HIDDEN}. 372 */ 373 @MainThread hideSoftInput(int flags, ResultReceiver resultReceiver)374 public void hideSoftInput(int flags, ResultReceiver resultReceiver); 375 376 /** 377 * Notify that the input method subtype is being changed in the same input method. 378 * @param subtype New subtype of the notified input method 379 */ 380 @MainThread changeInputMethodSubtype(InputMethodSubtype subtype)381 public void changeInputMethodSubtype(InputMethodSubtype subtype); 382 383 /** 384 * Update token of the client window requesting {@link #showSoftInput(int, ResultReceiver)} 385 * @param showInputToken dummy app window token for window requesting 386 * {@link InputMethodManager#showSoftInput(View, int)} 387 * @hide 388 */ setCurrentShowInputToken(IBinder showInputToken)389 public void setCurrentShowInputToken(IBinder showInputToken); 390 391 /** 392 * Update token of the client window requesting {@link #hideSoftInput(int, ResultReceiver)} 393 * @param hideInputToken dummy app window token for window requesting 394 * {@link InputMethodManager#hideSoftInputFromWindow(IBinder, int)} 395 * @hide 396 */ setCurrentHideInputToken(IBinder hideInputToken)397 public void setCurrentHideInputToken(IBinder hideInputToken); 398 399 } 400