Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for Cordova-Android 6.0.0 - Evaluate Javascript bridge as default #320

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions bin/templates/project/assets/www/cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ module.exports = cordova;

});

// file: /Users/steveng/repo/cordova/cordova-android/cordova-js-src/android/nativeapiprovider.js
// file: /Users/jbowser/cordova/cordova-android/cordova-js-src/android/nativeapiprovider.js
define("cordova/android/nativeapiprovider", function(require, exports, module) {

/**
Expand All @@ -353,7 +353,7 @@ module.exports = {

});

// file: /Users/steveng/repo/cordova/cordova-android/cordova-js-src/android/promptbasednativeapi.js
// file: /Users/jbowser/cordova/cordova-android/cordova-js-src/android/promptbasednativeapi.js
define("cordova/android/promptbasednativeapi", function(require, exports, module) {

/**
Expand Down Expand Up @@ -862,7 +862,7 @@ module.exports = channel;

});

// file: /Users/steveng/repo/cordova/cordova-android/cordova-js-src/exec.js
// file: /Users/jbowser/cordova/cordova-android/cordova-js-src/exec.js
define("cordova/exec", function(require, exports, module) {

/**
Expand Down Expand Up @@ -897,10 +897,11 @@ var cordova = require('cordova'),
// For the ONLINE_EVENT to be viable, it would need to intercept all event
// listeners (both through addEventListener and window.ononline) as well
// as set the navigator property itself.
ONLINE_EVENT: 2
ONLINE_EVENT: 2,
EVAL_BRIDGE: 3
},
jsToNativeBridgeMode, // Set lazily.
nativeToJsBridgeMode = nativeToJsModes.ONLINE_EVENT,
nativeToJsBridgeMode = nativeToJsModes.EVAL_BRIDGE,
pollEnabled = false,
bridgeSecret = -1;

Expand Down Expand Up @@ -1611,7 +1612,7 @@ exports.reset();

});

// file: /Users/steveng/repo/cordova/cordova-android/cordova-js-src/platform.js
// file: /Users/jbowser/cordova/cordova-android/cordova-js-src/platform.js
define("cordova/platform", function(require, exports, module) {

// The last resume event that was received that had the result of a plugin call.
Expand Down Expand Up @@ -1721,7 +1722,7 @@ function onMessageFromNative(msg) {

});

// file: /Users/steveng/repo/cordova/cordova-android/cordova-js-src/plugin/android/app.js
// file: /Users/jbowser/cordova/cordova-android/cordova-js-src/plugin/android/app.js
define("cordova/plugin/android/app", function(require, exports, module) {

var exec = require('cordova/exec');
Expand Down Expand Up @@ -2164,4 +2165,4 @@ window.cordova = require('cordova');

require('cordova/init');

})();
})();
5 changes: 3 additions & 2 deletions cordova-js-src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ var cordova = require('cordova'),
// For the ONLINE_EVENT to be viable, it would need to intercept all event
// listeners (both through addEventListener and window.ononline) as well
// as set the navigator property itself.
ONLINE_EVENT: 2
ONLINE_EVENT: 2,
EVAL_BRIDGE: 3
},
jsToNativeBridgeMode, // Set lazily.
nativeToJsBridgeMode = nativeToJsModes.ONLINE_EVENT,
nativeToJsBridgeMode = nativeToJsModes.EVAL_BRIDGE,
pollEnabled = false,
bridgeSecret = -1;

Expand Down
4 changes: 4 additions & 0 deletions framework/src/org/apache/cordova/CordovaWebViewEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;

/**
* Interface for all Cordova engines.
Expand Down Expand Up @@ -58,6 +59,9 @@ void init(CordovaWebView parentWebView, CordovaInterface cordova, Client client,
/** Clean up all resources associated with the WebView. */
void destroy();

/** Add the evaulate Javascript method **/
void evaluateJavascript(String js, ValueCallback<String> callback);

/**
* Used to retrieve the associated CordovaWebView given a View without knowing the type of Engine.
* E.g. ((CordovaWebView.EngineView)activity.findViewById(android.R.id.webView)).getCordovaWebView();
Expand Down
25 changes: 25 additions & 0 deletions framework/src/org/apache/cordova/NativeToJsMessageQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,31 @@ public void notifyOfFlush(final NativeToJsMessageQueue queue, boolean fromOnline
}
}

/** Uses webView.evaluateJavascript to execute messages. */
public static class EvalBridgeMode extends BridgeMode {
private final CordovaWebViewEngine engine;
private final CordovaInterface cordova;

public EvalBridgeMode(CordovaWebViewEngine engine, CordovaInterface cordova) {
this.engine = engine;
this.cordova = cordova;
}

@Override
public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
String js = queue.popAndEncodeAsJs();
if (js != null) {
engine.evaluateJavascript(js, null);
}
}
});
}
}



private static class JsMessage {
final String jsPayloadOrCallbackId;
final PluginResult pluginResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
Expand Down Expand Up @@ -116,7 +117,8 @@ public void runOnUiThread(Runnable r) {
SystemWebViewEngine.this.cordova.getActivity().runOnUiThread(r);
}
}));
bridge = new CordovaBridge(pluginManager, nativeToJsMessageQueue);
nativeToJsMessageQueue.addBridgeMode(new NativeToJsMessageQueue.EvalBridgeMode(this, cordova));
bridge = new CordovaBridge(pluginManager, nativeToJsMessageQueue);
exposeJsInterface(webView, bridge);
}

Expand Down Expand Up @@ -331,4 +333,9 @@ public void destroy() {
}
}
}

@Override
public void evaluateJavascript(String js, ValueCallback<String> callback) {
webView.evaluateJavascript(js, callback);
}
}