Posted by & filed under Android.

Purple RobotWhen I started working for CBITs back in September, my initial task was to develop a set of tools that would complement existing mobile apps. Since these apps had been constructed using the Apache Cordova platform, the advantages of using JavaScript as the base technology platform were tempered by Cordova’s inability to fully integrate with native platforms (example, example) once the user was outside the app hosting the root WebView. With this challenge in hand, I started writing Purple Robot to bridge this gap in functionality. The initial incarnation of Purple Robot was primarily concerned with providing a time-based triggering mechanism that could notify the user in one of three ways: status-bar notifications, app widgets, and a full dialog when needed. The system operated by downloading a JSON configuration file from a remote web server that encoded schedules and other conditions that dictated when a given notification was to be presented. A date trigger example:

{
        "type": "datetime",
        "name": "Repeating Test Date Trigger",
        "action": "PurpleRobot.launchUrl('http://www.twitter.com');",
        "datetime_start": "20120920T092000",
        "datetime_end": "20120920T092100",
        "datetime_repeat": "FREQ=HOURLY;INTERVAL=1;BYHOUR=9,10,11,12,13,14,15,16"
    }

This trigger opens the Twitter web page in the default browser 20 minutes after the hours between 9am and 4pm. (If you’ve used a calendaring program, the repeating concept will be familiar to you.) We created a small JavaScript runtime and inserted our own PurpleRobot global object (and mini-API) that serves as the interface between the JavaScript action and the rest of the system. If we wanted to show a status-bar notification (like you get when a new text message arrives), we could change the action parameter:

"action": "PurpleRobot.showApplicationLaunchNotification
('Open CBITs App', 'Tap this notification to open CBITs.app.', 'CBITs', 0);",

When the user selected the notification, it would launch a local app named CBITs. With the triggering mechanism, Purple Robot addressed some initial pain points and provided a way for us to push content and activities to mobile devices out in the field. In the months since then, Purple Robot has been extended dramatically to provide the additional services to our other apps on Android devices:
* A full local sensor acquisition platform and methods to intelligently save sensor data to external web endpoints.
* Local interfaces to visualize and understand local sensor history. (e.g. Where have I been today?)
* An embedded web server that builds upon the initial JavaScript runtime that allows local apps and webpages to access PurpleRobot services. (e.g. A Cordova app needs to update the content of a Purple Robot-provided app widget.) Over the past three months, the application has grown from single idea into an 80 thousand of lines of code system that’s providing native system integration & infrastructure to applications that would otherwise leave that out. Over the next several weeks, I’ll dive into particular aspects of the system to describe how it’s built and why