cordova-plugin-background-mode-mv
Advanced tools
Comparing version
## ChangeLog | ||
#### Version 0.7.3 (not yet released) | ||
- Check if screen is off on Android | ||
- Wake-up device on Android | ||
- Unlock device on Android | ||
#### Version 0.7.2 (02.02.2017) | ||
@@ -8,0 +3,0 @@ - Fixed app freeze on iOS using wkwebview-engine |
{ | ||
"name": "cordova-plugin-background-mode-mv", | ||
"version": "0.7.7", | ||
"description": "Prevent apps from going to sleep in background.", | ||
"cordova": { | ||
"id": "cordova-plugin-background-mode-mv", | ||
"platforms": [ | ||
"ios", | ||
"android", | ||
"browser" | ||
] | ||
}, | ||
"version": "0.7.8", | ||
"description": "Prevent app from going to sleep in background.", | ||
"keywords": [ | ||
"appplant", | ||
"background", | ||
"ecosystem:cordova", | ||
"cordova-ios", | ||
"cordova-android", | ||
"cordova-browser" | ||
"cordova", | ||
"ecosystem:cordova" | ||
], | ||
"platforms": [ | ||
"ios", | ||
"android" | ||
], | ||
"engines": [ | ||
{ | ||
"name": "cordova", | ||
"version": ">=6.0.0" | ||
} | ||
], | ||
"author": "Sebastián Katzer", | ||
@@ -27,2 +27,2 @@ "license": "Apache 2.0", | ||
"homepage": "https://github.com/katzer/cordova-plugin-background-mode#readme" | ||
} | ||
} |
@@ -50,3 +50,3 @@ | ||
```js | ||
```javascript | ||
document.addEventListener('deviceready', function () { | ||
@@ -60,3 +60,3 @@ // cordova.plugins.backgroundMode is now available | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.enable(); | ||
@@ -68,3 +68,3 @@ // or | ||
To disable the background mode: | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.disable(); | ||
@@ -78,3 +78,3 @@ // or | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.isActive(); // => boolean | ||
@@ -88,3 +88,3 @@ ``` | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.on('EVENT', function); | ||
@@ -94,3 +94,3 @@ ``` | ||
To remove an event listeners: | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.un('EVENT', function); | ||
@@ -105,3 +105,3 @@ ``` | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.moveToBackground(); | ||
@@ -115,3 +115,3 @@ // or | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.overrideBackButton(); | ||
@@ -123,25 +123,6 @@ ``` | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.excludeFromTaskList(); | ||
``` | ||
### Detect screen status | ||
The method works async instead of _isActive()_ or _isEnabled()_. | ||
```js | ||
cordova.plugins.backgroundMode.isScreenOff(function(bool) { | ||
... | ||
}); | ||
``` | ||
### Unlock and wake-up | ||
A wake-up turns on the screen while unlocking moves the app to foreground even the device is locked. | ||
```js | ||
// Turn screen on | ||
cordova.plugins.backgroundMode.wakeUp(); | ||
// Turn screen on and show app even locked | ||
cordova.plugins.backgroundMode.unlock(); | ||
``` | ||
### Notification | ||
@@ -153,3 +134,3 @@ To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar. | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.setDefaults({ | ||
@@ -167,3 +148,3 @@ title: String, | ||
To modify the currently displayed notification | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.configure({ ... }); | ||
@@ -177,4 +158,4 @@ ``` | ||
```js | ||
cordova.plugins.backgroundMode.setDefaults({ silent: true }); | ||
```javascript | ||
cordova.plugins.backgroundMode.configure({ silent: true }); | ||
``` | ||
@@ -187,3 +168,3 @@ | ||
```js | ||
```javascript | ||
cordova.plugins.backgroundMode.on('activate', function() { | ||
@@ -212,3 +193,3 @@ cordova.plugins.backgroundMode.disableWebViewOptimizations(); | ||
? 2017 [appPlant GmbH][appplant] & [meshfields][meshfields] | ||
© 2017 [appPlant GmbH][appplant] | ||
@@ -222,2 +203,1 @@ | ||
[appplant]: http://appplant.de | ||
[meshfields]: http://meshfields.de |
/* | ||
Copyright 2013 Sebastián Katzer | ||
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 | ||
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 | ||
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. | ||
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. | ||
*/ | ||
@@ -21,0 +21,0 @@ |
/* | ||
Copyright 2013 Sebastián Katzer | ||
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 | ||
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 | ||
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. | ||
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. | ||
*/ | ||
@@ -21,0 +21,0 @@ |
@@ -25,2 +25,7 @@ /* | ||
/************* | ||
* INTERFACE * | ||
*************/ | ||
/** | ||
@@ -33,11 +38,10 @@ * Activates the background mode. When activated the application | ||
*/ | ||
exports.enable = function() | ||
{ | ||
exports.enable = function () { | ||
if (this.isEnabled()) | ||
return; | ||
var fn = function() { | ||
exports._isEnabled = true; | ||
exports.fireEvent('enable'); | ||
}; | ||
var fn = function () { | ||
exports._isEnabled = true; | ||
exports.fireEvent('enable'); | ||
}; | ||
@@ -53,11 +57,10 @@ cordova.exec(fn, null, 'BackgroundMode', 'enable', []); | ||
*/ | ||
exports.disable = function() | ||
{ | ||
exports.disable = function () { | ||
if (!this.isEnabled()) | ||
return; | ||
var fn = function() { | ||
exports._isEnabled = false; | ||
exports.fireEvent('disable'); | ||
}; | ||
var fn = function () { | ||
exports._isEnabled = false; | ||
exports.fireEvent('disable'); | ||
}; | ||
@@ -74,4 +77,3 @@ cordova.exec(fn, null, 'BackgroundMode', 'disable', []); | ||
*/ | ||
exports.setEnabled = function (enable) | ||
{ | ||
exports.setEnabled = function (enable) { | ||
if (enable) { | ||
@@ -89,4 +91,3 @@ this.enable(); | ||
*/ | ||
exports.getDefaults = function() | ||
{ | ||
exports.getDefaults = function () { | ||
return this._defaults; | ||
@@ -96,12 +97,2 @@ }; | ||
/** | ||
* The actual applied settings. | ||
* | ||
* @return [ Object ] | ||
*/ | ||
exports.getSettings = function() | ||
{ | ||
return this._settings || {}; | ||
}; | ||
/** | ||
* Overwrite the default settings. | ||
@@ -113,10 +104,7 @@ * | ||
*/ | ||
exports.setDefaults = function (overrides) | ||
{ | ||
exports.setDefaults = function (overrides) { | ||
var defaults = this.getDefaults(); | ||
for (var key in defaults) | ||
{ | ||
if (overrides.hasOwnProperty(key)) | ||
{ | ||
for (var key in defaults) { | ||
if (overrides.hasOwnProperty(key)) { | ||
defaults[key] = overrides[key]; | ||
@@ -126,4 +114,3 @@ } | ||
if (this._isAndroid) | ||
{ | ||
if (this._isAndroid) { | ||
cordova.exec(null, null, 'BackgroundMode', 'configure', [defaults, false]); | ||
@@ -137,25 +124,12 @@ } | ||
* | ||
* @param [ Object ] options Dict of options to be overridden. | ||
* @param [ Object ] overrides Dict of options to be overridden. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports.configure = function (options) | ||
{ | ||
var settings = this.getSettings(), | ||
defaults = this.getDefaults(); | ||
exports.configure = function (options) { | ||
var settings = this.mergeWithDefaults(options); | ||
if (!this._isAndroid) | ||
return; | ||
if (!this._isActive) | ||
{ | ||
console.log('BackgroundMode is not active, skipped...'); | ||
return; | ||
if (this._isAndroid) { | ||
cordova.exec(null, null, 'BackgroundMode', 'configure', [settings, true]); | ||
} | ||
this._mergeObjects(options, settings); | ||
this._mergeObjects(options, defaults); | ||
this._settings = options; | ||
cordova.exec(null, null, 'BackgroundMode', 'configure', [options, true]); | ||
}; | ||
@@ -168,7 +142,5 @@ | ||
*/ | ||
exports.disableWebViewOptimizations = function() | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'webview', []); | ||
exports.disableWebViewOptimizations = function () { | ||
if (this._isAndroid) { | ||
cordova.exec(null, null, 'BackgroundMode', 'optimizations', []); | ||
} | ||
@@ -178,32 +150,2 @@ }; | ||
/** | ||
* Disables battery optimazation mode for the app. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports.disableBatteryOptimizations = function() | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'battery', []); | ||
} | ||
}; | ||
/** | ||
* Opens the system settings dialog where the user can tweak or turn off any | ||
* custom app start settings added by the manufacturer if available. | ||
* | ||
* @param [ Object|Bool ] options Set to false if you dont want to display an | ||
* alert dialog first. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports.openAppStartSettings = function (options) | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]); | ||
} | ||
}; | ||
/** | ||
* Move app to background (Android only). | ||
@@ -213,7 +155,5 @@ * | ||
*/ | ||
exports.moveToBackground = function() | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'background', []); | ||
exports.moveToBackground = function () { | ||
if (this._isAndroid) { | ||
cordova.exec(null, null, 'BackgroundMode', 'background', []); | ||
} | ||
@@ -227,7 +167,5 @@ }; | ||
*/ | ||
exports.moveToForeground = function() | ||
{ | ||
if (this.isActive() && this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []); | ||
exports.moveToForeground = function () { | ||
if (this.isActive() && this._isAndroid) { | ||
cordova.exec(null, null, 'BackgroundMode', 'foreground', []); | ||
} | ||
@@ -241,7 +179,5 @@ }; | ||
*/ | ||
exports.excludeFromTaskList = function() | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []); | ||
exports.excludeFromTaskList = function () { | ||
if (this._isAndroid) { | ||
cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); | ||
} | ||
@@ -256,4 +192,3 @@ }; | ||
*/ | ||
exports.overrideBackButton = function() | ||
{ | ||
exports.overrideBackButton = function () { | ||
document.addEventListener('backbutton', function() { | ||
@@ -265,47 +200,2 @@ exports.moveToBackground(); | ||
/** | ||
* If the screen is off. | ||
* | ||
* @param [ Function ] fn Callback function to invoke with boolean arg. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports.isScreenOff = function (fn) | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []); | ||
} | ||
else | ||
{ | ||
fn(undefined); | ||
} | ||
}; | ||
/** | ||
* Wake up the device. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports.wakeUp = function() | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []); | ||
} | ||
}; | ||
/** | ||
* Wake up and unlock the device. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports.unlock = function() | ||
{ | ||
if (this._isAndroid) | ||
{ | ||
cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []); | ||
} | ||
}; | ||
/** | ||
* If the mode is enabled or disabled. | ||
@@ -315,4 +205,3 @@ * | ||
*/ | ||
exports.isEnabled = function() | ||
{ | ||
exports.isEnabled = function () { | ||
return this._isEnabled !== false; | ||
@@ -326,7 +215,11 @@ }; | ||
*/ | ||
exports.isActive = function() | ||
{ | ||
exports.isActive = function () { | ||
return this._isActive !== false; | ||
}; | ||
/********** | ||
* EVENTS * | ||
**********/ | ||
exports._listener = {}; | ||
@@ -342,4 +235,3 @@ | ||
*/ | ||
exports.fireEvent = function (event) | ||
{ | ||
exports.fireEvent = function (event) { | ||
var args = Array.apply(null, arguments).slice(1), | ||
@@ -351,4 +243,3 @@ listener = this._listener[event]; | ||
for (var i = 0; i < listener.length; i++) | ||
{ | ||
for (var i = 0; i < listener.length; i++) { | ||
var fn = listener[i][0], | ||
@@ -370,9 +261,8 @@ scope = listener[i][1]; | ||
*/ | ||
exports.on = function (event, callback, scope) | ||
{ | ||
exports.on = function (event, callback, scope) { | ||
if (typeof callback !== "function") | ||
return; | ||
if (!this._listener[event]) | ||
{ | ||
if (!this._listener[event]) { | ||
this._listener[event] = []; | ||
@@ -394,4 +284,3 @@ } | ||
*/ | ||
exports.un = function (event, callback) | ||
{ | ||
exports.un = function (event, callback) { | ||
var listener = this._listener[event]; | ||
@@ -402,8 +291,6 @@ | ||
for (var i = 0; i < listener.length; i++) | ||
{ | ||
for (var i = 0; i < listener.length; i++) { | ||
var fn = listener[i][0]; | ||
if (fn == callback) | ||
{ | ||
if (fn == callback) { | ||
listener.splice(i, 1); | ||
@@ -416,32 +303,30 @@ break; | ||
/** | ||
* @private | ||
* @deprecated | ||
* | ||
* Flag indicates if the mode is enabled. | ||
* Called when the background mode has been activated. | ||
*/ | ||
exports._isEnabled = false; | ||
exports.onactivate = function () {}; | ||
/** | ||
* @private | ||
* @deprecated | ||
* | ||
* Flag indicates if the mode is active. | ||
* Called when the background mode has been deaktivated. | ||
*/ | ||
exports._isActive = false; | ||
exports.ondeactivate = function () {}; | ||
/** | ||
* @private | ||
* @deprecated | ||
* | ||
* Default values of all available options. | ||
* Called when the background mode could not been activated. | ||
* | ||
* @param {Integer} errorCode | ||
* Error code which describes the error | ||
*/ | ||
exports._defaults = | ||
{ | ||
title: 'App is running in background', | ||
text: 'Doing heavy tasks.', | ||
bigText: false, | ||
resume: true, | ||
silent: false, | ||
hidden: true, | ||
color: undefined, | ||
icon: 'icon' | ||
}; | ||
exports.onfailure = function () {}; | ||
/********* | ||
* UTILS * | ||
*********/ | ||
/** | ||
@@ -453,13 +338,11 @@ * @private | ||
* @param [ Object ] options The custom options. | ||
* @param [ Object ] toMergeIn The options to merge in. | ||
* | ||
* @return [ Object ] Default values merged with custom values. | ||
*/ | ||
exports._mergeObjects = function (options, toMergeIn) | ||
{ | ||
for (var key in toMergeIn) | ||
{ | ||
if (!options.hasOwnProperty(key)) | ||
{ | ||
options[key] = toMergeIn[key]; | ||
exports.mergeWithDefaults = function (options) { | ||
var defaults = this.getDefaults(); | ||
for (var key in defaults) { | ||
if (!options.hasOwnProperty(key)) { | ||
options[key] = defaults[key]; | ||
continue; | ||
@@ -475,21 +358,2 @@ } | ||
* | ||
* Setter for the isActive flag. Resets the | ||
* settings if the mode isnt active anymore. | ||
* | ||
* @param [ Boolean] value The new value for the flag. | ||
* | ||
* @return [ Void ] | ||
*/ | ||
exports._setActive = function(value) | ||
{ | ||
if (this._isActive == value) | ||
return; | ||
this._isActive = value; | ||
this._settings = value ? this._mergeObjects({}, this._defaults) : {}; | ||
}; | ||
/** | ||
* @private | ||
* | ||
* Initialize the plugin. | ||
@@ -502,9 +366,7 @@ * | ||
*/ | ||
exports._pluginInitialize = function() | ||
{ | ||
exports.pluginInitialize = function () { | ||
this._isAndroid = device.platform.match(/^android|amazon/i) !== null; | ||
this.setDefaults({}); | ||
if (device.platform == 'browser') | ||
{ | ||
if (device.platform == 'browser') { | ||
this.enable(); | ||
@@ -517,7 +379,41 @@ this._isEnabled = true; | ||
/*********** | ||
* PRIVATE * | ||
***********/ | ||
/** | ||
* @private | ||
* | ||
* Flag indicates if the mode is enabled. | ||
*/ | ||
exports._isEnabled = false; | ||
/** | ||
* @private | ||
* | ||
* Flag indicates if the mode is active. | ||
*/ | ||
exports._isActive = false; | ||
/** | ||
* @private | ||
* | ||
* Default values of all available options. | ||
*/ | ||
exports._defaults = { | ||
title: 'App is running in background', | ||
text: 'Doing heavy tasks.', | ||
bigText: false, | ||
resume: true, | ||
silent: false, | ||
hidden: true, | ||
color: undefined, | ||
icon: 'icon' | ||
}; | ||
// Called before 'deviceready' listener will be called | ||
channel.onCordovaReady.subscribe(function() | ||
{ | ||
channel.onCordovaInfoReady.subscribe(function() { | ||
exports._pluginInitialize(); | ||
channel.onCordovaReady.subscribe(function () { | ||
channel.onCordovaInfoReady.subscribe(function () { | ||
exports.pluginInitialize(); | ||
}); | ||
@@ -527,13 +423,10 @@ }); | ||
// Called after 'deviceready' event | ||
channel.deviceready.subscribe(function() | ||
{ | ||
if (exports.isEnabled()) | ||
{ | ||
channel.deviceready.subscribe(function () { | ||
if (exports.isEnabled()) { | ||
exports.fireEvent('enable'); | ||
} | ||
if (exports.isActive()) | ||
{ | ||
if (exports.isActive()) { | ||
exports.fireEvent('activate'); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
169296
-7.99%17
-5.56%1109
-25.57%189
-9.13%