Additional improvements of method configure

This commit is contained in:
Sebastián Katzer 2017-02-10 20:52:42 +01:00
parent 5fdfb9885c
commit 110ec9d024
2 changed files with 103 additions and 94 deletions

View File

@ -268,29 +268,19 @@ public class BackgroundMode extends CordovaPlugin {
* @param params Optional arguments for the event * @param params Optional arguments for the event
*/ */
private void fireEvent (Event event, String params) { private void fireEvent (Event event, String params) {
String eventName; String eventName = event.name().toLowerCase();
Boolean active = event == Event.ACTIVATE;
switch (event) { String str = String.format("%s._setActive(%b)",
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;",
JS_NAMESPACE, active); JS_NAMESPACE, active);
String depFn = String.format("%s.on%s(%s);", str = String.format("%s;%s.on%s(%s)",
JS_NAMESPACE, eventName, params); str, JS_NAMESPACE, eventName, params);
String fn = String.format("%s.fireEvent('%s',%s);", str = String.format("%s;%s.fireEvent('%s',%s);",
JS_NAMESPACE, eventName, params); str, JS_NAMESPACE, eventName, params);
final String js = flag + fn + depFn; final String js = str;
cordova.getActivity().runOnUiThread(new Runnable() { cordova.getActivity().runOnUiThread(new Runnable() {
@Override @Override

View File

@ -34,11 +34,11 @@ var exec = require('cordova/exec'),
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.enable = function () { exports.enable = function() {
if (this.isEnabled()) if (this.isEnabled())
return; return;
var fn = function () { var fn = function() {
exports._isEnabled = true; exports._isEnabled = true;
exports.fireEvent('enable'); exports.fireEvent('enable');
}; };
@ -52,11 +52,11 @@ exports.enable = function () {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.disable = function () { exports.disable = function() {
if (!this.isEnabled()) if (!this.isEnabled())
return; return;
var fn = function () { var fn = function() {
exports._isEnabled = false; exports._isEnabled = false;
exports.fireEvent('disable'); exports.fireEvent('disable');
}; };
@ -84,7 +84,7 @@ exports.setEnabled = function (enable) {
* *
* @return [ Object ] * @return [ Object ]
*/ */
exports.getDefaults = function () { exports.getDefaults = function() {
return this._defaults; return this._defaults;
}; };
@ -93,7 +93,7 @@ exports.getDefaults = function () {
* *
* @return [ Object ] * @return [ Object ]
*/ */
exports.getSettings = function () { exports.getSettings = function() {
return this._settings || {}; return this._settings || {};
}; };
@ -122,7 +122,7 @@ exports.setDefaults = function (overrides) {
* Configures the notification settings for Android. * Configures the notification settings for Android.
* Will be merged with the defaults. * 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 ] * @return [ Void ]
*/ */
@ -130,13 +130,19 @@ exports.configure = function (options) {
var settings = this.getSettings(), var settings = this.getSettings(),
defaults = this.getDefaults(); defaults = this.getDefaults();
this.mergeObjects(options, settings); if (!this._isAndroid)
this.mergeObjects(options, defaults); return;
if (!this._isActive) {
console.log('BackgroundMode is not active, skipped...');
return;
}
this._mergeObjects(options, settings);
this._mergeObjects(options, defaults);
this._settings = options; this._settings = options;
if (this._isAndroid) { cordova.exec(null, null, 'BackgroundMode', 'configure', [options, true]);
cordova.exec(null, null, 'BackgroundMode', 'configure', [options, true]);
}
}; };
/** /**
@ -144,7 +150,7 @@ exports.configure = function (options) {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.disableWebViewOptimizations = function () { exports.disableWebViewOptimizations = function() {
if (this._isAndroid) { if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'optimizations', []); cordova.exec(null, null, 'BackgroundMode', 'optimizations', []);
} }
@ -155,7 +161,7 @@ exports.disableWebViewOptimizations = function () {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.moveToBackground = function () { exports.moveToBackground = function() {
if (this._isAndroid) { if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'background', []); cordova.exec(null, null, 'BackgroundMode', 'background', []);
} }
@ -166,7 +172,7 @@ exports.moveToBackground = function () {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.moveToForeground = function () { exports.moveToForeground = function() {
if (this.isActive() && this._isAndroid) { if (this.isActive() && this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'foreground', []); cordova.exec(null, null, 'BackgroundMode', 'foreground', []);
} }
@ -177,7 +183,7 @@ exports.moveToForeground = function () {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.excludeFromTaskList = function () { exports.excludeFromTaskList = function() {
if (this._isAndroid) { if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); cordova.exec(null, null, 'BackgroundMode', 'tasklist', []);
} }
@ -189,7 +195,7 @@ exports.excludeFromTaskList = function () {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.overrideBackButton = function () { exports.overrideBackButton = function() {
document.addEventListener('backbutton', function() { document.addEventListener('backbutton', function() {
exports.moveToBackground(); exports.moveToBackground();
}, false); }, false);
@ -215,7 +221,7 @@ exports.isScreenOff = function (fn) {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.wakeUp = function () { exports.wakeUp = function() {
if (this._isAndroid) { if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'wakeup', []); cordova.exec(null, null, 'BackgroundMode', 'wakeup', []);
} }
@ -226,7 +232,7 @@ exports.wakeUp = function () {
* *
* @return [ Void ] * @return [ Void ]
*/ */
exports.unlock = function () { exports.unlock = function() {
if (this._isAndroid) { if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'unlock', []); cordova.exec(null, null, 'BackgroundMode', 'unlock', []);
} }
@ -237,7 +243,7 @@ exports.unlock = function () {
* *
* @return [ Boolean ] * @return [ Boolean ]
*/ */
exports.isEnabled = function () { exports.isEnabled = function() {
return this._isEnabled !== false; return this._isEnabled !== false;
}; };
@ -246,7 +252,7 @@ exports.isEnabled = function () {
* *
* @return [ Boolean ] * @return [ Boolean ]
*/ */
exports.isActive = function () { exports.isActive = function() {
return this._isActive !== false; return this._isActive !== false;
}; };
@ -332,14 +338,14 @@ exports.un = function (event, callback) {
* *
* Called when the background mode has been activated. * Called when the background mode has been activated.
*/ */
exports.onactivate = function () {}; exports.onactivate = function() {};
/** /**
* @deprecated * @deprecated
* *
* Called when the background mode has been deaktivated. * Called when the background mode has been deaktivated.
*/ */
exports.ondeactivate = function () {}; exports.ondeactivate = function() {};
/** /**
* @deprecated * @deprecated
@ -349,55 +355,7 @@ exports.ondeactivate = function () {};
* @param {Integer} errorCode * @param {Integer} errorCode
* Error code which describes the error * Error code which describes the error
*/ */
exports.onfailure = 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';
};
/*********** /***********
@ -434,15 +392,76 @@ exports._defaults = {
icon: 'icon' 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 // Called before 'deviceready' listener will be called
channel.onCordovaReady.subscribe(function () { channel.onCordovaReady.subscribe(function() {
channel.onCordovaInfoReady.subscribe(function () { channel.onCordovaInfoReady.subscribe(function() {
exports.pluginInitialize(); exports._pluginInitialize();
}); });
}); });
// Called after 'deviceready' event // Called after 'deviceready' event
channel.deviceready.subscribe(function () { channel.deviceready.subscribe(function() {
if (exports.isEnabled()) { if (exports.isEnabled()) {
exports.fireEvent('enable'); exports.fireEvent('enable');
} }