Merge branch 'master' into 4.0.x (loadUrl->sendEvent)

Conflicts:
	framework/src/org/apache/cordova/CordovaWebView.java
This commit is contained in:
Andrew Grieve 2015-01-05 16:25:09 -05:00
commit 311bdbd360
2 changed files with 38 additions and 10 deletions

View File

@ -1,5 +1,5 @@
// Platform: android // Platform: android
// 12489aed3d93ecc98adfc2091fe566067f2d39fc // 07125ef9d481fab81c2c29eafec253846f05a8ca
/* /*
Licensed to the Apache Software Foundation (ASF) under one Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file or more contributor license agreements. See the NOTICE file
@ -1555,12 +1555,27 @@ function onMessageFromNative(msg) {
switch (action) switch (action)
{ {
// Button events
case 'backbutton':
case 'menubutton':
case 'searchbutton':
// App life cycle events
case 'pause':
case 'resume':
// Keyboard events
case 'hidekeyboard': case 'hidekeyboard':
case 'showkeyboard': case 'showkeyboard':
cordova.fireDocumentEvent(action); // Volume events
case 'volumedownbutton':
case 'volumeupbutton':
try {
cordova.fireDocumentEvent(action);
} catch(e) {
console.log('exception firing ' + action + ' event from native:' + e);
}
break; break;
default: default:
throw new Error('Unknown event action ' + msg.action); throw new Error('Unknown event action ' + action);
} }
} }

View File

@ -89,6 +89,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
private Whitelist internalWhitelist; private Whitelist internalWhitelist;
private Whitelist externalWhitelist; private Whitelist externalWhitelist;
private CordovaPreferences preferences; private CordovaPreferences preferences;
private CoreAndroid appPlugin;
// The URL passed to loadUrl(), not necessarily the URL of the current page. // The URL passed to loadUrl(), not necessarily the URL of the current page.
String loadedUrl; String loadedUrl;
@ -471,11 +472,11 @@ public class AndroidWebView extends WebView implements CordovaWebView {
if(boundKeyCodes.contains(keyCode)) if(boundKeyCodes.contains(keyCode))
{ {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');"); sendJavascriptEvent("volumedownbutton");
return true; return true;
} }
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');"); sendJavascriptEvent("volumeupbutton");
return true; return true;
} }
else else
@ -519,7 +520,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
// The webview is currently displayed // The webview is currently displayed
// If back key is bound, then send event to JavaScript // If back key is bound, then send event to JavaScript
if (isButtonPlumbedToJs(KeyEvent.KEYCODE_BACK)) { if (isButtonPlumbedToJs(KeyEvent.KEYCODE_BACK)) {
this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');"); sendJavascriptEvent("backbutton");
return true; return true;
} else { } else {
// If not bound // If not bound
@ -534,14 +535,14 @@ public class AndroidWebView extends WebView implements CordovaWebView {
// Legacy // Legacy
else if (keyCode == KeyEvent.KEYCODE_MENU) { else if (keyCode == KeyEvent.KEYCODE_MENU) {
if (this.lastMenuEventTime < event.getEventTime()) { if (this.lastMenuEventTime < event.getEventTime()) {
this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');"); sendJavascriptEvent("menubutton");
} }
this.lastMenuEventTime = event.getEventTime(); this.lastMenuEventTime = event.getEventTime();
return super.onKeyUp(keyCode, event); return super.onKeyUp(keyCode, event);
} }
// If search key // If search key
else if (keyCode == KeyEvent.KEYCODE_SEARCH) { else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');"); sendJavascriptEvent("searchbutton");
return true; return true;
} }
@ -549,6 +550,18 @@ public class AndroidWebView extends WebView implements CordovaWebView {
return super.onKeyUp(keyCode, event); return super.onKeyUp(keyCode, event);
} }
private void sendJavascriptEvent(String event) {
if (appPlugin == null) {
appPlugin = (CoreAndroid)this.pluginManager.getPlugin(CoreAndroid.PLUGIN_NAME);
}
if (appPlugin == null) {
LOG.w(TAG, "Unable to fire event without existing plugin");
return;
}
appPlugin.fireJavascriptEvent(event);
}
@Override @Override
public void setButtonPlumbedToJs(int keyCode, boolean override) { public void setButtonPlumbedToJs(int keyCode, boolean override) {
switch (keyCode) { switch (keyCode) {
@ -577,7 +590,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
{ {
LOG.d(TAG, "Handle the pause"); LOG.d(TAG, "Handle the pause");
// Send pause event to JavaScript // Send pause event to JavaScript
this.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};"); sendJavascriptEvent("pause");
// Forward to plugins // Forward to plugins
if (this.pluginManager != null) { if (this.pluginManager != null) {
@ -593,7 +606,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
public void handleResume(boolean keepRunning, boolean activityResultKeepRunning) public void handleResume(boolean keepRunning, boolean activityResultKeepRunning)
{ {
this.loadUrl("javascript:try{cordova.fireDocumentEvent('resume');}catch(e){console.log('exception firing resume event from native');};"); sendJavascriptEvent("resume");
// Forward to plugins // Forward to plugins
if (this.pluginManager != null) { if (this.pluginManager != null) {