From 337e39b2db6c1ecc5ec06d1b2e1a7d8885f1880d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Sun, 15 Jan 2017 14:19:48 +0100 Subject: [PATCH] enable/disable event --- src/android/BackgroundMode.java | 10 +++------- www/background-mode.js | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index fdebc8f..43557e7 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -101,35 +101,31 @@ public class BackgroundMode extends CordovaPlugin { boolean update = args.getBoolean(1); configure(settings, update); - return true; } if (action.equalsIgnoreCase("disableWebViewOptimizations")) { disableWebViewOptimizations(); - return true; } if (action.equalsIgnoreCase("background")) { moveToBackground(); - return true; } if (action.equalsIgnoreCase("foreground")) { moveToForeground(); - return true; } if (action.equalsIgnoreCase("enable")) { enableMode(); - return true; } if (action.equalsIgnoreCase("disable")) { disableMode(); - return true; } - return false; + callback.success(); + + return true; } /** diff --git a/www/background-mode.js b/www/background-mode.js index 50d3367..53199c6 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -33,8 +33,14 @@ var exec = require('cordova/exec'), * for the next time. */ exports.enable = function () { + if (this.isEnabled()) + return; + + var me = this, + fireEvent = function () { me.fireEvent('enable'); }; + this._isEnabled = true; - cordova.exec(null, null, 'BackgroundMode', 'enable', []); + cordova.exec(fireEvent, null, 'BackgroundMode', 'enable', []); }; /** @@ -42,8 +48,14 @@ exports.enable = function () { * will not stay awake while in background. */ exports.disable = function () { + if (!this.isEnabled()) + return; + + var me = this, + fireEvent = function () { me.fireEvent('disable'); }; + this._isEnabled = false; - cordova.exec(null, null, 'BackgroundMode', 'disable', []); + cordova.exec(fireEvent, null, 'BackgroundMode', 'disable', []); }; /**