* Mobile Geolocation Implementations

Posted on August 19th, 2010 by Dave Johnson. Filed under Uncategorized, phonegap.

Today I found some time to take a look at the various mobile implementations of geolocation.

Below is a table showing the properties that the geolocation API uses on the various platforms. The W3C geolocation API for browsers specifies a PositionOptions interface that requires a maximum age, timeout and accuracy. Below that row is all of the mobile platforms that we are interested in for PhoneGap and the properties that they support in their native implementations. Some of them support a distance filter that will cause the device to only return geolocation positions if the location has changed by more than that distance while others also support an explicit interval for watching for location changes (note that on Android that interval is only a hint to let the device know how much power to use in getting a location - the position may be reported at an interval either shorter or longer than the specified interval).

I am probably going to be adding these two additional arguments to the PhoneGap PositionOptions and they can be used on platforms that support them.

Max age Timeout Accuracy Distance Interval
BlackBerry maxAge timeout - - interval
iPhone - - desiredAccuracy distanceFilter -
Android - - - minDistance minTime
webOS maximumAge responseTime accuracy - -
WRT updateMaxAge updateTimeout - - updateInterval
Winphone - - DesiredAccuracy MovementThreshold

Image from cloneofsnake

Tags: , , , .

* PhoneGap Native Bridge

Posted on August 13th, 2010 by Dave Johnson. Filed under JavaScript, mobile, phonegap.

The “native bridge” is the secret sauce of PhoneGap and is what allows JavaScript in an embedded browser talk to native code and vice-versa.

On every platform we do this differently depending on what features that native browser has. Here is the list of platforms and how we do it.

iPhone: To communicate from JavaScript to native we call window.location = “gap://Class.method/args” and then intercept the url in native code and parse our the parameters. The class / method is then dynamically loaded / called. To call JavaScript from native we use UIWebView.stringByEvaluatingJavaScriptFromString.

Android: Communication from JavaScript to native takes advantage of the native WebView.addJavascriptInterface to expose Java objects directly to JavaScript. To call JavaScript from native we currently use WebView.loadUrl(”javascript:…”) but that has some problems so we are soon moving over to polling a Java message queue calling a local HTTP server via a long-lived XHR connection.

BlackBerry 4.x: document.cookie is the only way to talk between JavaScript and native. JavaScript sets the cookie for native to read and JavaScript polls the cookie for messages from native. Fucking elegant!

BlackBerry Widgets: Astoundingly the new BlackBerry Widgets SDK is pretty damn nice. You can expose Java objects to JavaScript with ScriptEngine.addExtension and you can call JavaScript from native with ScriptEngine.executeScript. The context for the JavaScript execution can even be specified. Seriously great work there.

QT: This one is not quite ready but it will use the QWebFrame.addToJavaScriptWindowObject to expose native objects to JavaScript and will likely use polling of those exposed native objects to get data back into JavaScript - much like the Android approach

Windows Phone 7: There is something called window.external.Notify in the browser (mapped to a native object through the ScriptNotify event) on Windows Phone 7 that we use to send messages into native code. Going the other way there is WebBrowser.InvokeScript.

webOS, WRT: The platforms that are just JavaScript don’t have any native code. Well, we might look into the native side of webOS one day but no time soon.

In an attempt to clean up the PhoneGap JavaScript API a bit, I have isolated all the places that we communicate from JavaScript to native code and put it into a single PhoneGap class. I am now in the process of converting over all the JavaScript to use this new PhoneGap interface and then most of the PhoneGap JavaScript will be completely cross platform. Any call to native code now has to go through the PhoneGap.exec() JavaScript method. The new code is up on Github and with any luck all the platforms will be using it soon!

Image by hb2

Tags: , , .

* PhoneGap and BlackBerry OS 6

Posted on August 5th, 2010 by Dave Johnson. Filed under Uncategorized.

RIM OS 6 for BlackBerry smartphones is being released very soon on the new BlackBerry Torch 9800.

It looks like a pretty nice upgrade from OS 5, most notably including a WebKit based browser. This should be a *huge* improvement for PhoneGap on BlackBerry.

The bad news is that OS 6 will not work on a lot of the older hardware. OS 6 will run on the following:

  • Storm 3 (as yet unreleased)
  • Bold 9700
  • Bold 9650
  • Perl 9100

Notable devices missing from that list are the original Bold 9000 and the Storm 1 and 2. Those will be stuck on OS 5.

The kicker here is that anecdotally, and based on AdMob stats from April 2010, the most widely used RIM devices (remember AdMob stats represent devices that ads are being served to) are from the BlackBerry Curve line 8300 and 8520 in the US and UK respectively.

The tentative plan for PhoneGap on BlackBerry is likely going to be a “final” release for BlackBerry 4.6 / 4.7 tagged on Github (sometime in the near future) and then shifting focus to BlackBerry Widgets on OS 5 and 6.


* BlackBerry JavaScript Oddities

Posted on July 18th, 2010 by Dave Johnson. Filed under Uncategorized.

I have just discovered that the Element object in the BlackBerry browser (4.6+) supports the prototype object on some objects but not others. In particular the Element object supports it like this:

Element.prototype.addEventListener = function() {

but the following does not work on the Document object:

Document.prototype.addEventListener = function() {

What does work on the Document is overriding instance method like this:

document.addEventListener = function() {

Strange but true!

If you have any other BlackBerry tidbits please let me know since these old versions of BlackBerry browser are going to be the IE6 of the the mobile web.


* JavaScript Event Merging

Posted on July 14th, 2010 by Dave Johnson. Filed under JavaScript.

In a recent project I finally decided to write something to help with JavaScript event merging. There are often cases in JavaScript where you want some code to run but only after certain events have fired or other code run - in my case I wanted to execute a JavaScript function only after two different bits of code had executed. The most common use case for event merging I have come across has got to be on page load / DOM ready where you may need several conditions to be true before executing some JavaScript code.

Event merging generally helps you to better separate your concerns and not have unwieldy if conditions strewn throughout your code checking if certain condi.

If you have a basic JavaScript object called the “Subject” that has subscribe, subscribeOnce (events that auto-unsubscribe themselves after being fired the first time), and notify methods on them. You can use the following merge function to have all subscribed code executed only after each of the events passed to the merge function are fired. In this case I have explicitly used the subscribeOnce method on the event.

function merge(handler, events) {
       var i = events.length;
       var f = function() {
           if (!(--i)) handler();
       for (var j=0; j<i; j++) {
           var e = events[j];
       if (!i) handler();

When the merge function is called it iterates over all the events in the event array argument and checks if that event has already been fired or not by checking the “fired” property on the event - note that this makes it so that it doesn’t matter if merge is called before or after the events have fired. If the event has not been fired yet then a handler is attached using subscribeOnce where the handler checks a counter indicating how many of the events have been fired, once the counter hits 0 then the final handler for the merged events is called.

I have put up an entire event merging code on Github here.

Tags: .

* Smart Grid Utilities

Posted on June 22nd, 2010 by Dave Johnson. Filed under smart grid.

The “smart grid” is a pretty popular term these days is used to describe a new generation of electricity distribution network or grid where electricity usage at by the end user is tracked in real time. Today, tracking of electricity is done by prediction until the utility physically sends a person to the consumers location who then reads the value on an electricity meter. This new real time tracking of usage also means that the consumer can not only see their usage at any given time, but they can also see the price of electricity at that time allowing them to make decisions to try and save money - generally that means using electricity at night when rates are lowest.

If there is one thing that is clear, it is that the smart grid is going to be built on Internet standards like TCP/IP, the only question is what medium it will use. It might be WiFi, mesh networks, power line communication, cell networks, radio, or just plain old broadband.

The interesting part is that however the data gets from your house or business to the utility, it will have to be over a TCP/IP network that could conceivably have enough bandwidth to also provide you with services ranging from Internet to phone to television - as many telecommunication companies do today.

With enough bandwidth a smart grid utility could provide ALL of your utilities.

Companies like Google are certainly ready to be the provider of all the software a utility might need on top of an Internet connection to be your single utility provider. With Chrome OS or Android as the nervous system of a smart house to Android on your mobile phone, TV, Voice and PowerMeter. These pieces of software could even come from various providers like Skype or Microsoft but certainly using a battle hardened, open source, developer friendly operating system like Android at the core makes sense. Beyond that they should also be taking advantage of the increasingly powerful smartphones like the Android, iPhone and Blackberry and using them as the user interface to all of their smart systems.

In the past the stringent security and reliability requirements placed on utilities probably forced them to write their own software preferring security through obscurity rather than using open source software that probably didn’t even exist. Today is different though and there are many open source software projects that can likely stand up to their requirements.

If utilities were as smart as they hope their grid will be, they would be ensuring that their smart grid offering gives them the ability to offer other value added services to their customers just as other communications companies have either moved from providing broadband to providing voice services or vice versa.

Image credit Tom Raftery

Tags: , , , .

* PhoneGap Desktop

Posted on April 20th, 2010 by Dave Johnson. Filed under phonegap.

So if PhoneGap was running on the desktop, what would you call it? Well I have not come up with a good name yet but after a few nights of hacking we threw together a version of PhoneGap for Mac OS and a version for Windows

@shazron has implemented the sound API on the Mac version and I did Geolocation on the Windows version using a simple GeoIP lookup.

Check it out, write some code, and let us know what you think!

For the Windows version I have used the awesome WebKit .NET project written by Peter Nelson. If you use the Visual Studio 2008 solution file, you will have to download the source or binary versions of WebKit .NET and setup the references correctly. I have so far used the 0.3 release of WebKit .NET. It is pretty cool being able to write a native Windows app (or Mac OS) in Canvas using all free and open source code :)

One other caveat on the Windows side is that you might need to install some Visual Studio C++ redistributables for building it … check the WebKit .NET forum on SourceForge for all the details.

Tags: , , .

* Crockford Facts

Posted on March 18th, 2010 by Dave Johnson. Filed under JavaScript.

Everything that you want to know (or not) about Douglas Crockford.

Or following things live on Twitter.

I contributed a few of my own like:

“Crockford doesn’t throw errors, he roundhouse kicks them.”

Andrew Lunny also directed me to this site with some Resig facts … hilarity!

Tags: .

* QUnit Server

Posted on March 1st, 2010 by Dave Johnson. Filed under Components, JavaScript, Testing.

QUnit is a great little JavaScript test framework written by none other than John Resig of jQuery fame.

I love how easy it is to write tests, especially asynchronous ones, with QUnit and so was quick to start using it for my JavaScript tests.

The one thing that I found it lacked was support for continuous integration products like Hudson, CruiseControl or Continuum. So I figured that I would change that.

For a long time I have been using JsUnit along with JsUnit Server to merge in client side JavaScript test results with a server side test integration workflow. Essentially, JsUnit Server is a Jetty server and JsUnit posts back a form with test results to that server, which logs the results to standard JUnit formatted test results files. Those files can then be read by most continuous integration products and included in their reporting.

What I decided to do was hack QUnit so that it too could submit test results back to the JsUnit Server in the same format that JsUnit submits results and could therefore continue using the same JsUnit Server workflow and simply change the client JavaScript test framework to QUnit. It worked like a charm!

I wrote a couple of simple function that in the background dynamically create an HTML form in the QUnit test page and add the results to the form as each test is run. When the tests are complete the page submits back to the JsUnit Server and the test results are logged as they were when I was using JsUnit.

It is some pretty simple changes but does make using QUnit that much better, especially if you were hesitant to switch just because the continuous integration support was not there for QUnit.

Check it out on GitHub.

Tags: , , , .

* Require RubyGems

Posted on February 19th, 2010 by Dave Johnson. Filed under ruby.

I have never really written much ruby outside of a Rails app and so when I wanted to write a short script for a cron job.

I kept getting an error like:

‘require’: no such file to load — some_gem (LoadError)

In the end it was all because I had forgotten to first require rubygems!

It was a hard thing to find an answer to from The Google.

Tags: , , .