diff --git a/README.md b/README.md index ce81a88..b7fbf4a 100644 --- a/README.md +++ b/README.md @@ -122,10 +122,33 @@ cordova.plugins.backgroundMode.unlock(); ### Request to disable battery optimizations Starting in Android 8, apps can be put to sleep to conserve battery. When this happens (usually after 5 minutes or so), the background task is killed. This will cause things like MQTT connections to break. + This method will show a permission prompt for the user (only if the app hasn't been granted permission) to ignore the optimization. ```js -cordova.plugins.backgroundMode.disableWebViewOptimizations(); +cordova.plugins.backgroundMode.disableBatteryOptimizations(); +``` + +You can also open the battery optimization settings menu directly, and get the user to set it manually. This may be a better option for devices which may ignore the prompt above. + +```js +cordova.plugins.backgroundMode.openBatteryOptimizationsSettings(); +``` + +To check if battery optimizations are disabled for the app: + +```js +cordova.plugins.backgroundMode.isIgnoringBatteryOptimizations(function(isIgnoring) { + ... +}) +``` + +Additionally, you may find that your JS code begins to run less frequently, or not at all while in the background. This can be due to the webview slowing down its execution due to being in the background. The `disableWebViewOptimizations` function can prevent that, but it's important that it is run _after_ the app goes to the background. + +```js +cordova.plugins.backgroundMode.on('activate', function() { + cordova.plugins.backgroundMode.disableWebViewOptimizations(); +}); ``` ### Notification diff --git a/src/android/BackgroundModeExt.java b/src/android/BackgroundModeExt.java index ed76598..25965e4 100644 --- a/src/android/BackgroundModeExt.java +++ b/src/android/BackgroundModeExt.java @@ -55,6 +55,7 @@ import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY; import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION_CODES.M; import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS; +import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS; import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON; import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD; import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; @@ -90,6 +91,12 @@ public class BackgroundModeExt extends CordovaPlugin { case "battery": disableBatteryOptimizations(); break; + case "batterysettings": + openBatterySettings(); + break; + case "optimizationstatus": + isIgnoringBatteryOptimizations(callback); + break; case "webview": disableWebViewOptimizations(); break; @@ -209,6 +216,39 @@ public class BackgroundModeExt extends CordovaPlugin { cordova.getActivity().startActivity(intent); } + /** + * Opens the Battery Optimization settings screen + */ + private void openBatterySettings() + { + if (SDK_INT < M) + return; + + Activity activity = cordova.getActivity(); + Intent intent = new Intent(ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); + + cordova.getActivity().startActivity(intent); + } + + /** + * Opens the Battery Optimization settings screen + * + * @param callback The callback to invoke. + */ + private void isIgnoringBatteryOptimizations(CallbackContext callback) + { + if (SDK_INT < M) + return; + + Activity activity = cordova.getActivity(); + String pkgName = activity.getPackageName(); + PowerManager pm = (PowerManager)getService(POWER_SERVICE); + boolean isIgnoring = pm.isIgnoringBatteryOptimizations(pkgName); + PluginResult res = new PluginResult(Status.OK, isIgnoring); + + callback.sendPluginResult(res); + } + /** * Opens the system settings dialog where the user can tweak or turn off any * custom app start settings added by the manufacturer if available. diff --git a/www/background-mode.js b/www/background-mode.js index b1f66a9..c6643b5 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -177,6 +177,38 @@ exports.disableBatteryOptimizations = function() } }; +/** + * Opens the system settings screen for battery optimization, allowing the user to + * manually change the optimization settings. + * + * @return [ Void ] + */ +exports.openBatteryOptimizationsSettings = function() +{ + if (this._isAndroid) + { + cordova.exec(null, null, 'BackgroundModeExt', 'batterysettings', []); + } +}; + +/** + * Opens the system settings screen for battery optimization, allowing the user to + * manually change the optimization settings. + * + * @return [ Void ] + */ +exports.isIgnoringBatteryOptimizations = function(callback) +{ + if (this._isAndroid) + { + cordova.exec(callback, null, 'BackgroundModeExt', 'optimizationstatus', []); + } + else + { + callback(true); + } +}; + /** * Opens the system settings dialog where the user can tweak or turn off any * custom app start settings added by the manufacturer if available.