Files
Max Lynch 762112c058 Revert "Temporarily removing native plugins"
This reverts commit 81d907e8b95eb3286200a5c5ab479bbade56f03b.
2015-09-15 09:15:23 -05:00

91 lines
2.4 KiB
TypeScript

import * as Rx from 'rx';
import * as util from 'ionic/util';
import {NativePlugin} from '../plugin';
@NativePlugin({
name: 'Device Motion',
platforms: {
cordova: 'cordova-plugin-device-motion'
}
})
export class DeviceMotion {
static _wrap(result) {
// Mimic the DeviceMotionEvent
return util.extend({
acceleration: result, // result will be x/y/z accel
accelerationIncludingGravity: result, //TODO: I know this isn't correct but not sure how to normalize from native plugin
rotationRate: 0,
interval: 0,
native: true
}, result);
}
static getCurrentAcceleration() {
return new Promise((resolve, reject) => {
if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
var fnCb = function fnCb(eventData) {
resolve(DeviceMotion._wrap(eventData));
window.removeEventListener('devicemotion', fnCb);
}
window.addEventListener('devicemotion', fnCb);
} else if(navigator.accelerometer) {
navigator.accelerometer.getCurrentAcceleration(function (result) {
resolve(DeviceMotion._wrap(result));
}, function (err) {
reject(err);
});
} else {
this.pluginWarn();
reject('The Device does not support device motion events.');
return;
}
});
}
static watchAcceleration(options) {
if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
let watchID;
let source = Rx.Observable.create((observer) => {
var fnCb = function fnCb(eventData) {
observer.onNext(DeviceMotion._wrap(eventData));
};
window.addEventListener('devicemotion', fnCb);
});
return {
source: source,
watchID: watchID,
clear: () => {
window.removeEventListener('devicemotion', fnCb);
}
}
} else if(navigator.accelerometer) {
let watchID;
let source = Rx.Observable.create((observer) => {
watchID = navigator.accelerometer.watchAcceleration(function (result) {
observer.onNext(DeviceMotion._wrap(result));
}, function (err) {
observer.onError(err, observer);
}, options);
});
return {
source: source,
watchID: watchID,
clear: () => {
navigator.accelerometer.clearWatch(watchID);
}
}
}
}
}