Merge branch 'master' of https://github.com/katzer/cordova-plugin-background-mode
This commit is contained in:
commit
70495eddb0
@ -30,7 +30,6 @@ import android.content.Intent;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.Window;
|
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
import org.apache.cordova.CordovaInterface;
|
import org.apache.cordova.CordovaInterface;
|
||||||
@ -39,24 +38,29 @@ import org.apache.cordova.CordovaWebView;
|
|||||||
import org.apache.cordova.PluginResult;
|
import org.apache.cordova.PluginResult;
|
||||||
import org.apache.cordova.PluginResult.Status;
|
import org.apache.cordova.PluginResult.Status;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static android.content.Context.ACTIVITY_SERVICE;
|
import static android.content.Context.ACTIVITY_SERVICE;
|
||||||
import static android.content.Context.POWER_SERVICE;
|
import static android.content.Context.POWER_SERVICE;
|
||||||
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
|
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_DISMISS_KEYGUARD;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
|
||||||
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
|
import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements extended functions around the main purpose
|
||||||
|
* of infinite execution in the background.
|
||||||
|
*/
|
||||||
class BackgroundExt {
|
class BackgroundExt {
|
||||||
|
|
||||||
// Weak reference to the cordova interface passed by the plugin
|
// Reference to the cordova interface passed by the plugin
|
||||||
private final WeakReference<CordovaInterface> cordova;
|
private final CordovaInterface cordova;
|
||||||
|
|
||||||
// Weak reference to the cordova web view passed by the plugin
|
// Reference to the cordova web view passed by the plugin
|
||||||
private final WeakReference<CordovaWebView> webView;
|
private final CordovaWebView webView;
|
||||||
|
|
||||||
|
// To keep the device awake
|
||||||
private PowerManager.WakeLock wakeLock;
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,35 +68,24 @@ class BackgroundExt {
|
|||||||
*
|
*
|
||||||
* @param plugin The cordova plugin.
|
* @param plugin The cordova plugin.
|
||||||
*/
|
*/
|
||||||
private BackgroundExt(CordovaPlugin plugin) {
|
BackgroundExt(CordovaPlugin plugin)
|
||||||
this.cordova = new WeakReference<CordovaInterface>(plugin.cordova);
|
{
|
||||||
this.webView = new WeakReference<CordovaWebView>(plugin.webView);
|
this.cordova = plugin.cordova;
|
||||||
|
this.webView = plugin.webView;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the request asynchronous.
|
* Executes the request within a thread.
|
||||||
*
|
*
|
||||||
* @param plugin The cordova plugin.
|
|
||||||
* @param action The action to execute.
|
* @param action The action to execute.
|
||||||
* @param callback The callback context used when
|
* @param callback The callback context used when
|
||||||
* calling back into JavaScript.
|
* calling back into JavaScript.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("UnusedParameters")
|
void executeAsync (String action, CallbackContext callback)
|
||||||
static void execute (CordovaPlugin plugin, final String action,
|
{
|
||||||
final CallbackContext callback) {
|
cordova.getThreadPool().execute(() -> execute(action, callback));
|
||||||
|
|
||||||
final BackgroundExt ext = new BackgroundExt(plugin);
|
|
||||||
|
|
||||||
plugin.cordova.getThreadPool().execute(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
ext.execute(action, callback);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// codebeat:disable[ABC]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the request.
|
* Executes the request.
|
||||||
*
|
*
|
||||||
@ -100,61 +93,61 @@ class BackgroundExt {
|
|||||||
* @param callback The callback context used when
|
* @param callback The callback context used when
|
||||||
* calling back into JavaScript.
|
* calling back into JavaScript.
|
||||||
*/
|
*/
|
||||||
private void execute (String action, CallbackContext callback) {
|
private void execute (String action, CallbackContext callback)
|
||||||
|
{
|
||||||
if (action.equalsIgnoreCase("optimizations")) {
|
switch (action)
|
||||||
disableWebViewOptimizations();
|
{
|
||||||
}
|
case "optimizations":
|
||||||
|
disableWebViewOptimizations();
|
||||||
if (action.equalsIgnoreCase("background")) {
|
break;
|
||||||
moveToBackground();
|
case "background":
|
||||||
}
|
moveToBackground();
|
||||||
|
break;
|
||||||
if (action.equalsIgnoreCase("foreground")) {
|
case "foreground":
|
||||||
moveToForeground();
|
moveToForeground();
|
||||||
}
|
break;
|
||||||
|
case "tasklist":
|
||||||
if (action.equalsIgnoreCase("tasklist")) {
|
excludeFromTaskList();
|
||||||
excludeFromTaskList();
|
break;
|
||||||
}
|
case "dimmed":
|
||||||
|
isDimmed(callback);
|
||||||
if (action.equalsIgnoreCase("dimmed")) {
|
break;
|
||||||
isDimmed(callback);
|
case "wakeup":
|
||||||
}
|
wakeup();
|
||||||
|
break;
|
||||||
if (action.equalsIgnoreCase("wakeup")) {
|
case "unlock":
|
||||||
wakeup();
|
wakeup();
|
||||||
}
|
unlock();
|
||||||
|
break;
|
||||||
if (action.equalsIgnoreCase("unlock")) {
|
|
||||||
wakeup();
|
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// codebeat:enable[ABC]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move app to background.
|
* Moves the app to the background.
|
||||||
*/
|
*/
|
||||||
private void moveToBackground() {
|
private void moveToBackground()
|
||||||
|
{
|
||||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||||
|
|
||||||
intent.addCategory(Intent.CATEGORY_HOME);
|
intent.addCategory(Intent.CATEGORY_HOME);
|
||||||
|
|
||||||
getApp().startActivity(intent);
|
getApp().startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move app to foreground.
|
* Moves the app to the foreground.
|
||||||
*/
|
*/
|
||||||
private void moveToForeground() {
|
private void moveToForeground()
|
||||||
|
{
|
||||||
Activity app = getApp();
|
Activity app = getApp();
|
||||||
Intent intent = getLaunchIntent();
|
Intent intent = getLaunchIntent();
|
||||||
|
|
||||||
intent.addFlags(
|
intent.addFlags(
|
||||||
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
|
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
|
||||||
Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
Intent.FLAG_ACTIVITY_SINGLE_TOP |
|
||||||
|
Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
|
||||||
|
clearSreenAndKeyguardFlags();
|
||||||
app.startActivity(intent);
|
app.startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,18 +159,15 @@ class BackgroundExt {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
getApp().runOnUiThread(new Runnable() {
|
getApp().runOnUiThread(() -> {
|
||||||
@Override
|
View view = webView.getEngine().getView();
|
||||||
public void run() {
|
|
||||||
View view = webView.get().getEngine().getView();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class.forName("org.crosswalk.engine.XWalkCordovaView")
|
Class.forName("org.crosswalk.engine.XWalkCordovaView")
|
||||||
.getMethod("onShow")
|
.getMethod("onShow")
|
||||||
.invoke(view);
|
.invoke(view);
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
view.dispatchWindowVisibilityChanged(View.VISIBLE);
|
view.dispatchWindowVisibilityChanged(View.VISIBLE);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@ -190,13 +180,14 @@ class BackgroundExt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exclude the app from the recent tasks list.
|
* Excludes the app from the recent tasks list.
|
||||||
*/
|
*/
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
private void excludeFromTaskList() {
|
private void excludeFromTaskList()
|
||||||
|
{
|
||||||
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
|
ActivityManager am = (ActivityManager) getService(ACTIVITY_SERVICE);
|
||||||
|
|
||||||
if (am == null || Build.VERSION.SDK_INT < 21)
|
if (am == null || SDK_INT < 21)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
List<AppTask> tasks = am.getAppTasks();
|
List<AppTask> tasks = am.getAppTasks();
|
||||||
@ -208,24 +199,29 @@ class BackgroundExt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoke the callback with information if the screen is on.
|
* Invokes the callback with information if the screen is on.
|
||||||
*
|
*
|
||||||
* @param callback The callback to invoke.
|
* @param callback The callback to invoke.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private void isDimmed(CallbackContext callback) {
|
private void isDimmed (CallbackContext callback)
|
||||||
PluginResult result = new PluginResult(Status.OK, isDimmed());
|
{
|
||||||
callback.sendPluginResult(result);
|
boolean status = isDimmed();
|
||||||
|
PluginResult res = new PluginResult(Status.OK, status);
|
||||||
|
|
||||||
|
callback.sendPluginResult(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the screen is active.
|
* Returns if the screen is active.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private boolean isDimmed() {
|
private boolean isDimmed()
|
||||||
|
{
|
||||||
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 20) {
|
if (SDK_INT < 20)
|
||||||
|
{
|
||||||
return !pm.isScreenOn();
|
return !pm.isScreenOn();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,7 +231,8 @@ class BackgroundExt {
|
|||||||
/**
|
/**
|
||||||
* Wakes up the device if the screen isn't still on.
|
* Wakes up the device if the screen isn't still on.
|
||||||
*/
|
*/
|
||||||
private void wakeup() {
|
private void wakeup()
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
acquireWakeLock();
|
acquireWakeLock();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -246,27 +243,29 @@ class BackgroundExt {
|
|||||||
/**
|
/**
|
||||||
* Unlocks the device even with password protection.
|
* Unlocks the device even with password protection.
|
||||||
*/
|
*/
|
||||||
private void unlock() {
|
private void unlock()
|
||||||
Intent intent = getLaunchIntent();
|
{
|
||||||
getApp().startActivity(intent);
|
addSreenAndKeyguardFlags();
|
||||||
|
getApp().startActivity(getLaunchIntent());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acquire a wake lock to wake up the device.
|
* Acquires a wake lock to wake up the device.
|
||||||
*/
|
*/
|
||||||
private void acquireWakeLock() {
|
@SuppressWarnings("deprecation")
|
||||||
|
private void acquireWakeLock()
|
||||||
|
{
|
||||||
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
PowerManager pm = (PowerManager) getService(POWER_SERVICE);
|
||||||
|
|
||||||
releaseWakeLock();
|
releaseWakeLock();
|
||||||
|
|
||||||
if (!isDimmed()) {
|
if (!isDimmed())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
|
int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
|
||||||
PowerManager.ACQUIRE_CAUSES_WAKEUP;
|
PowerManager.ACQUIRE_CAUSES_WAKEUP;
|
||||||
|
|
||||||
wakeLock = pm.newWakeLock(level, "BackgroundModeExt");
|
wakeLock = pm.newWakeLock(level, "backgroundmode:wakelock");
|
||||||
wakeLock.setReferenceCounted(false);
|
wakeLock.setReferenceCounted(false);
|
||||||
wakeLock.acquire(1000);
|
wakeLock.acquire(1000);
|
||||||
}
|
}
|
||||||
@ -274,7 +273,8 @@ class BackgroundExt {
|
|||||||
/**
|
/**
|
||||||
* Releases the previously acquire wake lock.
|
* Releases the previously acquire wake lock.
|
||||||
*/
|
*/
|
||||||
private void releaseWakeLock() {
|
private void releaseWakeLock()
|
||||||
|
{
|
||||||
if (wakeLock != null && wakeLock.isHeld()) {
|
if (wakeLock != null && wakeLock.isHeld()) {
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
wakeLock = null;
|
wakeLock = null;
|
||||||
@ -282,36 +282,41 @@ class BackgroundExt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add required flags to the window to unlock/wakeup the device.
|
* Adds required flags to the window to unlock/wakeup the device.
|
||||||
*/
|
*/
|
||||||
static void addWindowFlags(Activity app) {
|
private void addSreenAndKeyguardFlags()
|
||||||
final Window window = app.getWindow();
|
{
|
||||||
|
getApp().runOnUiThread(() -> getApp().getWindow().addFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
||||||
app.runOnUiThread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
window.addFlags(
|
|
||||||
FLAG_ALLOW_LOCK_WHILE_SCREEN_ON |
|
|
||||||
FLAG_SHOW_WHEN_LOCKED |
|
|
||||||
FLAG_TURN_SCREEN_ON |
|
|
||||||
FLAG_DISMISS_KEYGUARD
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The activity referenced by cordova.
|
* Clears required flags to the window to unlock/wakeup the device.
|
||||||
*
|
*/
|
||||||
* @return The main activity of the app.
|
private void clearSreenAndKeyguardFlags()
|
||||||
|
{
|
||||||
|
getApp().runOnUiThread(() -> getApp().getWindow().clearFlags(FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes required flags to the window to unlock/wakeup the device.
|
||||||
|
*/
|
||||||
|
static void clearKeyguardFlags (Activity app)
|
||||||
|
{
|
||||||
|
app.runOnUiThread(() -> app.getWindow().clearFlags(FLAG_DISMISS_KEYGUARD));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the activity referenced by cordova.
|
||||||
*/
|
*/
|
||||||
Activity getApp() {
|
Activity getApp() {
|
||||||
return cordova.get().getActivity();
|
return cordova.getActivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The launch intent for the main activity.
|
* Gets the launch intent for the main activity.
|
||||||
*/
|
*/
|
||||||
private Intent getLaunchIntent() {
|
private Intent getLaunchIntent()
|
||||||
|
{
|
||||||
Context app = getApp().getApplicationContext();
|
Context app = getApp().getApplicationContext();
|
||||||
String pkgName = app.getPackageName();
|
String pkgName = app.getPackageName();
|
||||||
|
|
||||||
@ -322,11 +327,9 @@ class BackgroundExt {
|
|||||||
* Get the requested system service by name.
|
* Get the requested system service by name.
|
||||||
*
|
*
|
||||||
* @param name The name of the service.
|
* @param name The name of the service.
|
||||||
*
|
|
||||||
* @return The service instance.
|
|
||||||
*/
|
*/
|
||||||
private Object getService(String name) {
|
private Object getService(String name)
|
||||||
|
{
|
||||||
return getApp().getSystemService(name);
|
return getApp().getSystemService(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -31,25 +31,22 @@ import org.apache.cordova.CordovaInterface;
|
|||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
import org.apache.cordova.CordovaWebView;
|
import org.apache.cordova.CordovaWebView;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;
|
import de.appplant.cordova.plugin.background.ForegroundService.ForegroundBinder;
|
||||||
|
|
||||||
import static android.content.Context.BIND_AUTO_CREATE;
|
import static android.content.Context.BIND_AUTO_CREATE;
|
||||||
|
import static de.appplant.cordova.plugin.background.BackgroundExt.clearKeyguardFlags;
|
||||||
|
|
||||||
public class BackgroundMode extends CordovaPlugin {
|
public class BackgroundMode extends CordovaPlugin {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Event types for callbacks
|
// Event types for callbacks
|
||||||
private enum Event {
|
private enum Event { ACTIVATE, DEACTIVATE, FAILURE }
|
||||||
ACTIVATE, DEACTIVATE, FAILURE
|
|
||||||
}
|
|
||||||
|
|
||||||
// Plugin namespace
|
// Plugin namespace
|
||||||
private static final String JS_NAMESPACE =
|
private static final String JS_NAMESPACE = "cordova.plugins.backgroundMode";
|
||||||
"cordova.plugins.backgroundMode";
|
|
||||||
|
|
||||||
// Flag indicates if the app is in background or foreground
|
// Flag indicates if the app is in background or foreground
|
||||||
private boolean inBackground = false;
|
private boolean inBackground = false;
|
||||||
@ -67,15 +64,18 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
private ForegroundService service;
|
private ForegroundService service;
|
||||||
|
|
||||||
// Used to (un)bind the service to with the activity
|
// Used to (un)bind the service to with the activity
|
||||||
private final ServiceConnection connection = new ServiceConnection() {
|
private final ServiceConnection connection = new ServiceConnection()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected (ComponentName name, IBinder service)
|
||||||
|
{
|
||||||
ForegroundBinder binder = (ForegroundBinder) service;
|
ForegroundBinder binder = (ForegroundBinder) service;
|
||||||
BackgroundMode.this.service = binder.getService();
|
BackgroundMode.this.service = binder.getService();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceDisconnected(ComponentName name) {
|
public void onServiceDisconnected (ComponentName name)
|
||||||
|
{
|
||||||
fireEvent(Event.FAILURE, "'service disconnected'");
|
fireEvent(Event.FAILURE, "'service disconnected'");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -102,9 +102,6 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
protected void pluginInitialize() {
|
protected void pluginInitialize() {
|
||||||
BackgroundExt.addWindowFlags(cordova.getActivity());
|
BackgroundExt.addWindowFlags(cordova.getActivity());
|
||||||
}
|
}
|
||||||
|
|
||||||
// codebeat:disable[ABC]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the request.
|
* Executes the request.
|
||||||
*
|
*
|
||||||
@ -114,47 +111,68 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* calling back into JavaScript.
|
* calling back into JavaScript.
|
||||||
*
|
*
|
||||||
* @return Returning false results in a "MethodNotFound" error.
|
* @return Returning false results in a "MethodNotFound" error.
|
||||||
*
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean execute (String action, JSONArray args,
|
public boolean execute (String action, JSONArray args,
|
||||||
CallbackContext callback) throws JSONException {
|
CallbackContext callback)
|
||||||
|
{
|
||||||
|
boolean validAction = true;
|
||||||
|
|
||||||
if (action.equalsIgnoreCase("configure")) {
|
switch (action)
|
||||||
configure(args.getJSONObject(0), args.getBoolean(1));
|
{
|
||||||
callback.success();
|
case "configure":
|
||||||
return true;
|
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")) {
|
if (validAction) {
|
||||||
enableMode();
|
|
||||||
callback.success();
|
callback.success();
|
||||||
return true;
|
} else {
|
||||||
|
callback.error("Invalid action: " + action);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action.equalsIgnoreCase("disable")) {
|
return validAction;
|
||||||
disableMode();
|
|
||||||
callback.success();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
BackgroundExt.execute(this, action, callback);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// codebeat:enable[ABC]
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the system is about to start resuming a previous activity.
|
* Called when the system is about to start resuming a previous activity.
|
||||||
*
|
*
|
||||||
* @param multitasking Flag indicating if multitasking is turned on for app.
|
* @param multitasking Flag indicating if multitasking is turned on for app.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onPause(boolean multitasking) {
|
public void onPause(boolean multitasking)
|
||||||
super.onPause(multitasking);
|
{
|
||||||
inBackground = true;
|
try {
|
||||||
startService();
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -163,8 +181,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* @param multitasking Flag indicating if multitasking is turned on for app.
|
* @param multitasking Flag indicating if multitasking is turned on for app.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onResume(boolean multitasking) {
|
public void onResume (boolean multitasking)
|
||||||
super.onResume(multitasking);
|
{
|
||||||
inBackground = false;
|
inBackground = false;
|
||||||
stopService();
|
stopService();
|
||||||
}
|
}
|
||||||
@ -173,16 +191,17 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* Called when the activity will be destroyed.
|
* Called when the activity will be destroyed.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy()
|
||||||
|
{
|
||||||
stopService();
|
stopService();
|
||||||
super.onDestroy();
|
|
||||||
android.os.Process.killProcess(android.os.Process.myPid());
|
android.os.Process.killProcess(android.os.Process.myPid());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable the background mode.
|
* Enable the background mode.
|
||||||
*/
|
*/
|
||||||
private void enableMode() {
|
private void enableMode()
|
||||||
|
{
|
||||||
isDisabled = false;
|
isDisabled = false;
|
||||||
|
|
||||||
if (inBackground) {
|
if (inBackground) {
|
||||||
@ -193,7 +212,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
/**
|
/**
|
||||||
* Disable the background mode.
|
* Disable the background mode.
|
||||||
*/
|
*/
|
||||||
private void disableMode() {
|
private void disableMode()
|
||||||
|
{
|
||||||
stopService();
|
stopService();
|
||||||
isDisabled = true;
|
isDisabled = true;
|
||||||
}
|
}
|
||||||
@ -204,7 +224,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* @param settings The settings
|
* @param settings The settings
|
||||||
* @param update A truthy value means to update the running service.
|
* @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) {
|
if (update) {
|
||||||
updateNotification(settings);
|
updateNotification(settings);
|
||||||
} else {
|
} else {
|
||||||
@ -217,17 +238,15 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
*
|
*
|
||||||
* @param settings The new default settings
|
* @param settings The new default settings
|
||||||
*/
|
*/
|
||||||
private void setDefaultSettings(JSONObject settings) {
|
private void setDefaultSettings(JSONObject settings)
|
||||||
|
{
|
||||||
defaultSettings = settings;
|
defaultSettings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The settings for the new/updated notification.
|
* Returns the settings for the new/updated notification.
|
||||||
*
|
|
||||||
* @return
|
|
||||||
* updateSettings if set or default settings
|
|
||||||
*/
|
*/
|
||||||
protected static JSONObject getSettings() {
|
static JSONObject getSettings () {
|
||||||
return defaultSettings;
|
return defaultSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +255,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
*
|
*
|
||||||
* @param settings The config settings
|
* @param settings The config settings
|
||||||
*/
|
*/
|
||||||
private void updateNotification(JSONObject settings) {
|
private void updateNotification(JSONObject settings)
|
||||||
|
{
|
||||||
if (isBind) {
|
if (isBind) {
|
||||||
service.updateNotification(settings);
|
service.updateNotification(settings);
|
||||||
}
|
}
|
||||||
@ -246,7 +266,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* Bind the activity to a background service and put them into foreground
|
* Bind the activity to a background service and put them into foreground
|
||||||
* state.
|
* state.
|
||||||
*/
|
*/
|
||||||
private void startService() {
|
private void startService()
|
||||||
|
{
|
||||||
Activity context = cordova.getActivity();
|
Activity context = cordova.getActivity();
|
||||||
|
|
||||||
if (isDisabled || isBind)
|
if (isDisabled || isBind)
|
||||||
@ -269,12 +290,12 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* Bind the activity to a background service and put them into foreground
|
* Bind the activity to a background service and put them into foreground
|
||||||
* state.
|
* state.
|
||||||
*/
|
*/
|
||||||
private void stopService() {
|
private void stopService()
|
||||||
|
{
|
||||||
Activity context = cordova.getActivity();
|
Activity context = cordova.getActivity();
|
||||||
Intent intent = new Intent(context, ForegroundService.class);
|
Intent intent = new Intent(context, ForegroundService.class);
|
||||||
|
|
||||||
if (!isBind)
|
if (!isBind) return;
|
||||||
return;
|
|
||||||
|
|
||||||
fireEvent(Event.DEACTIVATE, null);
|
fireEvent(Event.DEACTIVATE, null);
|
||||||
context.unbindService(connection);
|
context.unbindService(connection);
|
||||||
@ -289,7 +310,8 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
* @param event The name of the event
|
* @param event The name of the event
|
||||||
* @param params Optional arguments for 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();
|
String eventName = event.name().toLowerCase();
|
||||||
Boolean active = event == Event.ACTIVATE;
|
Boolean active = event == Event.ACTIVATE;
|
||||||
|
|
||||||
@ -304,12 +326,7 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
|
|
||||||
final String js = str;
|
final String js = str;
|
||||||
|
|
||||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
cordova.getActivity().runOnUiThread(() -> webView.loadUrl("javascript:" + js));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
webView.loadUrl("javascript:" + js);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2013-2017 appPlant GmbH
|
Copyright 2013-2017 appPlant GmbH
|
||||||
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
or more contributor license agreements. See the NOTICE file
|
or more contributor license agreements. See the NOTICE file
|
||||||
distributed with this work for additional information
|
distributed with this work for additional information
|
||||||
@ -7,7 +8,9 @@
|
|||||||
to you under the Apache License, Version 2.0 (the
|
to you under the Apache License, Version 2.0 (the
|
||||||
"License"); you may not use this file except in compliance
|
"License"); you may not use this file except in compliance
|
||||||
with the License. You may obtain a copy of the License at
|
with the License. You may obtain a copy of the License at
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing,
|
Unless required by applicable law or agreed to in writing,
|
||||||
software distributed under the License is distributed on an
|
software distributed under the License is distributed on an
|
||||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
@ -18,6 +21,7 @@
|
|||||||
|
|
||||||
package de.appplant.cordova.plugin.background;
|
package de.appplant.cordova.plugin.background;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.*;
|
import android.app.*;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@ -55,7 +59,7 @@ public class ForegroundService extends Service {
|
|||||||
private static final String NOTIFICATION_ICON = "icon";
|
private static final String NOTIFICATION_ICON = "icon";
|
||||||
|
|
||||||
// Binder given to clients
|
// Binder given to clients
|
||||||
private final IBinder mBinder = new ForegroundBinder();
|
private final IBinder binder = new ForegroundBinder();
|
||||||
|
|
||||||
// Partial wake lock to prevent the app from going to sleep when locked
|
// Partial wake lock to prevent the app from going to sleep when locked
|
||||||
private PowerManager.WakeLock wakeLock;
|
private PowerManager.WakeLock wakeLock;
|
||||||
@ -65,15 +69,17 @@ public class ForegroundService extends Service {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind (Intent intent) {
|
public IBinder onBind (Intent intent) {
|
||||||
return mBinder;
|
return binder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class used for the client Binder. Because we know this service always
|
* Class used for the client Binder. Because we know this service always
|
||||||
* runs in the same process as its clients, we don't need to deal with IPC.
|
* runs in the same process as its clients, we don't need to deal with IPC.
|
||||||
*/
|
*/
|
||||||
public class ForegroundBinder extends Binder {
|
class ForegroundBinder extends Binder
|
||||||
ForegroundService getService() {
|
{
|
||||||
|
ForegroundService getService()
|
||||||
|
{
|
||||||
// Return this instance of ForegroundService
|
// Return this instance of ForegroundService
|
||||||
// so clients can call public methods
|
// so clients can call public methods
|
||||||
return ForegroundService.this;
|
return ForegroundService.this;
|
||||||
@ -85,7 +91,8 @@ public class ForegroundService extends Service {
|
|||||||
* by the OS.
|
* by the OS.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onCreate () {
|
public void onCreate()
|
||||||
|
{
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
keepAwake();
|
keepAwake();
|
||||||
}
|
}
|
||||||
@ -94,16 +101,27 @@ public class ForegroundService extends Service {
|
|||||||
* No need to run headless on destroy.
|
* No need to run headless on destroy.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy()
|
||||||
|
{
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
sleepWell();
|
sleepWell();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prevent Android from stopping the background service automatically.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int onStartCommand (Intent intent, int flags, int startId) {
|
||||||
|
return START_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put the service in a foreground state to prevent app from being killed
|
* Put the service in a foreground state to prevent app from being killed
|
||||||
* by the OS.
|
* by the OS.
|
||||||
*/
|
*/
|
||||||
private void keepAwake() {
|
@SuppressLint("WakelockTimeout")
|
||||||
|
private void keepAwake()
|
||||||
|
{
|
||||||
JSONObject settings = BackgroundMode.getSettings();
|
JSONObject settings = BackgroundMode.getSettings();
|
||||||
boolean isSilent = settings.optBoolean("silent", false);
|
boolean isSilent = settings.optBoolean("silent", false);
|
||||||
|
|
||||||
@ -111,11 +129,10 @@ public class ForegroundService extends Service {
|
|||||||
startForeground(NOTIFICATION_ID, makeNotification());
|
startForeground(NOTIFICATION_ID, makeNotification());
|
||||||
}
|
}
|
||||||
|
|
||||||
PowerManager pm = (PowerManager)
|
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
|
||||||
getSystemService(POWER_SERVICE);
|
|
||||||
|
|
||||||
wakeLock = pm.newWakeLock(
|
wakeLock = pm.newWakeLock(
|
||||||
PARTIAL_WAKE_LOCK, "BackgroundMode");
|
PARTIAL_WAKE_LOCK, "backgroundmode:wakelock");
|
||||||
|
|
||||||
wakeLock.acquire();
|
wakeLock.acquire();
|
||||||
}
|
}
|
||||||
@ -123,7 +140,8 @@ public class ForegroundService extends Service {
|
|||||||
/**
|
/**
|
||||||
* Stop background mode.
|
* Stop background mode.
|
||||||
*/
|
*/
|
||||||
private void sleepWell() {
|
private void sleepWell()
|
||||||
|
{
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
getNotificationManager().cancel(NOTIFICATION_ID);
|
getNotificationManager().cancel(NOTIFICATION_ID);
|
||||||
|
|
||||||
@ -137,7 +155,8 @@ public class ForegroundService extends Service {
|
|||||||
* Create a notification as the visible part to be able to put the service
|
* Create a notification as the visible part to be able to put the service
|
||||||
* in a foreground state by using the default settings.
|
* in a foreground state by using the default settings.
|
||||||
*/
|
*/
|
||||||
private Notification makeNotification() {
|
private Notification makeNotification()
|
||||||
|
{
|
||||||
return makeNotification(BackgroundMode.getSettings());
|
return makeNotification(BackgroundMode.getSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +166,8 @@ public class ForegroundService extends Service {
|
|||||||
*
|
*
|
||||||
* @param settings The config settings
|
* @param settings The config settings
|
||||||
*/
|
*/
|
||||||
private Notification makeNotification(JSONObject settings) {
|
private Notification makeNotification (JSONObject settings)
|
||||||
|
{
|
||||||
// use channelid for Oreo and higher
|
// use channelid for Oreo and higher
|
||||||
String CHANNEL_ID = "cordova-plugin-background-mode-id";
|
String CHANNEL_ID = "cordova-plugin-background-mode-id";
|
||||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
@ -223,7 +243,8 @@ public class ForegroundService extends Service {
|
|||||||
*
|
*
|
||||||
* @param settings The config settings
|
* @param settings The config settings
|
||||||
*/
|
*/
|
||||||
protected void updateNotification (JSONObject settings) {
|
protected void updateNotification (JSONObject settings)
|
||||||
|
{
|
||||||
boolean isSilent = settings.optBoolean("silent", false);
|
boolean isSilent = settings.optBoolean("silent", false);
|
||||||
|
|
||||||
if (isSilent) {
|
if (isSilent) {
|
||||||
@ -242,7 +263,6 @@ public class ForegroundService extends Service {
|
|||||||
* @param name Name of the resource to return
|
* @param name Name of the resource to return
|
||||||
*/
|
*/
|
||||||
private int getIconResId(String name) {
|
private int getIconResId(String name) {
|
||||||
// cordova-android 6 uses mipmaps
|
|
||||||
int resId = getIconResId(name, "mipmap");
|
int resId = getIconResId(name, "mipmap");
|
||||||
|
|
||||||
if (resId == 0) {
|
if (resId == 0) {
|
||||||
@ -280,7 +300,8 @@ public class ForegroundService extends Service {
|
|||||||
*
|
*
|
||||||
* @return The resource id or 0 if not found.
|
* @return The resource id or 0 if not found.
|
||||||
*/
|
*/
|
||||||
private int getIconResId(String icon, String type) {
|
private int getIconResId (String icon, String type)
|
||||||
|
{
|
||||||
Resources res = getResources();
|
Resources res = getResources();
|
||||||
String pkgName = getPackageName();
|
String pkgName = getPackageName();
|
||||||
|
|
||||||
@ -311,9 +332,10 @@ public class ForegroundService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shared manager for the notification service.
|
* Returns the shared notification service manager.
|
||||||
*/
|
*/
|
||||||
private NotificationManager getNotificationManager() {
|
private NotificationManager getNotificationManager()
|
||||||
|
{
|
||||||
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user