Skip to content

Commit b6aa04f

Browse files
committed
optimize a bit of code
1 parent 145b8d9 commit b6aa04f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/cn/pedant/SafeWebViewBridge/bridge/JsCallJava.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import java.util.HashMap;
1313

1414
public class JsCallJava {
15-
private final String TAG = "JsCallJava";
15+
private final static String TAG = "JsCallJava";
16+
private final static String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
1617
private HashMap<String, Method> mMethodsMap;
17-
private final String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
1818
private String mPreloadInterfaceJS;
1919

2020
public JsCallJava (String injectedName, Class injectedCls) {
@@ -33,7 +33,9 @@ public JsCallJava (String injectedName, Class injectedCls) {
3333
sb.append(String.format("a.%s=", method.getName()));
3434
}
3535

36-
sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"HostApp call error, message:miss method name\"}var e=[];for(var h=1;h<f.length;h++){var c=f[h];var j=typeof c;e[e.length]=j;if(j==\"function\"){var d=a.queue.length;a.queue[d]=c;f[h]=d}}var g=JSON.parse(prompt(JSON.stringify({method:f.shift(),types:e,args:f})));if(g.code!=200){throw\"HostApp call error, code:\"+g.code+\", message:\"+g.result}return g.result};Object.getOwnPropertyNames(a).forEach(function(d){var c=a[d];if(typeof c===\"function\"&&d!==\"callback\"){a[d]=function(){return c.apply(a,[d].concat(Array.prototype.slice.call(arguments,0)))}}});b." + injectedName + "=a;console.log(\"HostApp initialization end\")})(window);");
36+
sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"HostApp call error, message:miss method name\"}var e=[];for(var h=1;h<f.length;h++){var c=f[h];var j=typeof c;e[e.length]=j;if(j==\"function\"){var d=a.queue.length;a.queue[d]=c;f[h]=d}}var g=JSON.parse(prompt(JSON.stringify({method:f.shift(),types:e,args:f})));if(g.code!=200){throw\"HostApp call error, code:\"+g.code+\", message:\"+g.result}return g.result};Object.getOwnPropertyNames(a).forEach(function(d){var c=a[d];if(typeof c===\"function\"&&d!==\"callback\"){a[d]=function(){return c.apply(a,[d].concat(Array.prototype.slice.call(arguments,0)))}}});b.");
37+
sb.append(injectedName);
38+
sb.append("=a;console.log(\"HostApp initialization end\")})(window);");
3739
mPreloadInterfaceJS = sb.toString();
3840
} catch(Exception e){
3941
Log.e(TAG, "init js error:" + e.getMessage());

src/cn/pedant/SafeWebViewBridge/demo/HostJsScope.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ public static void goBack (WebView view) {
123123
* @return 返回对象的第一个键值对
124124
* */
125125
public static String passJson2Java (WebView view, JSONObject jo) {
126-
Iterator<String> iterator = jo.keys();
126+
Iterator iterator = jo.keys();
127127
String res = null;
128128
if(iterator.hasNext()) {
129129
try {
130-
String keyW = iterator.next();
130+
String keyW = (String)iterator.next();
131131
res = keyW + ": " + jo.getString(keyW);
132132
} catch (JSONException je) {
133133

0 commit comments

Comments
 (0)