Skip to content

Commit

Permalink
Add silent mode for Android
Browse files Browse the repository at this point in the history
Fixes issue #39 and #48
  • Loading branch information
katzer committed Jan 1, 2015
1 parent 0b9854e commit 270abbf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
3 changes: 3 additions & 0 deletions 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`.
Expand Down
18 changes: 12 additions & 6 deletions README.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
21 changes: 19 additions & 2 deletions src/android/ForegroundService.java
Expand Up @@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one
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
Expand Down Expand Up @@ -84,7 +85,11 @@ public void onDestroy() {
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();

Expand Down Expand Up @@ -160,7 +165,7 @@ private Notification makeNotification() {
* @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();
Expand All @@ -170,4 +175,16 @@ private int getIconResId () {

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);
}
}
3 changes: 2 additions & 1 deletion www/background-mode.js
Expand Up @@ -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
};


Expand Down

0 comments on commit 270abbf

Please sign in to comment.