-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNIWebView.java
More file actions
113 lines (94 loc) · 2.63 KB
/
NIWebView.java
File metadata and controls
113 lines (94 loc) · 2.63 KB
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
/**
* Created by Trent Ahrens on 11/20/2012
*
* NIWebView allows you to evaluate and get result of javascript from native code.
*/
package com.lolboxen.ni;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
public class NIWebView extends WebView {
public static String NI_LOG_TAG = "com.lolboxen.NI";
private NIWebViewSyncInterface syncInterface;
public NIWebView(Context context) {
super(context);
setup();
}
public NIWebView(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
public NIWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup();
}
private void setup()
{
getSettings().setJavaScriptEnabled(true);
syncInterface = new NIWebViewSyncInterface();
addJavascriptInterface(syncInterface, "NISyncInterface");
}
public String stringByEvaluatingJavaScript(String javascript)
{
String escapedJavascript = javascript.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\\"", "\\\\\"");
String finalCode =
"javascript:try { " +
"NISyncInterface.didCompile();" +
"NISyncInterface.setReturnValue(eval(\"" + escapedJavascript + "\"));" +
"} catch(err) {" +
"NISyncInterface.setReturnValue('');" +
"}";
Log.v(NI_LOG_TAG, finalCode);
syncInterface.startLatches();
this.loadUrl(finalCode);
return syncInterface.getReturnValue();
}
private class NIWebViewSyncInterface
{
private CountDownLatch compileLatch;
private CountDownLatch finishLatch;
private String returnValue;
public void startLatches()
{
compileLatch = new CountDownLatch(1);
finishLatch = new CountDownLatch(1);
}
public String getReturnValue()
{
// returnValue = "";
try {
if (compileLatch.await(1, TimeUnit.SECONDS) == false)
{
Log.e(NI_LOG_TAG, "script did not compile");
return returnValue;
}
} catch (InterruptedException e) {
return returnValue;
}
try {
finishLatch.await(30, TimeUnit.SECONDS);
return returnValue;
} catch (InterruptedException e) {
Log.e(NI_LOG_TAG, "Timed out waiting for JS response");
throw new RuntimeException("Timed out waiting for JS response");
}
}
@SuppressWarnings("unused")
@JavascriptInterface
public void setReturnValue(String returnValue)
{
this.returnValue = returnValue;
finishLatch.countDown();
}
@SuppressWarnings("unused")
@JavascriptInterface
public void didCompile()
{
compileLatch.countDown();
}
}
}