Skip to content
Browse files

initial add

  • Loading branch information...
1 parent f55f319 commit 22cb1c45d8c7d000bbe5864749b86e1018118a9d @bthornton32 bthornton32 committed Jun 1, 2012
View
96 Smartphone/GlobalEvent/README.md
@@ -0,0 +1,96 @@
+# Global Event
+This extension will allow the user listen for Global Events (given a GUID). They can also fire Global Events using this extension.
+
+**Author:** [Brent Thornton](https://github.com/bthornton32)
+
+## Tested On
+
+* BlackBerry Bold 9790 v7.1.0.221
+* BlackBerry Bold 9900 v7.1.0.251
+* BlackBerry Bold 9900 v7.0.0.540
+
+**Requires BlackBerry WebWorks SDK for Smartphones v2.0 or higher**
+
+Have a problem with this extension? [Log an Issue](https://github.com/blackberry/WebWorks-Community-APIs/issues) or contact the [Author](https://github.com/bthornton32)
+
+## How To Configure The Extension For Use
+
+1. Locate your BlackBerry WebWorks SDK for Smartphone extensions directory using your File Explorer. Default path is _**C:\Program Files\Research In Motion\BlackBerry WebWorks Packager\ext**_
+
+2. Create a new _**webworks.event.globalEvent**_ directory in the _**ext**_ directory
+
+3. Download the source from this repository and unzip it to a location on your computer
+
+4. Using File Explorer browse to this extension's downloaded source code _**Smartphone\GlobalEvent**_
+
+5. Copy the _**library.xml**_ file from the downloaded _**Smartphone\GlobalEvent**_ directory to your new _**ext\webworks.event.globalEvent**_ directory
+
+6. Copy the downloaded _**Smartphone\GlobalEvent\webworks**_ directory to your new _**ext\webworks.event.globalEvent\webworks**_ directory
+
+**NOTE:** Be sure to back-up this _**ext\webworks.event.globalEvent**_ directory in your WebWorks SDK extensions directory before performing a WebWorks SDK upgrade. Simply copy it back into the _**ext**_ directory after you have completed your SDK upgrade.
+
+## Required Feature ID
+Whenever you use the below feature id in any of your WebWorks applications this extension will be loaded for use.
+
+ <feature id="webworks.event.globalEvent" />
+
+## Summary
+
+ //setup a listener - the myKey string gets converted to a GUID by the extension
+ webworks.event.globalEvent.listen("myKey", myCallBack);
+
+ //callback will fire when GUIDs match. The callback returns the key in case you have multiple listeners setup
+ function myCallBack(mykey){
+ debug("Global Event Occurred! Key=" + mykey);
+ }
+
+ //to fire the event - returns true or false depending on if the fire event was successful or not
+ var myResult = webworks.event.globalEvent.fire("myKey");
+ debug("Fire event: " + myResult);
+
+ **NOTE: debug is just a function that outputs the results to a div on the page.
+
+
+## Code Example
+
+ <html>
+ <head>
+ <meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />
+ <script type="text/javascript" >
+
+ //setup a listener - the myKey string gets converted to a GUID by the extension
+ webworks.event.globalEvent.listen("myKey", myCallBack);
+
+ function doClick(){
+ var myResult = webworks.event.globalEvent.fire("myKey");
+ debug("Fire event: " + myResult);
+ }
+
+ //callback will fire when GUIDs match. The callback returns the key in case you have multiple listeners setup
+ function myCallBack(mykey){
+ debug("Global Event Occurred! Key=" + mykey);
+ }
+
+ function debug(result){
+ document.getElementById('debug').innerHTML += '<br / >' + result;
+ }
+ </script>
+ </head>
+ <body >
+ <h1>Hello World</h1>
+ <button onclick="doClick();">Fire Event</button>
+ <div id="debug"></div>
+ </body>
+ </html>
+
+## Usage Information
+Pass the key as the first parameter to the listen method and the callback function as the second. This will setup a Global Listener for your key. Now any time
+a Global Event is fired with your key the callback will fire. Keep in mind that the string you use for the key actually gets converted to a GUID.
+To Fire a Global Event pass in the key as the first parameter. Any Global Listeners setup to listen for that key will fire.
+
+
+## Disclaimer
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
View
19 Smartphone/GlobalEvent/library.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<library>
+ <extension id="webworks.event.globalEvent">
+ <entryClass>webworks.event.globalEvent.GlobalEventExtension</entryClass>
+ </extension>
+ <platforms>
+ <platform value="JAVA">
+ <target version="default" config="JAVA" />
+ </platform>
+ </platforms>
+ <configurations>
+ <configuration name="JAVA" version="1.0" >
+ <src type="text/java" path="webworks" comment="API implementation in Java" />
+ </configuration>
+ </configurations>
+ <features>
+ <feature id="webworks.event.globalEvent" version="1.0.0" ></feature>
+ </features>
+</library>
View
51 Smartphone/GlobalEvent/webworks/event/globalEvent/GlobalEventExtension.java
@@ -0,0 +1,51 @@
+/*
+* Copyright 2010-2011 Research In Motion Limited.
+*
+* Licensed 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 webworks.event.globalEvent;
+
+import org.w3c.dom.Document;
+import net.rim.device.api.browser.field2.BrowserField;
+import net.rim.device.api.script.ScriptEngine;
+import net.rim.device.api.web.WidgetConfig;
+import net.rim.device.api.web.WidgetExtension;
+
+public final class GlobalEventExtension implements WidgetExtension
+{
+ private String FEATURE_SYSTEM_DISPLAY = "webworks.event.globalEvent";
+ public String[] getFeatureList()
+ {
+ String[] result = new String[] { FEATURE_SYSTEM_DISPLAY };
+ return result;
+ }
+
+ public void loadFeature(String feature, String version, Document doc, ScriptEngine scriptEngine) throws Exception
+ {
+ if (feature.equals(FEATURE_SYSTEM_DISPLAY))
+ {
+ scriptEngine.addExtension(FEATURE_SYSTEM_DISPLAY, new GlobalEventNamespace());
+ }
+ }
+
+ public void register(WidgetConfig config, BrowserField bf)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ public void unloadFeatures(Document doc)
+ {
+ // TODO Auto-generated method stub
+ }
+}
View
77 Smartphone/GlobalEvent/webworks/event/globalEvent/GlobalEventNamespace.java
@@ -0,0 +1,77 @@
+/*
+* Copyright 2010-2011 Research In Motion Limited.
+*
+* Licensed 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 webworks.event.globalEvent;
+
+import net.rim.device.api.script.Scriptable;
+import net.rim.device.api.script.ScriptableFunction;
+import net.rim.device.api.system.Application;
+import net.rim.device.api.system.ApplicationManager;
+import net.rim.device.api.util.StringUtilities;
+
+public class GlobalEventNamespace extends Scriptable
+{
+ public static final String FIELD_LISTEN = "listen";
+ public static final String FIELD_FIRE = "fire";
+ public Object getField(String name) throws Exception
+ {
+ if (name.equals(FIELD_LISTEN))
+ {
+ return new GlobalListen();
+ }
+ if (name.equals(FIELD_FIRE))
+ {
+ return new GlobalFireEvent();
+ }
+ return super.getField(name);
+ }
+
+ public class GlobalListen extends ScriptableFunction
+ {
+ private static final String NAME = "listen";
+ public Object invoke(Object obj, Object[] args) throws Exception
+ {
+ if (args.length == 2)
+ {
+ //the event guid we are listening for will be the first param
+ final String myKey = args[0].toString();
+
+ //the second param will be the callback
+ final ScriptableFunction callback = (ScriptableFunction)args[1];
+
+ Application.getApplication().addGlobalEventListener(new MyGlobalEventListener(myKey, callback));
+ }
+ return UNDEFINED;
+ }
+ }
+
+ public class GlobalFireEvent extends ScriptableFunction
+ {
+ private static final String NAME = "fire";
+ public Object invoke(Object obj, Object[] args) throws Exception
+ {
+ boolean result = false;
+ if (args.length == 1)
+ {
+ long myLong = StringUtilities.stringHashToLong(args[0].toString());
+ result = ApplicationManager.getApplicationManager().postGlobalEvent(myLong);
+
+ }
+ //return if event was successfully posted.
+ return new Boolean(result);
+ }
+ }
+}
View
46 Smartphone/GlobalEvent/webworks/event/globalEvent/MyGlobalEventListener.java
@@ -0,0 +1,46 @@
+package webworks.event.globalEvent;
+
+import net.rim.device.api.script.ScriptableFunction;
+import net.rim.device.api.system.GlobalEventListener;
+import net.rim.device.api.util.StringUtilities;
+
+public class MyGlobalEventListener implements GlobalEventListener{
+ private String _myKey;
+ private long _myGuid;
+ private ScriptableFunction _callback;
+
+ public MyGlobalEventListener(String myKey, ScriptableFunction callback){
+ _myKey = myKey;
+ _myGuid = StringUtilities.stringHashToLong(myKey);
+ _callback = callback;
+ }
+
+ public void eventOccurred(long guid, int data0, int data1, Object object0,
+ Object object1) {
+ System.out.println("Event Occured.");
+ System.out.println("guid: " + Long.toString(guid));
+
+ if (guid == _myGuid){
+ System.out.println("GUID MATCH!");
+
+ // Create a final thread safe result to pass into the thread object
+ final Object[] threadedResult = new Object[] {_myKey};
+
+ // Create a new thread to make sure that the invoke of the JavaScript callback
+ // does not initiate from the UI thread. This can otherwise cause a deadlock scenario
+ new Thread () {
+ public void run() {
+ try
+ {
+ // Pass the result of the picker back to the handle of the JavaScript callback
+ _callback.invoke(_callback, threadedResult);
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+ }.start();
+ }
+ }
+
+}

0 comments on commit 22cb1c4

Please sign in to comment.
Something went wrong with that request. Please try again.