mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-23 07:44:53 +00:00
Support windows platform - requires windows 10.14393 or newer
This commit is contained in:
parent
0ed766a91c
commit
096d2b2557
BIN
appbeep.wma
Normal file
BIN
appbeep.wma
Normal file
Binary file not shown.
14
plugin.xml
14
plugin.xml
@ -84,7 +84,7 @@
|
|||||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||||
</platform>
|
</platform>
|
||||||
|
|
||||||
<!-- windows
|
<!-- windows (10.14393) -->
|
||||||
<platform name="windows">
|
<platform name="windows">
|
||||||
<config-file target="config.xml" parent="/*">
|
<config-file target="config.xml" parent="/*">
|
||||||
<feature name="BackgroundMode" >
|
<feature name="BackgroundMode" >
|
||||||
@ -92,9 +92,17 @@
|
|||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
|
|
||||||
<js-module src="src/windows/backgroundmodeProxy.js" name="backgroundmodeProxy">
|
<config-file target="package.appxmanifest" parent="/Package/Capabilities" device-target="windows">
|
||||||
|
<!-- https://issues.apache.org/jira/browse/CB-12380 -->
|
||||||
|
<Capability Name="backgroundMediaPlayback" />
|
||||||
|
</config-file>
|
||||||
|
|
||||||
|
<!-- https://github.com/apache/cordova-windows/pull/213 -->
|
||||||
|
<resource-file src="appbeep.wma" target="appbeep.wma" />
|
||||||
|
|
||||||
|
<js-module src="src/windows/BackgroundModeProxy.js" name="BackgroundModeProxy">
|
||||||
<merges target="" />
|
<merges target="" />
|
||||||
</js-module>
|
</js-module>
|
||||||
</platform> -->
|
</platform>
|
||||||
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
123
src/windows/BackgroundModeProxy.js
Normal file
123
src/windows/BackgroundModeProxy.js
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
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);
|
@ -1,90 +0,0 @@
|
|||||||
/**
|
|
||||||
* BackgroundMode plugin for Windows 10 Universal
|
|
||||||
*
|
|
||||||
* Copyright (c) 2015 Next Wave Software, Inc.
|
|
||||||
**/
|
|
||||||
var backgroundMode = {
|
|
||||||
extendedSession: null,
|
|
||||||
settings: { text: "Background processing" },
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called internally to enable background execution.
|
|
||||||
*/
|
|
||||||
requestExtendedExecution: function () {
|
|
||||||
|
|
||||||
// Release the current active extended session if we have one.
|
|
||||||
backgroundMode.releaseExtendedExecution();
|
|
||||||
|
|
||||||
// Set up to request an extended session
|
|
||||||
backgroundMode.extendedSession = new Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession();
|
|
||||||
backgroundMode.extendedSession.description = backgroundMode.settings.text;
|
|
||||||
backgroundMode.extendedSession.reason = Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionReason.unspecified;
|
|
||||||
|
|
||||||
// When our app is running in the background and returns to the foreground, Windows will revoke our extended session permission.
|
|
||||||
// This is normal, so just request another session.
|
|
||||||
backgroundMode.extendedSession.onrevoked = function (args) {
|
|
||||||
backgroundMode.requestExtendedExecution();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Request the session
|
|
||||||
backgroundMode.extendedSession.requestExtensionAsync().done(
|
|
||||||
function success() {
|
|
||||||
cordova.plugins.backgroundMode.onactivate();
|
|
||||||
},
|
|
||||||
function error(error) {
|
|
||||||
cordova.plugins.backgroundMode.onfailure(0);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called internally to disable background execution.
|
|
||||||
*/
|
|
||||||
releaseExtendedExecution: function () {
|
|
||||||
|
|
||||||
if (backgroundMode.extendedSession != null) {
|
|
||||||
backgroundMode.extendedSession.close();
|
|
||||||
backgroundMode.extendedSession = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
cordova.commandProxy.add("BackgroundMode", {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enables background execution.
|
|
||||||
*/
|
|
||||||
// exec(null, null, 'BackgroundMode', 'enable', []);
|
|
||||||
enable: function () {
|
|
||||||
|
|
||||||
// Whenever our app is launched, request permission to continue running in the background the next time Windows wants to suspend us.
|
|
||||||
WinJS.Application.addEventListener("activated", function (args) {
|
|
||||||
if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch)
|
|
||||||
backgroundMode.requestExtendedExecution();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Whenever Windows tries to suspend us, again request permission to continue running in the background.
|
|
||||||
WinJS.Application.addEventListener("checkpoint", function (args) {
|
|
||||||
backgroundMode.requestExtendedExecution();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disables background execution.
|
|
||||||
*/
|
|
||||||
// exec(null, null, 'BackgroundMode', 'disable', []);
|
|
||||||
disable: function () {
|
|
||||||
|
|
||||||
backgroundMode.releaseExtendedExecution();
|
|
||||||
cordova.plugins.backgroundMode.ondeactivate();
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets configuration values.
|
|
||||||
*/
|
|
||||||
// exec(null, null, 'BackgroundMode', 'configure', [settingsObject, isUpdate]);
|
|
||||||
configure: function (args) {
|
|
||||||
|
|
||||||
backgroundMode.settings = args[0];
|
|
||||||
}
|
|
||||||
});
|
|
@ -99,7 +99,7 @@ exports.setDefaults = function (overrides) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((device.platform == 'Android') || (device.platform == 'Windows')){
|
if (device.platform == 'Android') {
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]);
|
cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -114,7 +114,7 @@ exports.setDefaults = function (overrides) {
|
|||||||
exports.configure = function (options) {
|
exports.configure = function (options) {
|
||||||
var settings = this.mergeWithDefaults(options);
|
var settings = this.mergeWithDefaults(options);
|
||||||
|
|
||||||
if ((device.platform == 'Android') || (device.platform == 'Windows')){
|
if (device.platform == 'Android') {
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'configure', [settings, true]);
|
cordova.exec(null, null, 'BackgroundMode', 'configure', [settings, true]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user