Add fix for moveToForeground. Requires new permission for Android 10

This commit is contained in:
Nathan Kerr 2020-06-08 12:19:04 -07:00
parent 5f50704564
commit 4d98947310
4 changed files with 33 additions and 1 deletions

View File

@ -79,7 +79,9 @@ cordova.plugins.backgroundMode.un('EVENT', function);
## Android specifics ## Android specifics
### Transit between application states ### 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 ```js
cordova.plugins.backgroundMode.moveToBackground(); cordova.plugins.backgroundMode.moveToBackground();

View File

@ -78,6 +78,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
</config-file> </config-file>
<source-file <source-file

View File

@ -34,6 +34,7 @@ import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.provider.Settings;
import android.view.View; import android.view.View;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
@ -109,6 +110,9 @@ public class BackgroundModeExt extends CordovaPlugin {
case "foreground": case "foreground":
moveToForeground(); moveToForeground();
break; break;
case "requestTopPermissions":
requestTopPermissions();
break;
case "tasklist": case "tasklist":
excludeFromTaskList(); excludeFromTaskList();
break; break;
@ -249,6 +253,20 @@ public class BackgroundModeExt extends CordovaPlugin {
callback.sendPluginResult(res); callback.sendPluginResult(res);
} }
private void requestTopPermissions() {
if (SDK_INT >= 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 * Opens the system settings dialog where the user can tweak or turn off any
* custom app start settings added by the manufacturer if available. * custom app start settings added by the manufacturer if available.

View File

@ -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). * Exclude the app from the recent tasks list (Android only).
* *