From a6a858f12b56c81c98142939d0ec02e0f381f9d5 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 10:48:28 -0400 Subject: [PATCH 01/19] [Android Only] Added 'isDeviceIdleMode' function --- src/android/PowerManagement.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/android/PowerManagement.java b/src/android/PowerManagement.java index 3348db0..2db7eac 100644 --- a/src/android/PowerManagement.java +++ b/src/android/PowerManagement.java @@ -79,6 +79,18 @@ 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 { + callbackContext.success((this.powerManager.isDeviceIdleMode() ? 1 : 0)); + } catch (Exception e) { + result = new PluginResult(PluginResult.Status.ERROR, "Could not get device idle status."); + } + } + else { + result = new PluginResult(PluginResult.Status.ERROR, "Android version too old to use isDeviceIdleMode()."); + } } } catch( JSONException e ) { From 865cde52fe62a8ede8cb0330dc98c658389ff6bb Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:02:15 -0400 Subject: [PATCH 02/19] [Android Only] Added 'isDeviceIdleMode' function --- www/powermanagement.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/www/powermanagement.js b/www/powermanagement.js index 35e0a1f..e9c93fc 100644 --- a/www/powermanagement.js +++ b/www/powermanagement.js @@ -58,4 +58,16 @@ 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', []); +} + module.exports = new PowerManagement(); From 05b28a09ea24748be7beda50b9dbfdd48d098d0d Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:08:50 -0400 Subject: [PATCH 03/19] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d514947..1d472f9 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ 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 ----- From 2790ab3478dd80193c116952cc1401553d39097e Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:19:25 -0400 Subject: [PATCH 04/19] Update README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 1d472f9..b60a276 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,24 @@ By default, the plugin will automatically release a wakelock when your app is pa }, 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 when it is asleep for too long, 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: + + // '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 check the device's idle state.'); + }); Note that in all the above examples, all callbacks are optional. From afe43155f421dc9354cb36c0a7fc0857a7d99a37 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:20:32 -0400 Subject: [PATCH 05/19] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b60a276..6289506 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ By default, the plugin will automatically release a wakelock when your app is pa ### [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 when it is asleep for too long, and is then placed in an 'idle' state. To check to see if the device is in this +components 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: // 'state' is either 1 (in idle) or 0 (not in idle) From 40883bf059f08c75e59f8e1494e997a6ee83ecc9 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:23:09 -0400 Subject: [PATCH 06/19] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6289506..baa5799 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ PowerManagement =============== Plugin for Cordova (3.0+) -changed SCREEN_DIM_WAKE_LOCK to PARTIAL_WAKE_LOCK -================================================= +Added 'isDeviceIdleMode' function for use with API >= 23 +======================================================== 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. From c7f4059721a7bc12c2c331a18f870e0379830309 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:25:16 -0400 Subject: [PATCH 07/19] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index baa5799..ec937c0 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ PowerManagement =============== Plugin for Cordova (3.0+) -Added 'isDeviceIdleMode' function for use with API >= 23 -======================================================== +Added 'isDeviceIdleMode' function 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. From d198a2902b44486e9fcc32bc98306337dd2176b4 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 11:56:57 -0400 Subject: [PATCH 08/19] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ec937c0..489235d 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ state, you can use the following function: } }, function() { - console.log('Failed check the device's idle state.'); + console.log('Failed to check the device's idle state.'); }); Note that in all the above examples, all callbacks are optional. From 0ebec4eab6afc896aa2cce03561ad193175a3deb Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Tue, 9 Aug 2016 15:01:48 -0400 Subject: [PATCH 09/19] Changed plugin ID --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 1c5e0fd..06608d5 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ PowerManagement PowerManagement plugin for Cordova From e65ce7b36c059bd2ec98a5548fc676842b61414d Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 13:43:30 -0400 Subject: [PATCH 10/19] [Android Only] Added more functions Notably 'isIgnoringBatteryOptimizations' and 'addAppToBatteryWhitelist'. --- plugin.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugin.xml b/plugin.xml index 06608d5..1d93615 100644 --- a/plugin.xml +++ b/plugin.xml @@ -55,8 +55,17 @@ + + + + + + + + + From b4f2f22e7bf9377de7e9b4ae3f92f107485ef1a7 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 13:58:28 -0400 Subject: [PATCH 11/19] [Android Only] Added more functions Notably 'isIgnoringBatteryOptimizations' and 'addAppToBatteryWhitelist' --- www/powermanagement.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/www/powermanagement.js b/www/powermanagement.js index e9c93fc..7961a7b 100644 --- a/www/powermanagement.js +++ b/www/powermanagement.js @@ -65,9 +65,30 @@ PowerManagement.prototype.dim = function(successCallback,failureCallback) { * @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 use 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(); From 6cc0464c76f177feb2c814aa5e57e85d6fb36b6c Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 13:59:50 -0400 Subject: [PATCH 12/19] Update powermanagement.js --- www/powermanagement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/powermanagement.js b/www/powermanagement.js index 7961a7b..6f1ce25 100644 --- a/www/powermanagement.js +++ b/www/powermanagement.js @@ -81,7 +81,7 @@ PowerManagement.prototype.isIgnoringBatteryOptimizations = function (successCall }; /** - * Opens a dialog that allows the use to add the app to the battery optimization whitelist. Android Only. + * 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 From 2fcf24ca230e79aabb06189b9cfe0b5d31879582 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 14:09:51 -0400 Subject: [PATCH 13/19] [Android Only] Added more functions Notably 'isIgnoringBatteryOptimizations' and 'addAppToBatteryWhitelist' --- src/android/PowerManagement.java | 56 ++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/android/PowerManagement.java b/src/android/PowerManagement.java index 2db7eac..2903910 100644 --- a/src/android/PowerManagement.java +++ b/src/android/PowerManagement.java @@ -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); } @@ -83,13 +89,38 @@ public class PowerManagement extends CordovaPlugin { // 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 { - result = new PluginResult(PluginResult.Status.ERROR, "Android version too old to use isDeviceIdleMode()."); + // 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); } } } @@ -101,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 From 6dcb9f73815f3dc1f100881b51b56ca8072b58e9 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 14:58:48 -0400 Subject: [PATCH 14/19] Updated README.md --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 489235d..2e40f5e 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,9 @@ By default, the plugin will automatically release a wakelock when your app is pa ### [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 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: +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: // 'state' is either 1 (in idle) or 0 (not in idle) window.powerManagement.isDeviceIdleMode(function(state) { @@ -70,13 +71,61 @@ state, you can use the following function: console.log('Device IS in idle mode.'); } else { - console.log('Device is NOT in idle mode.'); + 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: + + 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: + + 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 conjuction with the `window.powerManagement.addAppToBatteryWhitelist` above. Example: + + 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 From 827ea04e1c65a37d145d6134872798076af01436 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 15:00:02 -0400 Subject: [PATCH 15/19] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e40f5e..048f59a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ PowerManagement =============== Plugin for Cordova (3.0+) -Added 'isDeviceIdleMode' function for use with Android 6.0.0+ +Added 'isIgnoringBatteryOptimizations' and 'addAppToBatteryWhitelist' functions for use with Android 6.0.0+ ============================================================= The PowerManagement plugin offers access to the devices power-management functionality. @@ -108,7 +108,7 @@ To check to see if the app that uses this plugin is on the device's battery opti }); -This function can be use in conjuction with the `window.powerManagement.addAppToBatteryWhitelist` above. Example: +This function can be use in conjunction with the `window.powerManagement.addAppToBatteryWhitelist` above. Example: window.powerManagement.isIgnoringBatteryOptimizations(function(result) { // If the app isn't on the battery whitelist, open the dialog to add it From f121af93c3a4d3581fb175cb267e61df2f2cd3a7 Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 15:08:30 -0400 Subject: [PATCH 16/19] Updated README.md, added colour coding for examples --- README.md | 150 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 048f59a..afb2748 100644 --- a/README.md +++ b/README.md @@ -25,107 +25,113 @@ 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. +```js +window.powerManagement.setReleaseOnPause(false, function() { + console.log('Set successfully'); +}, function() { + console.log('Failed to set'); +}); +``` - 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: - - // '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.'); - }); +```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: - - 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.'); - }); +```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: - - 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.'); - }); - +```js +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: - - 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.'); - }); - +```js +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 From 906aff19ef154051b973082f5a9a6172a9cb439d Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 15:12:26 -0400 Subject: [PATCH 17/19] Escaping apostrophes --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index afb2748..d8a21a2 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ window.powerManagement.isDeviceIdleMode(function(state) { if (state === 1) { console.log('Device IS in idle mode.'); } - else { +else { console.log('Device IS NOT in idle mode.'); } @@ -92,7 +92,7 @@ To add the app that uses this plugin to the device's battery optimization whitel 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.'); + console.log('Failed to add the app to the device\'s battery optimization whitelist.'); }); ``` @@ -104,10 +104,10 @@ To check to see if the app that uses this plugin is on the device's battery opti ```js window.powerManagement.isIgnoringBatteryOptimizations(function(result) { if (result === 1) { - console.log('This app IS on the device's battery optimization whitelist.'); + 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.'); + 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.'); @@ -122,14 +122,14 @@ window.powerManagement.isIgnoringBatteryOptimizations(function(result) { 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.'); + 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.'); + 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.'); + 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. From c6eae791e72e13126cdd2b9dbbf77cf556b8368e Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 15:13:27 -0400 Subject: [PATCH 18/19] More apostrophe escaping --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d8a21a2..0633ba5 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ else { } }, function() { - console.log('Failed to check the device's idle state.'); + console.log('Failed to check the device\'s idle state.'); }); ``` @@ -110,7 +110,7 @@ window.powerManagement.isIgnoringBatteryOptimizations(function(result) { 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.'); + console.log('Failed to add the app to the device\'s battery optimization whitelist.'); }); ``` From 9c248c5aba4290d21cc449532e66175d77cde5bc Mon Sep 17 00:00:00 2001 From: Max Bassett Date: Wed, 10 Aug 2016 15:17:26 -0400 Subject: [PATCH 19/19] Argument clarification --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0633ba5..47d205f 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ components (network connection, GPS, etc.) when it is asleep for an extended per 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.'); @@ -116,6 +117,7 @@ window.powerManagement.isIgnoringBatteryOptimizations(function(result) { 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) {