1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60// install : cordova plugin add cordova-plugin-device-motion
// link : https://github.com/apache/cordova-plugin-device-motion
angular.module('ngCordova.plugins.deviceMotion', [])
.factory('$cordovaDeviceMotion', ['$q', function ($q) {
return {
getCurrentAcceleration: function () {
var q = $q.defer();
if (angular.isUndefined(navigator.accelerometer) ||
!angular.isFunction(navigator.accelerometer.getCurrentAcceleration)) {
q.reject('Device do not support watchAcceleration');
return q.promise;
}
navigator.accelerometer.getCurrentAcceleration(function (result) {
q.resolve(result);
}, function (err) {
q.reject(err);
});
return q.promise;
},
watchAcceleration: function (options) {
var q = $q.defer();
if (angular.isUndefined(navigator.accelerometer) ||
!angular.isFunction(navigator.accelerometer.watchAcceleration)) {
q.reject('Device do not support watchAcceleration');
return q.promise;
}
var watchID = navigator.accelerometer.watchAcceleration(function (result) {
q.notify(result);
}, function (err) {
q.reject(err);
}, options);
q.promise.cancel = function () {
navigator.accelerometer.clearWatch(watchID);
};
q.promise.clearWatch = function (id) {
navigator.accelerometer.clearWatch(id || watchID);
};
q.promise.watchID = watchID;
return q.promise;
},
clearWatch: function (watchID) {
return navigator.accelerometer.clearWatch(watchID);
}
};
}]);