Skip to content

Commit

Permalink
With webkit the plugin is enabled by default without the need to play…
Browse files Browse the repository at this point in the history
… audio
  • Loading branch information
katzer committed Jan 18, 2017
1 parent 4764ffe commit e25beda
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
36 changes: 28 additions & 8 deletions src/ios/APPBackgroundMode.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ + (void) load
- (void) pluginInitialize
{
[self.commandDelegate runInBackground:^{
enabled = NO;
enabled = [self.class isRunningWebKit];
[self configureAudioPlayer];
[self configureAudioSession];
[self observeLifeCycle];
Expand All @@ -66,7 +66,7 @@ - (void) observeLifeCycle
{
NSNotificationCenter* listener = [NSNotificationCenter
defaultCenter];

[listener addObserver:self
selector:@selector(keepAwake)
name:UIApplicationDidEnterBackgroundNotification
Expand All @@ -76,6 +76,9 @@ - (void) observeLifeCycle
selector:@selector(stopKeepingAwake)
name:UIApplicationWillEnterForegroundNotification
object:nil];

if ([self.class isRunningWebKit])
return;

[listener addObserver:self
selector:@selector(handleAudioSessionInterruption:)
Expand All @@ -92,6 +95,9 @@ - (void) observeLifeCycle
*/
- (void) enable:(CDVInvokedUrlCommand*)command
{
if (enabled)
return;

enabled = YES;
[self execCallback:command];
}
Expand All @@ -102,6 +108,9 @@ - (void) enable:(CDVInvokedUrlCommand*)command
*/
- (void) disable:(CDVInvokedUrlCommand*)command
{
if (!enabled || [self.class isRunningWebKit])
return;

enabled = NO;
[self stopKeepingAwake];
[self execCallback:command];
Expand All @@ -118,7 +127,10 @@ - (void) keepAwake
if (!enabled)
return;

[audioPlayer play];
if (![self.class isRunningWebKit]) {
[audioPlayer play];
}

[self fireEvent:kAPPBackgroundEventActivate];
}

Expand All @@ -131,7 +143,7 @@ - (void) stopKeepingAwake
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
}

if (audioPlayer.isPlaying) {
if (audioPlayer.isPlaying || [self.class isRunningWebKit]) {
[self fireEvent:kAPPBackgroundEventDeactivate];
}

Expand Down Expand Up @@ -164,6 +176,9 @@ - (void) configureAudioSession
AVAudioSession* session = [AVAudioSession
sharedInstance];

if ([self.class isRunningWebKit])
return;

// Don't activate the audio session yet
[session setActive:NO error:NULL];

Expand Down Expand Up @@ -201,6 +216,14 @@ - (void) handleAudioSessionInterruption:(NSNotification*)notification
[self keepAwake];
}

/**
* Find out if the app runs inside the webkit powered webview.
*/
+ (BOOL) isRunningWebKit
{
return IsAtLeastiOSVersion(@"8.0") && NSClassFromString(@"CDVWKWebViewEngine");
}

/**
* Method to fire an event with some parameters in the browser.
*/
Expand Down Expand Up @@ -231,15 +254,12 @@ - (void) fireEvent:(NSString*)event
*/
+ (void) swizzleWKWebViewEngine
{
if (!IsAtLeastiOSVersion(@"8.0"))
if (![self isRunningWebKit])
return;

Class wkWebViewEngineCls = NSClassFromString(@"CDVWKWebViewEngine");
SEL selector = NSSelectorFromString(@"createConfigurationFromSettings:");

if (!wkWebViewEngineCls)
return;

SwizzleSelectorWithBlock_Begin(wkWebViewEngineCls, selector)
^(CDVPlugin *self, NSDictionary *settings) {
id obj = ((id (*)(id, SEL, NSDictionary*))_imp)(self, _cmd, settings);
Expand Down
1 change: 0 additions & 1 deletion src/ios/APPMethodMagic.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,3 @@ IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl)

return class_replaceMethod(clazz, selector, newImpl, types);
}

34 changes: 18 additions & 16 deletions www/background-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ exports.enable = function () {
if (this.isEnabled())
return;

var me = this,
fireEvent = function () { me.fireEvent('enable'); };
var fn = function () {
exports._isEnabled = true;
exports.fireEvent('enable');
};

this._isEnabled = true;
cordova.exec(fireEvent, null, 'BackgroundMode', 'enable', []);
cordova.exec(fn, null, 'BackgroundMode', 'enable', []);
};

/**
Expand All @@ -51,11 +52,12 @@ exports.disable = function () {
if (!this.isEnabled())
return;

var me = this,
fireEvent = function () { me.fireEvent('disable'); };
var fn = function () {
exports._isEnabled = false;
exports.fireEvent('disable');
};

this._isEnabled = false;
cordova.exec(fireEvent, null, 'BackgroundMode', 'disable', []);
cordova.exec(fn, null, 'BackgroundMode', 'disable', []);
};

/**
Expand Down Expand Up @@ -302,7 +304,7 @@ exports.mergeWithDefaults = function (options) {
*
* Flag indicates if the mode is enabled.
*/
exports._isEnabled = false;
exports._isEnabled = window.webkit !== undefined;

/**
* @private
Expand All @@ -317,14 +319,14 @@ exports._isActive = false;
* Default values of all available options.
*/
exports._defaults = {
title: 'App is running in background',
text: "Doing heavy tasks.",
ticker: 'Running in background',
title: 'App is running in background',
text: 'Doing heavy tasks.',
ticker: 'Running in background',
bigText: false,
resume: true,
silent: false,
color: undefined,
icon: 'icon'
resume: true,
silent: false,
color: undefined,
icon: 'icon'
};

// Called before 'deviceready' listener will be called
Expand Down

0 comments on commit e25beda

Please sign in to comment.