forked from twig/twigstechtips-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GingerbreadJSFixExample.java
214 lines (177 loc) · 6.94 KB
/
GingerbreadJSFixExample.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
package twig.nguyen.codepeeker;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class GingerbreadJSFixExample {
private boolean javascriptInterfaceBroken = false;
/**
* Detect all Gingerbread builds (2.3.x) as it has issues with addJavascriptInterface() and wrap the
* jsInterface object so it works.
*
* This requires any webview $(document).ready() JS code that requires the interface to be moved into
* android_init(), which is only called after the JS injection has occurred.
*
* Method initially discovered by Jason Shah, tweaked by Mr S (StackOverflow) to detect multiple versions of Gingerbread.
*
* Modified by twig:
* - Calls are now synchronous and can return values!
* - Still able to use Android.methodName() - does not require change in the way the JS is called
* - Now automatically maps all JSInterface methods to interface
* - Automatically handle function calls from JS without having to split/tokenize strings manually
* - Information passed is now wrapped as JSON, deals with single/double quotes correctly
* - No "major" rewrites of JS required.
* - Tweaked interface call detection a bit so it's less like to be called by accident.
* - Configurable interface name and signature
*
* Drawbacks:
* - Unable to access interface from iframes
* - Android interface is not always available, have to wait until the JS injection has been executed - see android_init()
* - All JSInterface class methods must only use Strings as parameters.
*
* @see http://twigstechtips.blogspot.com.au/2013/09/android-webviewaddjavascriptinterface.html
*/
protected void fixWebViewJSInterface(WebView webview, Object jsInterface, String jsInterfaceName, String jsSignature) {
// Gingerbread specific code
if (Build.VERSION.RELEASE.startsWith("2.3")) {
javascriptInterfaceBroken = true;
}
// Everything else is fine
else {
webview.addJavascriptInterface(jsInterface, jsInterfaceName);
}
webview.setWebViewClient(new GingerbreadWebViewClient(jsInterface, jsInterfaceName, jsSignature));
webview.setWebChromeClient(new GingerbreadWebViewChrome(jsInterface, jsSignature));
}
/**
* Handle JS injection and re-injection to fix the Android 2.3 JSInterface bug.
*/
private class GingerbreadWebViewClient extends WebViewClient {
private Object jsInterface;
private String jsInterfaceName;
private String jsSignature;
public GingerbreadWebViewClient(Object jsInterface, String jsInterfaceName, String jsSignature) {
this.jsInterface = jsInterface;
this.jsInterfaceName = jsInterfaceName;
this.jsSignature = jsSignature;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/*
* Inject our own JS into it if Android 2.3 detected.
* This is not called until after the page has initialised $(document).ready().
*/
if (javascriptInterfaceBroken) {
StringBuilder gbjs = new StringBuilder();
gbjs.append("javascript: ");
gbjs.append(generateJS());
view.loadUrl(gbjs.toString());
}
// Initialise the page
view.loadUrl("javascript: android_init();");
}
/**
* What this JS wrapper function does is convert all the arguments to strings,
* in JSON format before sending it to Android in the form of a prompt() alert.
*
* JSON data is returned by Android and unwrapped as the result.
*/
public String generateJS() {
StringBuilder gbjs = new StringBuilder();
if (javascriptInterfaceBroken) {
StringBuilder sb;
gbjs.append("var "); gbjs.append(jsInterfaceName); gbjs.append(" = { " +
" _gbFix: function(fxname, xargs) {" +
" var args = new Array();" +
" for (var i = 0; i < xargs.length; i++) {" +
" args.push(xargs[i].toString());" +
" };" +
" var data = { name: fxname, len: args.length, args: args };" +
" var json = JSON.stringify(data);" +
" var res = prompt('"); gbjs.append(jsSignature); gbjs.append("' + json);" +
" return JSON.parse(res)['result'];" +
" }" +
"};");
// Build methods for each method in the JSInterface class.
for (Method m : jsInterface.getClass().getMethods()) {
sb = new StringBuilder();
// Output = "Android.showToast = function() { return this._gbFix('showToast', arguments); };"
sb.append(jsInterfaceName);
sb.append(".");
sb.append(m.getName());
sb.append(" = function() { return this._gbFix('");
sb.append(m.getName());
sb.append("', arguments); };");
gbjs.append(sb);
}
}
return gbjs.toString();
}
}
/**
* Handle JS calls to fix the Android 2.3 JSInterface bug.
*/
private class GingerbreadWebViewChrome extends WebChromeClient {
private Object jsInterface;
private String jsSignature;
public GingerbreadWebViewChrome(Object jsInterface, String jsSignature) {
this.jsInterface = jsInterface;
this.jsSignature = jsSignature;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
if (!javascriptInterfaceBroken || TextUtils.isEmpty(message) || !message.startsWith(jsSignature)) {
return false;
}
// We've hit some code through _gbFix()
JSONObject jsonData;
String functionName;
String encodedData;
try {
encodedData = message.substring(jsSignature.length());
jsonData = new JSONObject(encodedData);
encodedData = null; // no longer needed, clear memory
functionName = jsonData.getString("name");
for (Method m : jsInterface.getClass().getMethods()) {
if (m.getName().equals(functionName)) {
JSONArray jsonArgs = jsonData.getJSONArray("args");
Object[] args = new Object[jsonArgs.length()];
for (int i = 0; i < jsonArgs.length(); i++) {
args[i] = jsonArgs.get(i);
}
Object ret = m.invoke(jsInterface, args);
JSONObject res = new JSONObject();
res.put("result", ret);
result.confirm(res.toString());
return true;
}
}
// No matching method name found, should throw an exception.
throw new RuntimeException("shouldOverrideUrlLoading: Could not find method '" + functionName + "()'.");
}
catch (IllegalArgumentException e) {
Log.e("GingerbreadWebViewClient", "shouldOverrideUrlLoading: Please ensure your JSInterface methods only have String as parameters.");
throw new RuntimeException(e);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
catch (JSONException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}