You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mm...@apache.org on 2014/05/05 18:09:05 UTC

[05/49] git commit: [CB-4825] use CoreMotion framework for accelerometer

[CB-4825] use CoreMotion framework for accelerometer


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/commit/ea304063
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/ea304063
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/ea304063

Branch: refs/heads/cdvtest
Commit: ea3040634aa2d86925c29a41f172eb96a1145fd4
Parents: 3312a40
Author: James Jong <wj...@gmail.com>
Authored: Wed Oct 2 11:34:30 2013 -0400
Committer: James Jong <wj...@gmail.com>
Committed: Wed Oct 2 11:34:30 2013 -0400

----------------------------------------------------------------------
 src/ios/CDVAccelerometer.h |  2 +-
 src/ios/CDVAccelerometer.m | 52 +++++++++++++++++++++--------------------
 2 files changed, 28 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/ea304063/src/ios/CDVAccelerometer.h
----------------------------------------------------------------------
diff --git a/src/ios/CDVAccelerometer.h b/src/ios/CDVAccelerometer.h
index 923d0c8..ee1664f 100755
--- a/src/ios/CDVAccelerometer.h
+++ b/src/ios/CDVAccelerometer.h
@@ -20,7 +20,7 @@
 #import <UIKit/UIKit.h>
 #import <Cordova/CDVPlugin.h>
 
-@interface CDVAccelerometer : CDVPlugin <UIAccelerometerDelegate>
+@interface CDVAccelerometer : CDVPlugin
 {
     double x;
     double y;

http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/ea304063/src/ios/CDVAccelerometer.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVAccelerometer.m b/src/ios/CDVAccelerometer.m
index 33093d0..b338245 100755
--- a/src/ios/CDVAccelerometer.m
+++ b/src/ios/CDVAccelerometer.m
@@ -17,10 +17,12 @@
  under the License.
  */
 
+#import <CoreMotion/CoreMotion.h>
 #import "CDVAccelerometer.h"
 
 @interface CDVAccelerometer () {}
 @property (readwrite, assign) BOOL isRunning;
+@property (readwrite, strong) CMMotionManager* motionManager;
 @end
 
 @implementation CDVAccelerometer
@@ -42,6 +44,7 @@
         timestamp = 0;
         self.callbackId = nil;
         self.isRunning = NO;
+        self.motionManager = nil;
     }
     return self;
 }
@@ -54,16 +57,29 @@
 - (void)start:(CDVInvokedUrlCommand*)command
 {
     NSString* cbId = command.callbackId;
-    NSTimeInterval desiredFrequency_num = kAccelerometerInterval;
-    UIAccelerometer* pAccel = [UIAccelerometer sharedAccelerometer];
 
-    // accelerometer expects fractional seconds, but we have msecs
-    pAccel.updateInterval = desiredFrequency_num / 1000;
-    self.callbackId = cbId;
-    if (!self.isRunning) {
-        pAccel.delegate = self;
-        self.isRunning = YES;
+    if (!self.motionManager)
+    {
+        self.motionManager = [[CMMotionManager alloc] init];
+    }
+
+    if ([self.motionManager isAccelerometerAvailable] == YES) {
+        // Assign the update interval to the motion manager and start updates
+        [self.motionManager setAccelerometerUpdateInterval:kAccelerometerInterval/1000];  // expected in seconds
+        [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
+            x = accelerometerData.acceleration.x;
+            y = accelerometerData.acceleration.y;
+            z = accelerometerData.acceleration.z;
+            timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
+            [self returnAccelInfo];
+        }];
+
+        if (!self.isRunning) {
+            self.isRunning = YES;
+        }
     }
+    
+    self.callbackId = cbId;
 }
 
 - (void)onReset
@@ -73,24 +89,10 @@
 
 - (void)stop:(CDVInvokedUrlCommand*)command
 {
-    UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
-
-    theAccelerometer.delegate = nil;
-    self.isRunning = NO;
-}
-
-/**
- * Picks up accel updates from device and stores them in this class
- */
-- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
-{
-    if (self.isRunning) {
-        x = acceleration.x;
-        y = acceleration.y;
-        z = acceleration.z;
-        timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
-        [self returnAccelInfo];
+    if ([self.motionManager isAccelerometerAvailable] == YES) {
+        [self.motionManager stopAccelerometerUpdates];
     }
+    self.isRunning = NO;
 }
 
 - (void)returnAccelInfo