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/03/16 19:14:49 UTC
[18/29] ios commit: Rewrite of accel and removing DeviceInfo drop
from old init approach
Rewrite of accel and removing DeviceInfo drop from old init approach
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/c1ef0b4a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/c1ef0b4a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/c1ef0b4a
Branch: refs/heads/master
Commit: c1ef0b4a4cceb5d97e6e6445dd344971497bbe5a
Parents: 7cdc053
Author: Fil Maj <fi...@nitobi.com>
Authored: Mon Feb 27 11:55:20 2012 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Fri Mar 16 10:56:49 2012 -0700
----------------------------------------------------------------------
CordovaLib/Classes/CDVAccelerometer.h | 16 ++--
CordovaLib/Classes/CDVAccelerometer.m | 109 ++++++++++++++++++++-------
CordovaLib/Classes/CDVViewController.m | 11 ---
3 files changed, 89 insertions(+), 47 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/c1ef0b4a/CordovaLib/Classes/CDVAccelerometer.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.h b/CordovaLib/Classes/CDVAccelerometer.h
index 5adab28..3d650f5 100755
--- a/CordovaLib/Classes/CDVAccelerometer.h
+++ b/CordovaLib/Classes/CDVAccelerometer.h
@@ -25,17 +25,19 @@
@interface CDVAccelerometer : CDVPlugin<UIAccelerometerDelegate>
{
bool _bIsRunning;
-
+ float x;
+ float y;
+ float z;
+ float timeout;
+ NSTimeInterval timestamp;
+ NSTimeInterval lastAccessTime;
}
+- (CDVAccelerometer*) init;
+- (void)start;
-- (void)start:(NSMutableArray*)arguments
- withDict:(NSMutableDictionary*)options;
-
-
-- (void)stop:(NSMutableArray*)arguments
- withDict:(NSMutableDictionary*)options;
+- (void)stop;
@end
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/c1ef0b4a/CordovaLib/Classes/CDVAccelerometer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVAccelerometer.m b/CordovaLib/Classes/CDVAccelerometer.m
index 6ae6ce4..ff12e24 100644
--- a/CordovaLib/Classes/CDVAccelerometer.m
+++ b/CordovaLib/Classes/CDVAccelerometer.m
@@ -29,33 +29,30 @@
// min rate of 1/sec
#define kMaxAccelerometerInterval 1000
+- (CDVAccelerometer*) init
+{
+ self = [super init];
+ if (self)
+ {
+ timeout = 30000;
+ x = 0;
+ y = 0;
+ z = 0;
+ timestamp = 0;
+ lastAccessTime = 0;
+ }
+ return self;
+}
+- (void) dealloc {
+ [self stop];
+ [super dealloc]; // pretty important.
+}
-
-- (void)start:(NSMutableArray*)arguments
- withDict:(NSMutableDictionary*)options
+- (void)start
{
NSTimeInterval desiredFrequency_num = kAccelerometerInterval;
-
- if ([options objectForKey:@"frequency"])
- {
- int nDesFreq = [(NSString *)[options objectForKey:@"frequency"] intValue];
- // Special case : returns 0 if int conversion fails
- if(nDesFreq == 0)
- {
- nDesFreq = desiredFrequency_num;
- }
- else if(nDesFreq < kMinAccelerometerInterval)
- {
- nDesFreq = kMinAccelerometerInterval;
- }
- else if(nDesFreq > kMaxAccelerometerInterval)
- {
- nDesFreq = kMaxAccelerometerInterval;
- }
- desiredFrequency_num = nDesFreq;
- }
UIAccelerometer* pAccel = [UIAccelerometer sharedAccelerometer];
// accelerometer expects fractional seconds, but we have msecs
pAccel.updateInterval = desiredFrequency_num / 1000;
@@ -67,26 +64,80 @@
}
-- (void)stop:(NSMutableArray*)arguments
- withDict:(NSMutableDictionary*)options
+- (void)stop
{
UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
theAccelerometer.delegate = nil;
_bIsRunning = NO;
}
-
/**
* Sends Accel Data back to the Device.
*/
+- (void)getAcceleration:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+{
+ CDVPluginResult* result = nil;
+ NSString* jsString = nil;
+ NSString* callbackId = [arguments objectAtIndex:0];
+
+ if(!_bIsRunning)
+ {
+ [self start];
+ }
+ // set last access time to right now
+ lastAccessTime = ([[NSDate date] timeIntervalSince1970] * 1000);
+
+ // Create an acceleration object
+ NSMutableDictionary *accelProps = [NSMutableDictionary dictionaryWithCapacity:4];
+ [accelProps setValue:[NSNumber numberWithInteger:x] forKey:@"x"];
+ [accelProps setObject:[NSNumber numberWithInteger:y] forKey:@"y"];
+ [accelProps setObject:[NSNumber numberWithInteger:z] forKey:@"z"];
+ [accelProps setObject:[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
+{
+ CDVPluginResult* result = nil;
+ NSString* jsString = nil;
+ NSString* callbackId = [arguments objectAtIndex:0];
+
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:timeout];
+ jsString = [result toSuccessCallbackString:callbackId];
+ [self writeJavascript:jsString];
+}
+
+- (void)setTimeout:(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];
+ [self writeJavascript:jsString];
+}
+/**
+ * Picks up accel updates from device and stores them in this class
+ */
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if(_bIsRunning)
{
- NSString * jsCallBack = nil;
- jsCallBack = [[NSString alloc] initWithFormat:@"navigator.accelerometer._onAccelUpdate(%f,%f,%f);", acceleration.x, acceleration.y, acceleration.z];
- [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
- [jsCallBack release];
+ 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];
+ }
}
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/c1ef0b4a/CordovaLib/Classes/CDVViewController.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.m b/CordovaLib/Classes/CDVViewController.m
index 680483d..82bcd30 100644
--- a/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/Classes/CDVViewController.m
@@ -819,17 +819,6 @@ BOOL gSplashScreenShown = NO;
[devProps setObject:[device name] forKey:@"name"];
[devProps setObject:[[self class] cordovaVersion ] forKey:@"gap"];
- // TODO: why do we blast this onto the device object? can't we just leave it alone as a plugin?
- id cmd = [self.commandDelegate getCommandInstance:@"org.apache.cordova.connection"];
- if (cmd && [cmd isKindOfClass:[CDVConnection class]])
- {
- NSMutableDictionary *connProps = [NSMutableDictionary dictionaryWithCapacity:3];
- if ([cmd respondsToSelector:@selector(connectionType)]) {
- [connProps setObject:[cmd connectionType] forKey:@"type"];
- }
- [devProps setObject:connProps forKey:@"connection"];
- }
-
NSDictionary *devReturn = [NSDictionary dictionaryWithDictionary:devProps];
return devReturn;
}