cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [2/3] android commit: CB-8210 Use PluginResult for various events from native (close #144)
Date Mon, 05 Jan 2015 21:27:45 GMT
CB-8210 Use PluginResult for various events from native (close #144)

- Change to send events via plugin message channel: various buttons, pause/resume


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/291f1119
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/291f1119
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/291f1119

Branch: refs/heads/master
Commit: 291f111913aff546ea8c27a2c1ce45f1b4667d0f
Parents: c2a6dcb
Author: Jason Chase <jasonpchase@gmail.com>
Authored: Mon Jan 5 14:07:22 2015 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Mon Jan 5 16:15:17 2015 -0500

----------------------------------------------------------------------
 framework/assets/www/cordova.js                 | 21 +++++++++++--
 .../src/org/apache/cordova/CordovaWebView.java  | 32 ++++++++++++++------
 2 files changed, 40 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/291f1119/framework/assets/www/cordova.js
----------------------------------------------------------------------
diff --git a/framework/assets/www/cordova.js b/framework/assets/www/cordova.js
index 0fdae04..4c90865 100644
--- a/framework/assets/www/cordova.js
+++ b/framework/assets/www/cordova.js
@@ -1,5 +1,5 @@
 // Platform: android
-// 12489aed3d93ecc98adfc2091fe566067f2d39fc
+// 07125ef9d481fab81c2c29eafec253846f05a8ca
 /*
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
@@ -1555,12 +1555,27 @@ function onMessageFromNative(msg) {
 
     switch (action)
     {
+        // Button events
+        case 'backbutton':
+        case 'menubutton':
+        case 'searchbutton':
+        // App life cycle events
+        case 'pause':
+        case 'resume':
+        // Keyboard events
         case 'hidekeyboard':
         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;
         default:
-            throw new Error('Unknown event action ' + msg.action);
+            throw new Error('Unknown event action ' + action);
     }
 }
 

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/291f1119/framework/src/org/apache/cordova/CordovaWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java
index 7c10e76..9fc480c 100755
--- a/framework/src/org/apache/cordova/CordovaWebView.java
+++ b/framework/src/org/apache/cordova/CordovaWebView.java
@@ -94,6 +94,7 @@ public class CordovaWebView extends WebView {
     // The URL passed to loadUrl(), not necessarily the URL of the current page.
     String loadedUrl;
     private CordovaPreferences preferences;
+    private App appPlugin;
 
     class ActivityResult {
         
@@ -626,13 +627,13 @@ public class CordovaWebView extends WebView {
         if(boundKeyCodes.contains(keyCode))
         {
             if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
-                    this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
-                    return true;
+                sendJavascriptEvent("volumedownbutton");
+                return true;
             }
             // If volumeup key
             else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
-                    this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
-                    return true;
+                sendJavascriptEvent("volumeupbutton");
+                return true;
             }
             else
             {
@@ -674,7 +675,7 @@ public class CordovaWebView extends WebView {
                 // The webview is currently displayed
                 // If back key is bound, then send event to JavaScript
                 if (isButtonPlumbedToJs(KeyEvent.KEYCODE_BACK)) {
-                    this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
+                    sendJavascriptEvent("backbutton");
                     return true;
                 } else {
                     // If not bound
@@ -689,14 +690,14 @@ public class CordovaWebView extends WebView {
         // Legacy
         else if (keyCode == KeyEvent.KEYCODE_MENU) {
             if (this.lastMenuEventTime < event.getEventTime()) {
-                this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
+                sendJavascriptEvent("menubutton");
             }
             this.lastMenuEventTime = event.getEventTime();
             return super.onKeyUp(keyCode, event);
         }
         // If search key
         else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
-            this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
+            sendJavascriptEvent("searchbutton");
             return true;
         }
 
@@ -704,6 +705,18 @@ public class CordovaWebView extends WebView {
         return super.onKeyUp(keyCode, event);
     }
 
+    private void sendJavascriptEvent(String event) {
+        if (appPlugin == null) {
+            appPlugin = (App)this.pluginManager.getPlugin(App.PLUGIN_NAME);
+        }
+
+        if (appPlugin == null) {
+            LOG.w(TAG, "Unable to fire event without existing plugin");
+            return;
+        }
+        appPlugin.fireJavascriptEvent(event);
+    }
+
     public void setButtonPlumbedToJs(int keyCode, boolean override) {
         switch (keyCode) {
             case KeyEvent.KEYCODE_VOLUME_DOWN:
@@ -757,7 +770,7 @@ public class CordovaWebView extends WebView {
     {
         LOG.d(TAG, "Handle the pause");
         // 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
         if (this.pluginManager != null) {
@@ -775,9 +788,8 @@ public class CordovaWebView extends WebView {
     
     public void handleResume(boolean keepRunning, boolean activityResultKeepRunning)
     {
+        sendJavascriptEvent("resume");
 
-        this.loadUrl("javascript:try{cordova.fireDocumentEvent('resume');}catch(e){console.log('exception
firing resume event from native');};");
-        
         // Forward to plugins
         if (this.pluginManager != null) {
             this.pluginManager.onResume(keepRunning);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message