From 4d98947310431433e29695bef93a13e519621fc3 Mon Sep 17 00:00:00 2001 From: Nathan Kerr Date: Mon, 8 Jun 2020 12:19:04 -0700 Subject: [PATCH] Add fix for moveToForeground. Requires new permission for Android 10 --- README.md | 4 +++- plugin.xml | 1 + src/android/BackgroundModeExt.java | 18 ++++++++++++++++++ www/background-mode.js | 11 +++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b7fbf4a..ed06f01 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,9 @@ cordova.plugins.backgroundMode.un('EVENT', function); ## Android specifics ### Transit between application states -Android allows to programmatically move from foreground to background or vice versa. +Android allows to programmatically move from foreground to background or vice versa. + +Note: starting with Android 10, you must request the "Draw on Top" permission from the user or the call to `moveToForeground` will silently fail. You can request it with `cordova.plugins.backgroundMode.requestForegroundPermission();`. This permission isn't necessary for `moveToBackground` ```js cordova.plugins.backgroundMode.moveToBackground(); diff --git a/plugin.xml b/plugin.xml index f4b8275..0a6cee4 100644 --- a/plugin.xml +++ b/plugin.xml @@ -78,6 +78,7 @@ + = M) { + + Activity activity = cordova.getActivity(); + if (Settings.canDrawOverlays(activity.getApplicationContext())) { + return; + } + + String pkgName = activity.getPackageName(); + Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + pkgName)); + activity.startActivity(intent); + } + } + /** * 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 c6643b5..4464425 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -252,6 +252,17 @@ exports.moveToForeground = function() } }; +/** + * Requests permission to "draw on top" which is necessary for the "moveToForeground" method in Android 10+ + * + * @return [ Void ] + */ +exports.requestForegroundPermission = function() { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundModeExt', 'requestTopPermissions', []); + } +}; + /** * Exclude the app from the recent tasks list (Android only). *