mirror of
https://github.com/apache/cordova-android.git
synced 2026-05-11 00:00:05 +08:00
CB-8210 Use PluginResult instead of sendJavascript() for keyboard events (close #142)
- Initialize a message channel for native -> Javascript in the core App plugin - Change keyboard detection to send events via plugin message channel, instead using eval() (i.e. webView.sendJavascript())
This commit is contained in:
committed by
Andrew Grieve
parent
b10fe465ab
commit
3439746645
@@ -41,8 +41,19 @@ import java.util.HashMap;
|
||||
*/
|
||||
public class App extends CordovaPlugin {
|
||||
|
||||
public static final String PLUGIN_NAME = "App";
|
||||
protected static final String TAG = "CordovaApp";
|
||||
private BroadcastReceiver telephonyReceiver;
|
||||
private CallbackContext messageChannel;
|
||||
|
||||
/**
|
||||
* Send an event to be fired on the Javascript side.
|
||||
*
|
||||
* @param action The name of the event to be fired
|
||||
*/
|
||||
public void fireJavascriptEvent(String action) {
|
||||
sendEventMessage(action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the context of the Command. This can then be used to do things like
|
||||
@@ -100,6 +111,11 @@ public class App extends CordovaPlugin {
|
||||
else if (action.equals("exitApp")) {
|
||||
this.exitApp();
|
||||
}
|
||||
else if (action.equals("messageChannel")) {
|
||||
messageChannel = callbackContext;
|
||||
return true;
|
||||
}
|
||||
|
||||
callbackContext.sendPluginResult(new PluginResult(status, result));
|
||||
return true;
|
||||
} catch (JSONException e) {
|
||||
@@ -249,7 +265,7 @@ public class App extends CordovaPlugin {
|
||||
public void exitApp() {
|
||||
this.webView.postMessage("exit", null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Listen for telephony events: RINGING, OFFHOOK and IDLE
|
||||
@@ -290,6 +306,18 @@ public class App extends CordovaPlugin {
|
||||
webView.getContext().registerReceiver(this.telephonyReceiver, intentFilter);
|
||||
}
|
||||
|
||||
private void sendEventMessage(String action) {
|
||||
JSONObject obj = new JSONObject();
|
||||
try {
|
||||
obj.put("action", action);
|
||||
} catch (JSONException e) {
|
||||
LOG.e(TAG, "Failed to create event message", e);
|
||||
}
|
||||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, obj);
|
||||
pluginResult.setKeepCallback(true);
|
||||
messageChannel.sendPluginResult(pluginResult);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unregister the receiver
|
||||
*
|
||||
|
||||
@@ -155,7 +155,7 @@ public class CordovaWebView extends WebView {
|
||||
bridge = new CordovaBridge(pluginManager, new NativeToJsMessageQueue(this, cordova), this.cordova.getActivity().getPackageName());
|
||||
resourceApi = new CordovaResourceApi(this.getContext(), pluginManager);
|
||||
|
||||
pluginManager.addService("App", "org.apache.cordova.App");
|
||||
pluginManager.addService(App.PLUGIN_NAME, "org.apache.cordova.App");
|
||||
// This will be removed in 4.0.x in favour of the plugin not being bundled.
|
||||
pluginManager.addService(new PluginEntry("SplashScreenInternal", "org.apache.cordova.SplashScreenInternal", true));
|
||||
pluginManager.init();
|
||||
|
||||
@@ -18,10 +18,7 @@
|
||||
*/
|
||||
package org.apache.cordova;
|
||||
|
||||
import org.apache.cordova.LOG;
|
||||
|
||||
import android.content.Context;
|
||||
//import android.view.View.MeasureSpec;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
/**
|
||||
@@ -36,6 +33,7 @@ public class LinearLayoutSoftKeyboardDetect extends LinearLayout {
|
||||
private int screenWidth = 0;
|
||||
private int screenHeight = 0;
|
||||
private CordovaActivity app = null;
|
||||
private App appPlugin = null;
|
||||
|
||||
public LinearLayoutSoftKeyboardDetect(Context context, int width, int height) {
|
||||
super(context);
|
||||
@@ -50,7 +48,7 @@ public class LinearLayoutSoftKeyboardDetect extends LinearLayout {
|
||||
* gets smaller fire a show keyboard event and when height gets bigger fire
|
||||
* a hide keyboard event.
|
||||
*
|
||||
* Note: We are using app.postMessage so that this is more compatible with the API
|
||||
* Note: We are using the core App plugin to send events over the bridge to Javascript
|
||||
*
|
||||
* @param widthMeasureSpec
|
||||
* @param heightMeasureSpec
|
||||
@@ -87,14 +85,12 @@ public class LinearLayoutSoftKeyboardDetect extends LinearLayout {
|
||||
// If the height as gotten bigger then we will assume the soft keyboard has
|
||||
// gone away.
|
||||
else if (height > oldHeight) {
|
||||
if (app != null)
|
||||
app.appView.sendJavascript("cordova.fireDocumentEvent('hidekeyboard');");
|
||||
sendEvent("hidekeyboard");
|
||||
}
|
||||
// If the height as gotten smaller then we will assume the soft keyboard has
|
||||
// If the height as gotten smaller then we will assume the soft keyboard has
|
||||
// been displayed.
|
||||
else if (height < oldHeight) {
|
||||
if (app != null)
|
||||
app.appView.sendJavascript("cordova.fireDocumentEvent('showkeyboard');");
|
||||
sendEvent("showkeyboard");
|
||||
}
|
||||
|
||||
// Update the old height for the next event
|
||||
@@ -102,4 +98,15 @@ public class LinearLayoutSoftKeyboardDetect extends LinearLayout {
|
||||
oldWidth = width;
|
||||
}
|
||||
|
||||
private void sendEvent(String event) {
|
||||
if (appPlugin == null) {
|
||||
appPlugin = (App)app.appView.pluginManager.getPlugin(App.PLUGIN_NAME);
|
||||
}
|
||||
|
||||
if (appPlugin == null) {
|
||||
LOG.w(TAG, "Unable to fire event without existing plugin");
|
||||
return;
|
||||
}
|
||||
appPlugin.fireJavascriptEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user