diff --git a/ionic/components/app/test/native/pages/device-orientation.ts b/ionic/components/app/test/native/pages/device-orientation.ts new file mode 100644 index 0000000000..1a1d661bb8 --- /dev/null +++ b/ionic/components/app/test/native/pages/device-orientation.ts @@ -0,0 +1,24 @@ +import {IonicView, DeviceOrientation} from 'ionic/ionic'; + + +@IonicView({ + template: ` + + + Device Motion + + + + ` +}) +export class DeviceMotionPage { + constructor() { + let device = DeviceOrientation.watchHeading().source.subscribe((resp) => { + console.log('Device orientation', resp); + }, (err) => { + console.log('Device err', err); + }); + } +} diff --git a/ionic/native/device-motion/device-motion.ts b/ionic/native/device-motion/device-motion.ts index a30066a506..7489bd164e 100644 --- a/ionic/native/device-motion/device-motion.ts +++ b/ionic/native/device-motion/device-motion.ts @@ -37,7 +37,7 @@ export class DeviceMotion { }, function (err) { reject(err); }); - else { + } else { this.pluginWarn(); reject('The Device does not support device motion events.'); return; diff --git a/ionic/native/device-orientation/device-orientation.ts b/ionic/native/device-orientation/device-orientation.ts new file mode 100644 index 0000000000..750f84cec9 --- /dev/null +++ b/ionic/native/device-orientation/device-orientation.ts @@ -0,0 +1,84 @@ +import * as Rx from 'rx'; + +import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; +import {Platform} from '../../platform/platform'; + +@NativePlugin({ + name: 'Device Orientation', + platforms: { + cordova: 'cordova-plugin-device-orientation' + } +}) +export class DeviceOrientation { + static _wrap(result) { + return result; + } + + static getCurrentAcceleration() { + return new Promise((resolve, reject) => { + if(window.DeviceOrientationEvent) { + var fnCb = function fnCb(eventData) { + resolve(eventData); + window.removeEventListener('deviceorientation', fnCb); + } + window.addEventListener('deviceorientation', fnCb); + } else if(navigator.compass) { + navigator.compass.getCurrentHeading(function (result) { + resolve(DeviceOrientation._wrap(result)); + }, function (err) { + reject(err); + }); + } else { + this.pluginWarn(); + reject('The Device does not support device orientation events.'); + return; + } + }); + } + + static watchHeading(options) { + if(window.DeviceOrientationEvent) { + let watchID; + + let source = Rx.Observable.create((observer) => { + + var fnCb = function fnCb(eventData) { + console.log(eventData); + observer.onNext(eventData); + }; + + window.addEventListener('deviceorientation', fnCb); + + }); + + return { + source: source, + watchID: watchID, + clear: () => { + window.removeEventListener('deviceorientation', cbFn); + } + } + } else if(navigator.accelerometer) { + let watchID; + + let source = Rx.Observable.create((observer) => { + + watchID = navigator.compass.watchHeading(function (result) { + observer.onNext(result); + }, function (err) { + observer.onError(err, observer); + }, options); + + }); + + return { + source: source, + watchID: watchID, + clear: () => { + navigator.compass.clearWatch(watchID); + } + } + } + } +}