Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Change to geolocation, orientation, and motion APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Jul 10, 2014
1 parent 2976cf4 commit 56d17a8
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 66 deletions.
18 changes: 12 additions & 6 deletions demo/app/www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ angular.module('starter.controllers', [])
})

.controller('GeolocationCtrl', function($scope, $cordovaGeolocation) {
$cordovaGeolocation.watchPosition().then(function(resp) {
$cordovaGeolocation.watchPosition({
frequency: 100
}).promise.then(function(resp) {
}, function(err) {
}, function(position) {
$scope.lat = position.coords.latitude;
Expand All @@ -45,7 +47,7 @@ angular.module('starter.controllers', [])

$scope.getLatLng = function() {
if(!$scope.lat && !$scope.lng) { return '45.787, -89.052'; }
return $scope.lat.toFixed(3) + ', ' + $scope.lng.toFixed(3);
return $scope.lat.toFixed(7) + ', ' + $scope.lng.toFixed(7);
}
/*
$scope.toggleTrack = function() {
Expand All @@ -60,7 +62,9 @@ angular.module('starter.controllers', [])
})

.controller('CompassCtrl', function($scope, $cordovaDeviceOrientation) {
$cordovaDeviceOrientation.watchHeading().then(function(resp) {
$cordovaDeviceOrientation.watchHeading({
frequency: 100
}).promise.then(function(resp) {
}, function(err) {
}, function(position) {
$scope.compass = position.magneticHeading;
Expand Down Expand Up @@ -160,13 +164,15 @@ angular.module('starter.controllers', [])
}
})

.controller('AccelCtrl', function($scope, $cordovaAccelerometer) {
.controller('AccelCtrl', function($scope, $cordovaDeviceMotion) {
console.log('Accel');
$scope.toggleTrack = function() {
$cordovaAccelerometer.watchAcceleration().then(function(resp) {
console.log('Accel tracking');
$cordovaDeviceMotion.watchAcceleration({
frequency: 100
}).promise.then(function(resp) {
}, function(err) {
}, function(data) {
console.log('Data', data)
$scope.x = data.x;
$scope.y = data.y;
$scope.z = data.z;
Expand Down
150 changes: 126 additions & 24 deletions demo/app/www/lib/ngCordova/ng-cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,18 @@ angular.module('ngCordova.plugins.deviceMotion', [])
watchAcceleration: function(options) {
var q = $q.defer();

navigator.accelerometer.watchAcceleration(function(result) {
var watchId = navigator.accelerometer.watchAcceleration(function(result) {
// Do any magic you need
//q.resolve(watchID);
q.notify(result);
}, function(err) {
q.reject(err);
}, options);

return q.promise;
return {
watchId: watchId,
promise: q.promise
}
},
clearWatch: function(watchID) {
return navigator.accelerometer.clearWatch(watchID);
Expand All @@ -306,16 +310,33 @@ angular.module('ngCordova.plugins.deviceOrientation', [])
.factory('$cordovaDeviceOrientation', ['$q', function($q) {

return {
getCurrentHeading: function() {
var q = $q.defer();

navigator.compass.getCurrentHeading(function(heading) {
q.resolve(heading);
}, function(err) {
q.reject(err);
});

return q.promise;
},
watchHeading: function(options) {
var q = $q.defer();

navigator.compass.watchHeading(function(result) {
var watchId = navigator.compass.watchHeading(function(result) {
q.notify(result);
}, function(err) {
q.reject(err);
}, options);

return q.promise;
return {
watchId: watchId,
promise: q.promise
}
},
clearWatch: function(watchID) {
navigator.compass.clearWatch();
}
}
}]);
Expand Down Expand Up @@ -389,12 +410,17 @@ angular.module('ngCordova.plugins.file', [])
);
},

checkFile: function (dir, file) {
checkFile: function (filePath) {
var q = $q.defer();

// Backward compatibility for previous function checkFile(dir, file)
if (arguments.length == 2) {
filePath = '/' + filePath + '/' + arguments[1];
}

getFilesystem().then(
function (filesystem) {
filesystem.root.getFile('/' + dir + '/' + file, {create: false},
filesystem.root.getFile(filePath, {create: false},
// File exists
function () {
q.resolve();
Expand All @@ -410,10 +436,16 @@ angular.module('ngCordova.plugins.file', [])
return q.promise;
},

createFile: function (dir, file, replaceBOOL) {
createFile: function (filePath, replaceBOOL) {
// Backward compatibility for previous function createFile(dir, file, replaceBOOL)
if (arguments.length == 3) {
filePath = '/' + filePath + '/' + arguments[1];
replaceBOOL = arguments[2];
}

getFilesystem().then(
function (filesystem) {
filesystem.root.getFile('/' + dir + '/' + file, {create: true, exclusive: replaceBOOL},
filesystem.root.getFile(filePath, {create: true, exclusive: replaceBOOL},
function (success) {

},
Expand All @@ -424,12 +456,17 @@ angular.module('ngCordova.plugins.file', [])
);
},

removeFile: function (dir, file) {
removeFile: function (filePath) {
var q = $q.defer();

// Backward compatibility for previous function removeFile(dir, file)
if (arguments.length == 2) {
filePath = '/' + filePath + '/' + arguments[1];
}

getFilesystem().then(
function (filesystem) {
filesystem.root.getFile('/' + dir + '/' + file, {create: false}, function (fileEntry) {
filesystem.root.getFile(filePath, {create: false}, function (fileEntry) {
fileEntry.remove(function () {
q.resolve();
});
Expand All @@ -440,12 +477,17 @@ angular.module('ngCordova.plugins.file', [])
return q.promise;
},

writeFile: function (dir, file) {
writeFile: function (filePath) {
var q = $q.defer();

// Backward compatibility for previous function writeFile(dir, file)
if (arguments.length == 2) {
filePath = '/' + filePath + '/' + arguments[1];
}

getFilesystem().then(
function (filesystem) {
filesystem.root.getFile('/' + dir + '/' + file, {create: false},
filesystem.root.getFile(filePath, {create: false},
function (fileEntry) {
fileEntry.createWriter(
function (fileWriter) {
Expand All @@ -462,13 +504,18 @@ angular.module('ngCordova.plugins.file', [])
return q.promise;
},

readFile: function (dir, file) {
readFile: function (filePath) {
var q = $q.defer();

// Backward compatibility for previous function readFile(dir, file)
if (arguments.length == 2) {
filePath = '/' + filePath + '/' + arguments[1];
}

getFilesystem().then(
function (filesystem) {

filesystem.root.getFile('/' + dir + '/' + file, {create: false},
filesystem.root.getFile(filePath, {create: false},
// success
function (fileEntry) {
fileEntry.file(function (file) {
Expand All @@ -490,16 +537,35 @@ angular.module('ngCordova.plugins.file', [])
return q.promise;
},

readFileMetadata: function (filePath) {
var q = $q.defer();

getFilesystem().then(
function (filesystem) {
filesystem.root.getFile(filePath, {create: false},
// success
function (fileEntry) {
fileEntry.file(function (file) {
q.resolve(file);
});
},
// error
function (error) {
q.reject(error);
});
}
);

return q.promise;
},

downloadFile: function (source, filePath, trustAllHosts, options) {
var q = $q.defer();
var fileTransfer = new FileTransfer();
var uri = encodeURI(source);

fileTransfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
q.notify(perc);
}
q.notify(progressEvent);
};

fileTransfer.download(
Expand All @@ -522,10 +588,7 @@ angular.module('ngCordova.plugins.file', [])
var uri = encodeURI(server);

fileTransfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.floor(progressEvent.loaded / progressEvent.total * 100);
q.notify(perc);
}
q.notify(progressEvent);
};

fileTransfer.upload(
Expand Down Expand Up @@ -557,6 +620,41 @@ angular.module('ngCordova.plugins.file', [])
return q.promise;
}
}]);
angular.module('ngCordova.plugins.flashlight', [])

.factory('$cordovaFlashlight', ['$q', function ($q) {

return {
available: function () {
var q = $q.defer();
window.plugins.flashlight.available(function (isAvailable) {
q.resolve(isAvailable);
});
return q.promise;
},

switchOn: function () {
var q = $q.defer();
window.plugins.flashlight.switchOn(function (response) {
q.resolve(response);
}, function (error) {
q.reject(error)
});
return q.promise;
},

switchOff: function () {
var q = $q.defer();
window.plugins.flashlight.switchOff(function (response) {
q.resolve(response);
}, function (error) {
q.reject(error)
});
return q.promise;
}
}
}
]);
angular.module('ngCordova.plugins.ga', [])

.factory('$cordovaGA', ['$q', function ($q) {
Expand Down Expand Up @@ -626,15 +724,18 @@ angular.module('ngCordova.plugins.geolocation', [])
watchPosition: function(options) {
var q = $q.defer();

navigator.geolocation.watchPosition(function(result) {
var watchId = navigator.geolocation.watchPosition(function(result) {
// Do any magic you need
q.notify(result);

}, function(err) {
q.reject(err);
}, options);

return q.promise;
return {
watchId: watchId,
promise: q.promise
}
},

clearWatch: function(watchID) {
Expand Down Expand Up @@ -856,6 +957,7 @@ angular.module('ngCordova.plugins', [
'ngCordova.plugins.pinDialog',
'ngCordova.plugins.localNotification',
'ngCordova.plugins.toast',
'ngCordova.plugins.flashlight',
'ngCordova.plugins.capture',
'ngCordova.plugins.appAvailability',
'ngCordova.plugins.prefs'
Expand Down
2 changes: 1 addition & 1 deletion demo/app/www/lib/ngCordova/ng-cordova.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/app/www/templates/plugins/geolocation.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#geo-val {
vertical-align: middle;
display: inline-block;
font-size: 35px;
font-size: 25px;
color: #4a87ee;
}
#geo-box {
Expand Down
Loading

0 comments on commit 56d17a8

Please sign in to comment.