mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +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 action = array.getString(1);
|
||||
String callbackId = array.getString(2);
|
||||
boolean async = array.getBoolean(3);
|
||||
PluginResult r = this.appView.pluginManager.exec(service, action, callbackId, message, async);
|
||||
result.confirm(r == null ? "" : r.getJSONString());
|
||||
result.confirm(this.appView.exposedJsApi.exec(service, action, callbackId, message));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -212,15 +210,13 @@ public class CordovaChromeClient extends WebChromeClient {
|
||||
|
||||
// Sets the native->JS 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("");
|
||||
}
|
||||
|
||||
// Polling for JavaScript messages
|
||||
else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
|
||||
// TODO(agrieve): Use popAll() here.
|
||||
String r = this.appView.jsMessageQueue.pop();
|
||||
result.confirm(r);
|
||||
result.confirm(this.appView.exposedJsApi.retrieveJsMessages());
|
||||
}
|
||||
|
||||
// Do NO-OP so older code doesn't display dialog
|
||||
|
@ -96,6 +96,7 @@ public class CordovaWebView extends WebView {
|
||||
private boolean handleButton = false;
|
||||
|
||||
NativeToJsMessageQueue jsMessageQueue;
|
||||
ExposedJsApi exposedJsApi;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -205,8 +206,6 @@ public class CordovaWebView extends WebView {
|
||||
@SuppressWarnings("deprecation")
|
||||
@SuppressLint("NewApi")
|
||||
private void setup() {
|
||||
jsMessageQueue = new NativeToJsMessageQueue(this, cordova);
|
||||
|
||||
this.setInitialScale(0);
|
||||
this.setVerticalScrollBarEnabled(false);
|
||||
this.requestFocusFromTouch();
|
||||
@ -252,14 +251,10 @@ public class CordovaWebView extends WebView {
|
||||
this.cordova.getActivity().registerReceiver(this.receiver, intentFilter);
|
||||
}
|
||||
// end CB-1405
|
||||
|
||||
//Start up the plugin manager
|
||||
try {
|
||||
this.pluginManager = new PluginManager(this, this.cordova);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
pluginManager = new PluginManager(this, this.cordova);
|
||||
jsMessageQueue = new NativeToJsMessageQueue(this, cordova);
|
||||
exposedJsApi = new ExposedJsApi(pluginManager, jsMessageQueue);
|
||||
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");
|
||||
return;
|
||||
}
|
||||
this.addJavascriptInterface(new Object() {
|
||||
@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");
|
||||
this.addJavascriptInterface(exposedJsApi, "_cordovaNative");
|
||||
}
|
||||
|
||||
/**
|
||||
|
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