mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Abstract JS->Native API calls into a class.
-setNativeToJsBridgeMode() and poll() can now be used via the JS interface exported via addJavascriptInterface. -prompt() now forwards calls to this class so that the logic will be the same whether prompt() or the JS object is used.
This commit is contained in:
parent
0a669077fb
commit
65a397fb63
@ -202,9 +202,7 @@ public class CordovaChromeClient extends WebChromeClient {
|
|||||||
String service = array.getString(0);
|
String service = array.getString(0);
|
||||||
String action = array.getString(1);
|
String action = array.getString(1);
|
||||||
String callbackId = array.getString(2);
|
String callbackId = array.getString(2);
|
||||||
boolean async = array.getBoolean(3);
|
result.confirm(this.appView.exposedJsApi.exec(service, action, callbackId, message));
|
||||||
PluginResult r = this.appView.pluginManager.exec(service, action, callbackId, message, async);
|
|
||||||
result.confirm(r == null ? "" : r.getJSONString());
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -212,15 +210,13 @@ public class CordovaChromeClient extends WebChromeClient {
|
|||||||
|
|
||||||
// Sets the native->JS bridge mode.
|
// Sets the native->JS bridge mode.
|
||||||
else if (reqOk && defaultValue != null && defaultValue.equals("gap_bridge_mode:")) {
|
else if (reqOk && defaultValue != null && defaultValue.equals("gap_bridge_mode:")) {
|
||||||
this.appView.jsMessageQueue.setBridgeMode(Integer.parseInt(message));
|
this.appView.exposedJsApi.setNativeToJsBridgeMode(Integer.parseInt(message));
|
||||||
result.confirm("");
|
result.confirm("");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Polling for JavaScript messages
|
// Polling for JavaScript messages
|
||||||
else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
|
else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
|
||||||
// TODO(agrieve): Use popAll() here.
|
result.confirm(this.appView.exposedJsApi.retrieveJsMessages());
|
||||||
String r = this.appView.jsMessageQueue.pop();
|
|
||||||
result.confirm(r);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do NO-OP so older code doesn't display dialog
|
// Do NO-OP so older code doesn't display dialog
|
||||||
|
@ -96,6 +96,7 @@ public class CordovaWebView extends WebView {
|
|||||||
private boolean handleButton = false;
|
private boolean handleButton = false;
|
||||||
|
|
||||||
NativeToJsMessageQueue jsMessageQueue;
|
NativeToJsMessageQueue jsMessageQueue;
|
||||||
|
ExposedJsApi exposedJsApi;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -205,8 +206,6 @@ public class CordovaWebView extends WebView {
|
|||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
private void setup() {
|
private void setup() {
|
||||||
jsMessageQueue = new NativeToJsMessageQueue(this, cordova);
|
|
||||||
|
|
||||||
this.setInitialScale(0);
|
this.setInitialScale(0);
|
||||||
this.setVerticalScrollBarEnabled(false);
|
this.setVerticalScrollBarEnabled(false);
|
||||||
this.requestFocusFromTouch();
|
this.requestFocusFromTouch();
|
||||||
@ -252,14 +251,10 @@ public class CordovaWebView extends WebView {
|
|||||||
this.cordova.getActivity().registerReceiver(this.receiver, intentFilter);
|
this.cordova.getActivity().registerReceiver(this.receiver, intentFilter);
|
||||||
}
|
}
|
||||||
// end CB-1405
|
// end CB-1405
|
||||||
|
|
||||||
//Start up the plugin manager
|
pluginManager = new PluginManager(this, this.cordova);
|
||||||
try {
|
jsMessageQueue = new NativeToJsMessageQueue(this, cordova);
|
||||||
this.pluginManager = new PluginManager(this, this.cordova);
|
exposedJsApi = new ExposedJsApi(pluginManager, jsMessageQueue);
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
exposeJsInterface();
|
exposeJsInterface();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,13 +268,7 @@ public class CordovaWebView extends WebView {
|
|||||||
Log.i(TAG, "Disabled addJavascriptInterface() bridge callback due to a bug on the 2.3 emulator");
|
Log.i(TAG, "Disabled addJavascriptInterface() bridge callback due to a bug on the 2.3 emulator");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.addJavascriptInterface(new Object() {
|
this.addJavascriptInterface(exposedJsApi, "_cordovaNative");
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public String exec(String service, String action, String callbackId, String arguments) throws JSONException {
|
|
||||||
PluginResult r = pluginManager.exec(service, action, callbackId, arguments, true /* async */);
|
|
||||||
return r == null ? "" : r.getJSONString();
|
|
||||||
}
|
|
||||||
}, "_cordovaExec");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
53
framework/src/org/apache/cordova/ExposedJsApi.java
Executable file
53
framework/src/org/apache/cordova/ExposedJsApi.java
Executable file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
or more contributor license agreements. See the NOTICE file
|
||||||
|
distributed with this work for additional information
|
||||||
|
regarding copyright ownership. The ASF licenses this file
|
||||||
|
to you under the Apache License, Version 2.0 (the
|
||||||
|
"License"); you may not use this file except in compliance
|
||||||
|
with the License. You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing,
|
||||||
|
software distributed under the License is distributed on an
|
||||||
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
KIND, either express or implied. See the License for the
|
||||||
|
specific language governing permissions and limitations
|
||||||
|
under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.cordova;
|
||||||
|
|
||||||
|
import org.apache.cordova.api.PluginManager;
|
||||||
|
import org.apache.cordova.api.PluginResult;
|
||||||
|
import org.json.JSONException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains APIs that the JS can call. All functions in here should also have
|
||||||
|
* an equivalent entry in CordovaChromeClient.java, and be added to
|
||||||
|
* cordova-js/lib/android/plugin/android/promptbasednativeapi.js
|
||||||
|
*/
|
||||||
|
/* package */ class ExposedJsApi {
|
||||||
|
|
||||||
|
private PluginManager pluginManager;
|
||||||
|
private NativeToJsMessageQueue jsMessageQueue;
|
||||||
|
|
||||||
|
public ExposedJsApi(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue) {
|
||||||
|
this.pluginManager = pluginManager;
|
||||||
|
this.jsMessageQueue = jsMessageQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String exec(String service, String action, String callbackId, String arguments) throws JSONException {
|
||||||
|
PluginResult r = pluginManager.exec(service, action, callbackId, arguments, true /* async */);
|
||||||
|
return r == null ? "" : r.getJSONString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNativeToJsBridgeMode(int value) {
|
||||||
|
jsMessageQueue.setBridgeMode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String retrieveJsMessages() {
|
||||||
|
// TODO(agrieve): Use popAll() here.
|
||||||
|
return jsMessageQueue.pop();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user