Restructure BackgroundModeExt as an internal cordova plugin

This commit is contained in:
Sebastián Katzer 2019-02-11 15:21:06 +01:00
parent 8fbf210e14
commit 2c4df332d3
7 changed files with 111 additions and 138 deletions

View File

@ -60,8 +60,10 @@
<platform name="android"> <platform name="android">
<config-file target="res/xml/config.xml" parent="/*"> <config-file target="res/xml/config.xml" parent="/*">
<feature name="BackgroundMode"> <feature name="BackgroundMode">
<param name="android-package" <param name="android-package" value="de.appplant.cordova.plugin.background.BackgroundMode"/>
value="de.appplant.cordova.plugin.background.BackgroundMode"/> </feature>
<feature name="BackgroundModeExt">
<param name="android-package" value="de.appplant.cordova.plugin.background.BackgroundModeExt"/>
</feature> </feature>
</config-file> </config-file>
@ -82,7 +84,7 @@
target-dir="src/de/appplant/cordova/plugin/background" /> target-dir="src/de/appplant/cordova/plugin/background" />
<source-file <source-file
src="src/android/BackgroundExt.java" src="src/android/BackgroundModeExt.java"
target-dir="src/de/appplant/cordova/plugin/background" /> target-dir="src/de/appplant/cordova/plugin/background" />
<source-file <source-file

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2013-2017 appPlant GmbH Copyright 2013 Sebastián Katzer
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
@ -35,7 +35,7 @@ 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; import static de.appplant.cordova.plugin.background.BackgroundModeExt.clearKeyguardFlags;
public class BackgroundMode extends CordovaPlugin { public class BackgroundMode extends CordovaPlugin {
@ -104,17 +104,6 @@ public class BackgroundMode extends CordovaPlugin {
case "disable": case "disable":
disableMode(); disableMode();
break; break;
case "webviewoptimizations":
case "batteryoptimizations":
case "appstart":
case "background":
case "foreground":
case "tasklist":
case "dimmed":
case "wakeup":
case "unlock":
BackgroundExt.execute(this, action, args, callback);
break;
default: default:
validAction = false; validAction = false;
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2013-2017 appPlant GmbH Copyright 2013 Sebastián Katzer
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
@ -37,9 +37,7 @@ import android.os.PowerManager;
import android.view.View; import android.view.View;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
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 org.json.JSONArray; import org.json.JSONArray;
@ -66,7 +64,7 @@ import static android.view.WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
* Implements extended functions around the main purpose * Implements extended functions around the main purpose
* of infinite execution in the background. * of infinite execution in the background.
*/ */
class BackgroundExt { public class BackgroundModeExt extends CordovaPlugin {
// List of intents for various manufactures to adjust the power saver mode. // List of intents for various manufactures to adjust the power saver mode.
private static final List<Intent> APPSTART_INTENTS = Arrays.asList( private static final List<Intent> APPSTART_INTENTS = Arrays.asList(
@ -91,40 +89,9 @@ class BackgroundExt {
new Intent().setComponent(new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity")) new Intent().setComponent(new ComponentName("com.zui.safecenter", "com.lenovo.safecenter.MainTab.LeSafeMainActivity"))
); );
// Reference to the cordova interface passed by the plugin
private final CordovaInterface cordova;
// Reference to the cordova web view passed by the plugin
private final CordovaWebView webView;
// To keep the device awake // To keep the device awake
private PowerManager.WakeLock wakeLock; private PowerManager.WakeLock wakeLock;
/**
* Initialize the extension to perform non-background related tasks.
*
* @param plugin The cordova plugin.
*/
private BackgroundExt (CordovaPlugin plugin)
{
this.cordova = plugin.cordova;
this.webView = plugin.webView;
}
/**
* Executes the request within a thread.
*
* @param action The action to execute.
* @param args The exec() arguments.
* @param callback The callback context used when
* calling back into JavaScript.
*/
static void execute (CordovaPlugin plugin, String action, JSONArray args,
CallbackContext callback)
{
plugin.cordova.getThreadPool().execute(() -> new BackgroundExt(plugin).execute(action, args, callback));
}
/** /**
* Executes the request. * Executes the request.
* *
@ -132,16 +99,21 @@ class BackgroundExt {
* @param args The exec() arguments. * @param args The exec() arguments.
* @param callback The callback context used when * @param callback The callback context used when
* calling back into JavaScript. * calling back into JavaScript.
*
* @return Returning false results in a "MethodNotFound" error.
*/ */
private void execute (String action, JSONArray args, @Override
public boolean execute (String action, JSONArray args,
CallbackContext callback) CallbackContext callback)
{ {
boolean validAction = true;
switch (action) switch (action)
{ {
case "batteryoptimizations": case "battery":
disableBatteryOptimizations(); disableBatteryOptimizations();
break; break;
case "webviewoptimizations": case "webview":
disableWebViewOptimizations(); disableWebViewOptimizations();
break; break;
case "appstart": case "appstart":
@ -166,7 +138,17 @@ class BackgroundExt {
wakeup(); wakeup();
unlock(); unlock();
break; break;
default:
validAction = false;
} }
if (validAction) {
callback.success();
} else {
callback.error("Invalid action: " + action);
}
return validAction;
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2013-2017 appPlant GmbH Copyright 2013 Sebastián Katzer
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

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2013-2017 appPlant GmbH Copyright 2013 Sebastián Katzer
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

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2013-2017 appPlant GmbH Copyright 2013 Sebastián Katzer
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

View File

@ -160,7 +160,7 @@ exports.disableWebViewOptimizations = function()
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'webviewoptimizations', []); cordova.exec(null, null, 'BackgroundModeExt', 'webview', []);
} }
}; };
@ -173,7 +173,7 @@ exports.disableBatteryOptimizations = function()
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'batteryoptimizations', []); cordova.exec(null, null, 'BackgroundModeExt', 'battery', []);
} }
}; };
@ -190,7 +190,7 @@ exports.openAppStartSettings = function (options)
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'appstart', [options]); cordova.exec(null, null, 'BackgroundModeExt', 'appstart', [options]);
} }
}; };
@ -203,7 +203,7 @@ exports.moveToBackground = function()
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'background', []); cordova.exec(null, null, 'BackgroundModeExt', 'background', []);
} }
}; };
@ -216,7 +216,7 @@ exports.moveToForeground = function()
{ {
if (this.isActive() && this._isAndroid) if (this.isActive() && this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'foreground', []); cordova.exec(null, null, 'BackgroundModeExt', 'foreground', []);
} }
}; };
@ -229,7 +229,7 @@ exports.excludeFromTaskList = function()
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); cordova.exec(null, null, 'BackgroundModeExt', 'tasklist', []);
} }
}; };
@ -257,7 +257,7 @@ exports.isScreenOff = function (fn)
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(fn, null, 'BackgroundMode', 'dimmed', []); cordova.exec(fn, null, 'BackgroundModeExt', 'dimmed', []);
} }
else else
{ {
@ -274,7 +274,7 @@ exports.wakeUp = function()
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'wakeup', []); cordova.exec(null, null, 'BackgroundModeExt', 'wakeup', []);
} }
}; };
@ -287,7 +287,7 @@ exports.unlock = function()
{ {
if (this._isAndroid) if (this._isAndroid)
{ {
cordova.exec(null, null, 'BackgroundMode', 'unlock', []); cordova.exec(null, null, 'BackgroundModeExt', 'unlock', []);
} }
}; };