This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardSwitchHelper.java
59 lines (46 loc) · 1.81 KB
/
KeyboardSwitchHelper.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
package io.github.sealor.prototype.android.espresso.keyboard.testing;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.inputmethod.InputMethodManager;
public class KeyboardSwitchHelper {
public static final int POLLING_PERIOD_IN_MILLIS = 1000;
private final Activity activity;
private final String packageName;
private final ContentResolver contentResolver;
private final InputMethodManager inputMethodManager;
public KeyboardSwitchHelper(Activity activity) {
this.activity = activity;
this.packageName = activity.getPackageName();
this.contentResolver = activity.getContentResolver();
this.inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
}
private String retrieveCurrentKeyboardName() {
return Settings.Secure.getString(this.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD);
}
private boolean isAppKeyboardTheCurrentKeyboard() {
return retrieveCurrentKeyboardName().startsWith(this.packageName);
}
private boolean isTestActivityFocused() {
return this.activity.hasWindowFocus();
}
private void showKeyboardPickerDialog() {
this.inputMethodManager.showInputMethodPicker();
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void showKeyboardPickerDialogIfNeededAndWaitForSwitch() {
if (!isAppKeyboardTheCurrentKeyboard()) {
showKeyboardPickerDialog();
do {
sleep(POLLING_PERIOD_IN_MILLIS);
} while (!isTestActivityFocused());
}
}
}