@@ -17,16 +17,22 @@ public class JsCallJava {
1717 private final static String TAG = "JsCallJava" ;
1818 private final static String RETURN_RESULT_FORMAT = "{\" code\" : %d, \" result\" : %s}" ;
1919 private HashMap <String , Method > mMethodsMap ;
20+ private String mInjectedName ;
2021 private String mPreloadInterfaceJS ;
2122 private Gson mGson ;
2223
2324 public JsCallJava (String injectedName , Class injectedCls ) {
2425 try {
26+ if (TextUtils .isEmpty (injectedName )) {
27+ throw new Exception ("injected name can not be null" );
28+ }
29+ mInjectedName = injectedName ;
2530 mMethodsMap = new HashMap <String , Method >();
2631 //获取自身声明的所有方法(包括public private protected), getMethods会获得所有继承与非继承的方法
2732 Method [] methods = injectedCls .getDeclaredMethods ();
28- StringBuilder sb = new StringBuilder ("javascript:(function(b){console.log(\" HostApp initialization begin\" );var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};" );
29-
33+ StringBuilder sb = new StringBuilder ("javascript:(function(b){console.log(\" " );
34+ sb .append (mInjectedName );
35+ sb .append (" initialization begin\" );var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};" );
3036 for (Method method : methods ) {
3137 String sign ;
3238 if (method .getModifiers () != (Modifier .PUBLIC | Modifier .STATIC ) || (sign = genJavaMethodSign (method )) == null ) {
@@ -36,9 +42,15 @@ public JsCallJava (String injectedName, Class injectedCls) {
3642 sb .append (String .format ("a.%s=" , method .getName ()));
3743 }
3844
39- 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." );
40- sb .append (injectedName );
41- sb .append ("=a;console.log(\" HostApp initialization end\" )})(window);" );
45+ sb .append ("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\" " );
46+ sb .append (mInjectedName );
47+ sb .append (" 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\" " );
48+ sb .append (mInjectedName );
49+ sb .append (" 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." );
50+ sb .append (mInjectedName );
51+ sb .append ("=a;console.log(\" " );
52+ sb .append (mInjectedName );
53+ sb .append (" initialization end\" )})(window);" );
4254 mPreloadInterfaceJS = sb .toString ();
4355 } catch (Exception e ){
4456 Log .e (TAG , "init js error:" + e .getMessage ());
@@ -110,7 +122,7 @@ public String call(WebView webView, String jsonStr) {
110122 values [k + 1 ] = argsVals .isNull (k ) ? null : argsVals .getJSONObject (k );
111123 } else if ("function" .equals (currType )) {
112124 sign += "_F" ;
113- values [k + 1 ] = new JsCallback (webView , argsVals .getInt (k ));
125+ values [k + 1 ] = new JsCallback (webView , mInjectedName , argsVals .getInt (k ));
114126 } else {
115127 sign += "_P" ;
116128 }
@@ -176,7 +188,7 @@ private String getReturn (String reqJson, int stateCode, Object result) {
176188 insertRes = String .valueOf (result );
177189 }
178190 String resStr = String .format (RETURN_RESULT_FORMAT , stateCode , insertRes );
179- Log .d (TAG , "HostApp call json: " + reqJson + " result:" + resStr );
191+ Log .d (TAG , mInjectedName + " call json: " + reqJson + " result:" + resStr );
180192 return resStr ;
181193 }
182194}
0 commit comments