mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-14 19:44:53 +00:00
Register events via backgroundMode.on(event)
This commit is contained in:
parent
11f5d0404d
commit
bb72a5e8c8
@ -57,7 +57,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
// Default settings for the notification
|
// Default settings for the notification
|
||||||
private static JSONObject defaultSettings = new JSONObject();
|
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
|
// Used to (un)bind the service to with the activity
|
||||||
private final ServiceConnection connection = new ServiceConnection() {
|
private final ServiceConnection connection = new ServiceConnection() {
|
||||||
@ -67,7 +68,7 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
ForegroundService.ForegroundBinder binder =
|
ForegroundService.ForegroundBinder binder =
|
||||||
(ForegroundService.ForegroundBinder) service;
|
(ForegroundService.ForegroundBinder) service;
|
||||||
|
|
||||||
mService = binder.getService();
|
BackgroundMode.this.service = binder.getService();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -201,7 +202,7 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
*/
|
*/
|
||||||
private void updateNotification(JSONObject settings) {
|
private void updateNotification(JSONObject settings) {
|
||||||
if (isBind) {
|
if (isBind) {
|
||||||
mService.updateNotification(settings);
|
service.updateNotification(settings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,10 +279,13 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
String flag = String.format("%s._isActive=%s;",
|
String flag = String.format("%s._isActive=%s;",
|
||||||
JS_NAMESPACE, active);
|
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);
|
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() {
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,45 +23,9 @@ var exec = require('cordova/exec'),
|
|||||||
channel = require('cordova/channel');
|
channel = require('cordova/channel');
|
||||||
|
|
||||||
|
|
||||||
// Called before 'deviceready' listener will be called
|
/*************
|
||||||
channel.onCordovaReady.subscribe(function () {
|
* INTERFACE *
|
||||||
// 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'
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activates the background mode. When activated the application
|
* Activates the background mode. When activated the application
|
||||||
@ -144,17 +108,100 @@ exports.isActive = function () {
|
|||||||
return this._isActive;
|
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.
|
* Called when the background mode has been activated.
|
||||||
*/
|
*/
|
||||||
exports.onactivate = function () {};
|
exports.onactivate = function () {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* Called when the background mode has been deaktivated.
|
* Called when the background mode has been deaktivated.
|
||||||
*/
|
*/
|
||||||
exports.ondeactivate = function () {};
|
exports.ondeactivate = function () {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* Called when the background mode could not been activated.
|
* Called when the background mode could not been activated.
|
||||||
*
|
*
|
||||||
* @param {Integer} errorCode
|
* @param {Integer} errorCode
|
||||||
@ -162,6 +209,11 @@ exports.ondeactivate = function () {};
|
|||||||
*/
|
*/
|
||||||
exports.onfailure = function () {};
|
exports.onfailure = function () {};
|
||||||
|
|
||||||
|
|
||||||
|
/*********
|
||||||
|
* UTILS *
|
||||||
|
*********/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*
|
*
|
||||||
@ -186,3 +238,44 @@ exports.mergeWithDefaults = function (options) {
|
|||||||
|
|
||||||
return 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({});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user