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/16 01:40:31 UTC

ios commit: [CB-464] rewrite of accel plugin. not working yet.

Updated Branches:
  refs/heads/464 [created] 221210d4d


[CB-464] rewrite of accel plugin. not working yet.


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/221210d4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/221210d4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/221210d4

Branch: refs/heads/464
Commit: 221210d4d3ff3365f5a300c5aaffd2064e9f1ce3
Parents: 1fbaaaf
Author: Fil Maj <ma...@gmail.com>
Authored: Tue May 15 16:43:58 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Tue May 15 16:43:58 2012 -0700

----------------------------------------------------------------------
 CordovaLib/Classes/CDVAccelerometer.h |   11 ++-
 CordovaLib/Classes/CDVAccelerometer.m |  118 ++++++++++++++++++----------
 2 files changed, 83 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/221210d4/CordovaLib/Classes/CDVAccelerometer.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.h b/CordovaLib/Classes/CDVAccelerometer.h
index dfe4d69..dfb6a83 100755
--- a/CordovaLib/Classes/CDVAccelerometer.h
+++ b/CordovaLib/Classes/CDVAccelerometer.h
@@ -28,16 +28,21 @@
 	double x;
     double y;
     double z;
-    double timeout;
     NSTimeInterval timestamp;
-    NSTimeInterval lastAccessTime;
+    NSMutableArray  *accelCallbacks;
+    NSMutableDictionary  *watchCallbacks;
 }
 
+@property (nonatomic, retain) NSMutableArray* accelCallbacks;
+@property (nonatomic, retain) NSMutableDictionary* watchCallbacks;
+
 - (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;
 
 @end
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/221210d4/CordovaLib/Classes/CDVAccelerometer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.m b/CordovaLib/Classes/CDVAccelerometer.m
index 611e28d..82e43bf 100644
--- a/CordovaLib/Classes/CDVAccelerometer.m
+++ b/CordovaLib/Classes/CDVAccelerometer.m
@@ -22,6 +22,8 @@
 
 @implementation CDVAccelerometer
 
+@synthesize accelCallbacks, watchCallbacks;
+
 // defaults to 100 msec
 #define kAccelerometerInterval      100 
 // max rate of 40 msec
@@ -36,12 +38,12 @@
     self = [super init];
     if (self)
     {
-        timeout = 30000;
         x = 0;
         y = 0;
         z = 0;
         timestamp = 0;
-        lastAccessTime = 0;
+        accelCallbacks = nil;
+        watchCallbacks = nil;
     }
     return self;
 }
@@ -57,6 +59,12 @@
 	UIAccelerometer* pAccel = [UIAccelerometer sharedAccelerometer];
 	// accelerometer expects fractional seconds, but we have msecs
 	pAccel.updateInterval = desiredFrequency_num / 1000;
+    if (!accelCallbacks) {
+        accelCallbacks = [NSMutableArray arrayWithCapacity:1];            
+    }
+    if (!watchCallbacks) {
+        watchCallbacks = [NSMutableDictionary dictionaryWithCapacity:1];
+    }
 	if(!_bIsRunning)
 	{
 		pAccel.delegate = self;
@@ -64,7 +72,6 @@
 	}
 }
 
-
 - (void)stop
 {
 	UIAccelerometer*  theAccelerometer = [UIAccelerometer sharedAccelerometer];
@@ -77,52 +84,54 @@
  */
 - (void)getAcceleration:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
 {
-    CDVPluginResult* result = nil;
-    NSString* jsString = nil;
 	NSString* callbackId = [arguments objectAtIndex:0];
     
     if(!_bIsRunning)
     {
+        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
+        [result setKeepCallback:[NSNumber numberWithBool:YES]];
+        if (!accelCallbacks) {
+            accelCallbacks = [NSMutableArray arrayWithCapacity:1];            
+        }
+        [accelCallbacks addObject:callbackId];
         [self start];
+        [self writeJavascript:[result toSuccessCallbackString:callbackId]];
+    } else {
+        [self returnAccelInfo:callbackId];
     }
-    // set last access time to right now
-    lastAccessTime = ([[NSDate date] timeIntervalSince1970] * 1000);
-    
-    // Create an acceleration object
-    NSMutableDictionary *accelProps = [NSMutableDictionary dictionaryWithCapacity:4];
-    [accelProps setValue:[NSNumber numberWithDouble:x*kGravitionalConstant] forKey:@"x"];
-    [accelProps setValue:[NSNumber numberWithDouble:y*kGravitionalConstant] forKey:@"y"];
-    [accelProps setValue:[NSNumber numberWithDouble:z*kGravitionalConstant] forKey:@"z"];
-    [accelProps setValue:[NSNumber numberWithDouble:timestamp] forKey:@"timestamp"];
-    
-    result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:accelProps];
-    jsString = [result toSuccessCallbackString:callbackId];
-    [self writeJavascript:jsString];
 }
 
-- (void)getTimeout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) addWatch:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
 {
-    CDVPluginResult* result = nil;
-    NSString* jsString = nil;
-	NSString* callbackId = [arguments objectAtIndex:0];
+    NSString* callbackId = [arguments objectAtIndex:0];
+    NSString* timerId = [arguments objectAtIndex:1];
     
-    result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:timeout];
-    jsString = [result toSuccessCallbackString:callbackId];
-    [self writeJavascript:jsString];
+    if (!watchCallbacks) {
+        watchCallbacks = [NSMutableDictionary dictionaryWithCapacity:1];
+    }
+    
+    // add the callbackId into the dictionary so we can call back whenever get data
+    [watchCallbacks setObject:callbackId forKey:timerId];
+    
+    if (!_bIsRunning) {
+        [self start];
+    }
+    CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
+    [result setKeepCallback:[NSNumber numberWithBool:YES]];
+    [self writeJavascript:[result toSuccessCallbackString:callbackId]];
 }
-
-- (void)setTimeout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) clearWatch:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
 {
-    CDVPluginResult* result = nil;
-    NSString* jsString = nil;
-	NSString* callbackId = [arguments objectAtIndex:0];
-    float newTimeout = [[arguments objectAtIndex:1] floatValue];
-    timeout = newTimeout;
-    
-    result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
-    jsString = [result toSuccessCallbackString:callbackId];
+    NSString* callbackId = [arguments objectAtIndex:0];
+    NSString* timerId = [arguments objectAtIndex:1];
+    if (watchCallbacks && [watchCallbacks objectForKey:timerId]) {
+        [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
  */
@@ -134,14 +143,40 @@
         y = acceleration.y;
         z = acceleration.z;
         timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
-        
-        // read frequency and compare to timeout so we can see if we should turn off accel listening
-        if ((timestamp - lastAccessTime) > timeout) {
-			[self stop];
-		}		
+        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];
+        }
 	}
 }
 
+- (void)returnAccelInfo: (NSString*) callbackId
+{
+    CDVPluginResult* result = nil;
+    NSString* jsString = nil;
+
+    // Create an acceleration object
+    NSMutableDictionary *accelProps = [NSMutableDictionary dictionaryWithCapacity:4];
+    [accelProps setValue:[NSNumber numberWithDouble:x*kGravitionalConstant] forKey:@"x"];
+    [accelProps setValue:[NSNumber numberWithDouble:y*kGravitionalConstant] forKey:@"y"];
+    [accelProps setValue:[NSNumber numberWithDouble:z*kGravitionalConstant] forKey:@"z"];
+    [accelProps setValue:[NSNumber numberWithDouble:timestamp] forKey:@"timestamp"];
+    
+    result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:accelProps];
+    jsString = [result toSuccessCallbackString:callbackId];
+    [self writeJavascript:jsString]; 
+}
+
 // TODO: Consider using filtering to isolate instantaneous data vs. gravity data -jm
 
 /* 
@@ -159,7 +194,4 @@
  
 
  */
-
-
-
 @end