advantages and limitations of phonegap for sensor processing
DESCRIPTION
My Droidcon Tunis 2013 presentation about the effect of the web application model to the battery consumption.TRANSCRIPT
![Page 1: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/1.jpg)
Advantages and limitations of PhoneGapfor sensor processing
Gabor [email protected]
Sfonge Ltd.http://www.sfonge.com
![Page 2: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/2.jpg)
Alternative application models for Android
● Basic Android application model → Java-based● Well-hidden in the Android Native Development
Kit (NDK) → there is a native application model too
● Application models based on other languages have been proposed but have been rarely deployed
![Page 3: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/3.jpg)
Browser as mobile application runtime
● Browser is already acting as mobile application runtime● Just check out the zillions of “how to optimize your website for
mobile” articles
● There were some efforts to make the web application model the main application model on other platforms → Bada, Tizen (both very silent recently)→ Firefox OS is making headlines now.
● Meanwhile, a modest tool adopted the web model widely and is in production: PhoneGap
![Page 4: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/4.jpg)
From the helicopter
● With PhoneGap, you create traditional, installable applications● PhoneGap does not change the operating system in any way, it acts like
a library embedded into the application● A PhoneGap application is implemented mainly in web technologies:
HTML, JavaScript, JQuery● Promises:
● Lower the cost of mobile development by employing millions of web programmers ● Improve the portability of applications by making the web model the greatest
common denominator (which it is for web sites)
![Page 5: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/5.jpg)
Test programs mentioned in this presentation are available here:
http://www.sfonge.com/forum/topic/droidcon-tunis-2013-presentation-advantages-and-limitations-phonegap-sensor-processing-t
White paper that this presentation is based on is available here:
http://www.sfonge.com/epaper/performance-context-aware-algorithms-implemented-web-technologies
(Both require free registration to Sfonge site)
![Page 6: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/6.jpg)
PhoneGap project
Displayed first
This directory is our playground
PhoneGap library
![Page 7: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/7.jpg)
index.html
Main stylesheet in css subdirectory
Main page starts here
Loads PhoneGap
Loads application-specificcode
Invokes application-specificinitialization
![Page 8: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/8.jpg)
index.js
● Application initialization:
var app = {
initialize: function() {...
},
● First listen to onDeviceReady events:var self = this;document.addEventListener('deviceready', self.onDeviceReady, false);
● Start invoking PhoneGap functions after the onDeviceReady event was fired: onDeviceReady: function() { ...},
![Page 9: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/9.jpg)
index.js (2)
● In onDeviceReady, initialize your UI ...var shakes = document.getElementById("shakes");app.shakeCounter = 0;shakes.innerHTML = app.shakeCounter.toString();
● … start listening for lifecycle events ...document.addEventListener('pause', app.onPause, false);document.addEventListener('resume', app.onResume, false);
● … and device events (sensor, in our case)this.accelerometerWatchID = navigator.accelerometer.watchAcceleration(
this.onSuccess, this.onError, { frequency: 50 });
![Page 10: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/10.jpg)
index.js (3)
● And then you can handle sensor events:onSuccess: function(acceleration) {
var currentTimeStamp = acceleration.timestamp; var x = acceleration.x; var y = acceleration.y; var z = acceleration.z;…}
![Page 11: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/11.jpg)
Brief interlude on context-aware applications
![Page 12: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/12.jpg)
Context-aware applications
● Context-aware applications are characterized by their capability of adapting to changes in their environment.
● More precisely:● Capture environmental changes● Decide whether the change is relevant enough to
adapt● Adapt the behavior if the change is relevant
![Page 13: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/13.jpg)
Architecture: sensor adapters
● Sensor adapters ● Process the input from probes in the environment● Simple: listener to operating system events● Complicated: signal-processing algorithm for a built-
in gyroscope sensor
![Page 14: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/14.jpg)
Architecture: decision logic
● Decision logic ● Rule engine that works on sensor adapter outputs
and produces application adaptation decisions● Simple: a set of “if” statements ● Complicated: rule inference engine.
![Page 15: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/15.jpg)
Architecture: adaptation logic
● Adaptation logic● Makes sure that the high-level context variables
produced by the decision logic affect the application logic in an application-specific way.
● Simple: set of “if” statements built into the application logic
● Complicated: dynamic component system
![Page 16: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/16.jpg)
Consequence of the application model
● If the sensor adapter processing is offloaded to specialized co-processors or native code, the application model has no impact (no special processing requirements)
● If the application code includes sensor adapter processing then the application model better provide efficient execution because sensor processing may be CPU-intensive
![Page 17: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/17.jpg)
Example application
● Very simple shake detector● Based on the accelerometer input
● High-pass filtering to remove effects of slow motions, e.g. walking
● Peak detector to extract shake signal● “Application adaptation”: simply count
![Page 18: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/18.jpg)
Recap: gravity and motion acceleration
![Page 19: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/19.jpg)
Recap: Absolute value
● x, y, z: acceleration vector components● g – value of the gravity acceleration (can be
approximated as 10)
a=√x2+ y2+ z2−g
![Page 20: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/20.jpg)
5 shakes
Movementstarts: accelerating
Direction reverses
Final deceleration
![Page 21: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/21.jpg)
Recap: Separating movements in the frequency domain
Hz
Walking Shaking
Walking
Walking+shaking
64-point Fast Fourier Transform performed at samples 50 and 200
![Page 22: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/22.jpg)
Applying the filter
4 shakes can be reliably recognized
![Page 23: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/23.jpg)
Filter in our example
● 6th-order IIR filter (N=6, 12 additions and 12 multiplications per sample)
yn=∑i=0
N
a i xn−i−∑i=1
N
bi yn−i
![Page 24: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/24.jpg)
Measurement
● Two implementations: Java and web technology (PhoneGap).
● Measurement:● Phone is restarted● Nexus S, about 50 Hz sampling rate● App is started, sampling is started● Wait 10 sec● Connect with “adb shell” and launch the “top”
command● Record the CPU% column
![Page 25: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/25.jpg)
Results:
● CPU consumption:● Java implementation: 1%● PhoneGap implementation: 9%
● Battery consumption:● PhoneGap : 0.1218%/min → 7.3 %/hour → about 13
hours of battery life● Java: 0.072%/min →4.32%/hour → about 23 hours of
battery life
● 60% more consumption, 40% less battery life
![Page 26: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/26.jpg)
PhoneGap “plugins”
● PhoneGap is built on the plugin concept● Its own services are also built as plugins and
you can define your own● Plugins are implemented as whatever is “native”
in the environment, in case of Android in Java
![Page 27: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/27.jpg)
PhoneGap plugin
● Plugin class:public class SamplingServiceAdapter extends CordovaPlugin {…}
● Plugin commands:public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("start")) { … }
● Register your plugin in res/xml/config.xml <plugin name="Shake" value="aexp.simpleshakejsplugin.SamplingServiceAdapter"/>
![Page 28: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/28.jpg)
Callbacks
● Use CallbackContext to initiate a callback:PluginResult result = new PluginResult(PluginResult.Status.OK, this.getStepCountJSON(count));result.setKeepCallback(true);callbackContext.sendPluginResult(result);
● The result must be in JSON:JSONObject r = new JSONObject();r.put("count", count);
● Which conforms in JSON to:{ count: 2 }
![Page 29: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/29.jpg)
From the JS side
● Sending a command from JS:cordova.exec(this.onShake,this.onError,"Shake","start",[]);
● Receiving a callback from native:onShake: function(s) { var count = s.count; // Remember the JSON format we generate! …}
![Page 30: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/30.jpg)
Plugin result
● With plugin implementation, the CPU load is comparable to native implementation (about 1%, within measurement error)
● No surprise here: if you check the code, you will see that “CPU-intensive” tasks are implemented similarly to the Java version.
![Page 31: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/31.jpg)
So what about the “web model”?
● Web model is great for some types of applications (if you have the competence)● These are typically UI-intensive applications when some
data is presented to the user and we are then expecting user interaction
● Try to handle moderately “CPU-intensive” tasks in the web model and you will be in for an unpleasant surprise● CPU load is directly translated to battery life and I haven't
even talked about memory footprint
![Page 32: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/32.jpg)
My advice
● If you are an experienced web developer, go for the web model and implement your apps in the web model, you will have quick success
● Always consider, whether you have “CPU-intensive” tasks – those that happen often and require non-trivial calculations
● When you are considering a web application platform, always look for native extension possibility, just in case. If there is none, beware.
![Page 33: Advantages and limitations of PhoneGap for sensor processing](https://reader033.vdocument.in/reader033/viewer/2022050920/54b702494a7959af3d8b45b5/html5/thumbnails/33.jpg)
Questions?