diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index cd00bd7..e236459 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -57,7 +57,8 @@ public class BackgroundMode extends CordovaPlugin { // Default settings for the notification private static JSONObject defaultSettings = new JSONObject(); - ForegroundService mService; + // Service that keeps the app awake + private ForegroundService service; // Used to (un)bind the service to with the activity private final ServiceConnection connection = new ServiceConnection() { @@ -67,7 +68,7 @@ public class BackgroundMode extends CordovaPlugin { ForegroundService.ForegroundBinder binder = (ForegroundService.ForegroundBinder) service; - mService = binder.getService(); + BackgroundMode.this.service = binder.getService(); } @Override @@ -201,7 +202,7 @@ public class BackgroundMode extends CordovaPlugin { */ private void updateNotification(JSONObject settings) { if (isBind) { - mService.updateNotification(settings); + service.updateNotification(settings); } } @@ -278,10 +279,13 @@ public class BackgroundMode extends CordovaPlugin { String flag = String.format("%s._isActive=%s;", JS_NAMESPACE, active); - String fn = String.format("setTimeout('%s.on%s(%s)',0);", + String depFn = String.format("%s.on%s(%s);", JS_NAMESPACE, eventName, params); - final String js = flag + fn; + String fn = String.format("%s.fireEvent('%s',%s);", + JS_NAMESPACE, eventName, params); + + final String js = flag + fn + depFn; cordova.getActivity().runOnUiThread(new Runnable() { @Override diff --git a/www/background-mode.js b/www/background-mode.js index e1213c7..d22628e 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -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({}); + }); +});