'use strict'; /* Services */ // Sensor value service. angular.module('App.sensorServices', []) // phonegap ready service - listens to deviceready .factory('phonegapReady', function() { return function (fn) { var queue = []; var impl = function () { queue.push(Array.prototype.slice.call(arguments)); }; document.addEventListener('deviceready', function () { queue.forEach(function (args) { fn.apply(this, args); }); impl = fn; }, false); return function () { return impl.apply(this, arguments); }; }; }) .factory('geolocation', function ($rootScope, phonegapReady) { var locationObj = { watchId: null, getCurrentPosition: phonegapReady(function (onSuccess, onError, options) { navigator.geolocation.getCurrentPosition(function () { var that = this, args = arguments; if (onSuccess) { $rootScope.$apply(function () { onSuccess.apply(that, args); }); } }, function () { var that = this, args = arguments; if (onError) { $rootScope.$apply(function () { onError.apply(that, args); }); } }, options); }), watchPosition: phonegapReady(function (onSuccess, onError, options) { var self = this; self.watchId = navigator.geolocation.watchPosition(function () { var that = this, args = arguments; if (onSuccess) { $rootScope.$apply(function () { onSuccess.apply(that, args); }); } }, function () { var that = this, args = arguments; if (onError) { $rootScope.$apply(function () { onError.apply(that, args); }); } }, options); }), clearWatch: phonegapReady(function () { var self = this; navigator.geolocation.clearWatch(self.watchId); }) } return locationObj; }) .factory('accelerometer', function ($rootScope, phonegapReady) { return { getCurrentAcceleration: phonegapReady(function (onSuccess, onError) { navigator.accelerometer.getCurrentAcceleration(function () { var that = this, args = arguments; if (onSuccess) { $rootScope.$apply(function () { onSuccess.apply(that, args); }); } }, function () { var that = this, args = arguments; if (onError) { $rootScope.$apply(function () { onError.apply(that, args); }); } }); }) }; }) .factory('notification', function ($rootScope, phonegapReady) { return { alert: phonegapReady(function (message, alertCallback, title, buttonName) { navigator.notification.alert(message, function () { var that = this, args = arguments; $rootScope.$apply(function () { alertCallback.apply(that, args); }); }, title, buttonName); }), confirm: phonegapReady(function (message, confirmCallback, title, buttonLabels) { navigator.notification.confirm(message, function () { var that = this, args = arguments; $rootScope.$apply(function () { confirmCallback.apply(that, args); }); }, title, buttonLabels); }), beep: phonegapReady(function (times) { navigator.notification.beep(times); }), vibrate: phonegapReady(function (milliseconds) { navigator.notification.vibrate(milliseconds); }) }; }) .factory('navSvc', function($navigate) { return { slidePage: function (path,type) { $navigate.go(path,type); }, back: function () { $navigate.back(); } } }) .factory('compass', function ($rootScope, phonegapReady) { return { getCurrentHeading: phonegapReady(function (onSuccess, onError) { navigator.compass.getCurrentHeading(function () { var that = this, args = arguments; if (onSuccess) { $rootScope.$apply(function () { onSuccess.apply(that, args); }); } }, function () { var that = this, args = arguments; if (onError) { $rootScope.$apply(function () { onError.apply(that, args); }); } }); }), watchHeading: phonegapReady(function (onSuccess, onError, options) { navigator.compass.watchHeading(function () { var that = this, args = arguments; if (onSuccess) { $rootScope.$apply(function () { onSuccess.apply(that, args); }); } }, function () { var that = this, args = arguments; if (onError) { $rootScope.$apply(function () { onError.apply(that, args); }); } }, options); }), clearWatch: phonegapReady(function (watchID) { navigator.compass.clearWatch(watchID); }) }; }) .factory('contacts', function ($rootScope, phonegapReady) { return { findContacts: phonegapReady(function (onSuccess, onError) { var options = new ContactFindOptions(); options.filter=""; options.multiple=true; var fields = ["displayName", "name"]; navigator.contacts.find(fields, function(r){console.log("Success" +r.length);var that = this, args = arguments; if (onSuccess) { $rootScope.$apply(function () { onSuccess.apply(that, args); }); } }, function () { var that = this, args = arguments; if (onError) { $rootScope.$apply(function () { onError.apply(that, args); }); } }, options) }) } });