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 android.os.Bundle;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.text.InputType;
23 import android.text.TextUtils;
24 import android.util.Printer;
25 
26 /**
27  * An EditorInfo describes several attributes of a text editing object
28  * that an input method is communicating with (typically an EditText), most
29  * importantly the type of text content it contains and the current cursor position.
30  */
31 public class EditorInfo implements InputType, Parcelable {
32     /**
33      * The content type of the text box, whose bits are defined by
34      * {@link InputType}.
35      *
36      * @see InputType
37      * @see #TYPE_MASK_CLASS
38      * @see #TYPE_MASK_VARIATION
39      * @see #TYPE_MASK_FLAGS
40      */
41     public int inputType = TYPE_NULL;
42 
43     /**
44      * Set of bits in {@link #imeOptions} that provide alternative actions
45      * associated with the "enter" key.  This both helps the IME provide
46      * better feedback about what the enter key will do, and also allows it
47      * to provide alternative mechanisms for providing that command.
48      */
49     public static final int IME_MASK_ACTION = 0x000000ff;
50 
51     /**
52      * Bits of {@link #IME_MASK_ACTION}: no specific action has been
53      * associated with this editor, let the editor come up with its own if
54      * it can.
55      */
56     public static final int IME_ACTION_UNSPECIFIED = 0x00000000;
57 
58     /**
59      * Bits of {@link #IME_MASK_ACTION}: there is no available action.
60      */
61     public static final int IME_ACTION_NONE = 0x00000001;
62 
63     /**
64      * Bits of {@link #IME_MASK_ACTION}: the action key performs a "go"
65      * operation to take the user to the target of the text they typed.
66      * Typically used, for example, when entering a URL.
67      */
68     public static final int IME_ACTION_GO = 0x00000002;
69 
70     /**
71      * Bits of {@link #IME_MASK_ACTION}: the action key performs a "search"
72      * operation, taking the user to the results of searching for the text
73      * they have typed (in whatever context is appropriate).
74      */
75     public static final int IME_ACTION_SEARCH = 0x00000003;
76 
77     /**
78      * Bits of {@link #IME_MASK_ACTION}: the action key performs a "send"
79      * operation, delivering the text to its target.  This is typically used
80      * when composing a message in IM or SMS where sending is immediate.
81      */
82     public static final int IME_ACTION_SEND = 0x00000004;
83 
84     /**
85      * Bits of {@link #IME_MASK_ACTION}: the action key performs a "next"
86      * operation, taking the user to the next field that will accept text.
87      */
88     public static final int IME_ACTION_NEXT = 0x00000005;
89 
90     /**
91      * Bits of {@link #IME_MASK_ACTION}: the action key performs a "done"
92      * operation, typically meaning there is nothing more to input and the
93      * IME will be closed.
94      */
95     public static final int IME_ACTION_DONE = 0x00000006;
96 
97     /**
98      * Bits of {@link #IME_MASK_ACTION}: like {@link #IME_ACTION_NEXT}, but
99      * for moving to the previous field.  This will normally not be used to
100      * specify an action (since it precludes {@link #IME_ACTION_NEXT}), but
101      * can be returned to the app if it sets {@link #IME_FLAG_NAVIGATE_PREVIOUS}.
102      */
103     public static final int IME_ACTION_PREVIOUS = 0x00000007;
104 
105     /**
106      * Flag of {@link #imeOptions}: used to request that the IME never go
107      * into fullscreen mode.
108      * By default, IMEs may go into full screen mode when they think
109      * it's appropriate, for example on small screens in landscape
110      * orientation where displaying a software keyboard may occlude
111      * such a large portion of the screen that the remaining part is
112      * too small to meaningfully display the application UI.
113      * If this flag is set, compliant IMEs will never go into full screen mode,
114      * and always leave some space to display the application UI.
115      * Applications need to be aware that the flag is not a guarantee, and
116      * some IMEs may ignore it.
117      */
118     public static final int IME_FLAG_NO_FULLSCREEN = 0x2000000;
119 
120     /**
121      * Flag of {@link #imeOptions}: like {@link #IME_FLAG_NAVIGATE_NEXT}, but
122      * specifies there is something interesting that a backward navigation
123      * can focus on.  If the user selects the IME's facility to backward
124      * navigate, this will show up in the application as an {@link #IME_ACTION_PREVIOUS}
125      * at {@link InputConnection#performEditorAction(int)
126      * InputConnection.performEditorAction(int)}.
127      */
128     public static final int IME_FLAG_NAVIGATE_PREVIOUS = 0x4000000;
129 
130     /**
131      * Flag of {@link #imeOptions}: used to specify that there is something
132      * interesting that a forward navigation can focus on. This is like using
133      * {@link #IME_ACTION_NEXT}, except allows the IME to be multiline (with
134      * an enter key) as well as provide forward navigation.  Note that some
135      * IMEs may not be able to do this, especially when running on a small
136      * screen where there is little space.  In that case it does not need to
137      * present a UI for this option.  Like {@link #IME_ACTION_NEXT}, if the
138      * user selects the IME's facility to forward navigate, this will show up
139      * in the application at {@link InputConnection#performEditorAction(int)
140      * InputConnection.performEditorAction(int)}.
141      */
142     public static final int IME_FLAG_NAVIGATE_NEXT = 0x8000000;
143 
144     /**
145      * Flag of {@link #imeOptions}: used to specify that the IME does not need
146      * to show its extracted text UI.  For input methods that may be fullscreen,
147      * often when in landscape mode, this allows them to be smaller and let part
148      * of the application be shown behind, through transparent UI parts in the
149      * fullscreen IME. The part of the UI visible to the user may not be responsive
150      * to touch because the IME will receive touch events, which may confuse the
151      * user; use {@link #IME_FLAG_NO_FULLSCREEN} instead for a better experience.
152      * Using this flag is discouraged and it may become deprecated in the future.
153      * Its meaning is unclear in some situations and it may not work appropriately
154      * on older versions of the platform.
155      */
156     public static final int IME_FLAG_NO_EXTRACT_UI = 0x10000000;
157 
158     /**
159      * Flag of {@link #imeOptions}: used in conjunction with one of the actions
160      * masked by {@link #IME_MASK_ACTION}, this indicates that the action
161      * should not be available as an accessory button on the right of the extracted
162      * text when the input method is full-screen. Note that by setting this flag,
163      * there can be cases where the action is simply never available to the
164      * user. Setting this generally means that you think that in fullscreen mode,
165      * where there is little space to show the text, it's not worth taking some
166      * screen real estate to display the action and it should be used instead
167      * to show more text.
168      */
169     public static final int IME_FLAG_NO_ACCESSORY_ACTION = 0x20000000;
170 
171     /**
172      * Flag of {@link #imeOptions}: used in conjunction with one of the actions
173      * masked by {@link #IME_MASK_ACTION}. If this flag is not set, IMEs will
174      * normally replace the "enter" key with the action supplied. This flag
175      * indicates that the action should not be available in-line as a replacement
176      * for the "enter" key. Typically this is because the action has such a
177      * significant impact or is not recoverable enough that accidentally hitting
178      * it should be avoided, such as sending a message. Note that
179      * {@link android.widget.TextView} will automatically set this flag for you
180      * on multi-line text views.
181      */
182     public static final int IME_FLAG_NO_ENTER_ACTION = 0x40000000;
183 
184     /**
185      * Flag of {@link #imeOptions}: used to request an IME that is capable of
186      * inputting ASCII characters.  The intention of this flag is to ensure that
187      * the user can type Roman alphabet characters in a {@link android.widget.TextView}.
188      * It is typically used for an account ID or password input. A lot of the time,
189      * IMEs are already able to input ASCII even without being told so (such IMEs
190      * already respect this flag in a sense), but there are cases when this is not
191      * the default. For instance, users of languages using a different script like
192      * Arabic, Greek, Hebrew or Russian typically have a keyboard that can't
193      * input ASCII characters by default. Applications need to be
194      * aware that the flag is not a guarantee, and some IMEs may not respect it.
195      * However, it is strongly recommended for IME authors to respect this flag
196      * especially when their IME could end up with a state where only languages
197      * using non-ASCII are enabled.
198      */
199     public static final int IME_FLAG_FORCE_ASCII = 0x80000000;
200 
201     /**
202      * Generic unspecified type for {@link #imeOptions}.
203      */
204     public static final int IME_NULL = 0x00000000;
205 
206     /**
207      * Extended type information for the editor, to help the IME better
208      * integrate with it.
209      */
210     public int imeOptions = IME_NULL;
211 
212     /**
213      * A string supplying additional information options that are
214      * private to a particular IME implementation.  The string must be
215      * scoped to a package owned by the implementation, to ensure there are
216      * no conflicts between implementations, but other than that you can put
217      * whatever you want in it to communicate with the IME.  For example,
218      * you could have a string that supplies an argument like
219      * <code>"com.example.myapp.SpecialMode=3"</code>.  This field is can be
220      * filled in from the {@link android.R.attr#privateImeOptions}
221      * attribute of a TextView.
222      */
223     public String privateImeOptions = null;
224 
225     /**
226      * In some cases an IME may be able to display an arbitrary label for
227      * a command the user can perform, which you can specify here. This is
228      * typically used as the label for the action to use in-line as a replacement
229      * for the "enter" key (see {@link #actionId}). Remember the key where
230      * this will be displayed is typically very small, and there are significant
231      * localization challenges to make this fit in all supported languages. Also
232      * you can not count absolutely on this being used, as some IMEs may
233      * ignore this.
234      */
235     public CharSequence actionLabel = null;
236 
237     /**
238      * If {@link #actionLabel} has been given, this is the id for that command
239      * when the user presses its button that is delivered back with
240      * {@link InputConnection#performEditorAction(int)
241      * InputConnection.performEditorAction()}.
242      */
243     public int actionId = 0;
244 
245     /**
246      * The text offset of the start of the selection at the time editing
247      * begins; -1 if not known. Keep in mind that, without knowing the cursor
248      * position, many IMEs will not be able to offer their full feature set and
249      * may even behave in unpredictable ways: pass the actual cursor position
250      * here if possible at all.
251      *
252      * <p>Also, this needs to be the cursor position <strong>right now</strong>,
253      * not at some point in the past, even if input is starting in the same text field
254      * as before. When the app is filling this object, input is about to start by
255      * definition, and this value will override any value the app may have passed to
256      * {@link InputMethodManager#updateSelection(android.view.View, int, int, int, int)}
257      * before.</p>
258      */
259     public int initialSelStart = -1;
260 
261     /**
262      * <p>The text offset of the end of the selection at the time editing
263      * begins; -1 if not known. Keep in mind that, without knowing the cursor
264      * position, many IMEs will not be able to offer their full feature set and
265      * may behave in unpredictable ways: pass the actual cursor position
266      * here if possible at all.</p>
267      *
268      * <p>Also, this needs to be the cursor position <strong>right now</strong>,
269      * not at some point in the past, even if input is starting in the same text field
270      * as before. When the app is filling this object, input is about to start by
271      * definition, and this value will override any value the app may have passed to
272      * {@link InputMethodManager#updateSelection(android.view.View, int, int, int, int)}
273      * before.</p>
274      */
275     public int initialSelEnd = -1;
276 
277     /**
278      * The capitalization mode of the first character being edited in the
279      * text.  Values may be any combination of
280      * {@link TextUtils#CAP_MODE_CHARACTERS TextUtils.CAP_MODE_CHARACTERS},
281      * {@link TextUtils#CAP_MODE_WORDS TextUtils.CAP_MODE_WORDS}, and
282      * {@link TextUtils#CAP_MODE_SENTENCES TextUtils.CAP_MODE_SENTENCES}, though
283      * you should generally just take a non-zero value to mean "start out in
284      * caps mode".
285      */
286     public int initialCapsMode = 0;
287 
288     /**
289      * The "hint" text of the text view, typically shown in-line when the
290      * text is empty to tell the user what to enter.
291      */
292     public CharSequence hintText;
293 
294     /**
295      * A label to show to the user describing the text they are writing.
296      */
297     public CharSequence label;
298 
299     /**
300      * Name of the package that owns this editor.
301      *
302      * <p><strong>IME authors:</strong> In API level 22
303      * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and prior, do not trust this package
304      * name. The system had not verified the consistency between the package name here and
305      * application's uid. Consider to use {@link InputBinding#getUid()}, which is trustworthy.
306      * Starting from {@link android.os.Build.VERSION_CODES#M}, the system verifies the consistency
307      * between this package name and application uid before {@link EditorInfo} is passed to the
308      * input method.</p>
309      *
310      * <p><strong>Editor authors:</strong> Starting from {@link android.os.Build.VERSION_CODES#M},
311      * the application is no longer
312      * able to establish input connections if the package name provided here is inconsistent with
313      * application's uid.</p>
314      */
315     public String packageName;
316 
317     /**
318      * Identifier for the editor's field.  This is optional, and may be
319      * 0.  By default it is filled in with the result of
320      * {@link android.view.View#getId() View.getId()} on the View that
321      * is being edited.
322      */
323     public int fieldId;
324 
325     /**
326      * Additional name for the editor's field.  This can supply additional
327      * name information for the field.  By default it is null.  The actual
328      * contents have no meaning.
329      */
330     public String fieldName;
331 
332     /**
333      * Any extra data to supply to the input method.  This is for extended
334      * communication with specific input methods; the name fields in the
335      * bundle should be scoped (such as "com.mydomain.im.SOME_FIELD") so
336      * that they don't conflict with others.  This field can be
337      * filled in from the {@link android.R.attr#editorExtras}
338      * attribute of a TextView.
339      */
340     public Bundle extras;
341 
342     /**
343      * Ensure that the data in this EditorInfo is compatible with an application
344      * that was developed against the given target API version.  This can
345      * impact the following input types:
346      * {@link InputType#TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS},
347      * {@link InputType#TYPE_TEXT_VARIATION_WEB_PASSWORD},
348      * {@link InputType#TYPE_NUMBER_VARIATION_NORMAL},
349      * {@link InputType#TYPE_NUMBER_VARIATION_PASSWORD}.
350      *
351      * <p>This is called by the framework for input method implementations;
352      * you should not generally need to call it yourself.
353      *
354      * @param targetSdkVersion The API version number that the compatible
355      * application was developed against.
356      */
makeCompatible(int targetSdkVersion)357     public final void makeCompatible(int targetSdkVersion) {
358         if (targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
359             switch (inputType&(TYPE_MASK_CLASS|TYPE_MASK_VARIATION)) {
360                 case TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS:
361                     inputType = TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_EMAIL_ADDRESS
362                             | (inputType&TYPE_MASK_FLAGS);
363                     break;
364                 case TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_WEB_PASSWORD:
365                     inputType = TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_PASSWORD
366                             | (inputType&TYPE_MASK_FLAGS);
367                     break;
368                 case TYPE_CLASS_NUMBER|TYPE_NUMBER_VARIATION_NORMAL:
369                 case TYPE_CLASS_NUMBER|TYPE_NUMBER_VARIATION_PASSWORD:
370                     inputType = TYPE_CLASS_NUMBER
371                             | (inputType&TYPE_MASK_FLAGS);
372                     break;
373             }
374         }
375     }
376 
377     /**
378      * Write debug output of this object.
379      */
dump(Printer pw, String prefix)380     public void dump(Printer pw, String prefix) {
381         pw.println(prefix + "inputType=0x" + Integer.toHexString(inputType)
382                 + " imeOptions=0x" + Integer.toHexString(imeOptions)
383                 + " privateImeOptions=" + privateImeOptions);
384         pw.println(prefix + "actionLabel=" + actionLabel
385                 + " actionId=" + actionId);
386         pw.println(prefix + "initialSelStart=" + initialSelStart
387                 + " initialSelEnd=" + initialSelEnd
388                 + " initialCapsMode=0x"
389                 + Integer.toHexString(initialCapsMode));
390         pw.println(prefix + "hintText=" + hintText
391                 + " label=" + label);
392         pw.println(prefix + "packageName=" + packageName
393                 + " fieldId=" + fieldId
394                 + " fieldName=" + fieldName);
395         pw.println(prefix + "extras=" + extras);
396     }
397 
398     /**
399      * Used to package this object into a {@link Parcel}.
400      *
401      * @param dest The {@link Parcel} to be written.
402      * @param flags The flags used for parceling.
403      */
writeToParcel(Parcel dest, int flags)404     public void writeToParcel(Parcel dest, int flags) {
405         dest.writeInt(inputType);
406         dest.writeInt(imeOptions);
407         dest.writeString(privateImeOptions);
408         TextUtils.writeToParcel(actionLabel, dest, flags);
409         dest.writeInt(actionId);
410         dest.writeInt(initialSelStart);
411         dest.writeInt(initialSelEnd);
412         dest.writeInt(initialCapsMode);
413         TextUtils.writeToParcel(hintText, dest, flags);
414         TextUtils.writeToParcel(label, dest, flags);
415         dest.writeString(packageName);
416         dest.writeInt(fieldId);
417         dest.writeString(fieldName);
418         dest.writeBundle(extras);
419     }
420 
421     /**
422      * Used to make this class parcelable.
423      */
424     public static final Parcelable.Creator<EditorInfo> CREATOR =
425             new Parcelable.Creator<EditorInfo>() {
426                 public EditorInfo createFromParcel(Parcel source) {
427                     EditorInfo res = new EditorInfo();
428                     res.inputType = source.readInt();
429                     res.imeOptions = source.readInt();
430                     res.privateImeOptions = source.readString();
431                     res.actionLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
432                     res.actionId = source.readInt();
433                     res.initialSelStart = source.readInt();
434                     res.initialSelEnd = source.readInt();
435                     res.initialCapsMode = source.readInt();
436                     res.hintText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
437                     res.label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
438                     res.packageName = source.readString();
439                     res.fieldId = source.readInt();
440                     res.fieldName = source.readString();
441                     res.extras = source.readBundle();
442                     return res;
443                 }
444 
445                 public EditorInfo[] newArray(int size) {
446                     return new EditorInfo[size];
447                 }
448             };
449 
describeContents()450     public int describeContents() {
451         return 0;
452     }
453 
454 }
455