Small ios code updates

This commit is contained in:
Sebastián Katzer 2017-01-16 20:44:46 +01:00
parent f2016a7c33
commit a2bcdb8942
2 changed files with 42 additions and 32 deletions

View File

@ -23,7 +23,7 @@
#import <Cordova/CDVPlugin.h> #import <Cordova/CDVPlugin.h>
@interface APPBackgroundMode : CDVPlugin { @interface APPBackgroundMode : CDVPlugin {
AVAudioPlayer *audioPlayer; AVAudioPlayer* audioPlayer;
BOOL enabled; BOOL enabled;
} }

View File

@ -23,10 +23,10 @@
@implementation APPBackgroundMode @implementation APPBackgroundMode
NSString *const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode"; NSString* const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode";
NSString *const kAPPBackgroundEventActivate = @"activate"; NSString* const kAPPBackgroundEventActivate = @"activate";
NSString *const kAPPBackgroundEventDeactivate = @"deactivate"; NSString* const kAPPBackgroundEventDeactivate = @"deactivate";
NSString *const kAPPBackgroundEventFailure = @"failure"; NSString* const kAPPBackgroundEventFailure = @"failure";
#pragma mark - #pragma mark -
#pragma mark Initialization #pragma mark Initialization
@ -36,10 +36,12 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
*/ */
- (void) pluginInitialize - (void) pluginInitialize
{ {
[self disable:NULL]; [self.commandDelegate runInBackground:^{
enabled = NO;
[self configureAudioPlayer]; [self configureAudioPlayer];
[self configureAudioSession]; [self configureAudioSession];
[self observeLifeCycle]; [self observeLifeCycle];
}];
} }
/** /**
@ -47,9 +49,8 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
*/ */
- (void) observeLifeCycle - (void) observeLifeCycle
{ {
NSNotificationCenter* listener = [NSNotificationCenter defaultCenter]; NSNotificationCenter* listener = [NSNotificationCenter
defaultCenter];
if (UIApplicationDidEnterBackgroundNotification && UIApplicationWillEnterForegroundNotification) {
[listener addObserver:self [listener addObserver:self
selector:@selector(keepAwake) selector:@selector(keepAwake)
@ -65,11 +66,6 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
selector:@selector(handleAudioSessionInterruption:) selector:@selector(handleAudioSessionInterruption:)
name:AVAudioSessionInterruptionNotification name:AVAudioSessionInterruptionNotification
object:nil]; object:nil];
} else {
[self enable:NULL];
[self keepAwake];
}
} }
#pragma mark - #pragma mark -
@ -82,6 +78,7 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
- (void) enable:(CDVInvokedUrlCommand*)command - (void) enable:(CDVInvokedUrlCommand*)command
{ {
enabled = YES; enabled = YES;
[self execCallback:command];
} }
/** /**
@ -91,8 +88,8 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
- (void) disable:(CDVInvokedUrlCommand*)command - (void) disable:(CDVInvokedUrlCommand*)command
{ {
enabled = NO; enabled = NO;
[self stopKeepingAwake]; [self stopKeepingAwake];
[self execCallback:command];
} }
#pragma mark - #pragma mark -
@ -103,10 +100,11 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
*/ */
- (void) keepAwake - (void) keepAwake
{ {
if (enabled) { if (!enabled)
return;
[audioPlayer play]; [audioPlayer play];
[self fireEvent:kAPPBackgroundEventActivate withParams:NULL]; [self fireEvent:kAPPBackgroundEventActivate];
}
} }
/** /**
@ -114,13 +112,12 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
*/ */
- (void) stopKeepingAwake - (void) stopKeepingAwake
{ {
if (TARGET_IPHONE_SIMULATOR) { if (TARGET_IPHONE_SIMULATOR) {
NSLog(@"BackgroundMode: On simulator apps never pause in background!"); NSLog(@"BackgroundMode: On simulator apps never pause in background!");
} }
if (audioPlayer.isPlaying) { if (audioPlayer.isPlaying) {
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL]; [self fireEvent:kAPPBackgroundEventDeactivate];
} }
[audioPlayer pause]; [audioPlayer pause];
@ -168,30 +165,43 @@ NSString *const kAPPBackgroundEventFailure = @"failure";
#pragma mark - #pragma mark -
#pragma mark Helper #pragma mark Helper
/**
* Simply invokes the callback without any parameter.
*/
- (void) execCallback:(CDVInvokedUrlCommand*)command
{
CDVPluginResult *result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result
callbackId:command.callbackId];
}
/** /**
* Restart playing sound when interrupted by phone calls. * Restart playing sound when interrupted by phone calls.
*/ */
- (void) handleAudioSessionInterruption:(NSNotification*)notification - (void) handleAudioSessionInterruption:(NSNotification*)notification
{ {
[self fireEvent:kAPPBackgroundEventDeactivate withParams:NULL]; [self fireEvent:kAPPBackgroundEventDeactivate];
[self keepAwake]; [self keepAwake];
} }
/** /**
* Method to fire an event with some parameters in the browser. * Method to fire an event with some parameters in the browser.
*/ */
- (void) fireEvent:(NSString*)event withParams:(NSString*)params - (void) fireEvent:(NSString*)event
{ {
NSString* active = [event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false"; NSString* active =
[event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false";
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;", NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
kAPPBackgroundJsNamespace, active]; kAPPBackgroundJsNamespace, active];
NSString* depFn = [NSString stringWithFormat:@"%@.on%@(%@);", NSString* depFn = [NSString stringWithFormat:@"%@.on%@();",
kAPPBackgroundJsNamespace, event, params]; kAPPBackgroundJsNamespace, event];
NSString* fn = [NSString stringWithFormat:@"%@.fireEvent('%@',%@);", NSString* fn = [NSString stringWithFormat:@"%@.fireEvent('%@');",
kAPPBackgroundJsNamespace, event, params]; kAPPBackgroundJsNamespace, event];
NSString* js = [NSString stringWithFormat:@"%@%@%@", flag, depFn, fn]; NSString* js = [NSString stringWithFormat:@"%@%@%@", flag, depFn, fn];