mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-14 19:44:53 +00:00
Additional improvements of method configure
This commit is contained in:
parent
5fdfb9885c
commit
110ec9d024
@ -268,29 +268,19 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* @param params Optional arguments for the event
|
||||
*/
|
||||
private void fireEvent (Event event, String params) {
|
||||
String eventName;
|
||||
String eventName = event.name().toLowerCase();
|
||||
Boolean active = event == Event.ACTIVATE;
|
||||
|
||||
switch (event) {
|
||||
case ACTIVATE:
|
||||
eventName = "activate"; break;
|
||||
case DEACTIVATE:
|
||||
eventName = "deactivate"; break;
|
||||
default:
|
||||
eventName = "failure";
|
||||
}
|
||||
|
||||
String active = event == Event.ACTIVATE ? "true" : "false";
|
||||
|
||||
String flag = String.format("%s._isActive=%s;",
|
||||
String str = String.format("%s._setActive(%b)",
|
||||
JS_NAMESPACE, active);
|
||||
|
||||
String depFn = String.format("%s.on%s(%s);",
|
||||
JS_NAMESPACE, eventName, params);
|
||||
str = String.format("%s;%s.on%s(%s)",
|
||||
str, JS_NAMESPACE, eventName, params);
|
||||
|
||||
String fn = String.format("%s.fireEvent('%s',%s);",
|
||||
JS_NAMESPACE, eventName, params);
|
||||
str = String.format("%s;%s.fireEvent('%s',%s);",
|
||||
str, JS_NAMESPACE, eventName, params);
|
||||
|
||||
final String js = flag + fn + depFn;
|
||||
final String js = str;
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
|
@ -122,7 +122,7 @@ exports.setDefaults = function (overrides) {
|
||||
* Configures the notification settings for Android.
|
||||
* Will be merged with the defaults.
|
||||
*
|
||||
* @param [ Object ] overrides Dict of options to be overridden.
|
||||
* @param [ Object ] options Dict of options to be overridden.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
@ -130,13 +130,19 @@ exports.configure = function (options) {
|
||||
var settings = this.getSettings(),
|
||||
defaults = this.getDefaults();
|
||||
|
||||
this.mergeObjects(options, settings);
|
||||
this.mergeObjects(options, defaults);
|
||||
if (!this._isAndroid)
|
||||
return;
|
||||
|
||||
if (!this._isActive) {
|
||||
console.log('BackgroundMode is not active, skipped...');
|
||||
return;
|
||||
}
|
||||
|
||||
this._mergeObjects(options, settings);
|
||||
this._mergeObjects(options, defaults);
|
||||
this._settings = options;
|
||||
|
||||
if (this._isAndroid) {
|
||||
cordova.exec(null, null, 'BackgroundMode', 'configure', [options, true]);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -352,54 +358,6 @@ exports.ondeactivate = function () {};
|
||||
exports.onfailure = function() {};
|
||||
|
||||
|
||||
/*********
|
||||
* UTILS *
|
||||
*********/
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Merge settings with default values.
|
||||
*
|
||||
* @param [ Object ] options The custom options.
|
||||
* @param [ Object ] toMergeIn The options to merge in.
|
||||
*
|
||||
* @return [ Object ] Default values merged with custom values.
|
||||
*/
|
||||
exports.mergeObjects = function (options, toMergeIn) {
|
||||
for (var key in toMergeIn) {
|
||||
if (!options.hasOwnProperty(key)) {
|
||||
options[key] = toMergeIn[key];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Initialize the plugin.
|
||||
*
|
||||
* Method should be called after the 'deviceready' event
|
||||
* but before the event listeners will be called.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.pluginInitialize = function () {
|
||||
this._isAndroid = device.platform.match(/^android|amazon/i) !== null;
|
||||
this.setDefaults({});
|
||||
|
||||
if (device.platform == 'browser') {
|
||||
this.enable();
|
||||
this._isEnabled = true;
|
||||
}
|
||||
|
||||
this._isActive = this._isActive || device.platform == 'browser';
|
||||
};
|
||||
|
||||
|
||||
/***********
|
||||
* PRIVATE *
|
||||
***********/
|
||||
@ -434,10 +392,71 @@ exports._defaults = {
|
||||
icon: 'icon'
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Merge settings with default values.
|
||||
*
|
||||
* @param [ Object ] options The custom options.
|
||||
* @param [ Object ] toMergeIn The options to merge in.
|
||||
*
|
||||
* @return [ Object ] Default values merged with custom values.
|
||||
*/
|
||||
exports._mergeObjects = function (options, toMergeIn) {
|
||||
for (var key in toMergeIn) {
|
||||
if (!options.hasOwnProperty(key)) {
|
||||
options[key] = toMergeIn[key];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Setter for the isActive flag. Resets the
|
||||
* settings if the mode isnt active anymore.
|
||||
*
|
||||
* @param [ Boolean] value The new value for the flag.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports._setActive = function(value) {
|
||||
if (this._isActive == value)
|
||||
return;
|
||||
|
||||
this._isActive = value;
|
||||
this._settings = value ? this._mergeObjects({}, this._defaults) : {};
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Initialize the plugin.
|
||||
*
|
||||
* Method should be called after the 'deviceready' event
|
||||
* but before the event listeners will be called.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports._pluginInitialize = function() {
|
||||
this._isAndroid = device.platform.match(/^android|amazon/i) !== null;
|
||||
this.setDefaults({});
|
||||
|
||||
if (device.platform == 'browser') {
|
||||
this.enable();
|
||||
this._isEnabled = true;
|
||||
}
|
||||
|
||||
this._isActive = this._isActive || device.platform == 'browser';
|
||||
};
|
||||
|
||||
// Called before 'deviceready' listener will be called
|
||||
channel.onCordovaReady.subscribe(function() {
|
||||
channel.onCordovaInfoReady.subscribe(function() {
|
||||
exports.pluginInitialize();
|
||||
exports._pluginInitialize();
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user