/
ShadowInputMethodManager.java
297 lines (255 loc) · 9.6 KB
/
ShadowInputMethodManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static org.robolectric.util.reflector.Reflector.reflector;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.os.ResultReceiver;
import android.util.SparseArray;
import android.view.View;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import java.util.List;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;
@Implements(value = InputMethodManager.class)
public class ShadowInputMethodManager {
/**
* Handler for receiving soft input visibility changed event.
*
* <p>Since Android does not have any API for retrieving soft input status, most application
* relies on GUI layout changes to detect the soft input change event. Currently, Robolectric are
* not able to simulate the GUI change when application changes the soft input through {@code
* InputMethodManager}, this handler can be used by application to simulate GUI change in response
* of the soft input change.
*/
public interface SoftInputVisibilityChangeHandler {
void handleSoftInputVisibilityChange(boolean softInputVisible);
}
/** Handler for receiving PrivateCommands. */
public interface PrivateCommandListener {
void onPrivateCommand(View view, String action, Bundle data);
}
private boolean softInputVisible;
private Optional<SoftInputVisibilityChangeHandler> visibilityChangeHandler = Optional.absent();
private Optional<PrivateCommandListener> privateCommandListener = Optional.absent();
private List<InputMethodInfo> inputMethodInfoList = ImmutableList.of();
private List<InputMethodInfo> enabledInputMethodInfoList = ImmutableList.of();
private Optional<InputMethodSubtype> inputMethodSubtype = Optional.absent();
@Implementation
protected boolean showSoftInput(View view, int flags) {
return showSoftInput(view, flags, null);
}
@Implementation(maxSdk = R)
protected boolean showSoftInput(View view, int flags, ResultReceiver resultReceiver) {
setSoftInputVisibility(true);
return true;
}
@Implementation(minSdk = S)
protected boolean showSoftInput(
View view, int flags, ResultReceiver resultReceiver, int ignoredReason) {
return showSoftInput(view, flags, resultReceiver);
}
@Implementation(minSdk = S)
protected boolean hideSoftInputFromWindow(
IBinder windowToken, int flags, ResultReceiver resultReceiver, int ignoredReason) {
return hideSoftInputFromWindow(windowToken, flags, resultReceiver);
}
@Implementation(maxSdk = R)
protected boolean hideSoftInputFromWindow(IBinder windowToken, int flags) {
return hideSoftInputFromWindow(windowToken, flags, null);
}
@Implementation
protected boolean hideSoftInputFromWindow(
IBinder windowToken, int flags, ResultReceiver resultReceiver) {
int resultCode;
if (isSoftInputVisible()) {
setSoftInputVisibility(false);
resultCode = InputMethodManager.RESULT_HIDDEN;
} else {
resultCode = InputMethodManager.RESULT_UNCHANGED_HIDDEN;
}
if (resultReceiver != null) {
resultReceiver.send(resultCode, null);
}
return true;
}
@Implementation
protected void toggleSoftInput(int showFlags, int hideFlags) {
setSoftInputVisibility(!isSoftInputVisible());
}
public boolean isSoftInputVisible() {
return softInputVisible;
}
public void setSoftInputVisibilityHandler(
SoftInputVisibilityChangeHandler visibilityChangeHandler) {
this.visibilityChangeHandler =
Optional.<SoftInputVisibilityChangeHandler>of(visibilityChangeHandler);
}
private void setSoftInputVisibility(boolean visible) {
if (visible == softInputVisible) {
return;
}
softInputVisible = visible;
if (visibilityChangeHandler.isPresent()) {
visibilityChangeHandler.get().handleSoftInputVisibilityChange(softInputVisible);
}
}
/**
* The framework implementation does a blocking call to system server. This will deadlock on
* Robolectric, so just stub out the method.
*/
@Implementation(minSdk = S)
protected void closeCurrentInput() {}
/**
* Returns the list of {@link InputMethodInfo} that are installed.
*
* <p>This method differs from Android implementation by allowing the list to be set using {@link
* #setInputMethodInfoList(List)}.
*/
@Implementation
protected List<InputMethodInfo> getInputMethodList() {
return inputMethodInfoList;
}
/**
* Sets the list of {@link InputMethodInfo} that are marked as installed. See {@link
* #getInputMethodList()}.
*/
public void setInputMethodInfoList(List<InputMethodInfo> inputMethodInfoList) {
this.inputMethodInfoList = inputMethodInfoList;
}
/**
* Returns the {@link InputMethodSubtype} that is installed.
*
* <p>This method differs from Android implementation by allowing the list to be set using {@link
* #setCurrentInputMethodSubtype(InputMethodSubtype)}.
*/
@Implementation
protected InputMethodSubtype getCurrentInputMethodSubtype() {
return inputMethodSubtype.orNull();
}
/**
* Sets the current {@link InputMethodSubtype} that will be returned by {@link
* #getCurrentInputMethodSubtype()}.
*/
public void setCurrentInputMethodSubtype(InputMethodSubtype inputMethodSubtype) {
this.inputMethodSubtype = Optional.of(inputMethodSubtype);
}
/**
* Returns the list of {@link InputMethodInfo} that are enabled.
*
* <p>This method differs from Android implementation by allowing the list to be set using {@link
* #setEnabledInputMethodInfoList(List)}.
*/
@Implementation
protected List<InputMethodInfo> getEnabledInputMethodList() {
return enabledInputMethodInfoList;
}
/**
* Sets the list of {@link InputMethodInfo} that are marked as enabled. See {@link
* #getEnabledInputMethodList()}.
*/
public void setEnabledInputMethodInfoList(List<InputMethodInfo> inputMethodInfoList) {
this.enabledInputMethodInfoList = inputMethodInfoList;
}
@Implementation
protected void restartInput(View view) {}
@Implementation
protected boolean isActive(View view) {
return false;
}
@Implementation
protected boolean isActive() {
return false;
}
@Implementation
protected boolean isFullscreenMode() {
return false;
}
@Implementation(maxSdk = Q)
protected void focusIn(View view) {}
@Implementation(minSdk = M, maxSdk = Q)
protected void onViewDetachedFromWindow(View view) {}
@Implementation
protected void displayCompletions(View view, CompletionInfo[] completions) {}
@Implementation(maxSdk = LOLLIPOP_MR1)
protected static InputMethodManager peekInstance() {
// Android has a bug pre M where peekInstance was dereferenced without a null check:-
// https://github.com/aosp-mirror/platform_frameworks_base/commit/a046faaf38ad818e6b5e981a39fd7394cf7cee03
// So for earlier versions, just call through directly to getInstance()
if (RuntimeEnvironment.getApiLevel() <= JELLY_BEAN_MR1) {
return ReflectionHelpers.callStaticMethod(
InputMethodManager.class,
"getInstance",
ClassParameter.from(Looper.class, Looper.getMainLooper()));
} else if (RuntimeEnvironment.getApiLevel() <= LOLLIPOP_MR1) {
return InputMethodManager.getInstance();
}
return reflector(_InputMethodManager_.class).peekInstance();
}
@Implementation(minSdk = N)
protected boolean startInputInner(
int startInputReason,
IBinder windowGainingFocus,
int startInputFlags,
int softInputMode,
int windowFlags) {
return true;
}
@Implementation(minSdk = M)
protected void sendAppPrivateCommand(View view, String action, Bundle data) {
if (privateCommandListener.isPresent()) {
privateCommandListener.get().onPrivateCommand(view, action, data);
}
}
public void setAppPrivateCommandListener(PrivateCommandListener listener) {
privateCommandListener = Optional.of(listener);
}
@Resetter
public static void reset() {
int apiLevel = RuntimeEnvironment.getApiLevel();
_InputMethodManager_ _reflector = reflector(_InputMethodManager_.class);
if (apiLevel <= JELLY_BEAN_MR1) {
_reflector.setMInstance(null);
} else {
_reflector.setInstance(null);
}
if (apiLevel > P) {
_reflector.getInstanceMap().clear();
}
}
@ForType(InputMethodManager.class)
interface _InputMethodManager_ {
@Static
@Direct
InputMethodManager peekInstance();
@Static
@Accessor("mInstance")
void setMInstance(InputMethodManager instance);
@Static
@Accessor("sInstance")
void setInstance(InputMethodManager instance);
@Static
@Accessor("sInstanceMap")
SparseArray<InputMethodManager> getInstanceMap();
}
}