incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [30/35] git commit: Add button overriding and lifecycle management.
Date Thu, 23 Feb 2012 17:53:19 GMT
Add button overriding and lifecycle management.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/26c05a3f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/26c05a3f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/26c05a3f

Branch: refs/heads/ios
Commit: 26c05a3f12f40e154aae2c37a35bd148b667dece
Parents: 1f869b1
Author: Drew Walters <deedubbu@gmail.com>
Authored: Thu Feb 16 11:07:32 2012 -0600
Committer: Drew Walters <deedubbu@gmail.com>
Committed: Thu Feb 16 11:07:32 2012 -0600

----------------------------------------------------------------------
 lib/builder.js               |    6 +++-
 lib/platform/blackberry.js   |   63 ++++++++++++++++++++++++++++++++++---
 lib/plugin/blackberry/app.js |   51 ++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/26c05a3f/lib/builder.js
----------------------------------------------------------------------
diff --git a/lib/builder.js b/lib/builder.js
index ada7322..ecc1be2 100644
--- a/lib/builder.js
+++ b/lib/builder.js
@@ -12,7 +12,11 @@ function include(parent, objects, clobber) {
           var result = obj.path ? require(obj.path) : {};
 
           if (clobber) {
-            parent[key] = obj.path ? result : parent[key];
+              // Set the value if it doesn't exist or if an override is specified.
+              if (typeof parent[key] === 'undefined' || typeof obj.path !== 'undefined')
{
+                  parent[key] = result;
+              }
+              result = parent[key];
           } else {
             // Don't clobber if something already exists there
             if (typeof parent[key] == 'undefined') {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/26c05a3f/lib/platform/blackberry.js
----------------------------------------------------------------------
diff --git a/lib/platform/blackberry.js b/lib/platform/blackberry.js
index ff20cda..de4e694 100644
--- a/lib/platform/blackberry.js
+++ b/lib/platform/blackberry.js
@@ -4,7 +4,8 @@ module.exports = {
         var cordova = require('cordova'),
             exec = require('cordova/exec'),
             channel = require('cordova/channel'),
-            blackberryManager = require('cordova/plugin/blackberry/manager');
+            blackberryManager = require('cordova/plugin/blackberry/manager'),
+            app = require('cordova/plugin/blackberry/app');
 
         // TODO: is there a better way to do this? build-time
         // convention? how can we save the few bytes from
@@ -20,10 +21,50 @@ module.exports = {
         DirectoryEntry.prototype.getDirectory = BB_DirectoryEntry.getDirectory;
         DirectoryEntry.prototype.getFile = BB_DirectoryEntry.getFile;
 
-        // Listeners for hardware buttons
-        cordova.addDocumentEventHandler('backbutton');
-        cordova.addDocumentEventHandler('menubutton');
-        cordova.addDocumentEventHandler('searchbutton');
+        // Mapping of button events to BlackBerry key identifier.
+        var buttonMapping = {
+            'backbutton'         : blackberry.system.event.KEY_BACK,
+            'conveniencebutton1' : blackberry.system.event.KEY_CONVENIENCE_1,
+            'conveniencebutton2' : blackberry.system.event.KEY_CONVENIENCE_2,
+            'endcallbutton'      : blackberry.system.event.KEY_ENDCALL,
+            'menubutton'         : blackberry.system.event.KEY_MENU,
+            'startcallbutton'    : blackberry.system.event.KEY_STARTCALL,
+            'volumedownbutton'   : blackberry.system.event.KEY_VOLUMEDOWN,
+            'volumeupbutton'     : blackberry.system.event.KEY_VOLUMEUP
+        };
+
+        // Generates a function which fires the specified event.
+        var fireEvent = function(event) {
+            return function() {
+                cordova.fireDocumentEvent(event, null);
+            };
+        };
+
+        var eventHandler = function(event) {
+            return { onSubscribe : function() {
+                // If we just attached the first handler, let native know we
+                // need to override the back button.
+                if (this.handlers.length === 1) {
+                    blackberry.system.event.onHardwareKey(
+                            buttonMapping[event], fireEvent(event));
+                }
+            },
+            onUnsubscribe : function() {
+                // If we just detached the last handler, let native know we
+                // no longer override the back button.
+                if (this.handlers.length === 0) {
+                    blackberry.system.event.onHardwareKey(
+                            buttonMapping[event], null);
+                }
+            }}
+        }
+
+        // Inject listeners for buttons on the document.
+        for (var button in buttonMapping) {
+            if (buttonMapping.hasOwnProperty(button)) {
+                cordova.addDocumentEventHandler(button, eventHandler(button));
+            }
+        }
 
         // Fires off necessary shite to pause/resume app
         var resume = function() {
@@ -91,12 +132,24 @@ module.exports = {
             },
             onUnsubscribe:onUnsubscribe
         });
+
+        // Fire resume event when application brought to foreground.
+        blackberry.app.event.onForeground(resume);
+
+        // Fire pause event when application sent to background.
+        blackberry.app.event.onBackground(pause);
+
+        // Trap BlackBerry WebWorks exit. Allow plugins to clean up before exiting.
+        blackberry.app.event.onExit(app.exitApp);
     },
     objects: {
         navigator: {
             children: {
                 device: {
                     path: "cordova/plugin/blackberry/device"
+                },
+                app: {
+                    path: "cordova/plugin/blackberry/app"
                 }
             }
         },

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/26c05a3f/lib/plugin/blackberry/app.js
----------------------------------------------------------------------
diff --git a/lib/plugin/blackberry/app.js b/lib/plugin/blackberry/app.js
new file mode 100644
index 0000000..6314352
--- /dev/null
+++ b/lib/plugin/blackberry/app.js
@@ -0,0 +1,51 @@
+var exec = require('cordova/exec');
+var manager = require('cordova/plugin/blackberry/manager');
+
+module.exports = {
+  /**
+   * Clear the resource cache.
+   */
+  clearCache:function() {
+      if (typeof blackberry.widgetcache === "undefined"
+          || blackberry.widgetcache === null) {
+          console.log("blackberry.widgetcache permission not found. Cache clear denied.");
+          return;
+      }
+      blackberry.widgetcache.clearAll();
+  },
+
+  /**
+   * Clear web history in this web view.
+   * Instead of BACK button loading the previous web page, it will exit the app.
+   */
+  clearHistory:function() {
+    exec(null, null, "App", "clearHistory", []);
+  },
+
+  /**
+   * Go to previous page displayed.
+   * This is the same as pressing the backbutton on Android device.
+   */
+  backHistory:function() {
+    // window.history.back() behaves oddly on BlackBerry, so use
+    // native implementation.
+    exec(null, null, "App", "backHistory", []);
+  },
+
+  /**
+   * Exit and terminate the application.
+   */
+  exitApp:function() {
+      // Call onunload if it is defined since BlackBerry does not invoke
+      // on application exit.
+      if (typeof window.onunload === "function") {
+          window.onunload();
+      }
+
+      // allow Cordova JavaScript Extension opportunity to cleanup
+      manager.destroy()
+
+      // exit the app
+      blackberry.app.exit();
+  }
+};


Mime
View raw message