From 270abbff02826669acae640d3894e10591bc4d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Thu, 1 Jan 2015 17:24:37 +0100 Subject: [PATCH] Add silent mode for Android Fixes issue #39 and #48 --- CHANGELOG.md | 3 +++ README.md | 18 ++++++++++++------ src/android/ForegroundService.java | 21 +++++++++++++++++++-- www/background-mode.js | 3 ++- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8870f16..7070915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ## ChangeLog +#### Version 0.6.3 (not yet released) +- [feature:] Silent mode for Android + #### Version 0.6.2 (14.12.2014) - [bugfix:] Type error - [bugfix:] Wrong default values for `isEnabled` and `isActive`. diff --git a/README.md b/README.md index ac5e8c6..9ddb0e4 100644 --- a/README.md +++ b/README.md @@ -68,16 +68,13 @@ More informations can be found [here][PGB_plugin]. ## ChangeLog +#### Version 0.6.3 (not yet released) +- [feature:] Silent mode for Android + #### Version 0.6.2 (14.12.2014) - [bugfix:] Type error - [bugfix:] Wrong default values for `isEnabled` and `isActive`. -#### Version 0.6.1 (14.12.2014) -- [enhancement:] Set default settings through `setDefaults`. -- [enhancement:] New method `isEnabled` to receive if mode is enabled. -- [enhancement:] New method `isActive` to receive if mode is active. -- [bugfix:] Events caused thread collision. - #### Further informations - The former `plugin.backgroundMode` namespace has been deprecated and will be removed with the next major release. - See [CHANGELOG.md][changelog] to get the full changelog for the plugin. @@ -229,6 +226,15 @@ cordova.plugins.backgroundMode.configure({ }) ``` +#### Run in background without notification +In silent mode the plugin will not display a notification - which is not the default. Be aware that Android recommends adding a notification otherwise the OS may pause the app. + +```javascript +cordova.plugins.backgroundMode.configure({ + silent: true +}) +``` + ## Contributing diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 66235db..a19283c 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -36,6 +36,7 @@ import android.content.res.Resources; import android.os.Build; import android.os.Handler; import android.os.IBinder; +import android.util.Log; /** * Puts the service in a foreground state, where the system considers it to be @@ -84,7 +85,11 @@ public class ForegroundService extends Service { public void keepAwake() { final Handler handler = new Handler(); - startForeground(NOTIFICATION_ID, makeNotification()); + if (!this.inSilentMode()) { + startForeground(NOTIFICATION_ID, makeNotification()); + } else { + Log.w("BackgroundMode", "In silent mode app may be paused by OS!"); + } BackgroundMode.deleteUpdateSettings(); @@ -160,7 +165,7 @@ public class ForegroundService extends Service { * @return * The resource ID of the app icon */ - private int getIconResId () { + private int getIconResId() { Context context = getApplicationContext(); Resources res = context.getResources(); String pkgName = context.getPackageName(); @@ -170,4 +175,16 @@ public class ForegroundService extends Service { return resId; } + + /** + * In silent mode no notification has to be added. + * + * @return + * True if silent: was set to true + */ + private boolean inSilentMode() { + JSONObject settings = BackgroundMode.getSettings(); + + return settings.optBoolean("silent", false); + } } diff --git a/www/background-mode.js b/www/background-mode.js index c07d8fe..d4a2836 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -64,7 +64,8 @@ exports._defaults = { title: 'App is running in background', text: 'Doing heavy tasks.', ticker: 'App is running in background', - resume: true + resume: true, + silent: false };