You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/05/19 00:10:34 UTC
ios commit: [CB-464] Simplified accel to start/stop actions.
Updated Branches:
refs/heads/464 d1e633221 -> 35ede9676
[CB-464] Simplified accel to start/stop actions.
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/35ede967
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/35ede967
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/35ede967
Branch: refs/heads/464
Commit: 35ede967667ee0a0476dc66a42167a4a0b790471
Parents: d1e6332
Author: Fil Maj <ma...@gmail.com>
Authored: Fri May 18 15:14:11 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Fri May 18 15:14:11 2012 -0700
----------------------------------------------------------------------
CordovaLib/Classes/CDVAccelerometer.h | 10 +--
CordovaLib/Classes/CDVAccelerometer.m | 93 +++---------------
CordovaLib/javascript/cordova.ios.js | 146 +++++++++++++++++++--------
3 files changed, 118 insertions(+), 131 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/35ede967/CordovaLib/Classes/CDVAccelerometer.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.h b/CordovaLib/Classes/CDVAccelerometer.h
index bc125f1..16f6200 100755
--- a/CordovaLib/Classes/CDVAccelerometer.h
+++ b/CordovaLib/Classes/CDVAccelerometer.h
@@ -31,16 +31,12 @@
}
@property (readonly, assign) BOOL isRunning;
-@property (nonatomic, retain) NSMutableArray* accelCallbacks;
-@property (nonatomic, retain) NSMutableDictionary* watchCallbacks;
+@property (nonatomic, retain) NSString* callbackId;
- (CDVAccelerometer*) init;
-- (void)start;
-- (void)stop;
-- (void)getAcceleration:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
-- (void) addWatch:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;
-- (void) clearWatch:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;
+- (void)start:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
+- (void)stop:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/35ede967/CordovaLib/Classes/CDVAccelerometer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.m b/CordovaLib/Classes/CDVAccelerometer.m
index 8f8d95b..8e821ab 100644
--- a/CordovaLib/Classes/CDVAccelerometer.m
+++ b/CordovaLib/Classes/CDVAccelerometer.m
@@ -28,7 +28,7 @@
@implementation CDVAccelerometer
-@synthesize accelCallbacks, watchCallbacks, isRunning;
+@synthesize callbackId, isRunning;
// defaults to 100 msec
#define kAccelerometerInterval 100
@@ -48,30 +48,25 @@
y = 0;
z = 0;
timestamp = 0;
- self.accelCallbacks = nil;
- self.watchCallbacks = nil;
+ self.callbackId = nil;
self.isRunning = NO;
}
return self;
}
- (void) dealloc {
- [self stop];
+ [self stop:nil withDict:nil];
[super dealloc]; // pretty important.
}
-- (void)start
+- (void)start:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
+ NSString* cbId = [arguments objectAtIndex:0];
NSTimeInterval desiredFrequency_num = kAccelerometerInterval;
UIAccelerometer* pAccel = [UIAccelerometer sharedAccelerometer];
// accelerometer expects fractional seconds, but we have msecs
pAccel.updateInterval = desiredFrequency_num / 1000;
- if (!self.accelCallbacks) {
- self.accelCallbacks = [NSMutableArray arrayWithCapacity:1];
- }
- if (!watchCallbacks) {
- self.watchCallbacks = [NSMutableDictionary dictionaryWithCapacity:1];
- }
+ self.callbackId = cbId;
if(!self.isRunning)
{
pAccel.delegate = self;
@@ -79,7 +74,7 @@
}
}
-- (void)stop
+- (void)stop:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
theAccelerometer.delegate = nil;
@@ -87,59 +82,6 @@
}
/**
- * Sends Accel Data back to the Device.
- */
-- (void)getAcceleration:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
-{
- NSString* callbackId = [arguments objectAtIndex:0];
-
- if(!self.isRunning)
- {
- CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
- [result setKeepCallback:[NSNumber numberWithBool:YES]];
- if (!self.accelCallbacks) {
- self.accelCallbacks = [NSMutableArray arrayWithCapacity:1];
- }
- [self.accelCallbacks addObject:callbackId];
- [self start];
- [self writeJavascript:[result toSuccessCallbackString:callbackId]];
- } else {
- [self returnAccelInfo:callbackId];
- }
-}
-
-- (void) addWatch:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
-{
- NSString* callbackId = [arguments objectAtIndex:0];
- NSString* timerId = [arguments objectAtIndex:1];
-
- if (!self.watchCallbacks) {
- self.watchCallbacks = [NSMutableDictionary dictionaryWithCapacity:1];
- }
-
- // add the callbackId into the dictionary so we can call back whenever get data
- [self.watchCallbacks setObject:callbackId forKey:timerId];
-
- if (!self.isRunning) {
- [self start];
- }
- CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
- [result setKeepCallback:[NSNumber numberWithBool:YES]];
- [self writeJavascript:[result toSuccessCallbackString:callbackId]];
-}
-- (void) clearWatch:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
-{
- NSString* callbackId = [arguments objectAtIndex:0];
- NSString* timerId = [arguments objectAtIndex:1];
- if (self.watchCallbacks && [self.watchCallbacks objectForKey:timerId]) {
- [self.watchCallbacks removeObjectForKey:timerId];
- }
- CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
- NSString* jsString = [result toSuccessCallbackString:callbackId];
- [self writeJavascript:jsString];
-}
-
-/**
* Picks up accel updates from device and stores them in this class
*/
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
@@ -150,24 +92,12 @@
y = acceleration.y;
z = acceleration.z;
timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
- if (self.accelCallbacks.count > 0) {
- for (NSString *callbackId in self.accelCallbacks) {
- [self returnAccelInfo:callbackId];
- }
- [self.accelCallbacks removeAllObjects];
- }
- if (self.watchCallbacks.count > 0) {
- for (NSString *timerId in self.watchCallbacks) {
- [self returnAccelInfo:[self.watchCallbacks objectForKey: timerId ]];
- }
- } else {
- // No callbacks waiting on us anymore, turn off listening.
- [self stop];
- }
+ [self returnAccelInfo];
+
}
}
-- (void)returnAccelInfo: (NSString*) callbackId
+- (void)returnAccelInfo
{
CDVPluginResult* result = nil;
NSString* jsString = nil;
@@ -180,7 +110,8 @@
[accelProps setValue:[NSNumber numberWithDouble:timestamp] forKey:@"timestamp"];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:accelProps];
- jsString = [result toSuccessCallbackString:callbackId];
+ [result setKeepCallback:[NSNumber numberWithBool:YES]];
+ jsString = [result toSuccessCallbackString:self.callbackId];
[self writeJavascript:jsString];
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/35ede967/CordovaLib/javascript/cordova.ios.js
----------------------------------------------------------------------
diff --git a/CordovaLib/javascript/cordova.ios.js b/CordovaLib/javascript/cordova.ios.js
index e86afd5..18bf5a9 100644
--- a/CordovaLib/javascript/cordova.ios.js
+++ b/CordovaLib/javascript/cordova.ios.js
@@ -1,6 +1,6 @@
-// commit facaa38a0bd924aa15c14c372537c00382f1e593
+// commit 4a4ba9985c920850fe3f229abc60de984e196ab5
-// File generated at :: Thu May 10 2012 16:39:13 GMT-0700 (PDT)
+// File generated at :: Fri May 18 2012 14:54:13 GMT-0700 (PDT)
/*
Licensed to the Apache Software Foundation (ASF) under one
@@ -1057,13 +1057,14 @@ module.exports = {
// file: lib/common/plugin/Acceleration.js
define("cordova/plugin/Acceleration", function(require, exports, module) {
var Acceleration = function(x, y, z, timestamp) {
- this.x = x;
- this.y = y;
- this.z = z;
- this.timestamp = timestamp || (new Date()).getTime();
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ this.timestamp = timestamp || (new Date()).getTime();
};
module.exports = Acceleration;
+
});
// file: lib/common/plugin/Camera.js
@@ -3282,11 +3283,60 @@ define("cordova/plugin/accelerometer", function(require, exports, module) {
* @constructor
*/
var utils = require("cordova/utils"),
- exec = require("cordova/exec");
+ exec = require("cordova/exec"),
+ Acceleration = require('cordova/plugin/Acceleration');
+
+// Is the accel sensor running?
+var running = false;
-// Local singleton variables.
+// Keeps reference to watchAcceleration calls.
var timers = {};
+// Array of listeners; used to keep track of when we should call start and stop.
+var listeners = [];
+
+// Last returned acceleration object from native
+var accel = null;
+
+// Tells native to start.
+function start() {
+ exec(function(a) {
+ var tempListeners = listeners.slice(0);
+ accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
+ for (var i = 0, l = tempListeners.length; i < l; i++) {
+ tempListeners[i].win(accel);
+ }
+ }, function(e) {
+ var tempListeners = listeners.slice(0);
+ for (var i = 0, l = tempListeners.length; i < l; i++) {
+ tempListeners[i].fail(e);
+ }
+ }, "Accelerometer", "start", []);
+ running = true;
+}
+
+// Tells native to stop.
+function stop() {
+ exec(null, null, "Accelerometer", "stop", []);
+ running = false;
+}
+
+// Adds a callback pair to the listeners array
+function createCallbackPair(win, fail) {
+ return {win:win, fail:fail};
+}
+
+// Removes a win/fail listener pair from the listeners array
+function removeListeners(l) {
+ var idx = listeners.indexOf(l);
+ if (idx > -1) {
+ listeners.splice(idx, 1);
+ if (listeners.length === 0) {
+ stop();
+ }
+ }
+}
+
var accelerometer = {
/**
* Asynchronously aquires the current acceleration.
@@ -3296,21 +3346,27 @@ var accelerometer = {
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
*/
getCurrentAcceleration: function(successCallback, errorCallback, options) {
-
// successCallback required
if (typeof successCallback !== "function") {
- console.log("Accelerometer Error: successCallback is not a function");
- return;
+ throw "getCurrentAcceleration must be called with at least a success callback function as first parameter.";
}
- // errorCallback optional
- if (errorCallback && (typeof errorCallback !== "function")) {
- console.log("Accelerometer Error: errorCallback is not a function");
- return;
- }
+ var p;
+ var win = function(a) {
+ successCallback(a);
+ removeListeners(p);
+ };
+ var fail = function(e) {
+ errorCallback(e);
+ removeListeners(p);
+ };
+
+ p = createCallbackPair(win, fail);
+ listeners.push(p);
- // Get acceleration
- exec(successCallback, errorCallback, "Accelerometer", "getAcceleration", []);
+ if (!running) {
+ start();
+ }
},
/**
@@ -3322,36 +3378,38 @@ var accelerometer = {
* @return String The watch id that must be passed to #clearWatch to stop watching.
*/
watchAcceleration: function(successCallback, errorCallback, options) {
-
// Default interval (10 sec)
- var frequency = (options !== undefined && options.frequency !== undefined)? options.frequency : 10000;
+ var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000;
// successCallback required
if (typeof successCallback !== "function") {
- console.log("Accelerometer Error: successCallback is not a function");
- return;
+ throw "watchAcceleration must be called with at least a success callback function as first parameter.";
}
- // errorCallback optional
- if (errorCallback && (typeof errorCallback !== "function")) {
- console.log("Accelerometer Error: errorCallback is not a function");
- return;
- }
+ // Keep reference to watch id, and report accel readings as often as defined in frequency
+ var id = utils.createUUID();
- // Make sure accelerometer timeout > frequency + 10 sec
- exec(
- function(timeout) {
- if (timeout < (frequency + 10000)) {
- exec(null, null, "Accelerometer", "setTimeout", [frequency + 10000]);
+ var p = createCallbackPair(function(){}, function(e) {
+ errorCallback(e);
+ removeListeners(p);
+ });
+ listeners.push(p);
+
+ timers[id] = {
+ timer:window.setInterval(function() {
+ if (accel) {
+ successCallback(accel);
}
- },
- function(e) { }, "Accelerometer", "getTimeout", []);
+ }, frequency),
+ listeners:p
+ };
- // Start watch timer
- var id = utils.createUUID();
- timers[id] = window.setInterval(function() {
- exec(successCallback, errorCallback, "Accelerometer", "getAcceleration", []);
- }, (frequency ? frequency : 1));
+ if (running) {
+ // If we're already running then immediately invoke the success callback
+ successCallback(accel);
+ } else {
+ start();
+ }
return id;
},
@@ -3362,16 +3420,17 @@ var accelerometer = {
* @param {String} id The id of the watch returned from #watchAcceleration.
*/
clearWatch: function(id) {
-
// Stop javascript timer & remove from timer list
- if (id && timers[id] !== undefined) {
- window.clearInterval(timers[id]);
+ if (id && timers[id]) {
+ window.clearInterval(timers[id].timer);
+ removeListeners(timers[id].listeners);
delete timers[id];
}
}
};
module.exports = accelerometer;
+
});
// file: lib/common/plugin/battery.js
@@ -4662,7 +4721,7 @@ window.cordova = require('cordova');
// Fire onDeviceReady event once all constructors have run and
// cordova info has been received from native side.
channel.join(function() {
- channel.onDeviceReady.fire();
+ require('cordova').fireDocumentEvent('deviceready');
}, channel.deviceReadyChannelsArray);
}, [ channel.onDOMContentLoaded, channel.onNativeReady ]);
@@ -4681,4 +4740,5 @@ window.cordova = require('cordova');
}(window));
+
})();
\ No newline at end of file