SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

In previous blog How is OData request routed to Offline data store by OData offline plugin, according to comment, the Offline store will only be available once it is opened successfully. The open operation is done in Device native API, delegated in line 232.

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

The exec is available in exported module returned by require(‘cordova/exec’):

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

And now I would like to understand how this exec is delegated to Java Native API in Android platform.
In folder android I have found this cordova.js:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

The exec call is implemented by module defined in cordova.js:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

Here means the exec is implemented by androidExec:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

Key logic of androidExec implementation

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

(1) meaning of parameters success, fail, service, action, args

a. success & fail: JavaScript callback function after the specified Java API is called
b. service: Java class name
c. action: Java class method name
d. args: arguments passed from JavaScript to Java

(2) There are two technical approaches for communication from JavaScript to Java in Android platform, see them in constant jsToNativeModes:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

Example of JS_OBJECT, or called JavaScript interface:
Java code:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewGUI extends Activity {
  WebView mWebView;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mWebView = new WebView(this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(new JavaScriptInterface(), "jsinterface");
    mWebView.loadUrl("file:///android_asset/www/index.html");
    setContentView(mWebView);
  }
  final class JavaScriptInterface {
    JavaScriptInterface () { }
    public String getSomeString() {
      return "string";
    }
  }
}

In JavaScript code, consume Java method getSomeString as below:

<script>
var String = window.jsinterface.getSomeString();
</script>
Back to implementation of AndroidExec:
var msgs = nativeApiProvider.get().exec(bridgeSecret, service, action, callbackId, argsJson);

it will again delegate the call to exec method of instance returned by nativeApiProvider.get().

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

Again look at implementation in nativeapiprovider.js, from the comment in line 21 we can know the currentApi returned by get comes from either ExposedJsApi.java or PromptBasedNativeAPI.

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

The ExposedJsApi.java could be found from this location:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

It is a Java interface with method exec defined:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

The call from JavaScript to Java is done via prompt call:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

And the Java class SystemExposedJsApi implements the interface, delegates call to the instance of class CordovaBridge.

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

CordovaBridge will delegate to PluginManager:

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

Plugin manager first gets responsible plugin Java class by name, then perform the execute method of plugin.

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

For example this is OData offline plugin service class, in its execute method we can find there are lots of IF-ELSE branch to implement different offline operation.

SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

要获取更多Jerry的原创文章,请关注公众号"汪子熙":
SAP offline OData插件的JavaScript代码是如何调用到Android平台的Java代码的

 

上一篇:WebStorm配置electron启动器


下一篇:standard_init_linux.go:228: exec user process caused “no such file or directory“ - Docker