OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
aelias_OOO_until_Jul13
2016/01/21 07:43:05
nit: Copyright 2016
Changwan Ryu
2016/01/22 10:22:16
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 package org.chromium.content.browser.input; | |
5 | |
6 import android.text.Editable; | |
7 import android.text.InputType; | |
8 import android.text.Selection; | |
9 import android.util.StringBuilderPrinter; | |
10 import android.view.inputmethod.BaseInputConnection; | |
11 import android.view.inputmethod.CorrectionInfo; | |
12 import android.view.inputmethod.EditorInfo; | |
13 | |
14 import org.chromium.base.ThreadUtils; | |
15 import org.chromium.blink_public.web.WebTextInputFlags; | |
16 import org.chromium.ui.base.ime.TextInputType; | |
17 | |
18 import java.util.Locale; | |
19 | |
20 /** | |
21 * Utilities for IME. | |
aelias_OOO_until_Jul13
2016/01/21 07:43:04
nit: no-information comment, please delete
Changwan Ryu
2016/01/22 10:22:16
I've added some more description. Without this I'l
| |
22 */ | |
23 public class ImeUtils { | |
24 public static void computeEditorInfo(int inputType, int inputFlags, int init ialSelStart, | |
25 int initialSelEnd, EditorInfo outAttrs) { | |
26 outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME _FLAG_NO_EXTRACT_UI; | |
27 outAttrs.inputType = | |
28 EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_ EDIT_TEXT; | |
29 | |
30 if ((inputFlags & WebTextInputFlags.AutocompleteOff) != 0) { | |
31 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS; | |
32 } | |
33 | |
34 if (inputType == TextInputType.TEXT) { | |
35 // Normal text field | |
36 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
37 if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) { | |
38 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; | |
39 } | |
40 } else if (inputType == TextInputType.TEXT_AREA | |
41 || inputType == TextInputType.CONTENT_EDITABLE) { | |
42 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; | |
43 if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) { | |
44 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; | |
45 } | |
46 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NONE; | |
47 } else if (inputType == TextInputType.PASSWORD) { | |
48 // Password | |
49 outAttrs.inputType = | |
50 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WE B_PASSWORD; | |
51 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
52 } else if (inputType == TextInputType.SEARCH) { | |
53 // Search | |
54 outAttrs.imeOptions |= EditorInfo.IME_ACTION_SEARCH; | |
55 } else if (inputType == TextInputType.URL) { | |
56 // Url | |
57 outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT _VARIATION_URI; | |
58 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
59 } else if (inputType == TextInputType.EMAIL) { | |
60 // Email | |
61 outAttrs.inputType = | |
62 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WE B_EMAIL_ADDRESS; | |
63 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
64 } else if (inputType == TextInputType.TELEPHONE) { | |
65 // Telephone | |
66 // Number and telephone do not have both a Tab key and an | |
67 // action in default OSK, so set the action to NEXT | |
68 outAttrs.inputType = InputType.TYPE_CLASS_PHONE; | |
69 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; | |
70 } else if (inputType == TextInputType.NUMBER) { | |
71 // Number | |
72 outAttrs.inputType = InputType.TYPE_CLASS_NUMBER | |
73 | InputType.TYPE_NUMBER_VARIATION_NORMAL | InputType.TYPE_NU MBER_FLAG_DECIMAL; | |
74 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; | |
75 } | |
76 | |
77 // Handling of autocapitalize. Blink will send the flag taking into acco unt the element's | |
78 // type. This is not using AutocapitalizeNone because Android does not a utocapitalize by | |
79 // default and there is no way to express no capitalization. | |
80 // Autocapitalize is meant as a hint to the virtual keyboard. | |
81 if ((inputFlags & WebTextInputFlags.AutocapitalizeCharacters) != 0) { | |
82 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; | |
83 } else if ((inputFlags & WebTextInputFlags.AutocapitalizeWords) != 0) { | |
84 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_WORDS; | |
85 } else if ((inputFlags & WebTextInputFlags.AutocapitalizeSentences) != 0 ) { | |
86 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
87 } | |
88 // Content editable doesn't use autocapitalize so we need to set it manu ally. | |
89 if (inputType == TextInputType.CONTENT_EDITABLE) { | |
90 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
91 } | |
92 | |
93 outAttrs.initialSelStart = initialSelStart; | |
94 outAttrs.initialSelEnd = initialSelEnd; | |
95 } | |
96 | |
97 public static String dumpEditorInfo(EditorInfo editorInfo) { | |
98 StringBuilder builder = new StringBuilder(); | |
99 StringBuilderPrinter printer = new StringBuilderPrinter(builder); | |
100 editorInfo.dump(printer, ""); | |
101 return builder.toString(); | |
102 } | |
103 | |
104 public static String dumpEditable(Editable editable) { | |
105 return String.format(Locale.US, "Editable {[%s] SEL[%d %d] COM[%d %d]}", | |
106 editable.toString(), Selection.getSelectionStart(editable), | |
107 Selection.getSelectionEnd(editable), | |
108 BaseInputConnection.getComposingSpanStart(editable), | |
109 BaseInputConnection.getComposingSpanEnd(editable)); | |
110 } | |
111 | |
112 /** | |
113 * Dump the given {@CorrectionInfo} into class. | |
114 * @param correctionInfo | |
115 * @return User-readable {@CorrectionInfo}. | |
116 */ | |
117 static String dumpCorrectionInfo(CorrectionInfo correctionInfo) { | |
118 // TODO(changwan): implement it properly if needed. | |
119 return correctionInfo.toString(); | |
120 } | |
121 | |
122 // TODO(changwan): remove these once implementation becomes stable. | |
123 static void assertReally(boolean value) { | |
124 if (!value) { | |
125 throw new AssertionError(); | |
126 } | |
127 } | |
128 | |
129 static void assertReally(String msg, boolean value) { | |
130 if (!value) { | |
131 throw new AssertionError(msg); | |
132 } | |
133 } | |
134 | |
135 static void assertOnUiThread() { | |
136 assertReally("Should be on UI thread.", ThreadUtils.runningOnUiThread()) ; | |
137 } | |
138 } | |
OLD | NEW |