diff --git a/www/background-mode.js b/www/background-mode.js index 440edc3..85f9b50 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -22,11 +22,6 @@ var exec = require('cordova/exec'), channel = require('cordova/channel'); - -/************* - * INTERFACE * - *************/ - /** * Activates the background mode. When activated the application * will be prevented from going to sleep while in background @@ -34,14 +29,15 @@ var exec = require('cordova/exec'), * * @return [ Void ] */ -exports.enable = function() { +exports.enable = function() +{ if (this.isEnabled()) return; var fn = function() { - exports._isEnabled = true; - exports.fireEvent('enable'); - }; + exports._isEnabled = true; + exports.fireEvent('enable'); + }; cordova.exec(fn, null, 'BackgroundMode', 'enable', []); }; @@ -52,14 +48,15 @@ exports.enable = function() { * * @return [ Void ] */ -exports.disable = function() { +exports.disable = function() +{ if (!this.isEnabled()) return; var fn = function() { - exports._isEnabled = false; - exports.fireEvent('disable'); - }; + exports._isEnabled = false; + exports.fireEvent('disable'); + }; cordova.exec(fn, null, 'BackgroundMode', 'disable', []); }; @@ -71,7 +68,8 @@ exports.disable = function() { * * @return [ Void ] */ -exports.setEnabled = function (enable) { +exports.setEnabled = function (enable) +{ if (enable) { this.enable(); } else { @@ -84,7 +82,8 @@ exports.setEnabled = function (enable) { * * @return [ Object ] */ -exports.getDefaults = function() { +exports.getDefaults = function() +{ return this._defaults; }; @@ -93,7 +92,8 @@ exports.getDefaults = function() { * * @return [ Object ] */ -exports.getSettings = function() { +exports.getSettings = function() +{ return this._settings || {}; }; @@ -104,16 +104,20 @@ exports.getSettings = function() { * * @return [ Void ] */ -exports.setDefaults = function (overrides) { +exports.setDefaults = function (overrides) +{ var defaults = this.getDefaults(); - for (var key in defaults) { - if (overrides.hasOwnProperty(key)) { + for (var key in defaults) + { + if (overrides.hasOwnProperty(key)) + { defaults[key] = overrides[key]; } } - if (this._isAndroid) { + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]); } }; @@ -126,14 +130,16 @@ exports.setDefaults = function (overrides) { * * @return [ Void ] */ -exports.configure = function (options) { +exports.configure = function (options) +{ var settings = this.getSettings(), defaults = this.getDefaults(); if (!this._isAndroid) return; - if (!this._isActive) { + if (!this._isActive) + { console.log('BackgroundMode is not active, skipped...'); return; } @@ -150,8 +156,10 @@ exports.configure = function (options) { * * @return [ Void ] */ -exports.disableWebViewOptimizations = function() { - if (this._isAndroid) { +exports.disableWebViewOptimizations = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'optimizations', []); } }; @@ -161,8 +169,10 @@ exports.disableWebViewOptimizations = function() { * * @return [ Void ] */ -exports.moveToBackground = function() { - if (this._isAndroid) { +exports.moveToBackground = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'background', []); } }; @@ -172,8 +182,10 @@ exports.moveToBackground = function() { * * @return [ Void ] */ -exports.moveToForeground = function() { - if (this.isActive() && this._isAndroid) { +exports.moveToForeground = function() +{ + if (this.isActive() && this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'foreground', []); } }; @@ -183,8 +195,10 @@ exports.moveToForeground = function() { * * @return [ Void ] */ -exports.excludeFromTaskList = function() { - if (this._isAndroid) { +exports.excludeFromTaskList = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); } }; @@ -195,7 +209,8 @@ exports.excludeFromTaskList = function() { * * @return [ Void ] */ -exports.overrideBackButton = function() { +exports.overrideBackButton = function() +{ document.addEventListener('backbutton', function() { exports.moveToBackground(); }, false); @@ -208,10 +223,14 @@ exports.overrideBackButton = function() { * * @return [ Void ] */ -exports.isScreenOff = function (fn) { - if (this._isAndroid) { +exports.isScreenOff = function (fn) +{ + if (this._isAndroid) + { cordova.exec(fn, null, 'BackgroundMode', 'dimmed', []); - } else { + } + else + { fn(undefined); } }; @@ -221,8 +240,10 @@ exports.isScreenOff = function (fn) { * * @return [ Void ] */ -exports.wakeUp = function() { - if (this._isAndroid) { +exports.wakeUp = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'wakeup', []); } }; @@ -232,8 +253,10 @@ exports.wakeUp = function() { * * @return [ Void ] */ -exports.unlock = function() { - if (this._isAndroid) { +exports.unlock = function() +{ + if (this._isAndroid) + { cordova.exec(null, null, 'BackgroundMode', 'unlock', []); } }; @@ -243,7 +266,8 @@ exports.unlock = function() { * * @return [ Boolean ] */ -exports.isEnabled = function() { +exports.isEnabled = function() +{ return this._isEnabled !== false; }; @@ -252,15 +276,11 @@ exports.isEnabled = function() { * * @return [ Boolean ] */ -exports.isActive = function() { +exports.isActive = function() +{ return this._isActive !== false; }; - -/********** - * EVENTS * - **********/ - exports._listener = {}; /** @@ -271,14 +291,16 @@ exports._listener = {}; * * @return [ Void ] */ -exports.fireEvent = function (event) { +exports.fireEvent = function (event) +{ var args = Array.apply(null, arguments).slice(1), listener = this._listener[event]; if (!listener) return; - for (var i = 0; i < listener.length; i++) { + for (var i = 0; i < listener.length; i++) + { var fn = listener[i][0], scope = listener[i][1]; @@ -295,12 +317,13 @@ exports.fireEvent = function (event) { * * @return [ Void ] */ -exports.on = function (event, callback, scope) { - +exports.on = function (event, callback, scope) +{ if (typeof callback !== "function") return; - if (!this._listener[event]) { + if (!this._listener[event]) + { this._listener[event] = []; } @@ -317,51 +340,25 @@ exports.on = function (event, callback, scope) { * * @return [ Void ] */ -exports.un = function (event, callback) { +exports.un = function (event, callback) +{ var listener = this._listener[event]; if (!listener) return; - for (var i = 0; i < listener.length; i++) { + for (var i = 0; i < listener.length; i++) + { var fn = listener[i][0]; - if (fn == callback) { + if (fn == callback) + { listener.splice(i, 1); break; } } }; -/** - * @deprecated - * - * Called when the background mode has been activated. - */ -exports.onactivate = function() {}; - -/** - * @deprecated - * - * Called when the background mode has been deaktivated. - */ -exports.ondeactivate = function() {}; - -/** - * @deprecated - * - * Called when the background mode could not been activated. - * - * @param {Integer} errorCode - * Error code which describes the error - */ -exports.onfailure = function() {}; - - -/*********** - * PRIVATE * - ***********/ - /** * @private * @@ -381,7 +378,8 @@ exports._isActive = false; * * Default values of all available options. */ -exports._defaults = { +exports._defaults = +{ title: 'App is running in background', text: 'Doing heavy tasks.', bigText: false, @@ -402,9 +400,12 @@ exports._defaults = { * * @return [ Object ] Default values merged with custom values. */ -exports._mergeObjects = function (options, toMergeIn) { - for (var key in toMergeIn) { - if (!options.hasOwnProperty(key)) { +exports._mergeObjects = function (options, toMergeIn) +{ + for (var key in toMergeIn) + { + if (!options.hasOwnProperty(key)) + { options[key] = toMergeIn[key]; continue; } @@ -423,7 +424,8 @@ exports._mergeObjects = function (options, toMergeIn) { * * @return [ Void ] */ -exports._setActive = function(value) { +exports._setActive = function(value) +{ if (this._isActive == value) return; @@ -441,11 +443,13 @@ exports._setActive = function(value) { * * @return [ Void ] */ -exports._pluginInitialize = function() { +exports._pluginInitialize = function() +{ this._isAndroid = device.platform.match(/^android|amazon/i) !== null; this.setDefaults({}); - if (device.platform == 'browser') { + if (device.platform == 'browser') + { this.enable(); this._isEnabled = true; } @@ -454,19 +458,23 @@ exports._pluginInitialize = function() { }; // Called before 'deviceready' listener will be called -channel.onCordovaReady.subscribe(function() { +channel.onCordovaReady.subscribe(function() +{ channel.onCordovaInfoReady.subscribe(function() { exports._pluginInitialize(); }); }); // Called after 'deviceready' event -channel.deviceready.subscribe(function() { - if (exports.isEnabled()) { +channel.deviceready.subscribe(function() +{ + if (exports.isEnabled()) + { exports.fireEvent('enable'); } - if (exports.isActive()) { + if (exports.isActive()) + { exports.fireEvent('activate'); } });