Skip to content

Commit

Permalink
Support windows platform - requires windows 10.14393 or newer
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Jan 21, 2017
1 parent 0ed766a commit 096d2b2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 95 deletions.
Binary file added appbeep.wma
Binary file not shown.
14 changes: 11 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,25 @@
target-dir="src/de/appplant/cordova/plugin/background" />
</platform>

<!-- windows
<!-- windows (10.14393) -->
<platform name="windows">
<config-file target="config.xml" parent="/*">
<feature name="BackgroundMode" >
<param name="windows-package" value="BackgroundMode"/>
</feature>
</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="" />
</js-module>
</platform> -->
</platform>

</plugin>
123 changes: 123 additions & 0 deletions src/windows/BackgroundModeProxy.js
Original file line number Diff line number Diff line change
@@ -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);
90 changes: 0 additions & 90 deletions src/windows/backgroundmodeProxy.js

This file was deleted.

4 changes: 2 additions & 2 deletions www/background-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
};
Expand All @@ -114,7 +114,7 @@ exports.setDefaults = function (overrides) {
exports.configure = function (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]);
}
};
Expand Down

0 comments on commit 096d2b2

Please sign in to comment.