OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
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; | |
Ted C
2016/02/17 19:09:33
blank line above this
Changwan Ryu
2016/02/18 06:03:26
Done.
| |
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.View; | |
11 import android.view.inputmethod.BaseInputConnection; | |
12 import android.view.inputmethod.CorrectionInfo; | |
13 import android.view.inputmethod.EditorInfo; | |
14 | |
15 import org.chromium.base.ThreadUtils; | |
16 import org.chromium.blink_public.web.WebTextInputFlags; | |
17 import org.chromium.ui.base.ime.TextInputType; | |
18 | |
19 import java.util.Locale; | |
20 | |
21 /** | |
22 * Utilities for IME such as computing outAttrs, and dumping object information. | |
23 */ | |
24 public class ImeUtils { | |
25 /** | |
26 * Compute {@link EditorInfo} based on the given parameters. This is needed for | |
27 * {@link View#onCreateInputConnection(EditorInfo)}. | |
28 * | |
29 * @param inputType Type defined in {@link TextInputType}. | |
30 * @param inputFlags Flags defined in {@link WebTextInputFlags}. | |
31 * @param initialSelStart The initial selection start position. | |
32 * @param initialSelEnd The initial selection end position. | |
33 * @param outAttrs An instance of {@link EditorInfo} that we are going to ch ange. | |
34 */ | |
35 public static void computeEditorInfo(int inputType, int inputFlags, int init ialSelStart, | |
36 int initialSelEnd, EditorInfo outAttrs) { | |
37 outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME _FLAG_NO_EXTRACT_UI; | |
38 outAttrs.inputType = | |
39 EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_WEB_ EDIT_TEXT; | |
40 | |
41 if ((inputFlags & WebTextInputFlags.AutocompleteOff) != 0) { | |
42 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS; | |
43 } | |
44 | |
45 if (inputType == TextInputType.TEXT) { | |
46 // Normal text field | |
47 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
48 if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) { | |
49 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; | |
50 } | |
51 } else if (inputType == TextInputType.TEXT_AREA | |
52 || inputType == TextInputType.CONTENT_EDITABLE) { | |
53 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; | |
54 if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) { | |
55 outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; | |
56 } | |
57 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NONE; | |
58 } else if (inputType == TextInputType.PASSWORD) { | |
59 // Password | |
60 outAttrs.inputType = | |
61 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WE B_PASSWORD; | |
62 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
63 } else if (inputType == TextInputType.SEARCH) { | |
64 // Search | |
65 outAttrs.imeOptions |= EditorInfo.IME_ACTION_SEARCH; | |
66 } else if (inputType == TextInputType.URL) { | |
67 // Url | |
68 outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT _VARIATION_URI; | |
69 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
70 } else if (inputType == TextInputType.EMAIL) { | |
71 // Email | |
72 outAttrs.inputType = | |
73 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WE B_EMAIL_ADDRESS; | |
74 outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO; | |
75 } else if (inputType == TextInputType.TELEPHONE) { | |
76 // Telephone | |
77 // Number and telephone do not have both a Tab key and an | |
78 // action in default OSK, so set the action to NEXT | |
79 outAttrs.inputType = InputType.TYPE_CLASS_PHONE; | |
80 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; | |
81 } else if (inputType == TextInputType.NUMBER) { | |
82 // Number | |
83 outAttrs.inputType = InputType.TYPE_CLASS_NUMBER | |
84 | InputType.TYPE_NUMBER_VARIATION_NORMAL | InputType.TYPE_NU MBER_FLAG_DECIMAL; | |
85 outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT; | |
86 } | |
87 | |
88 // Handling of autocapitalize. Blink will send the flag taking into acco unt the element's | |
89 // type. This is not using AutocapitalizeNone because Android does not a utocapitalize by | |
90 // default and there is no way to express no capitalization. | |
91 // Autocapitalize is meant as a hint to the virtual keyboard. | |
92 if ((inputFlags & WebTextInputFlags.AutocapitalizeCharacters) != 0) { | |
93 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; | |
94 } else if ((inputFlags & WebTextInputFlags.AutocapitalizeWords) != 0) { | |
95 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_WORDS; | |
96 } else if ((inputFlags & WebTextInputFlags.AutocapitalizeSentences) != 0 ) { | |
97 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
98 } | |
99 // Content editable doesn't use autocapitalize so we need to set it manu ally. | |
100 if (inputType == TextInputType.CONTENT_EDITABLE) { | |
101 outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; | |
102 } | |
103 | |
104 outAttrs.initialSelStart = initialSelStart; | |
105 outAttrs.initialSelEnd = initialSelEnd; | |
106 } | |
107 | |
108 public static String getEditorInfoDebugString(EditorInfo editorInfo) { | |
109 StringBuilder builder = new StringBuilder(); | |
110 StringBuilderPrinter printer = new StringBuilderPrinter(builder); | |
111 editorInfo.dump(printer, ""); | |
112 return builder.toString(); | |
113 } | |
114 | |
115 public static String getEditableDebugString(Editable editable) { | |
116 return String.format(Locale.US, "Editable {[%s] SEL[%d %d] COM[%d %d]}", | |
117 editable.toString(), Selection.getSelectionStart(editable), | |
118 Selection.getSelectionEnd(editable), | |
119 BaseInputConnection.getComposingSpanStart(editable), | |
120 BaseInputConnection.getComposingSpanEnd(editable)); | |
121 } | |
122 | |
123 /** | |
124 * Dump the given {@CorrectionInfo} into class. | |
125 * @param correctionInfo | |
126 * @return User-readable {@CorrectionInfo}. | |
127 */ | |
128 static String getCorrectInfoDebugString(CorrectionInfo correctionInfo) { | |
129 // TODO(changwan): implement it properly if needed. | |
130 return correctionInfo.toString(); | |
131 } | |
132 | |
133 // TODO(changwan): remove these once implementation becomes stable. | |
134 static void checkCondition(boolean value) { | |
135 if (!value) { | |
136 throw new AssertionError(); | |
137 } | |
138 } | |
139 | |
140 static void checkCondition(String msg, boolean value) { | |
141 if (!value) { | |
142 throw new AssertionError(msg); | |
143 } | |
144 } | |
145 | |
146 static void checkOnUiThread() { | |
147 checkCondition("Should be on UI thread.", ThreadUtils.runningOnUiThread( )); | |
148 } | |
149 } | |
OLD | NEW |