pull/1/merge
Max Bassett 7 years ago committed by GitHub
commit ce5e6515c4

@ -2,8 +2,8 @@ PowerManagement
===============
Plugin for Cordova (3.0+)
changed SCREEN_DIM_WAKE_LOCK to PARTIAL_WAKE_LOCK
=================================================
Added 'isIgnoringBatteryOptimizations' and 'addAppToBatteryWhitelist' functions for use with Android 6.0.0+
=============================================================
The PowerManagement plugin offers access to the devices power-management functionality.
It should be used for applications which keep running for a long time without any user interaction.
@ -18,47 +18,122 @@ Installation
---------
Install the plugin using the cordova command line utility:
`$ cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git`
`$ cordova plugin add https://github.com/bassena/cordova-plugin-powermanagement.git`
Usage
-----
### window.powerManagement.acquire(successCallback, failureCallback)
Acquire a wakelock by calling this.
window.powerManagement.acquire(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
```js
window.powerManagement.acquire(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
```
### window.powerManagement.dim(successCallback, failureCallback)
This acquires a partial wakelock, allowing the screen to be dimmed.
window.powerManagement.dim(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
```js
window.powerManagement.dim(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
```
### window.powerManagement.release(successCallback, failureCallback)
Release the wakelock. It's important to do this when you're finished with the wakelock, to avoid unnecessary battery drain.
window.powerManagement.release(function() {
console.log('Wakelock released');
}, function() {
console.log('Failed to release wakelock');
});
```js
window.powerManagement.release(function() {
console.log('Wakelock released');
}, function() {
console.log('Failed to release wakelock');
});
```
### [Android Only] window.powerManagement.setReleaseOnPause(enabled, successCallback, failureCallback)
By default, the plugin will automatically release a wakelock when your app is paused (e.g. when the screen is turned off, or the user switches to another app). It will reacquire the wakelock upon app resume. If you would prefer to disable this behaviour, you can use this function.
window.powerManagement.setReleaseOnPause(false, function() {
console.log('Set successfully');
}, function() {
console.log('Failed to set');
});
```js
window.powerManagement.setReleaseOnPause(false, function() {
console.log('Set successfully');
}, function() {
console.log('Failed to set');
});
```
### [Android Only, API >= 23] window.powerManagement.isDeviceIdleMode(successCallback, failureCallback)
As of Android 6.0.0+, Android now has an extra power management feature called 'Doze'. This feature disables most device
components (network connection, GPS, etc.) when it is asleep for an extended period of time and is then placed in an 'idle' state.
To check to see if the device is in this state, you can use the following function:
```js
// 'state' is either 1 (in idle) or 0 (not in idle)
window.powerManagement.isDeviceIdleMode(function(state) {
if (state === 1) {
console.log('Device IS in idle mode.');
}
else {
console.log('Device IS NOT in idle mode.');
}
}, function() {
console.log('Failed to check the device\'s idle state.');
});
```
### [Android Only, API >= 23] window.powerManagement.addAppToBatteryWhitelist(successCallback, failureCallback)
As of Android 6.0.0+, Android now has an extra power management feature called 'Doze'. This feature disables most device
components (network connection, GPS, etc.) when it is asleep for an extended period of time and is then placed in an 'idle' state. Adding the app to the battery optimization whitelist can circumvent some of these issues.
To add the app that uses this plugin to the device's battery optimization whitelist, use the following function:
```js
window.powerManagement.addAppToBatteryWhitelist(function() {
console.log('A dialog has popped up asking you to accept adding the app to the whitelist.');
}, function() {
console.log('Failed to add the app to the device\'s battery optimization whitelist.');
});
```
### [Android Only, API >= 23] window.powerManagement.isIgnoringBatteryOptimizations(successCallback, failureCallback)
As of Android 6.0.0+, Android now has an extra power management feature called 'Doze'. This feature disables most device
components (network connection, GPS, etc.) when it is asleep for an extended period of time and is then placed in an 'idle' state.
To check to see if the app that uses this plugin is on the device's battery optimization whitelist, use the following function:
```js
// 'result' is either 1 (in whitelist), or 0 (not in whitelist)
window.powerManagement.isIgnoringBatteryOptimizations(function(result) {
if (result === 1) {
console.log('This app IS on the device\'s battery optimization whitelist.');
}
else {
console.log('This app IS NOT on the device\'s battery optimization whitelist.');
}
}, function() {
console.log('Failed to add the app to the device\'s battery optimization whitelist.');
});
```
This function can be use in conjunction with the `window.powerManagement.addAppToBatteryWhitelist` above. Example:
```js
// 'result' is either 1 (in whitelist), or 0 (not in whitelist)
window.powerManagement.isIgnoringBatteryOptimizations(function(result) {
// If the app isn't on the battery whitelist, open the dialog to add it
if (result !== 1) {
window.powerManagement.addAppToBatteryWhitelist(function() {
console.log('A dialog has popped up asking you to accept adding the app to the whitelist.');
}, function() {
console.log('Failed to add the app to the device\'s battery optimization whitelist.');
});
}
else {
console.log('This app IS NOT on the device\'s battery optimization whitelist.');
}
}, function() {
console.log('Failed to add the app to the device\'s battery optimization whitelist.');
});
```
Note that in all the above examples, all callbacks are optional.
License

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="at.gofg.sportscomputer.powermanagement"
id="com.bassena.powermanagement"
version="1.1.0">
<name>PowerManagement</name>
<description>PowerManagement plugin for Cordova</description>
@ -55,8 +55,17 @@
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<receiver android:exported="false" android:name="org.apache.cordova.powermanagement.PowerManagement">
<intent-filter>
<action android:name="android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
</intent-filter>
</receiver>
</config-file>
<source-file src="src/android/PowerManagement.java" target-dir="src/org/apache/cordova/powermanagement" />
</platform>
</plugin>

@ -24,7 +24,10 @@ import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import org.apache.cordova.CordovaWebView;
@ -42,6 +45,8 @@ public class PowerManagement extends CordovaPlugin {
private PowerManager.WakeLock wakeLock = null;
private PowerManager powerManager = null;
private boolean releaseOnPause = true;
private CordovaInterface cordova = null;
private Context appContext = null;
/**
* Fetch a reference to the power-service when the plugin is initialized
@ -49,7 +54,8 @@ public class PowerManagement extends CordovaPlugin {
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
this.cordova = cordova;
this.appContext = cordova.getActivity().getApplicationContext();
this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);
}
@ -79,6 +85,43 @@ public class PowerManagement extends CordovaPlugin {
} catch (Exception e) {
result = new PluginResult(PluginResult.Status.ERROR, "Could not set releaseOnPause");
}
} else if( action.equals("isDeviceIdleMode")) {
// Only available since API 23
if (android.os.Build.VERSION.SDK_INT >= 23) {
try {
// returns 1: device in idle mode, or 0: device not in idle mode
callbackContext.success((this.powerManager.isDeviceIdleMode() ? 1 : 0));
} catch (Exception e) {
result = new PluginResult(PluginResult.Status.ERROR, "Could not get device idle status.");
}
}
else {
// For APIs < 23, device never reaches idle state (Doze).
callbackContext.success(0);
}
} else if( action.equals("isIgnoringBatteryOptimizations")) {
// Only available since API 23
if (android.os.Build.VERSION.SDK_INT >= 23) {
try {
// returns 1: app is in battery whitelist, or 0: app is not in battery whitelist
callbackContext.success((this.powerManager.isIgnoringBatteryOptimizations(appContext.getPackageName()) ? 1 : 0));
} catch (Exception e) {
result = new PluginResult(PluginResult.Status.ERROR, "Could not check device's battery optimization list.");
}
}
else {
// Always true, Doze feature not available in APIs < 23
callbackContext.success(1);
}
} else if( action.equals("addAppToBatteryWhitelist")) {
// Only available since API 23
if (android.os.Build.VERSION.SDK_INT >= 23) {
result = addAppToBatteryWhitelist();
}
else {
// Nothing to do, so just return success
result = new PluginResult(PluginResult.Status.OK);
}
}
}
catch( JSONException e ) {
@ -89,6 +132,27 @@ public class PowerManagement extends CordovaPlugin {
return true;
}
/**
* Opens an activity (in this case a dialog) to allow the user to add the app to the battery optimization whitelist
* @return PluginResult containing the status of adding the app to the battery optimization whitelist
*/
private PluginResult addAppToBatteryWhitelist () {
PluginResult result = null;
try {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + appContext.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(intent);
result = new PluginResult(PluginResult.Status.OK);
} catch (Exception e) {
result = new PluginResult(PluginResult.Status.ERROR, "Could not add app to device battery optimization list. Error:" + e.getMessage());
}
return result;
}
/**
* Acquire a wake-lock
* @param p_flags Type of wake-lock to acquire

@ -58,4 +58,37 @@ PowerManagement.prototype.dim = function(successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'acquire', [true]);
}
/**
* Checks the device's idle state. Android Only.
* *** Requires minimum API level 23 ***
*
* @param successCallback function to be called when the device's idle state returns successfully
* @param errorCallback function to be called when there was a problem with checking the device's idle state
*/
PowerManagement.prototype.isDeviceIdleMode = function(successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'isDeviceIdleMode', []);
}
/**
* Checks if the app has been added to the battery optimization whitelist (i.e. ignores battery optimization). Android Only.
* *** Requires minimum API level 23 ***
*
* @param successCallback function to be called when the check of the battery optimization whitelist returns successfully
* @param errorCallback function to be called when there was a problem with the check of the battery optimization whitelist
*/
PowerManagement.prototype.isIgnoringBatteryOptimizations = function (successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'isIgnoringBatteryOptimizations', []);
};
/**
* Opens a dialog that allows the user to add the app to the battery optimization whitelist. Android Only.
* *** Requires minimum API level 23 ***
*
* @param successCallback function to be called when adding the app to the battery optimization whitelist is successful
* @param errorCallback function to be called when there was a problem with adding the app to the battery optimization whitelist
*/
PowerManagement.prototype.addAppToBatteryWhitelist = function (successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'addAppToBatteryWhitelist', []);
};
module.exports = new PowerManagement();

Loading…
Cancel
Save