incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shaz...@apache.org
Subject ios commit: Fixes CB-464 - Refactor accelerometer native code in iOS
Date Wed, 16 May 2012 22:05:48 GMT
Updated Branches:
  refs/heads/master 1fbaaaf8f -> 787171505


Fixes CB-464 - Refactor accelerometer native code in iOS

commit d1e6332218aba39a11cad3dda24e7a66c4dfa079
Author: Shazron Abdullah <shazron@apache.org>
Date:   Wed May 16 15:02:55 2012 -0700

    Fixed EXC_BAD_ACCESS error in CDVAccelerometer

commit 221210d4d3ff3365f5a300c5aaffd2064e9f1ce3
Author: Fil Maj <maj.fil@gmail.com>
Date:   Tue May 15 16:43:58 2012 -0700

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

Branch: refs/heads/master
Commit: 787171505c42a620f77bb0c1d25dcaf8e270884b
Parents: 1fbaaaf
Author: Shazron Abdullah <shazron@apache.org>
Authored: Wed May 16 15:05:41 2012 -0700
Committer: Shazron Abdullah <shazron@apache.org>
Committed: Wed May 16 15:05:41 2012 -0700

----------------------------------------------------------------------
 CordovaLib/Classes/CDVAccelerometer.h |   11 ++-
 CordovaLib/Classes/CDVAccelerometer.m |  135 ++++++++++++++++++----------
 2 files changed, 94 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/78717150/CordovaLib/Classes/CDVAccelerometer.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.h b/CordovaLib/Classes/CDVAccelerometer.h
index dfe4d69..bc125f1 100755
--- a/CordovaLib/Classes/CDVAccelerometer.h
+++ b/CordovaLib/Classes/CDVAccelerometer.h
@@ -24,20 +24,23 @@
 
 @interface CDVAccelerometer : CDVPlugin<UIAccelerometerDelegate> 
 {
-	bool _bIsRunning;
 	double x;
     double y;
     double z;
-    double timeout;
     NSTimeInterval timestamp;
-    NSTimeInterval lastAccessTime;
 }
 
+@property (readonly, assign) BOOL isRunning;
+@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/78717150/CordovaLib/Classes/CDVAccelerometer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.m b/CordovaLib/Classes/CDVAccelerometer.m
index 611e28d..8f8d95b 100644
--- a/CordovaLib/Classes/CDVAccelerometer.m
+++ b/CordovaLib/Classes/CDVAccelerometer.m
@@ -20,8 +20,16 @@
 
 #import "CDVAccelerometer.h"
 
+@interface CDVAccelerometer () {
+}
+@property (readwrite, assign) BOOL isRunning;
+@end
+
+
 @implementation CDVAccelerometer
 
+@synthesize accelCallbacks, watchCallbacks, isRunning;
+
 // defaults to 100 msec
 #define kAccelerometerInterval      100 
 // max rate of 40 msec
@@ -36,12 +44,13 @@
     self = [super init];
     if (self)
     {
-        timeout = 30000;
         x = 0;
         y = 0;
         z = 0;
         timestamp = 0;
-        lastAccessTime = 0;
+        self.accelCallbacks = nil;
+        self.watchCallbacks = nil;
+        self.isRunning = NO;
     }
     return self;
 }
@@ -57,19 +66,24 @@
 	UIAccelerometer* pAccel = [UIAccelerometer sharedAccelerometer];
 	// accelerometer expects fractional seconds, but we have msecs
 	pAccel.updateInterval = desiredFrequency_num / 1000;
-	if(!_bIsRunning)
+    if (!self.accelCallbacks) {
+        self.accelCallbacks = [NSMutableArray arrayWithCapacity:1];            
+    }
+    if (!watchCallbacks) {
+        self.watchCallbacks = [NSMutableDictionary dictionaryWithCapacity:1];
+    }
+	if(!self.isRunning)
 	{
 		pAccel.delegate = self;
-		_bIsRunning = YES;
+		self.isRunning = YES;
 	}
 }
 
-
 - (void)stop
 {
 	UIAccelerometer*  theAccelerometer = [UIAccelerometer sharedAccelerometer];
 	theAccelerometer.delegate = nil;
-	_bIsRunning = NO;
+	self.isRunning = NO;
 }
 
 /**
@@ -77,71 +91,99 @@
  */
 - (void)getAcceleration:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
 {
-    CDVPluginResult* result = nil;
-    NSString* jsString = nil;
 	NSString* callbackId = [arguments objectAtIndex:0];
     
-    if(!_bIsRunning)
+    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];
     }
-    // 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 (!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)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 (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

 {
-	if(_bIsRunning)
+	if(self.isRunning)
 	{
         x = acceleration.x;
         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 +201,4 @@
  
 
  */
-
-
-
 @end


Mime
View raw message