-
Notifications
You must be signed in to change notification settings - Fork 41
/
TextAction.java
106 lines (88 loc) · 3.99 KB
/
TextAction.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
package sh.calaba.instrumentationbackend.actions.text;
import android.view.View;
import android.view.inputmethod.InputConnection;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.util.concurrent.*;
import sh.calaba.instrumentationbackend.Result;
import sh.calaba.instrumentationbackend.actions.Action;
import sh.calaba.instrumentationbackend.query.CompletedFuture;
import sh.calaba.instrumentationbackend.query.ast.UIQueryUtils;
import sh.calaba.instrumentationbackend.utils.CompletableFuture;
public abstract class TextAction implements Action {
@Override
public final Result execute(String... args) {
try {
parseArguments(args);
} catch (IllegalArgumentException e) {
return Result.failedResult(e.getMessage());
}
try {
final View inputView = InfoMethodUtil.getInputView();
if (inputView == null) {
return Result.failedResult(getNoFocusedViewMessage());
}
FutureTask<Future<Result>> futureResult = new FutureTask<>(new Callable<Future<Result>>() {
@Override
public Future<Result> call() throws InfoMethodUtil.UnexpectedInputMethodManagerStructureException {
final InputConnection inputConnection = InfoMethodUtil.getInputConnection();
if (inputConnection == null) {
return new CompletedFuture<>(Result.failedResult(getNoFocusedViewMessage()));
}
return executeOnInputThread(inputView, inputConnection);
}
});
UIQueryUtils.postOnViewHandlerOrUiThread(inputView, futureResult);
Future<Result> res = futureResult.get(10, TimeUnit.SECONDS);
return res.get(10, TimeUnit.SECONDS);
} catch (ExecutionException executionException) {
throw new RuntimeException(executionException.getCause());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
}
/**
* Executes JS to handle WebView input operations.
* @param webView active WebView
* @param script script to execute inside the WebView
* @return operation result
*/
public static Future<Result> evalWebViewInputScript(final WebView webView, final String script) {
try {
final CompletableFuture<Result> future = new CompletableFuture<>();
Runnable runnable = new Runnable() {
@Override
public void run() {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.evaluateJavascript(script, new ValueCallback<String>() {
@Override
public void onReceiveValue(String jsError) {
// Result from JS is string not a Java null
if (jsError.equals("null")) {
future.complete(Result.successResult());
} else {
future.complete(Result.failedResult("JS input injection failed: " + jsError));
}
}
});
}
};
// Execute JS on the UI thread
webView.post(runnable);
return future;
} catch (Exception e) {
e.printStackTrace();
return new CompletedFuture<>(Result.failedResult(e.getMessage()));
}
}
protected abstract void parseArguments(String... args) throws IllegalArgumentException;
protected abstract String getNoFocusedViewMessage();
/*
This method is run on the main thread.
*/
protected abstract Future<Result> executeOnInputThread(final View servedView, final InputConnection inputConnection);
}