forked from Github/cordova-plugin-run-in-background
Some fixes and enhancements for Android
This commit is contained in:
@@ -30,23 +30,20 @@ import android.os.IBinder;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;
|
||||
|
||||
import static android.content.Context.BIND_AUTO_CREATE;
|
||||
import static de.appplant.cordova.plugin.background.BackgroundExt.clearKeyguardFlags;
|
||||
|
||||
public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
// Event types for callbacks
|
||||
private enum Event {
|
||||
ACTIVATE, DEACTIVATE, FAILURE
|
||||
}
|
||||
private enum Event { ACTIVATE, DEACTIVATE, FAILURE }
|
||||
|
||||
// Plugin namespace
|
||||
private static final String JS_NAMESPACE =
|
||||
"cordova.plugins.backgroundMode";
|
||||
private static final String JS_NAMESPACE = "cordova.plugins.backgroundMode";
|
||||
|
||||
// Flag indicates if the app is in background or foreground
|
||||
private boolean inBackground = false;
|
||||
@@ -64,26 +61,22 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
private ForegroundService service;
|
||||
|
||||
// Used to (un)bind the service to with the activity
|
||||
private final ServiceConnection connection = new ServiceConnection() {
|
||||
private final ServiceConnection connection = new ServiceConnection()
|
||||
{
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
public void onServiceConnected (ComponentName name, IBinder service)
|
||||
{
|
||||
ForegroundBinder binder = (ForegroundBinder) service;
|
||||
BackgroundMode.this.service = binder.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
public void onServiceDisconnected (ComponentName name)
|
||||
{
|
||||
fireEvent(Event.FAILURE, "'service disconnected'");
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void pluginInitialize() {
|
||||
BackgroundExt.addWindowFlags(cordova.getActivity());
|
||||
}
|
||||
|
||||
// codebeat:disable[ABC]
|
||||
|
||||
/**
|
||||
* Executes the request.
|
||||
*
|
||||
@@ -93,47 +86,68 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* calling back into JavaScript.
|
||||
*
|
||||
* @return Returning false results in a "MethodNotFound" error.
|
||||
*
|
||||
* @throws JSONException
|
||||
*/
|
||||
@Override
|
||||
public boolean execute (String action, JSONArray args,
|
||||
CallbackContext callback) throws JSONException {
|
||||
CallbackContext callback)
|
||||
{
|
||||
boolean validAction = true;
|
||||
|
||||
if (action.equalsIgnoreCase("configure")) {
|
||||
configure(args.getJSONObject(0), args.getBoolean(1));
|
||||
callback.success();
|
||||
return true;
|
||||
switch (action)
|
||||
{
|
||||
case "configure":
|
||||
configure(args.optJSONObject(0), args.optBoolean(1));
|
||||
break;
|
||||
case "enable":
|
||||
enableMode();
|
||||
break;
|
||||
case "disable":
|
||||
disableMode();
|
||||
break;
|
||||
case "optimizations":
|
||||
case "background":
|
||||
case "foreground":
|
||||
case "tasklist":
|
||||
case "dimmed":
|
||||
case "wakeup":
|
||||
case "unlock":
|
||||
new BackgroundExt(this).executeAsync(action, callback);
|
||||
break;
|
||||
default:
|
||||
validAction = false;
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("enable")) {
|
||||
enableMode();
|
||||
if (validAction) {
|
||||
callback.success();
|
||||
return true;
|
||||
} else {
|
||||
callback.error("Invalid action: " + action);
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("disable")) {
|
||||
disableMode();
|
||||
callback.success();
|
||||
return true;
|
||||
}
|
||||
|
||||
BackgroundExt.execute(this, action, callback);
|
||||
return true;
|
||||
return validAction;
|
||||
}
|
||||
|
||||
// codebeat:enable[ABC]
|
||||
|
||||
/**
|
||||
* Called when the system is about to start resuming a previous activity.
|
||||
*
|
||||
* @param multitasking Flag indicating if multitasking is turned on for app.
|
||||
*/
|
||||
@Override
|
||||
public void onPause(boolean multitasking) {
|
||||
super.onPause(multitasking);
|
||||
inBackground = true;
|
||||
startService();
|
||||
public void onPause(boolean multitasking)
|
||||
{
|
||||
try {
|
||||
inBackground = true;
|
||||
startService();
|
||||
} finally {
|
||||
clearKeyguardFlags(cordova.getActivity());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the activity is no longer visible to the user.
|
||||
*/
|
||||
@Override
|
||||
public void onStop () {
|
||||
clearKeyguardFlags(cordova.getActivity());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,8 +156,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* @param multitasking Flag indicating if multitasking is turned on for app.
|
||||
*/
|
||||
@Override
|
||||
public void onResume(boolean multitasking) {
|
||||
super.onResume(multitasking);
|
||||
public void onResume (boolean multitasking)
|
||||
{
|
||||
inBackground = false;
|
||||
stopService();
|
||||
}
|
||||
@@ -152,16 +166,17 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* Called when the activity will be destroyed.
|
||||
*/
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
public void onDestroy()
|
||||
{
|
||||
stopService();
|
||||
super.onDestroy();
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the background mode.
|
||||
*/
|
||||
private void enableMode() {
|
||||
private void enableMode()
|
||||
{
|
||||
isDisabled = false;
|
||||
|
||||
if (inBackground) {
|
||||
@@ -172,7 +187,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
/**
|
||||
* Disable the background mode.
|
||||
*/
|
||||
private void disableMode() {
|
||||
private void disableMode()
|
||||
{
|
||||
stopService();
|
||||
isDisabled = true;
|
||||
}
|
||||
@@ -183,7 +199,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* @param settings The settings
|
||||
* @param update A truthy value means to update the running service.
|
||||
*/
|
||||
private void configure(JSONObject settings, boolean update) {
|
||||
private void configure(JSONObject settings, boolean update)
|
||||
{
|
||||
if (update) {
|
||||
updateNotification(settings);
|
||||
} else {
|
||||
@@ -196,17 +213,15 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
*
|
||||
* @param settings The new default settings
|
||||
*/
|
||||
private void setDefaultSettings(JSONObject settings) {
|
||||
private void setDefaultSettings(JSONObject settings)
|
||||
{
|
||||
defaultSettings = settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* The settings for the new/updated notification.
|
||||
*
|
||||
* @return
|
||||
* updateSettings if set or default settings
|
||||
* Returns the settings for the new/updated notification.
|
||||
*/
|
||||
protected static JSONObject getSettings() {
|
||||
static JSONObject getSettings () {
|
||||
return defaultSettings;
|
||||
}
|
||||
|
||||
@@ -215,7 +230,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
*
|
||||
* @param settings The config settings
|
||||
*/
|
||||
private void updateNotification(JSONObject settings) {
|
||||
private void updateNotification(JSONObject settings)
|
||||
{
|
||||
if (isBind) {
|
||||
service.updateNotification(settings);
|
||||
}
|
||||
@@ -225,7 +241,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* Bind the activity to a background service and put them into foreground
|
||||
* state.
|
||||
*/
|
||||
private void startService() {
|
||||
private void startService()
|
||||
{
|
||||
Activity context = cordova.getActivity();
|
||||
|
||||
if (isDisabled || isBind)
|
||||
@@ -248,12 +265,12 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* Bind the activity to a background service and put them into foreground
|
||||
* state.
|
||||
*/
|
||||
private void stopService() {
|
||||
private void stopService()
|
||||
{
|
||||
Activity context = cordova.getActivity();
|
||||
Intent intent = new Intent(context, ForegroundService.class);
|
||||
|
||||
if (!isBind)
|
||||
return;
|
||||
if (!isBind) return;
|
||||
|
||||
fireEvent(Event.DEACTIVATE, null);
|
||||
context.unbindService(connection);
|
||||
@@ -268,7 +285,8 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
* @param event The name of the event
|
||||
* @param params Optional arguments for the event
|
||||
*/
|
||||
private void fireEvent (Event event, String params) {
|
||||
private void fireEvent (Event event, String params)
|
||||
{
|
||||
String eventName = event.name().toLowerCase();
|
||||
Boolean active = event == Event.ACTIVATE;
|
||||
|
||||
@@ -283,12 +301,7 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
final String js = str;
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
webView.loadUrl("javascript:" + js);
|
||||
}
|
||||
});
|
||||
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user