Skip to content

Commit

Permalink
optimize a bit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
pedant committed Nov 3, 2014
1 parent 145b8d9 commit b6aa04f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/cn/pedant/SafeWebViewBridge/bridge/JsCallJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.HashMap;

public class JsCallJava {
private final String TAG = "JsCallJava";
private final static String TAG = "JsCallJava";
private final static String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
private HashMap<String, Method> mMethodsMap;
private final String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
private String mPreloadInterfaceJS;

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

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);");
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.");
sb.append(injectedName);
sb.append("=a;console.log(\"HostApp initialization end\")})(window);");
mPreloadInterfaceJS = sb.toString();
} catch(Exception e){
Log.e(TAG, "init js error:" + e.getMessage());
Expand Down
4 changes: 2 additions & 2 deletions src/cn/pedant/SafeWebViewBridge/demo/HostJsScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public static void goBack (WebView view) {
* @return 返回对象的第一个键值对
* */
public static String passJson2Java (WebView view, JSONObject jo) {
Iterator<String> iterator = jo.keys();
Iterator iterator = jo.keys();
String res = null;
if(iterator.hasNext()) {
try {
String keyW = iterator.next();
String keyW = (String)iterator.next();
res = keyW + ": " + jo.getString(keyW);
} catch (JSONException je) {

Expand Down

0 comments on commit b6aa04f

Please sign in to comment.