| 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.view.inputmethod; |
| 18 | |
| 19 | import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 20 | import static android.view.inputmethod.EditorInfoProto.FIELD_ID; |
| 21 | import static android.view.inputmethod.EditorInfoProto.IME_OPTIONS; |
| 22 | import static android.view.inputmethod.EditorInfoProto.INPUT_TYPE; |
| 23 | import static android.view.inputmethod.EditorInfoProto.PACKAGE_NAME; |
| 24 | import static android.view.inputmethod.EditorInfoProto.PRIVATE_IME_OPTIONS; |
| 25 | import static android.view.inputmethod.EditorInfoProto.TARGET_INPUT_METHOD_USER_ID; |
| 26 | |
| 27 | import android.annotation.IntDef; |
| 28 | import android.annotation.IntRange; |
| 29 | import android.annotation.NonNull; |
| 30 | import android.annotation.Nullable; |
| 31 | import android.annotation.RequiresPermission; |
| 32 | import android.content.res.Configuration; |
| 33 | import android.os.Build.VERSION_CODES; |
| 34 | import android.os.Bundle; |
| 35 | import android.os.LocaleList; |
| 36 | import android.os.Parcel; |
| 37 | import android.os.Parcelable; |
| 38 | import android.os.UserHandle; |
| 39 | import android.text.InputType; |
| 40 | import android.text.TextUtils; |
| 41 | import android.util.Printer; |
| 42 | import android.util.proto.ProtoOutputStream; |
| 43 | import android.view.View; |
| 44 | import android.view.autofill.AutofillId; |
| 45 | |
| 46 | import com.android.internal.annotations.VisibleForTesting; |
| 47 | import com.android.internal.util.ArrayUtils; |
| 48 | import com.android.internal.util.Preconditions; |
| 49 | |
| 50 | import java.lang.annotation.Retention; |
| 51 | import java.lang.annotation.RetentionPolicy; |
| 52 | import java.util.Arrays; |
| 53 | import java.util.Objects; |
| 54 | |
| 55 | /** |
| 56 | * An EditorInfo describes several attributes of a text editing object |
| 57 | * that an input method is communicating with (typically an EditText), most |
| 58 | * importantly the type of text content it contains and the current cursor position. |
| 59 | */ |
| 60 | public class EditorInfo implements InputType, Parcelable { |
| 61 | /** |
| 62 | * Masks for {@link inputType} |
| 63 | * |
| 64 | * <pre> |
| 65 | * |-------|-------|-------|-------| |
| 66 | * 1111 TYPE_MASK_CLASS |
| 67 | * 11111111 TYPE_MASK_VARIATION |
| 68 | * 111111111111 TYPE_MASK_FLAGS |
| 69 | * |-------|-------|-------|-------| |
| 70 | * TYPE_NULL |
| 71 | * |-------|-------|-------|-------| |
| 72 | * 1 TYPE_CLASS_TEXT |
| 73 | * 1 TYPE_TEXT_VARIATION_URI |
| 74 | * 1 TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
| 75 | * 11 TYPE_TEXT_VARIATION_EMAIL_SUBJECT |
| 76 | * 1 TYPE_TEXT_VARIATION_SHORT_MESSAGE |
| 77 | * 1 1 TYPE_TEXT_VARIATION_LONG_MESSAGE |
| 78 | * 11 TYPE_TEXT_VARIATION_PERSON_NAME |
| 79 | * 111 TYPE_TEXT_VARIATION_POSTAL_ADDRESS |
| 80 | * 1 TYPE_TEXT_VARIATION_PASSWORD |
| 81 | * 1 1 TYPE_TEXT_VARIATION_VISIBLE_PASSWORD |
| 82 | * 1 1 TYPE_TEXT_VARIATION_WEB_EDIT_TEXT |
| 83 | * 1 11 TYPE_TEXT_VARIATION_FILTER |
| 84 | * 11 TYPE_TEXT_VARIATION_PHONETIC |
| 85 | * 11 1 TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS |
| 86 | * 111 TYPE_TEXT_VARIATION_WEB_PASSWORD |
| 87 | * 1 TYPE_TEXT_FLAG_CAP_CHARACTERS |
| 88 | * 1 TYPE_TEXT_FLAG_CAP_WORDS |
| 89 | * 1 TYPE_TEXT_FLAG_CAP_SENTENCES |
| 90 | * 1 TYPE_TEXT_FLAG_AUTO_CORRECT |
| 91 | * 1 TYPE_TEXT_FLAG_AUTO_COMPLETE |
| 92 | * 1 TYPE_TEXT_FLAG_MULTI_LINE |
| 93 | * 1 TYPE_TEXT_FLAG_IME_MULTI_LINE |
| 94 | * 1 TYPE_TEXT_FLAG_NO_SUGGESTIONS |
| 95 | * 1 TYPE_TEXT_FLAG_ENABLE_TEXT_CONVERSION_SUGGESTIONS |
| 96 | * |-------|-------|-------|-------| |
| 97 | * 1 TYPE_CLASS_NUMBER |
| 98 | * 1 TYPE_NUMBER_VARIATION_PASSWORD |
| 99 | * 1 TYPE_NUMBER_FLAG_SIGNED |
| 100 | * 1 TYPE_NUMBER_FLAG_DECIMAL |
| 101 | * |-------|-------|-------|-------| |
| 102 | * 11 TYPE_CLASS_PHONE |
| 103 | * |-------|-------|-------|-------| |
| 104 | * 1 TYPE_CLASS_DATETIME |
| 105 | * 1 TYPE_DATETIME_VARIATION_DATE |
| 106 | * 1 TYPE_DATETIME_VARIATION_TIME |
| 107 | * |-------|-------|-------|-------|</pre> |
| 108 | */ |
| 109 | |
| 110 | /** |
| 111 | * The content type of the text box, whose bits are defined by |
| 112 | * {@link InputType}. |
| 113 | * |
| 114 | * @see InputType |
| 115 | * @see #TYPE_MASK_CLASS |
| 116 | * @see #TYPE_MASK_VARIATION |
| 117 | * @see #TYPE_MASK_FLAGS |
| 118 | */ |
| 119 | public int inputType = TYPE_NULL; |
| 120 | |
| 121 | /** |
| 122 | * Set of bits in {@link #imeOptions} that provide alternative actions |
| 123 | * associated with the "enter" key. This both helps the IME provide |
| 124 | * better feedback about what the enter key will do, and also allows it |
| 125 | * to provide alternative mechanisms for providing that command. |
| 126 | */ |
| 127 | public static final int IME_MASK_ACTION = 0x000000ff; |
| 128 | |
| 129 | /** |
| 130 | * Bits of {@link #IME_MASK_ACTION}: no specific action has been |
| 131 | * associated with this editor, let the editor come up with its own if |
| 132 | * it can. |
| 133 | */ |
| 134 | public static final int IME_ACTION_UNSPECIFIED = 0x00000000; |
| 135 | |
| 136 | /** |
| 137 | * Bits of {@link #IME_MASK_ACTION}: there is no available action. |
| 138 | */ |
| 139 | public static final int IME_ACTION_NONE = 0x00000001; |
| 140 | |
| 141 | /** |
| 142 | * Bits of {@link #IME_MASK_ACTION}: the action key performs a "go" |
| 143 | * operation to take the user to the target of the text they typed. |
| 144 | * Typically used, for example, when entering a URL. |
| 145 | */ |
| 146 | public static final int IME_ACTION_GO = 0x00000002; |
| 147 | |
| 148 | /** |
| 149 | * Bits of {@link #IME_MASK_ACTION}: the action key performs a "search" |
| 150 | * operation, taking the user to the results of searching for the text |
| 151 | * they have typed (in whatever context is appropriate). |
| 152 | */ |
| 153 | public static final int IME_ACTION_SEARCH = 0x00000003; |
| 154 | |
| 155 | /** |
| 156 | * Bits of {@link #IME_MASK_ACTION}: the action key performs a "send" |
| 157 | * operation, delivering the text to its target. This is typically used |
| 158 | * when composing a message in IM or SMS where sending is immediate. |
| 159 | */ |
| 160 | public static final int IME_ACTION_SEND = 0x00000004; |
| 161 | |
| 162 | /** |
| 163 | * Bits of {@link #IME_MASK_ACTION}: the action key performs a "next" |
| 164 | * operation, taking the user to the next field that will accept text. |
| 165 | */ |
| 166 | public static final int IME_ACTION_NEXT = 0x00000005; |
| 167 | |
| 168 | /** |
| 169 | * Bits of {@link #IME_MASK_ACTION}: the action key performs a "done" |
| 170 | * operation, typically meaning there is nothing more to input and the |
| 171 | * IME will be closed. |
| 172 | */ |
| 173 | public static final int IME_ACTION_DONE = 0x00000006; |
| 174 | |
| 175 | /** |
| 176 | * Bits of {@link #IME_MASK_ACTION}: like {@link #IME_ACTION_NEXT}, but |
| 177 | * for moving to the previous field. This will normally not be used to |
| 178 | * specify an action (since it precludes {@link #IME_ACTION_NEXT}), but |
| 179 | * can be returned to the app if it sets {@link #IME_FLAG_NAVIGATE_PREVIOUS}. |
| 180 | */ |
| 181 | public static final int IME_ACTION_PREVIOUS = 0x00000007; |
| 182 | |
| 183 | /** |
| 184 | * Flag of {@link #imeOptions}: used to request that the IME should not update any personalized |
| 185 | * data such as typing history and personalized language model based on what the user typed on |
| 186 | * this text editing object. Typical use cases are: |
| 187 | * <ul> |
| 188 | * <li>When the application is in a special mode, where user's activities are expected to be |
| 189 | * not recorded in the application's history. Some web browsers and chat applications may |
| 190 | * have this kind of modes.</li> |
| 191 | * <li>When storing typing history does not make much sense. Specifying this flag in typing |
| 192 | * games may help to avoid typing history from being filled up with words that the user is |
| 193 | * less likely to type in their daily life. Another example is that when the application |
| 194 | * already knows that the expected input is not a valid word (e.g. a promotion code that is |
| 195 | * not a valid word in any natural language).</li> |
| 196 | * </ul> |
| 197 | * |
| 198 | * <p>Applications need to be aware that the flag is not a guarantee, and some IMEs may not |
| 199 | * respect it.</p> |
| 200 | */ |
| 201 | public static final int IME_FLAG_NO_PERSONALIZED_LEARNING = 0x1000000; |
| 202 | |
| 203 | /** |
| 204 | * Flag of {@link #imeOptions}: used to request that the IME never go |
| 205 | * into fullscreen mode. |
| 206 | * By default, IMEs may go into full screen mode when they think |
| 207 | * it's appropriate, for example on small screens in landscape |
| 208 | * orientation where displaying a software keyboard may occlude |
| 209 | * such a large portion of the screen that the remaining part is |
| 210 | * too small to meaningfully display the application UI. |
| 211 | * If this flag is set, compliant IMEs will never go into full screen mode, |
| 212 | * and always leave some space to display the application UI. |
| 213 | * Applications need to be aware that the flag is not a guarantee, and |
| 214 | * some IMEs may ignore it. |
| 215 | */ |
| 216 | public static final int IME_FLAG_NO_FULLSCREEN = 0x2000000; |
| 217 | |
| 218 | /** |
| 219 | * Flag of {@link #imeOptions}: like {@link #IME_FLAG_NAVIGATE_NEXT}, but |
| 220 | * specifies there is something interesting that a backward navigation |
| 221 | * can focus on. If the user selects the IME's facility to backward |
| 222 | * navigate, this will show up in the application as an {@link #IME_ACTION_PREVIOUS} |
| 223 | * at {@link InputConnection#performEditorAction(int) |
| 224 | * InputConnection.performEditorAction(int)}. |
| 225 | */ |
| 226 | public static final int IME_FLAG_NAVIGATE_PREVIOUS = 0x4000000; |
| 227 | |
| 228 | /** |
| 229 | * Flag of {@link #imeOptions}: used to specify that there is something |
| 230 | * interesting that a forward navigation can focus on. This is like using |
| 231 | * {@link #IME_ACTION_NEXT}, except allows the IME to be multiline (with |
| 232 | * an enter key) as well as provide forward navigation. Note that some |
| 233 | * IMEs may not be able to do this, especially when running on a small |
| 234 | * screen where there is little space. In that case it does not need to |
| 235 | * present a UI for this option. Like {@link #IME_ACTION_NEXT}, if the |
| 236 | * user selects the IME's facility to forward navigate, this will show up |
| 237 | * in the application at {@link InputConnection#performEditorAction(int) |
| 238 | * InputConnection.performEditorAction(int)}. |
| 239 | */ |
| 240 | public static final int IME_FLAG_NAVIGATE_NEXT = 0x8000000; |
| 241 | |
| 242 | /** |
| 243 | * Flag of {@link #imeOptions}: used to specify that the IME does not need |
| 244 | * to show its extracted text UI. For input methods that may be fullscreen, |
| 245 | * often when in landscape mode, this allows them to be smaller and let part |
| 246 | * of the application be shown behind, through transparent UI parts in the |
| 247 | * fullscreen IME. The part of the UI visible to the user may not be responsive |
| 248 | * to touch because the IME will receive touch events, which may confuse the |
| 249 | * user; use {@link #IME_FLAG_NO_FULLSCREEN} instead for a better experience. |
| 250 | * Using this flag is discouraged and it may become deprecated in the future. |
| 251 | * Its meaning is unclear in some situations and it may not work appropriately |
| 252 | * on older versions of the platform. |
| 253 | */ |
| 254 | public static final int IME_FLAG_NO_EXTRACT_UI = 0x10000000; |
| 255 | |
| 256 | /** |
| 257 | * Flag of {@link #imeOptions}: used in conjunction with one of the actions |
| 258 | * masked by {@link #IME_MASK_ACTION}, this indicates that the action |
| 259 | * should not be available as an accessory button on the right of the extracted |
| 260 | * text when the input method is full-screen. Note that by setting this flag, |
| 261 | * there can be cases where the action is simply never available to the |
| 262 | * user. Setting this generally means that you think that in fullscreen mode, |
| 263 | * where there is little space to show the text, it's not worth taking some |
| 264 | * screen real estate to display the action and it should be used instead |
| 265 | * to show more text. |
| 266 | */ |
| 267 | public static final int IME_FLAG_NO_ACCESSORY_ACTION = 0x20000000; |
| 268 | |
| 269 | /** |
| 270 | * Flag of {@link #imeOptions}: used in conjunction with one of the actions |
| 271 | * masked by {@link #IME_MASK_ACTION}. If this flag is not set, IMEs will |
| 272 | * normally replace the "enter" key with the action supplied. This flag |
| 273 | * indicates that the action should not be available in-line as a replacement |
| 274 | * for the "enter" key. Typically this is because the action has such a |
| 275 | * significant impact or is not recoverable enough that accidentally hitting |
| 276 | * it should be avoided, such as sending a message. Note that |
| 277 | * {@link android.widget.TextView} will automatically set this flag for you |
| 278 | * on multi-line text views. |
| 279 | */ |
| 280 | public static final int IME_FLAG_NO_ENTER_ACTION = 0x40000000; |
| 281 | |
| 282 | /** |
| 283 | * Flag of {@link #imeOptions}: used to request an IME that is capable of |
| 284 | * inputting ASCII characters. The intention of this flag is to ensure that |
| 285 | * the user can type Roman alphabet characters in a {@link android.widget.TextView}. |
| 286 | * It is typically used for an account ID or password input. A lot of the time, |
| 287 | * IMEs are already able to input ASCII even without being told so (such IMEs |
| 288 | * already respect this flag in a sense), but there are cases when this is not |
| 289 | * the default. For instance, users of languages using a different script like |
| 290 | * Arabic, Greek, Hebrew or Russian typically have a keyboard that can't |
| 291 | * input ASCII characters by default. Applications need to be |
| 292 | * aware that the flag is not a guarantee, and some IMEs may not respect it. |
| 293 | * However, it is strongly recommended for IME authors to respect this flag |
| 294 | * especially when their IME could end up with a state where only languages |
| 295 | * using non-ASCII are enabled. |
| 296 | */ |
| 297 | public static final int IME_FLAG_FORCE_ASCII = 0x80000000; |
| 298 | |
| 299 | /** |
| 300 | * Flag of {@link #internalImeOptions}: flag is set when app window containing this |
| 301 | * {@link EditorInfo} is using {@link Configuration#ORIENTATION_PORTRAIT} mode. |
| 302 | * @hide |
| 303 | */ |
| 304 | public static final int IME_INTERNAL_FLAG_APP_WINDOW_PORTRAIT = 0x00000001; |
| 305 | |
| 306 | /** |
| 307 | * Generic unspecified type for {@link #imeOptions}. |
| 308 | */ |
| 309 | public static final int IME_NULL = 0x00000000; |
| 310 | |
| 311 | /** |
| 312 | * Masks for {@link imeOptions} |
| 313 | * |
| 314 | * <pre> |
| 315 | * |-------|-------|-------|-------| |
| 316 | * 1111 IME_MASK_ACTION |
| 317 | * |-------|-------|-------|-------| |
| 318 | * IME_ACTION_UNSPECIFIED |
| 319 | * 1 IME_ACTION_NONE |
| 320 | * 1 IME_ACTION_GO |
| 321 | * 11 IME_ACTION_SEARCH |
| 322 | * 1 IME_ACTION_SEND |
| 323 | * 1 1 IME_ACTION_NEXT |
| 324 | * 11 IME_ACTION_DONE |
| 325 | * 111 IME_ACTION_PREVIOUS |
| 326 | * 1 IME_FLAG_NO_PERSONALIZED_LEARNING |
| 327 | * 1 IME_FLAG_NO_FULLSCREEN |
| 328 | * 1 IME_FLAG_NAVIGATE_PREVIOUS |
| 329 | * 1 IME_FLAG_NAVIGATE_NEXT |
| 330 | * 1 IME_FLAG_NO_EXTRACT_UI |
| 331 | * 1 IME_FLAG_NO_ACCESSORY_ACTION |
| 332 | * 1 IME_FLAG_NO_ENTER_ACTION |
| 333 | * 1 IME_FLAG_FORCE_ASCII |
| 334 | * |-------|-------|-------|-------|</pre> |
| 335 | */ |
| 336 | |
| 337 | /** |
| 338 | * Extended type information for the editor, to help the IME better |
| 339 | * integrate with it. |
| 340 | */ |
| 341 | public int imeOptions = IME_NULL; |
| 342 | |
| 343 | /** |
| 344 | * A string supplying additional information options that are |
| 345 | * private to a particular IME implementation. The string must be |
| 346 | * scoped to a package owned by the implementation, to ensure there are |
| 347 | * no conflicts between implementations, but other than that you can put |
| 348 | * whatever you want in it to communicate with the IME. For example, |
| 349 | * you could have a string that supplies an argument like |
| 350 | * <code>"com.example.myapp.SpecialMode=3"</code>. This field is can be |
| 351 | * filled in from the {@link android.R.attr#privateImeOptions} |
| 352 | * attribute of a TextView. |
| 353 | */ |
| 354 | public String privateImeOptions = null; |
| 355 | |
| 356 | /** |
| 357 | * Masks for {@link internalImeOptions} |
| 358 | * |
| 359 | * <pre> |
| 360 | * 1 IME_INTERNAL_FLAG_APP_WINDOW_PORTRAIT |
| 361 | * |-------|-------|-------|-------|</pre> |
| 362 | */ |
| 363 | |
| 364 | /** |
| 365 | * Same as {@link android.R.attr#imeOptions} but for framework's internal-use only. |
| 366 | * @hide |
| 367 | */ |
| 368 | public int internalImeOptions = IME_NULL; |
| 369 | |
| 370 | /** |
| 371 | * In some cases an IME may be able to display an arbitrary label for |
| 372 | * a command the user can perform, which you can specify here. This is |
| 373 | * typically used as the label for the action to use in-line as a replacement |
| 374 | * for the "enter" key (see {@link #actionId}). Remember the key where |
| 375 | * this will be displayed is typically very small, and there are significant |
| 376 | * localization challenges to make this fit in all supported languages. Also |
| 377 | * you can not count absolutely on this being used, as some IMEs may |
| 378 | * ignore this. |
| 379 | */ |
| 380 | public CharSequence actionLabel = null; |
| 381 | |
| 382 | /** |
| 383 | * If {@link #actionLabel} has been given, this is the id for that command |
| 384 | * when the user presses its button that is delivered back with |
| 385 | * {@link InputConnection#performEditorAction(int) |
| 386 | * InputConnection.performEditorAction()}. |
| 387 | */ |
| 388 | public int actionId = 0; |
| 389 | |
| 390 | /** |
| 391 | * The text offset of the start of the selection at the time editing |
| 392 | * begins; -1 if not known. Keep in mind that, without knowing the cursor |
| 393 | * position, many IMEs will not be able to offer their full feature set and |
| 394 | * may even behave in unpredictable ways: pass the actual cursor position |
| 395 | * here if possible at all. |
| 396 | * |
| 397 | * <p>Also, this needs to be the cursor position <strong>right now</strong>, |
| 398 | * not at some point in the past, even if input is starting in the same text field |
| 399 | * as before. When the app is filling this object, input is about to start by |
| 400 | * definition, and this value will override any value the app may have passed to |
| 401 | * {@link InputMethodManager#updateSelection(android.view.View, int, int, int, int)} |
| 402 | * before.</p> |
| 403 | */ |
| 404 | public int initialSelStart = -1; |
| 405 | |
| 406 | /** |
| 407 | * <p>The text offset of the end of the selection at the time editing |
| 408 | * begins; -1 if not known. Keep in mind that, without knowing the cursor |
| 409 | * position, many IMEs will not be able to offer their full feature set and |
| 410 | * may behave in unpredictable ways: pass the actual cursor position |
| 411 | * here if possible at all.</p> |
| 412 | * |
| 413 | * <p>Also, this needs to be the cursor position <strong>right now</strong>, |
| 414 | * not at some point in the past, even if input is starting in the same text field |
| 415 | * as before. When the app is filling this object, input is about to start by |
| 416 | * definition, and this value will override any value the app may have passed to |
| 417 | * {@link InputMethodManager#updateSelection(android.view.View, int, int, int, int)} |
| 418 | * before.</p> |
| 419 | */ |
| 420 | public int initialSelEnd = -1; |
| 421 | |
| 422 | /** |
| 423 | * The capitalization mode of the first character being edited in the |
| 424 | * text. Values may be any combination of |
| 425 | * {@link TextUtils#CAP_MODE_CHARACTERS TextUtils.CAP_MODE_CHARACTERS}, |
| 426 | * {@link TextUtils#CAP_MODE_WORDS TextUtils.CAP_MODE_WORDS}, and |
| 427 | * {@link TextUtils#CAP_MODE_SENTENCES TextUtils.CAP_MODE_SENTENCES}, though |
| 428 | * you should generally just take a non-zero value to mean "start out in |
| 429 | * caps mode". |
| 430 | */ |
| 431 | public int initialCapsMode = 0; |
| 432 | |
| 433 | /** |
| 434 | * The "hint" text of the text view, typically shown in-line when the |
| 435 | * text is empty to tell the user what to enter. |
| 436 | */ |
| 437 | public CharSequence hintText; |
| 438 | |
| 439 | /** |
| 440 | * A label to show to the user describing the text they are writing. |
| 441 | */ |
| 442 | public CharSequence label; |
| 443 | |
| 444 | /** |
| 445 | * Name of the package that owns this editor. |
| 446 | * |
| 447 | * <p><strong>IME authors:</strong> In API level 22 |
| 448 | * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and prior, do not trust this package |
| 449 | * name. The system had not verified the consistency between the package name here and |
| 450 | * application's uid. Consider to use {@link InputBinding#getUid()}, which is trustworthy. |
| 451 | * Starting from {@link android.os.Build.VERSION_CODES#M}, the system verifies the consistency |
| 452 | * between this package name and application uid before {@link EditorInfo} is passed to the |
| 453 | * input method.</p> |
| 454 | * |
| 455 | * <p><strong>Editor authors:</strong> Starting from {@link android.os.Build.VERSION_CODES#M}, |
| 456 | * the application is no longer |
| 457 | * able to establish input connections if the package name provided here is inconsistent with |
| 458 | * application's uid.</p> |
| 459 | */ |
| 460 | public String packageName; |
| 461 | |
| 462 | /** |
| 463 | * Autofill Id for the field that's currently on focus. |
| 464 | * |
| 465 | * <p> Marked as hide since it's only used by framework.</p> |
| 466 | * @hide |
| 467 | */ |
| 468 | public AutofillId autofillId; |
| 469 | |
| 470 | /** |
| 471 | * Identifier for the editor's field. This is optional, and may be |
| 472 | * 0. By default it is filled in with the result of |
| 473 | * {@link android.view.View#getId() View.getId()} on the View that |
| 474 | * is being edited. |
| 475 | */ |
| 476 | public int fieldId; |
| 477 | |
| 478 | /** |
| 479 | * Additional name for the editor's field. This can supply additional |
| 480 | * name information for the field. By default it is null. The actual |
| 481 | * contents have no meaning. |
| 482 | */ |
| 483 | public String fieldName; |
| 484 | |
| 485 | /** |
| 486 | * Any extra data to supply to the input method. This is for extended |
| 487 | * communication with specific input methods; the name fields in the |
| 488 | * bundle should be scoped (such as "com.mydomain.im.SOME_FIELD") so |
| 489 | * that they don't conflict with others. This field can be |
| 490 | * filled in from the {@link android.R.attr#editorExtras} |
| 491 | * attribute of a TextView. |
| 492 | */ |
| 493 | public Bundle extras; |
| 494 | |
| 495 | /** |
| 496 | * List of the languages that the user is supposed to switch to no matter what input method |
| 497 | * subtype is currently used. This special "hint" can be used mainly for, but not limited to, |
| 498 | * multilingual users who want IMEs to switch language context automatically. |
| 499 | * |
| 500 | * <p>{@code null} means that no special language "hint" is needed.</p> |
| 501 | * |
| 502 | * <p><strong>Editor authors:</strong> Specify this only when you are confident that the user |
| 503 | * will switch to certain languages in this context no matter what input method subtype is |
| 504 | * currently selected. Otherwise, keep this {@code null}. Explicit user actions and/or |
| 505 | * preferences would be good signals to specify this special "hint", For example, a chat |
| 506 | * application may be able to put the last used language at the top of {@link #hintLocales} |
| 507 | * based on whom the user is going to talk, by remembering what language is used in the last |
| 508 | * conversation. Do not specify {@link android.widget.TextView#getTextLocales()} only because |
| 509 | * it is used for text rendering.</p> |
| 510 | * |
| 511 | * @see android.widget.TextView#setImeHintLocales(LocaleList) |
| 512 | * @see android.widget.TextView#getImeHintLocales() |
| 513 | */ |
| 514 | @Nullable |
| 515 | public LocaleList hintLocales = null; |
| 516 | |
| 517 | |
| 518 | /** |
| 519 | * List of acceptable MIME types for |
| 520 | * {@link InputConnection#commitContent(InputContentInfo, int, Bundle)}. |
| 521 | * |
| 522 | * <p>{@code null} or an empty array means that |
| 523 | * {@link InputConnection#commitContent(InputContentInfo, int, Bundle)} is not supported in this |
| 524 | * editor.</p> |
| 525 | */ |
| 526 | @Nullable |
| 527 | public String[] contentMimeTypes = null; |
| 528 | |
| 529 | /** |
| 530 | * If not {@code null}, this editor needs to talk to IMEs that run for the specified user, no |
| 531 | * matter what user ID the calling process has. |
| 532 | * |
| 533 | * <p>Note also that pseudo handles such as {@link UserHandle#ALL} are not supported.</p> |
| 534 | * |
| 535 | * @hide |
| 536 | */ |
| 537 | @RequiresPermission(INTERACT_ACROSS_USERS_FULL) |
| 538 | @Nullable |
| 539 | public UserHandle targetInputMethodUser = null; |
| 540 | |
| 541 | @IntDef({TrimPolicy.HEAD, TrimPolicy.TAIL}) |
| 542 | @Retention(RetentionPolicy.SOURCE) |
| 543 | @interface TrimPolicy { |
| 544 | int HEAD = 0; |
| 545 | int TAIL = 1; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * The maximum length of initialSurroundingText. When the input text from |
| 550 | * {@code setInitialSurroundingText(CharSequence)} is longer than this, trimming shall be |
| 551 | * performed to keep memory efficiency. |
| 552 | */ |
| 553 | @VisibleForTesting |
| 554 | static final int MEMORY_EFFICIENT_TEXT_LENGTH = 2048; |
| 555 | /** |
| 556 | * When the input text is longer than {@code #MEMORY_EFFICIENT_TEXT_LENGTH}, we start trimming |
| 557 | * the input text into three parts: BeforeCursor, Selection, and AfterCursor. We don't want to |
| 558 | * trim the Selection but we also don't want it consumes all available space. Therefore, the |
| 559 | * maximum acceptable Selection length is half of {@code #MEMORY_EFFICIENT_TEXT_LENGTH}. |
| 560 | */ |
| 561 | @VisibleForTesting |
| 562 | static final int MAX_INITIAL_SELECTION_LENGTH = MEMORY_EFFICIENT_TEXT_LENGTH / 2; |
| 563 | |
| 564 | @Nullable |
| 565 | private SurroundingText mInitialSurroundingText = null; |
| 566 | |
| 567 | |
| 568 | /** |
| 569 | * Editors may use this method to provide initial input text to IMEs. As the surrounding text |
| 570 | * could be used to provide various input assistance, we recommend editors to provide the |
| 571 | * complete initial input text in its {@link View#onCreateInputConnection(EditorInfo)} callback. |
| 572 | * The supplied text will then be processed to serve {@code #getInitialTextBeforeCursor}, |
| 573 | * {@code #getInitialSelectedText}, and {@code #getInitialTextBeforeCursor}. System is allowed |
| 574 | * to trim {@code sourceText} for various reasons while keeping the most valuable data to IMEs. |
| 575 | * |
| 576 | * Starting from {@link VERSION_CODES#S}, spans that do not implement {@link Parcelable} will |
| 577 | * be automatically dropped. |
| 578 | * |
| 579 | * <p><strong>Editor authors: </strong>Providing the initial input text helps reducing IPC calls |
| 580 | * for IMEs to provide many modern features right after the connection setup. We recommend |
| 581 | * calling this method in your implementation. |
| 582 | * |
| 583 | * @param sourceText The complete input text. |
| 584 | */ |
| 585 | public void setInitialSurroundingText(@NonNull CharSequence sourceText) { |
| 586 | setInitialSurroundingSubText(sourceText, /* subTextStart = */ 0); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * An internal variant of {@link #setInitialSurroundingText(CharSequence)}. |
| 591 | * |
| 592 | * @param surroundingText {@link SurroundingText} to be set. |
| 593 | * @hide |
| 594 | */ |
| 595 | public final void setInitialSurroundingTextInternal(@NonNull SurroundingText surroundingText) { |
| 596 | mInitialSurroundingText = surroundingText; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Editors may use this method to provide initial input text to IMEs. As the surrounding text |
| 601 | * could be used to provide various input assistance, we recommend editors to provide the |
| 602 | * complete initial input text in its {@link View#onCreateInputConnection(EditorInfo)} callback. |
| 603 | * When trimming the input text is needed, call this method instead of |
| 604 | * {@code setInitialSurroundingText(CharSequence)} and provide the trimmed position info. Always |
| 605 | * try to include the selected text within {@code subText} to give the system best flexibility |
| 606 | * to choose where and how to trim {@code subText} when necessary. |
| 607 | * |
| 608 | * Starting from {@link VERSION_CODES#S}, spans that do not implement {@link Parcelable} will |
| 609 | * be automatically dropped. |
| 610 | * |
| 611 | * @param subText The input text. When it was trimmed, {@code subTextStart} must be provided |
| 612 | * correctly. |
| 613 | * @param subTextStart The position that the input text got trimmed. For example, when the |
| 614 | * editor wants to trim out the first 10 chars, subTextStart should be 10. |
| 615 | */ |
| 616 | public void setInitialSurroundingSubText(@NonNull CharSequence subText, int subTextStart) { |
| 617 | Objects.requireNonNull(subText); |
| 618 | |
| 619 | // For privacy protection reason, we don't carry password inputs to IMEs. |
| 620 | if (isPasswordInputType(inputType)) { |
| 621 | mInitialSurroundingText = null; |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | // Swap selection start and end if necessary. |
| 626 | final int subTextSelStart = initialSelStart > initialSelEnd |
| 627 | ? initialSelEnd - subTextStart : initialSelStart - subTextStart; |
| 628 | final int subTextSelEnd = initialSelStart > initialSelEnd |
| 629 | ? initialSelStart - subTextStart : initialSelEnd - subTextStart; |
| 630 | |
| 631 | final int subTextLength = subText.length(); |
| 632 | // Unknown or invalid selection. |
| 633 | if (subTextStart < 0 || subTextSelStart < 0 || subTextSelEnd > subTextLength) { |
| 634 | mInitialSurroundingText = null; |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | if (subTextLength <= MEMORY_EFFICIENT_TEXT_LENGTH) { |
| 639 | mInitialSurroundingText = new SurroundingText(subText, subTextSelStart, |
| 640 | subTextSelEnd, subTextStart); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | trimLongSurroundingText(subText, subTextSelStart, subTextSelEnd, subTextStart); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Trims the initial surrounding text when it is over sized. Fundamental trimming rules are: |
| 649 | * - The text before the cursor is the most important information to IMEs. |
| 650 | * - The text after the cursor is the second important information to IMEs. |
| 651 | * - The selected text is the least important information but it shall NEVER be truncated. When |
| 652 | * it is too long, just drop it. |
| 653 | *<p><pre> |
| 654 | * For example, the subText can be viewed as |
| 655 | * TextBeforeCursor + Selection + TextAfterCursor |
| 656 | * The result could be |
| 657 | * 1. (maybeTrimmedAtHead)TextBeforeCursor + Selection + TextAfterCursor(maybeTrimmedAtTail) |
| 658 | * 2. (maybeTrimmedAtHead)TextBeforeCursor + TextAfterCursor(maybeTrimmedAtTail)</pre> |
| 659 | * |
| 660 | * @param subText The long text that needs to be trimmed. |
| 661 | * @param selStart The text offset of the start of the selection. |
| 662 | * @param selEnd The text offset of the end of the selection |
| 663 | * @param subTextStart The position that the input text got trimmed. |
| 664 | */ |
| 665 | private void trimLongSurroundingText(CharSequence subText, int selStart, int selEnd, |
| 666 | int subTextStart) { |
| 667 | final int sourceSelLength = selEnd - selStart; |
| 668 | // When the selected text is too long, drop it. |
| 669 | final int newSelLength = (sourceSelLength > MAX_INITIAL_SELECTION_LENGTH) |
| 670 | ? 0 : sourceSelLength; |
| 671 | |
| 672 | // Distribute rest of length quota to TextBeforeCursor and TextAfterCursor in 4:1 ratio. |
| 673 | final int subTextBeforeCursorLength = selStart; |
| 674 | final int subTextAfterCursorLength = subText.length() - selEnd; |
| 675 | final int maxLengthMinusSelection = MEMORY_EFFICIENT_TEXT_LENGTH - newSelLength; |
| 676 | final int possibleMaxBeforeCursorLength = |
| 677 | Math.min(subTextBeforeCursorLength, (int) (0.8 * maxLengthMinusSelection)); |
| 678 | int newAfterCursorLength = Math.min(subTextAfterCursorLength, |
| 679 | maxLengthMinusSelection - possibleMaxBeforeCursorLength); |
| 680 | int newBeforeCursorLength = Math.min(subTextBeforeCursorLength, |
| 681 | maxLengthMinusSelection - newAfterCursorLength); |
| 682 | |
| 683 | // As trimming may happen at the head of TextBeforeCursor, calculate new starting position. |
| 684 | int newBeforeCursorHead = subTextBeforeCursorLength - newBeforeCursorLength; |
| 685 | |
| 686 | // We don't want to cut surrogate pairs in the middle. Exam that at the new head and tail. |
| 687 | if (isCutOnSurrogate(subText, |
| 688 | selStart - newBeforeCursorLength, TrimPolicy.HEAD)) { |
| 689 | newBeforeCursorHead = newBeforeCursorHead + 1; |
| 690 | newBeforeCursorLength = newBeforeCursorLength - 1; |
| 691 | } |
| 692 | if (isCutOnSurrogate(subText, |
| 693 | selEnd + newAfterCursorLength - 1, TrimPolicy.TAIL)) { |
| 694 | newAfterCursorLength = newAfterCursorLength - 1; |
| 695 | } |
| 696 | |
| 697 | // Now we know where to trim, compose the initialSurroundingText. |
| 698 | final int newTextLength = newBeforeCursorLength + newSelLength + newAfterCursorLength; |
| 699 | final CharSequence newInitialSurroundingText; |
| 700 | if (newSelLength != sourceSelLength) { |
| 701 | final CharSequence beforeCursor = subText.subSequence(newBeforeCursorHead, |
| 702 | newBeforeCursorHead + newBeforeCursorLength); |
| 703 | final CharSequence afterCursor = subText.subSequence(selEnd, |
| 704 | selEnd + newAfterCursorLength); |
| 705 | |
| 706 | newInitialSurroundingText = TextUtils.concat(beforeCursor, afterCursor); |
| 707 | } else { |
| 708 | newInitialSurroundingText = subText |
| 709 | .subSequence(newBeforeCursorHead, newBeforeCursorHead + newTextLength); |
| 710 | } |
| 711 | |
| 712 | // As trimming may happen at the head, adjust cursor position in the initialSurroundingText |
| 713 | // obj. |
| 714 | newBeforeCursorHead = 0; |
| 715 | final int newSelHead = newBeforeCursorHead + newBeforeCursorLength; |
| 716 | final int newOffset = subTextStart + selStart - newSelHead; |
| 717 | mInitialSurroundingText = new SurroundingText( |
| 718 | newInitialSurroundingText, newSelHead, newSelHead + newSelLength, |
| 719 | newOffset); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | /** |
| 724 | * Get <var>length</var> characters of text before the current cursor position. May be |
| 725 | * {@code null} when the protocol is not supported. |
| 726 | * |
| 727 | * @param length The expected length of the text. |
| 728 | * @param flags Supplies additional options controlling how the text is returned. May be |
| 729 | * either 0 or {@link InputConnection#GET_TEXT_WITH_STYLES}. |
| 730 | * @return the text before the cursor position; the length of the returned text might be less |
| 731 | * than <var>length</var>. When there is no text before the cursor, an empty string will be |
| 732 | * returned. It could also be {@code null} when the editor or system could not support this |
| 733 | * protocol. |
| 734 | */ |
| 735 | @Nullable |
| 736 | public CharSequence getInitialTextBeforeCursor( |
| 737 | @IntRange(from = 0) int length, @InputConnection.GetTextType int flags) { |
| 738 | if (mInitialSurroundingText == null) { |
| 739 | return null; |
| 740 | } |
| 741 | |
| 742 | int selStart = Math.min(mInitialSurroundingText.getSelectionStart(), |
| 743 | mInitialSurroundingText.getSelectionEnd()); |
| 744 | int n = Math.min(length, selStart); |
| 745 | return ((flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) |
| 746 | ? mInitialSurroundingText.getText().subSequence(selStart - n, selStart) |
| 747 | : TextUtils.substring(mInitialSurroundingText.getText(), selStart - n, |
| 748 | selStart); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Gets the selected text, if any. May be {@code null} when the protocol is not supported or the |
| 753 | * selected text is way too long. |
| 754 | * |
| 755 | * @param flags Supplies additional options controlling how the text is returned. May be |
| 756 | * either 0 or {@link InputConnection#GET_TEXT_WITH_STYLES}. |
| 757 | * @return the text that is currently selected, if any. It could be an empty string when there |
| 758 | * is no text selected. When {@code null} is returned, the selected text might be too long or |
| 759 | * this protocol is not supported. |
| 760 | */ |
| 761 | @Nullable |
| 762 | public CharSequence getInitialSelectedText(@InputConnection.GetTextType int flags) { |
| 763 | if (mInitialSurroundingText == null) { |
| 764 | return null; |
| 765 | } |
| 766 | |
| 767 | // Swap selection start and end if necessary. |
| 768 | final int correctedTextSelStart = initialSelStart > initialSelEnd |
| 769 | ? initialSelEnd : initialSelStart; |
| 770 | final int correctedTextSelEnd = initialSelStart > initialSelEnd |
| 771 | ? initialSelStart : initialSelEnd; |
| 772 | |
| 773 | final int sourceSelLength = correctedTextSelEnd - correctedTextSelStart; |
| 774 | int selStart = mInitialSurroundingText.getSelectionStart(); |
| 775 | int selEnd = mInitialSurroundingText.getSelectionEnd(); |
| 776 | if (selStart > selEnd) { |
| 777 | int tmp = selStart; |
| 778 | selStart = selEnd; |
| 779 | selEnd = tmp; |
| 780 | } |
| 781 | final int selLength = selEnd - selStart; |
| 782 | if (initialSelStart < 0 || initialSelEnd < 0 || selLength != sourceSelLength) { |
| 783 | return null; |
| 784 | } |
| 785 | |
| 786 | return ((flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) |
| 787 | ? mInitialSurroundingText.getText().subSequence(selStart, selEnd) |
| 788 | : TextUtils.substring(mInitialSurroundingText.getText(), selStart, selEnd); |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Get <var>length</var> characters of text after the current cursor position. May be |
| 793 | * {@code null} when the protocol is not supported. |
| 794 | * |
| 795 | * @param length The expected length of the text. |
| 796 | * @param flags Supplies additional options controlling how the text is returned. May be |
| 797 | * either 0 or {@link InputConnection#GET_TEXT_WITH_STYLES}. |
| 798 | * @return the text after the cursor position; the length of the returned text might be less |
| 799 | * than <var>length</var>. When there is no text after the cursor, an empty string will be |
| 800 | * returned. It could also be {@code null} when the editor or system could not support this |
| 801 | * protocol. |
| 802 | */ |
| 803 | @Nullable |
| 804 | public CharSequence getInitialTextAfterCursor( |
| 805 | @IntRange(from = 0) int length, @InputConnection.GetTextType int flags) { |
| 806 | if (mInitialSurroundingText == null) { |
| 807 | return null; |
| 808 | } |
| 809 | |
| 810 | int surroundingTextLength = mInitialSurroundingText.getText().length(); |
| 811 | int selEnd = Math.max(mInitialSurroundingText.getSelectionStart(), |
| 812 | mInitialSurroundingText.getSelectionEnd()); |
| 813 | int n = Math.min(length, surroundingTextLength - selEnd); |
| 814 | return ((flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) |
| 815 | ? mInitialSurroundingText.getText().subSequence(selEnd, selEnd + n) |
| 816 | : TextUtils.substring(mInitialSurroundingText.getText(), selEnd, selEnd + n); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Gets the surrounding text around the current cursor, with <var>beforeLength</var> characters |
| 821 | * of text before the cursor (start of the selection), <var>afterLength</var> characters of text |
| 822 | * after the cursor (end of the selection), and all of the selected text. |
| 823 | * |
| 824 | * <p>The initial surrounding text for return could be trimmed if oversize. Fundamental trimming |
| 825 | * rules are:</p> |
| 826 | * <ul> |
| 827 | * <li>The text before the cursor is the most important information to IMEs.</li> |
| 828 | * <li>The text after the cursor is the second important information to IMEs.</li> |
| 829 | * <li>The selected text is the least important information but it shall NEVER be truncated. |
| 830 | * When it is too long, just drop it.</li> |
| 831 | * </ul> |
| 832 | * |
| 833 | * <p>For example, the subText can be viewed as TextBeforeCursor + Selection + TextAfterCursor. |
| 834 | * The result could be:</p> |
| 835 | * <ol> |
| 836 | * <li>(maybeTrimmedAtHead)TextBeforeCursor + Selection |
| 837 | * + TextAfterCursor(maybeTrimmedAtTail)</li> |
| 838 | * <li>(maybeTrimmedAtHead)TextBeforeCursor + TextAfterCursor(maybeTrimmedAtTail)</li> |
| 839 | * </ol> |
| 840 | * |
| 841 | * @param beforeLength The expected length of the text before the cursor. |
| 842 | * @param afterLength The expected length of the text after the cursor. |
| 843 | * @param flags Supplies additional options controlling how the text is returned. May be either |
| 844 | * {@code 0} or {@link InputConnection#GET_TEXT_WITH_STYLES}. |
| 845 | * @return an {@link android.view.inputmethod.SurroundingText} object describing the surrounding |
| 846 | * text and state of selection, or {@code null} if the editor or system could not support this |
| 847 | * protocol. |
| 848 | * @throws IllegalArgumentException if {@code beforeLength} or {@code afterLength} is negative. |
| 849 | */ |
| 850 | @Nullable |
| 851 | public SurroundingText getInitialSurroundingText( |
| 852 | @IntRange(from = 0) int beforeLength, @IntRange(from = 0) int afterLength, |
| 853 | @InputConnection.GetTextType int flags) { |
| 854 | Preconditions.checkArgumentNonnegative(beforeLength); |
| 855 | Preconditions.checkArgumentNonnegative(afterLength); |
| 856 | |
| 857 | if (mInitialSurroundingText == null) { |
| 858 | return null; |
| 859 | } |
| 860 | |
| 861 | int length = mInitialSurroundingText.getText().length(); |
| 862 | int selStart = mInitialSurroundingText.getSelectionStart(); |
| 863 | int selEnd = mInitialSurroundingText.getSelectionEnd(); |
| 864 | if (selStart > selEnd) { |
| 865 | int tmp = selStart; |
| 866 | selStart = selEnd; |
| 867 | selEnd = tmp; |
| 868 | } |
| 869 | |
| 870 | int before = Math.min(beforeLength, selStart); |
| 871 | int after = Math.min(selEnd + afterLength, length); |
| 872 | int offset = selStart - before; |
| 873 | CharSequence newText = ((flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) |
| 874 | ? mInitialSurroundingText.getText().subSequence(offset, after) |
| 875 | : TextUtils.substring(mInitialSurroundingText.getText(), offset, after); |
| 876 | int newSelEnd = Math.min(selEnd - offset, length); |
| 877 | return new SurroundingText(newText, before, newSelEnd, |
| 878 | mInitialSurroundingText.getOffset() + offset); |
| 879 | } |
| 880 | |
| 881 | private static boolean isCutOnSurrogate(CharSequence sourceText, int cutPosition, |
| 882 | @TrimPolicy int policy) { |
| 883 | switch (policy) { |
| 884 | case TrimPolicy.HEAD: |
| 885 | return Character.isLowSurrogate(sourceText.charAt(cutPosition)); |
| 886 | case TrimPolicy.TAIL: |
| 887 | return Character.isHighSurrogate(sourceText.charAt(cutPosition)); |
| 888 | default: |
| 889 | return false; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | private static boolean isPasswordInputType(int inputType) { |
| 894 | final int variation = |
| 895 | inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); |
| 896 | return variation |
| 897 | == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD) |
| 898 | || variation |
| 899 | == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD) |
| 900 | || variation |
| 901 | == (TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD); |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * Ensure that the data in this EditorInfo is compatible with an application |
| 906 | * that was developed against the given target API version. This can |
| 907 | * impact the following input types: |
| 908 | * {@link InputType#TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS}, |
| 909 | * {@link InputType#TYPE_TEXT_VARIATION_WEB_PASSWORD}, |
| 910 | * {@link InputType#TYPE_NUMBER_VARIATION_NORMAL}, |
| 911 | * {@link InputType#TYPE_NUMBER_VARIATION_PASSWORD}. |
| 912 | * |
| 913 | * <p>This is called by the framework for input method implementations; |
| 914 | * you should not generally need to call it yourself. |
| 915 | * |
| 916 | * @param targetSdkVersion The API version number that the compatible |
| 917 | * application was developed against. |
| 918 | */ |
| 919 | public final void makeCompatible(int targetSdkVersion) { |
| 920 | if (targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 921 | switch (inputType&(TYPE_MASK_CLASS|TYPE_MASK_VARIATION)) { |
| 922 | case TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS: |
| 923 | inputType = TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
| 924 | | (inputType&TYPE_MASK_FLAGS); |
| 925 | break; |
| 926 | case TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_WEB_PASSWORD: |
| 927 | inputType = TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_PASSWORD |
| 928 | | (inputType&TYPE_MASK_FLAGS); |
| 929 | break; |
| 930 | case TYPE_CLASS_NUMBER|TYPE_NUMBER_VARIATION_NORMAL: |
| 931 | case TYPE_CLASS_NUMBER|TYPE_NUMBER_VARIATION_PASSWORD: |
| 932 | inputType = TYPE_CLASS_NUMBER |
| 933 | | (inputType&TYPE_MASK_FLAGS); |
| 934 | break; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Export the state of {@link EditorInfo} into a protocol buffer output stream. |
| 941 | * |
| 942 | * @param proto Stream to write the state to |
| 943 | * @param fieldId FieldId of ViewRootImpl as defined in the parent message |
| 944 | * @hide |
| 945 | */ |
| 946 | public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 947 | final long token = proto.start(fieldId); |
| 948 | proto.write(INPUT_TYPE, inputType); |
| 949 | proto.write(IME_OPTIONS, imeOptions); |
| 950 | proto.write(PRIVATE_IME_OPTIONS, privateImeOptions); |
| 951 | proto.write(PACKAGE_NAME, packageName); |
| 952 | proto.write(FIELD_ID, this.fieldId); |
| 953 | if (targetInputMethodUser != null) { |
| 954 | proto.write(TARGET_INPUT_METHOD_USER_ID, targetInputMethodUser.getIdentifier()); |
| 955 | } |
| 956 | proto.end(token); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Write debug output of this object. |
| 961 | */ |
| 962 | public void dump(Printer pw, String prefix) { |
| 963 | pw.println(prefix + "inputType=0x" + Integer.toHexString(inputType) |
| 964 | + " imeOptions=0x" + Integer.toHexString(imeOptions) |
| 965 | + " privateImeOptions=" + privateImeOptions); |
| 966 | pw.println(prefix + "actionLabel=" + actionLabel |
| 967 | + " actionId=" + actionId); |
| 968 | pw.println(prefix + "initialSelStart=" + initialSelStart |
| 969 | + " initialSelEnd=" + initialSelEnd |
| 970 | + " initialCapsMode=0x" |
| 971 | + Integer.toHexString(initialCapsMode)); |
| 972 | pw.println(prefix + "hintText=" + hintText |
| 973 | + " label=" + label); |
| 974 | pw.println(prefix + "packageName=" + packageName |
| 975 | + " autofillId=" + autofillId |
| 976 | + " fieldId=" + fieldId |
| 977 | + " fieldName=" + fieldName); |
| 978 | pw.println(prefix + "extras=" + extras); |
| 979 | pw.println(prefix + "hintLocales=" + hintLocales); |
| 980 | pw.println(prefix + "contentMimeTypes=" + Arrays.toString(contentMimeTypes)); |
| 981 | if (targetInputMethodUser != null) { |
| 982 | pw.println(prefix + "targetInputMethodUserId=" + targetInputMethodUser.getIdentifier()); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * @return A deep copy of {@link EditorInfo}. |
| 988 | * @hide |
| 989 | */ |
| 990 | @NonNull |
| 991 | public final EditorInfo createCopyInternal() { |
| 992 | final EditorInfo newEditorInfo = new EditorInfo(); |
| 993 | newEditorInfo.inputType = inputType; |
| 994 | newEditorInfo.imeOptions = imeOptions; |
| 995 | newEditorInfo.privateImeOptions = privateImeOptions; |
| 996 | newEditorInfo.internalImeOptions = internalImeOptions; |
| 997 | newEditorInfo.actionLabel = TextUtils.stringOrSpannedString(actionLabel); |
| 998 | newEditorInfo.actionId = actionId; |
| 999 | newEditorInfo.initialSelStart = initialSelStart; |
| 1000 | newEditorInfo.initialSelEnd = initialSelEnd; |
| 1001 | newEditorInfo.initialCapsMode = initialCapsMode; |
| 1002 | newEditorInfo.hintText = TextUtils.stringOrSpannedString(hintText); |
| 1003 | newEditorInfo.label = TextUtils.stringOrSpannedString(label); |
| 1004 | newEditorInfo.packageName = packageName; |
| 1005 | newEditorInfo.autofillId = autofillId; |
| 1006 | newEditorInfo.fieldId = fieldId; |
| 1007 | newEditorInfo.fieldName = fieldName; |
| 1008 | newEditorInfo.extras = extras != null ? extras.deepCopy() : null; |
| 1009 | newEditorInfo.mInitialSurroundingText = mInitialSurroundingText; |
| 1010 | newEditorInfo.hintLocales = hintLocales; |
| 1011 | newEditorInfo.contentMimeTypes = ArrayUtils.cloneOrNull(contentMimeTypes); |
| 1012 | return newEditorInfo; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Used to package this object into a {@link Parcel}. |
| 1017 | * |
| 1018 | * @param dest The {@link Parcel} to be written. |
| 1019 | * @param flags The flags used for parceling. |
| 1020 | */ |
| 1021 | public void writeToParcel(Parcel dest, int flags) { |
| 1022 | dest.writeInt(inputType); |
| 1023 | dest.writeInt(imeOptions); |
| 1024 | dest.writeString(privateImeOptions); |
| 1025 | dest.writeInt(internalImeOptions); |
| 1026 | TextUtils.writeToParcel(actionLabel, dest, flags); |
| 1027 | dest.writeInt(actionId); |
| 1028 | dest.writeInt(initialSelStart); |
| 1029 | dest.writeInt(initialSelEnd); |
| 1030 | dest.writeInt(initialCapsMode); |
| 1031 | TextUtils.writeToParcel(hintText, dest, flags); |
| 1032 | TextUtils.writeToParcel(label, dest, flags); |
| 1033 | dest.writeString(packageName); |
| 1034 | dest.writeParcelable(autofillId, flags); |
| 1035 | dest.writeInt(fieldId); |
| 1036 | dest.writeString(fieldName); |
| 1037 | dest.writeBundle(extras); |
| 1038 | dest.writeBoolean(mInitialSurroundingText != null); |
| 1039 | if (mInitialSurroundingText != null) { |
| 1040 | mInitialSurroundingText.writeToParcel(dest, flags); |
| 1041 | } |
| 1042 | if (hintLocales != null) { |
| 1043 | hintLocales.writeToParcel(dest, flags); |
| 1044 | } else { |
| 1045 | LocaleList.getEmptyLocaleList().writeToParcel(dest, flags); |
| 1046 | } |
| 1047 | dest.writeStringArray(contentMimeTypes); |
| 1048 | UserHandle.writeToParcel(targetInputMethodUser, dest); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Used to make this class parcelable. |
| 1053 | */ |
| 1054 | public static final @android.annotation.NonNull Parcelable.Creator<EditorInfo> CREATOR = |
| 1055 | new Parcelable.Creator<EditorInfo>() { |
| 1056 | public EditorInfo createFromParcel(Parcel source) { |
| 1057 | EditorInfo res = new EditorInfo(); |
| 1058 | res.inputType = source.readInt(); |
| 1059 | res.imeOptions = source.readInt(); |
| 1060 | res.privateImeOptions = source.readString(); |
| 1061 | res.internalImeOptions = source.readInt(); |
| 1062 | res.actionLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
| 1063 | res.actionId = source.readInt(); |
| 1064 | res.initialSelStart = source.readInt(); |
| 1065 | res.initialSelEnd = source.readInt(); |
| 1066 | res.initialCapsMode = source.readInt(); |
| 1067 | res.hintText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
| 1068 | res.label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
| 1069 | res.packageName = source.readString(); |
| 1070 | res.autofillId = source.readParcelable(AutofillId.class.getClassLoader(), android.view.autofill.AutofillId.class); |
| 1071 | res.fieldId = source.readInt(); |
| 1072 | res.fieldName = source.readString(); |
| 1073 | res.extras = source.readBundle(); |
| 1074 | boolean hasInitialSurroundingText = source.readBoolean(); |
| 1075 | if (hasInitialSurroundingText) { |
| 1076 | res.mInitialSurroundingText = |
| 1077 | SurroundingText.CREATOR.createFromParcel(source); |
| 1078 | } |
| 1079 | LocaleList hintLocales = LocaleList.CREATOR.createFromParcel(source); |
| 1080 | res.hintLocales = hintLocales.isEmpty() ? null : hintLocales; |
| 1081 | res.contentMimeTypes = source.readStringArray(); |
| 1082 | res.targetInputMethodUser = UserHandle.readFromParcel(source); |
| 1083 | return res; |
| 1084 | } |
| 1085 | |
| 1086 | public EditorInfo[] newArray(int size) { |
| 1087 | return new EditorInfo[size]; |
| 1088 | } |
| 1089 | }; |
| 1090 | |
| 1091 | public int describeContents() { |
| 1092 | return 0; |
| 1093 | } |
| 1094 | } |