删除ios平台相关内容
This commit is contained in:
parent
dc6100b399
commit
687cd871ab
49
plugin.xml
49
plugin.xml
@ -24,7 +24,6 @@
|
||||
<engines>
|
||||
<engine name="cordova" version=">=3.0.0" />
|
||||
<engine name="android-sdk" version=">=16" />
|
||||
<!-- <engine name="windows-sdk" version=">=10.0.14393.0" /> -->
|
||||
</engines>
|
||||
|
||||
<!-- js -->
|
||||
@ -33,28 +32,6 @@
|
||||
<clobbers target="plugin.backgroundMode" />
|
||||
</js-module>
|
||||
|
||||
<!-- ios -->
|
||||
<platform name="ios">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="BackgroundMode">
|
||||
<param name="ios-package" value="APPBackgroundMode" />
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<config-file target="*-Info.plist" parent="UIBackgroundModes">
|
||||
<array>
|
||||
<string>audio</string>
|
||||
</array>
|
||||
</config-file>
|
||||
|
||||
<resource-file src="appbeep.wav" />
|
||||
|
||||
<header-file src="src/ios/APPBackgroundMode.h" />
|
||||
<source-file src="src/ios/APPBackgroundMode.m" />
|
||||
<header-file src="src/ios/APPMethodMagic.h" />
|
||||
<source-file src="src/ios/APPMethodMagic.m" />
|
||||
</platform>
|
||||
|
||||
<!-- android -->
|
||||
<platform name="android">
|
||||
<config-file target="res/xml/config.xml" parent="/*">
|
||||
@ -90,32 +67,6 @@
|
||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||
</platform>
|
||||
|
||||
<!-- windows
|
||||
<platform name="windows">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="BackgroundMode" >
|
||||
<param name="windows-package" value="BackgroundMode"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<config-file target="package.appxmanifest" parent="/Package/Capabilities" device-target="windows">
|
||||
<Capability Name="backgroundMediaPlayback" />
|
||||
</config-file>
|
||||
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<preference name="windows-target-version" value="UAP" />
|
||||
<preference name="uap-target-min-version" value="10.0.14393.0" />
|
||||
<preference name="Windows.Universal-MinVersion" value="10.0.14393.0" />
|
||||
<preference name="Windows.Universal" value="10.0.14393.0" />
|
||||
</config-file>
|
||||
|
||||
<resource-file src="appbeep.wma" target="appbeep.wma" />
|
||||
|
||||
<js-module src="src/windows/BackgroundModeProxy.js" name="BackgroundMode.Proxy">
|
||||
<runs />
|
||||
</js-module>
|
||||
</platform> -->
|
||||
|
||||
<!-- browser -->
|
||||
<platform name="browser">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
Copyright 2013-2017 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#import <Cordova/CDVPlugin.h>
|
||||
|
||||
@interface APPBackgroundMode : CDVPlugin {
|
||||
AVAudioPlayer* audioPlayer;
|
||||
BOOL enabled;
|
||||
}
|
||||
|
||||
// Activate the background mode
|
||||
- (void) enable:(CDVInvokedUrlCommand*)command;
|
||||
// Deactivate the background mode
|
||||
- (void) disable:(CDVInvokedUrlCommand*)command;
|
||||
|
||||
@end
|
@ -1,277 +0,0 @@
|
||||
/*
|
||||
Copyright 2013-2017 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
#import "APPMethodMagic.h"
|
||||
#import "APPBackgroundMode.h"
|
||||
#import <Cordova/CDVAvailability.h>
|
||||
|
||||
@implementation APPBackgroundMode
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Constants
|
||||
|
||||
NSString* const kAPPBackgroundJsNamespace = @"cordova.plugins.backgroundMode";
|
||||
NSString* const kAPPBackgroundEventActivate = @"activate";
|
||||
NSString* const kAPPBackgroundEventDeactivate = @"deactivate";
|
||||
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Life Cycle
|
||||
|
||||
/**
|
||||
* Called by runtime once the Class has been loaded.
|
||||
* Exchange method implementations to hook into their execution.
|
||||
*/
|
||||
+ (void) load
|
||||
{
|
||||
[self swizzleWKWebViewEngine];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the plugin.
|
||||
*/
|
||||
- (void) pluginInitialize
|
||||
{
|
||||
enabled = NO;
|
||||
[self configureAudioPlayer];
|
||||
[self configureAudioSession];
|
||||
[self observeLifeCycle];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listener for pause and resume events.
|
||||
*/
|
||||
- (void) observeLifeCycle
|
||||
{
|
||||
NSNotificationCenter* listener = [NSNotificationCenter
|
||||
defaultCenter];
|
||||
|
||||
[listener addObserver:self
|
||||
selector:@selector(keepAwake)
|
||||
name:UIApplicationDidEnterBackgroundNotification
|
||||
object:nil];
|
||||
|
||||
[listener addObserver:self
|
||||
selector:@selector(stopKeepingAwake)
|
||||
name:UIApplicationWillEnterForegroundNotification
|
||||
object:nil];
|
||||
|
||||
[listener addObserver:self
|
||||
selector:@selector(handleAudioSessionInterruption:)
|
||||
name:AVAudioSessionInterruptionNotification
|
||||
object:nil];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Interface
|
||||
|
||||
/**
|
||||
* Enable the mode to stay awake
|
||||
* when switching to background for the next time.
|
||||
*/
|
||||
- (void) enable:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
if (enabled)
|
||||
return;
|
||||
|
||||
enabled = YES;
|
||||
[self execCallback:command];
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the background mode
|
||||
* and stop being active in background.
|
||||
*/
|
||||
- (void) disable:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
if (!enabled)
|
||||
return;
|
||||
|
||||
enabled = NO;
|
||||
[self stopKeepingAwake];
|
||||
[self execCallback:command];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Core
|
||||
|
||||
/**
|
||||
* Keep the app awake.
|
||||
*/
|
||||
- (void) keepAwake
|
||||
{
|
||||
if (!enabled)
|
||||
return;
|
||||
|
||||
[audioPlayer play];
|
||||
[self fireEvent:kAPPBackgroundEventActivate];
|
||||
}
|
||||
|
||||
/**
|
||||
* Let the app going to sleep.
|
||||
*/
|
||||
- (void) stopKeepingAwake
|
||||
{
|
||||
if (TARGET_IPHONE_SIMULATOR) {
|
||||
NSLog(@"BackgroundMode: On simulator apps never pause in background!");
|
||||
}
|
||||
|
||||
if (audioPlayer.isPlaying) {
|
||||
[self fireEvent:kAPPBackgroundEventDeactivate];
|
||||
}
|
||||
|
||||
[audioPlayer pause];
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the audio player.
|
||||
*/
|
||||
- (void) configureAudioPlayer
|
||||
{
|
||||
NSString* path = [[NSBundle mainBundle]
|
||||
pathForResource:@"appbeep" ofType:@"wav"];
|
||||
|
||||
NSURL* url = [NSURL fileURLWithPath:path];
|
||||
|
||||
|
||||
audioPlayer = [[AVAudioPlayer alloc]
|
||||
initWithContentsOfURL:url error:NULL];
|
||||
|
||||
audioPlayer.volume = 0;
|
||||
audioPlayer.numberOfLoops = -1;
|
||||
};
|
||||
|
||||
/**
|
||||
* Configure the audio session.
|
||||
*/
|
||||
- (void) configureAudioSession
|
||||
{
|
||||
AVAudioSession* session = [AVAudioSession
|
||||
sharedInstance];
|
||||
|
||||
// Don't activate the audio session yet
|
||||
[session setActive:NO error:NULL];
|
||||
|
||||
// Play music even in background and dont stop playing music
|
||||
// even another app starts playing sound
|
||||
[session setCategory:AVAudioSessionCategoryPlayback
|
||||
withOptions:AVAudioSessionCategoryOptionMixWithOthers
|
||||
error:NULL];
|
||||
|
||||
// Active the audio session
|
||||
[session setActive:YES error:NULL];
|
||||
};
|
||||
|
||||
#pragma mark -
|
||||
#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.
|
||||
*/
|
||||
- (void) handleAudioSessionInterruption:(NSNotification*)notification
|
||||
{
|
||||
[self fireEvent:kAPPBackgroundEventDeactivate];
|
||||
[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.
|
||||
*/
|
||||
- (void) fireEvent:(NSString*)event
|
||||
{
|
||||
NSString* active =
|
||||
[event isEqualToString:kAPPBackgroundEventActivate] ? @"true" : @"false";
|
||||
|
||||
NSString* flag = [NSString stringWithFormat:@"%@._isActive=%@;",
|
||||
kAPPBackgroundJsNamespace, active];
|
||||
|
||||
NSString* depFn = [NSString stringWithFormat:@"%@.on%@();",
|
||||
kAPPBackgroundJsNamespace, event];
|
||||
|
||||
NSString* fn = [NSString stringWithFormat:@"%@.fireEvent('%@');",
|
||||
kAPPBackgroundJsNamespace, event];
|
||||
|
||||
NSString* js = [NSString stringWithFormat:@"%@%@%@", flag, depFn, fn];
|
||||
|
||||
[self.commandDelegate evalJs:js];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Swizzling
|
||||
|
||||
/**
|
||||
* Method to swizzle.
|
||||
*/
|
||||
+ (NSString*) wkProperty
|
||||
{
|
||||
NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5";
|
||||
NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0];
|
||||
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
/**
|
||||
* Swizzle some implementations of CDVWKWebViewEngine.
|
||||
*/
|
||||
+ (void) swizzleWKWebViewEngine
|
||||
{
|
||||
if (![self isRunningWebKit])
|
||||
return;
|
||||
|
||||
Class wkWebViewEngineCls = NSClassFromString(@"CDVWKWebViewEngine");
|
||||
SEL selector = NSSelectorFromString(@"createConfigurationFromSettings:");
|
||||
|
||||
SwizzleSelectorWithBlock_Begin(wkWebViewEngineCls, selector)
|
||||
^(CDVPlugin *self, NSDictionary *settings) {
|
||||
id obj = ((id (*)(id, SEL, NSDictionary*))_imp)(self, _cmd, settings);
|
||||
|
||||
[obj setValue:[NSNumber numberWithBool:YES]
|
||||
forKey:[APPBackgroundMode wkProperty]];
|
||||
|
||||
[obj setValue:[NSNumber numberWithBool:NO]
|
||||
forKey:@"requiresUserActionForMediaPlayback"];
|
||||
|
||||
return obj;
|
||||
}
|
||||
SwizzleSelectorWithBlock_End;
|
||||
}
|
||||
|
||||
@end
|
@ -1,93 +0,0 @@
|
||||
/*
|
||||
Copyright 2013-2017 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Code extracted from
|
||||
* - http://defagos.github.io/yet_another_article_about_method_swizzling/
|
||||
* - https://gist.github.com/defagos/1312fec96b48540efa5c
|
||||
*/
|
||||
|
||||
#import <objc/runtime.h>
|
||||
#import <objc/message.h>
|
||||
|
||||
#define SwizzleSelector(clazz, selector, newImpl, oldImpl) \
|
||||
(*oldImpl) = (__typeof((*oldImpl)))class_swizzleSelector((clazz), (selector), (IMP)(newImpl))
|
||||
|
||||
#define SwizzleClassSelector(clazz, selector, newImpl, oldImpl) \
|
||||
(*oldImpl) = (__typeof((*oldImpl)))class_swizzleClassSelector((clazz), (selector), (IMP)(newImpl))
|
||||
|
||||
#define SwizzleSelectorWithBlock_Begin(clazz, selector) { \
|
||||
SEL _cmd = selector; \
|
||||
__block IMP _imp = class_swizzleSelectorWithBlock((clazz), (selector),
|
||||
#define SwizzleSelectorWithBlock_End );}
|
||||
|
||||
#define SwizzleClassSelectorWithBlock_Begin(clazz, selector) { \
|
||||
SEL _cmd = selector; \
|
||||
__block IMP _imp = class_swizzleClassSelectorWithBlock((clazz), (selector),
|
||||
#define SwizzleClassSelectorWithBlock_End );}
|
||||
|
||||
/**
|
||||
* Swizzle class method specified by class and selector
|
||||
* through the provided method implementation.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ IMP ] newImpl The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleClassSelector(Class clazz, SEL selector, IMP newImpl);
|
||||
|
||||
/**
|
||||
* Swizzle class method specified by class and selector
|
||||
* through the provided code block.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ id ] newImplBlock The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleClassSelectorWithBlock(Class clazz, SEL selector, id newImplBlock);
|
||||
|
||||
/**
|
||||
* Swizzle method specified by class and selector
|
||||
* through the provided code block.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ id ] newImplBlock The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleSelectorWithBlock(Class clazz, SEL selector, id newImplBlock);
|
||||
|
||||
/**
|
||||
* Swizzle method specified by class and selector
|
||||
* through the provided method implementation.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ IMP ] newImpl The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl);
|
@ -1,105 +0,0 @@
|
||||
/*
|
||||
Copyright 2013-2017 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Code extracted from
|
||||
* - http://defagos.github.io/yet_another_article_about_method_swizzling/
|
||||
* - https://gist.github.com/defagos/1312fec96b48540efa5c
|
||||
*/
|
||||
|
||||
#import "APPMethodMagic.h"
|
||||
#import <objc/runtime.h>
|
||||
#import <objc/message.h>
|
||||
|
||||
/**
|
||||
* Swizzle class method specified by class and selector
|
||||
* through the provided method implementation.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ IMP ] newImpl The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleClassSelector(Class clazz, SEL selector, IMP newImpl)
|
||||
{
|
||||
return class_swizzleSelector(object_getClass(clazz), selector, newImpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Swizzle class method specified by class and selector
|
||||
* through the provided code block.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ id ] newImplBlock The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleClassSelectorWithBlock(Class clazz, SEL selector, id newImplBlock)
|
||||
{
|
||||
IMP newImpl = imp_implementationWithBlock(newImplBlock);
|
||||
return class_swizzleClassSelector(clazz, selector, newImpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Swizzle method specified by class and selector
|
||||
* through the provided code block.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ id ] newImplBlock The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleSelectorWithBlock(Class clazz, SEL selector, id newImplBlock)
|
||||
{
|
||||
IMP newImpl = imp_implementationWithBlock(newImplBlock);
|
||||
return class_swizzleSelector(clazz, selector, newImpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Swizzle method specified by class and selector
|
||||
* through the provided method implementation.
|
||||
*
|
||||
* @param [ Class ] clazz The class containing the method.
|
||||
* @param [ SEL ] selector The selector of the method.
|
||||
* @param [ IMP ] newImpl The new implementation of the method.
|
||||
*
|
||||
* @return [ IMP ] The previous implementation of the method.
|
||||
*/
|
||||
IMP class_swizzleSelector(Class clazz, SEL selector, IMP newImpl)
|
||||
{
|
||||
Method method = class_getInstanceMethod(clazz, selector);
|
||||
const char *types = method_getTypeEncoding(method);
|
||||
|
||||
class_addMethod(clazz, selector, imp_implementationWithBlock(^(__unsafe_unretained id self, va_list argp) {
|
||||
struct objc_super super = {
|
||||
.receiver = self,
|
||||
.super_class = class_getSuperclass(clazz)
|
||||
};
|
||||
|
||||
id (*objc_msgSendSuper_typed)(struct objc_super*, SEL, va_list) = (void*)&objc_msgSendSuper;
|
||||
return objc_msgSendSuper_typed(&super, selector, argp);
|
||||
}), types);
|
||||
|
||||
return class_replaceMethod(clazz, selector, newImpl, types);
|
||||
}
|
@ -1,123 +0,0 @@
|
||||
/*
|
||||
Copyright 2013-2017 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
var plugin = cordova.plugins.backgroundMode;
|
||||
|
||||
var Uri = Windows.Foundation.Uri,
|
||||
MediaSource = Windows.Media.Core.MediaSource,
|
||||
MediaPlaybackItem = Windows.Media.Playback.MediaPlaybackItem,
|
||||
MediaPlaybackList = Windows.Media.Playback.MediaPlaybackList,
|
||||
AudioCategory = Windows.Media.Playback.MediaPlayerAudioCategory,
|
||||
MediaPlayer = Windows.Media.Playback.MediaPlayer,
|
||||
WebUIApplication = Windows.UI.WebUI.WebUIApplication;
|
||||
|
||||
/**
|
||||
* Activates the background mode. When activated the application
|
||||
* will be prevented from going to sleep while in background
|
||||
* for the next time.
|
||||
*
|
||||
* @param [ Function ] success The success callback to use.
|
||||
* @param [ Function ] error The error callback to use.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.enable = function (success, error) {
|
||||
success();
|
||||
};
|
||||
|
||||
/**
|
||||
* Deactivates the background mode. When deactivated the application
|
||||
* will not stay awake while in background.
|
||||
*
|
||||
* @param [ Function ] success The success callback to use.
|
||||
* @param [ Function ] error The error callback to use.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.disable = function (success, error) {
|
||||
exports.stopKeepingAwake();
|
||||
success();
|
||||
};
|
||||
|
||||
/**
|
||||
* Keep the app awake.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.keepAwake = function () {
|
||||
if (!plugin.isEnabled() || plugin.isActive())
|
||||
return;
|
||||
|
||||
exports.configureAudioPlayer();
|
||||
exports.audioPlayer.play();
|
||||
|
||||
plugin._isActive = true;
|
||||
plugin.fireEvent('activate');
|
||||
};
|
||||
|
||||
/**
|
||||
* Let the app going to sleep.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.stopKeepingAwake = function () {
|
||||
if (!exports.audioPlayer)
|
||||
return;
|
||||
|
||||
exports.audioPlayer.close();
|
||||
exports.audioPlayer = null;
|
||||
|
||||
cordova.plugins.backgroundMode._isActive = false;
|
||||
cordova.plugins.backgroundMode.fireEvent('deactivate');
|
||||
};
|
||||
|
||||
/**
|
||||
* Configure the audio player for playback in background.
|
||||
*
|
||||
* @return [ Void ]
|
||||
*/
|
||||
exports.configureAudioPlayer = function () {
|
||||
if (exports.audioPlayer)
|
||||
return;
|
||||
|
||||
var pkg = Windows.ApplicationModel.Package.current,
|
||||
pkgName = pkg.id.name;
|
||||
|
||||
var audioPlayer = new MediaPlayer(),
|
||||
audioFile = new Uri('ms-appx://' + pkgName + '/appbeep.wma'),
|
||||
audioSource = MediaSource.createFromUri(audioFile),
|
||||
playList = new MediaPlaybackList();
|
||||
|
||||
playList.items.append(new MediaPlaybackItem(audioSource));
|
||||
playList.autoRepeatEnabled = true;
|
||||
|
||||
audioPlayer.source = playList;
|
||||
audioPlayer.autoPlay = false;
|
||||
audioPlayer.audioCategory = AudioCategory.soundEffects;
|
||||
audioPlayer.volume = 0;
|
||||
|
||||
exports.audioPlayer = audioPlayer;
|
||||
};
|
||||
|
||||
WebUIApplication.addEventListener('enteredbackground', exports.keepAwake, false);
|
||||
WebUIApplication.addEventListener('leavingbackground', exports.stopKeepingAwake, false);
|
||||
|
||||
cordova.commandProxy.add('BackgroundMode', exports);
|
Loading…
Reference in New Issue
Block a user