New method isActive to receive if mode is active

This commit is contained in:
Sebastián Katzer 2014-12-14 14:23:57 +01:00
parent fc6e6cf51d
commit 8f2c75321c
5 changed files with 66 additions and 25 deletions

View File

@ -1,7 +1,8 @@
## ChangeLog
#### Version 0.6.1 (not yet released)
- [enhancement:] Set default settings through `setDefaults`.
- [enhancement:] New method `isEnabled`
- [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.

View File

@ -70,7 +70,8 @@ More informations can be found [here][PGB_plugin].
## ChangeLog
#### Version 0.6.1 (not yet released)
- [enhancement:] Set default settings through `setDefaults`.
- [enhancement:] New method `isEnabled`
- [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
@ -82,17 +83,18 @@ More informations can be found [here][PGB_plugin].
## Usage
The plugin creates the object ```cordova.plugins.backgroundMode``` with the following methods:
The plugin creates the object `cordova.plugins.backgroundMode` with the following methods:
1. [backgroundMode.enable][enable]
2. [backgroundMode.disable][disable]
3. [backgroundMode.isEnabled][is_enabled]
3. [backgroundMode.getDefaults][android_specifics]
4. [backgroundMode.setDefaults][android_specifics]
2. [backgroundMode.configure][android_specifics]
3. [backgroundMode.onactivate][onactivate]
4. [backgroundMode.ondeactivate][ondeactivate]
5. [backgroundMode.onfailure][onfailure]
4. [backgroundMode.isActive][is_active]
5. [backgroundMode.getDefaults][android_specifics]
6. [backgroundMode.setDefaults][android_specifics]
7. [backgroundMode.configure][android_specifics]
8. [backgroundMode.onactivate][onactivate]
9. [backgroundMode.ondeactivate][ondeactivate]
10. [backgroundMode.onfailure][onfailure]
### Plugin initialization
The plugin and its methods are not available before the *deviceready* event has been fired.
@ -125,10 +127,17 @@ cordova.plugins.backgroundMode.disable();
```
### Receive if the background mode is enabled
The `backgroundMode.isActivated` interface can be used to get the information if the background mode is enabled or disabled.
The `backgroundMode.isEnabled` interface can be used to get the information if the background mode is enabled or disabled.
```javascript
var enabled = cordova.plugins.backgroundMode.isEnabled();
cordova.plugins.backgroundMode.isEnabled(); // => boolean
```
### Receive if the background mode is active
The `backgroundMode.isActive` interface can be used to get the information if the background mode is active.
```javascript
cordova.plugins.backgroundMode.isActive(); // => boolean
```
### Get informed when the background mode has been activated
@ -240,6 +249,8 @@ This software is released under the [Apache 2.0 License][apache2_license].
[changelog]: CHANGELOG.md
[enable]: #prevent_the_app_from_going_to_sleep_in_background
[disable]: #pause_the_app_while_in_background
[is_enabled]: #receive_if_the_background_mode_is_enabled
[is_active]: #receive_if_the_background_mode_is_active
[android_specifics]: #android_customization
[onactivate]: #get_informed_when_the_background_mode_has_been_activated
[ondeactivate]: #get_informed_when_the_background_mode_has_been_deactivated

View File

@ -290,12 +290,18 @@ public class BackgroundMode extends CordovaPlugin {
eventName = "deactivate"; break;
default:
eventName = "failure";
}
final String js = String.format("setTimeout('%s.on%s(%s)',0)",
String active = event == Event.ACTIVATE ? "true" : false;
String flag = String.format("%s._isActive=%s;",
JS_NAMESPACE, active);
String fn = String.format("setTimeout('%s.on%s(%s)',0);",
JS_NAMESPACE, eventName, params);
final String js = flag + fn;
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {

View File

@ -23,10 +23,10 @@
@implementation APPBackgroundMode
NSString *const kAPPBackgroundModeNamespace = @"cordova.plugins.backgroundMode";
NSString *const kAPPBackgroundModeActivateEvent = @"activate";
NSString *const kAPPBackgroundModeDeactivateEvent = @"deactivate";
NSString *const kAPPBackgroundModeFailureEvent = @"failure";
NSString *const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode";
NSString *const kAPPBackgroundEventActivate = @"activate";
NSString *const kAPPBackgroundEventDeactivate = @"deactivate";
NSString *const kAPPBackgroundEventFailure = @"failure";
#pragma mark -
#pragma mark Initialization methods
@ -104,7 +104,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
- (void) keepAwake {
if (enabled) {
[audioPlayer play];
[self fireEvent:kAPPBackgroundModeActivateEvent withParams:NULL];
[self fireEvent:kAPPBackgroundEventActivate withParams:NULL];
}
}
@ -118,7 +118,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
}
if (audioPlayer.isPlaying) {
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
}
[audioPlayer pause];
@ -166,7 +166,7 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
* Restart playing sound when interrupted by phone calls.
*/
- (void) handleAudioSessionInterruption:(NSNotification*)notification {
[self fireEvent:kAPPBackgroundModeDeactivateEvent withParams:NULL];
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL];
[self keepAwake];
}
@ -175,8 +175,15 @@ NSString *const kAPPBackgroundModeFailureEvent = @"failure";
*/
- (void) fireEvent:(NSString*)event withParams:(NSString*)params
{
NSString* js = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0)",
kAPPBackgroundModeNamespace, event, params];
NSString* active = [event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false";
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
kAPPBackgroundJsNamespace, active];
NSString* fn = [NSString stringWithFormat:@"setTimeout('%@.on%@(%@)',0);",
kAPPBackgroundJsNamespace, event, params];
NSString* js = [flag stringByAppendingString:fn];
[self.commandDelegate evalJs:js];
}

View File

@ -29,7 +29,7 @@ document.addEventListener('backbutton', function () {}, false);
// Called before 'deviceready' listener will be called
channel.onCordovaReady.subscribe(function () {
// Device plugin is ready now
channel.onCordovaInfoReady.subscribe( function () {
channel.onCordovaInfoReady.subscribe(function () {
// Set the defaults
exports.setDefaults({});
});
@ -44,10 +44,17 @@ channel.onCordovaReady.subscribe(function () {
/**
* @private
*
* Flag indicated if the mod is enabled.
* Flag indicated if the mode is enabled.
*/
exports._isEnabled = true;
/**
* @private
*
* Flag indicated if the mode is active.
*/
exports._isActive = true;
/**
* @private
*
@ -125,7 +132,7 @@ exports.configure = function (options) {
};
/**
* If the mode is enabled or not.
* If the mode is enabled or disabled.
*
* @return {Boolean}
*/
@ -133,6 +140,15 @@ exports.isEnabled = function () {
return this._isEnabled;
};
/**
* If the mode is active.
*
* @return {Boolean}
*/
exports.isActive = function () {
return this._isActive;
};
/**
* Called when the background mode has been activated.
*/