mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
Stuff
This commit is contained in:
@@ -20,9 +20,11 @@ export class Battery {
|
||||
|
||||
} else {
|
||||
|
||||
window.addEventListener('batterystatus', (battery) => {
|
||||
resolve(Battery._format(battery));
|
||||
});
|
||||
var fnCb = function fnCb(battery) {
|
||||
resolve(battery);
|
||||
window.removeEventListener('batterystatus', fnCb);
|
||||
}
|
||||
window.addEventListener('batterystatus', fnCb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
79
ionic/native/device-motion/device-motion.ts
Normal file
79
ionic/native/device-motion/device-motion.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import * as Rx from 'rx';
|
||||
|
||||
import * as util from 'ionic/util';
|
||||
import {NativePlugin} from '../plugin';
|
||||
import {Platform} from '../../platform/platform';
|
||||
|
||||
@NativePlugin({
|
||||
name: 'Device Motion',
|
||||
platforms: {
|
||||
cordova: 'cordova-plugin-device-motion'
|
||||
}
|
||||
})
|
||||
export class DeviceMotion {
|
||||
getCurrentAcceleration() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(navigator.accelerometer) {
|
||||
navigator.accelerometer.getCurrentAcceleration(function (result) {
|
||||
resolve(result);
|
||||
}, function (err) {
|
||||
reject(err);
|
||||
});
|
||||
} else if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
|
||||
var fnCb = function fnCb(eventData) {
|
||||
resolve(eventData);
|
||||
window.removeEventListener('devicemotion', fnCb);
|
||||
}
|
||||
window.addEventListener('devicemotion', fnCb);
|
||||
} else {
|
||||
this.pluginWarn();
|
||||
reject('The Device does not support device motion events.');
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
watchAcceleration(options) {
|
||||
if(navigator.accelerometer) {
|
||||
let watchID;
|
||||
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
watchID = navigator.accelerometer.watchAcceleration(function (result) {
|
||||
observer.onNext(result);
|
||||
}, function (err) {
|
||||
observer.onError(err, observer);
|
||||
}, options);
|
||||
|
||||
});
|
||||
|
||||
return {
|
||||
source: source,
|
||||
watchID: watchID,
|
||||
clear: () => {
|
||||
navigator.accelerometer.clearWatch(watchID);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let watchID;
|
||||
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
var fnCb = function fnCb(eventData) {
|
||||
observer.onNext(eventData);
|
||||
};
|
||||
|
||||
window.addEventListener('devicemotion', cbFn);
|
||||
|
||||
});
|
||||
|
||||
return {
|
||||
source: source,
|
||||
watchID: watchID,
|
||||
clear: () => {
|
||||
window.removeEventListener('devicemotion', cbFn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,6 @@ export * from './battery/battery'
|
||||
export * from './camera/camera'
|
||||
export * from './contacts/contacts'
|
||||
export * from './device/device'
|
||||
export * from './device-motion/device-motion'
|
||||
export * from './geolocation/geolocation'
|
||||
export * from './vibration/vibration'
|
||||
|
||||
Reference in New Issue
Block a user