mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2025-12-13 09:53:22 +00:00
Register events via backgroundMode.on(event)
This commit is contained in:
@@ -23,45 +23,9 @@ var exec = require('cordova/exec'),
|
||||
channel = require('cordova/channel');
|
||||
|
||||
|
||||
// Called before 'deviceready' listener will be called
|
||||
channel.onCordovaReady.subscribe(function () {
|
||||
// Device plugin is ready now
|
||||
channel.onCordovaInfoReady.subscribe(function () {
|
||||
// Set the defaults
|
||||
exports.setDefaults({});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Flag indicated if the mode is enabled.
|
||||
*/
|
||||
exports._isEnabled = false;
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Flag indicated if the mode is active.
|
||||
*/
|
||||
exports._isActive = false;
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Default values of all available options.
|
||||
*/
|
||||
exports._defaults = {
|
||||
title: 'App is running in background',
|
||||
text: 'Doing heavy tasks.',
|
||||
ticker: 'Running in background',
|
||||
resume: true,
|
||||
silent: false,
|
||||
color: undefined,
|
||||
icon: 'icon'
|
||||
};
|
||||
|
||||
/*************
|
||||
* INTERFACE *
|
||||
*************/
|
||||
|
||||
/**
|
||||
* Activates the background mode. When activated the application
|
||||
@@ -144,17 +108,100 @@ exports.isActive = function () {
|
||||
return this._isActive;
|
||||
};
|
||||
|
||||
|
||||
/**********
|
||||
* EVENTS *
|
||||
**********/
|
||||
|
||||
exports._listener = {};
|
||||
|
||||
/**
|
||||
* Fire event with given arguments.
|
||||
*
|
||||
* @param [ String ] event The event's name.
|
||||
* @param {args*} The callback's arguments.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
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++) {
|
||||
var fn = listener[i][0],
|
||||
scope = listener[i][1];
|
||||
|
||||
fn.apply(scope, args);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Register callback for given event.
|
||||
*
|
||||
* @param [ String ] event The event's name.
|
||||
* @param [ Function ] callback The function to be exec as callback.
|
||||
* @param [ Object ] scope The callback function's scope.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.on = function (event, callback, scope) {
|
||||
|
||||
if (typeof callback !== "function")
|
||||
return;
|
||||
|
||||
if (!this._listener[event]) {
|
||||
this._listener[event] = [];
|
||||
}
|
||||
|
||||
var item = [callback, scope || window];
|
||||
|
||||
this._listener[event].push(item);
|
||||
};
|
||||
|
||||
/**
|
||||
* Unregister callback for given event.
|
||||
*
|
||||
* @param [ String ] event The event's name.
|
||||
* @param [ Function ] callback The function to be exec as callback.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.un = function (event, callback) {
|
||||
var listener = this._listener[event];
|
||||
|
||||
if (!listener)
|
||||
return;
|
||||
|
||||
for (var i = 0; i < listener.length; i++) {
|
||||
var fn = listener[i][0];
|
||||
|
||||
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
|
||||
@@ -162,6 +209,11 @@ exports.ondeactivate = function () {};
|
||||
*/
|
||||
exports.onfailure = function () {};
|
||||
|
||||
|
||||
/*********
|
||||
* UTILS *
|
||||
*********/
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
@@ -186,3 +238,44 @@ exports.mergeWithDefaults = function (options) {
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
|
||||
/***********
|
||||
* PRIVATE *
|
||||
***********/
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Flag indicates if the mode is enabled.
|
||||
*/
|
||||
exports._isEnabled = false;
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Flag indicates if the mode is active.
|
||||
*/
|
||||
exports._isActive = false;
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Default values of all available options.
|
||||
*/
|
||||
exports._defaults = {
|
||||
title: 'App is running in background',
|
||||
text: 'Doing heavy tasks.',
|
||||
ticker: 'Running in background',
|
||||
resume: true,
|
||||
silent: false,
|
||||
color: undefined,
|
||||
icon: 'icon'
|
||||
};
|
||||
|
||||
// Called before 'deviceready' listener will be called
|
||||
channel.onCordovaReady.subscribe(function () {
|
||||
channel.onCordovaInfoReady.subscribe(function () {
|
||||
exports.setDefaults({});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user