mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-14 19:44:53 +00:00
More code refactoring
This commit is contained in:
parent
9eb7f9f755
commit
4cd3302853
@ -82,7 +82,7 @@
|
||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||
|
||||
<source-file
|
||||
src="src/android/BackgroundModeExt.java"
|
||||
src="src/android/BackgroundExt.java"
|
||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||
|
||||
<source-file
|
||||
|
@ -35,7 +35,7 @@ import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
class BackgroundModeExt {
|
||||
class BackgroundExt {
|
||||
|
||||
// Weak reference to the cordova interface passed by the plugin
|
||||
private final WeakReference<CordovaInterface> cordova;
|
||||
@ -49,15 +49,44 @@ class BackgroundModeExt {
|
||||
* @param cordova The cordova interface.
|
||||
* @param webView The cordova web view.
|
||||
*/
|
||||
BackgroundModeExt(CordovaInterface cordova, CordovaWebView webView) {
|
||||
private BackgroundExt(CordovaInterface cordova, CordovaWebView webView) {
|
||||
this.cordova = new WeakReference<CordovaInterface>(cordova);
|
||||
this.webView = new WeakReference<CordovaWebView>(webView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the request.
|
||||
*
|
||||
* @param action The action to execute.
|
||||
* @param cordova The cordova interface.
|
||||
* @param webView The cordova web view.
|
||||
*/
|
||||
static void execute(String action, CordovaInterface cordova,
|
||||
CordovaWebView webView) {
|
||||
|
||||
BackgroundExt ext = new BackgroundExt(cordova, webView);
|
||||
|
||||
if (action.equalsIgnoreCase("optimizations")) {
|
||||
ext.disableWebViewOptimizations();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("background")) {
|
||||
ext.moveToBackground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("foreground")) {
|
||||
ext.moveToForeground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("tasklist")) {
|
||||
ext.excludeFromTaskList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move app to background.
|
||||
*/
|
||||
void moveToBackground() {
|
||||
private void moveToBackground() {
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
|
||||
intent.addCategory(Intent.CATEGORY_HOME);
|
||||
@ -67,7 +96,7 @@ class BackgroundModeExt {
|
||||
/**
|
||||
* Move app to foreground.
|
||||
*/
|
||||
void moveToForeground() {
|
||||
private void moveToForeground() {
|
||||
Activity app = getActivity();
|
||||
String pkgName = app.getPackageName();
|
||||
|
||||
@ -75,8 +104,8 @@ class BackgroundModeExt {
|
||||
.getPackageManager()
|
||||
.getLaunchIntentForPackage(pkgName);
|
||||
|
||||
intent.addFlags(
|
||||
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
intent.addFlags( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
|
||||
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
|
||||
app.startActivity(intent);
|
||||
}
|
||||
@ -84,7 +113,7 @@ class BackgroundModeExt {
|
||||
/**
|
||||
* Enable GPS position tracking while in background.
|
||||
*/
|
||||
void disableWebViewOptimizations() {
|
||||
private void disableWebViewOptimizations() {
|
||||
Thread thread = new Thread(){
|
||||
public void run() {
|
||||
try {
|
||||
@ -119,7 +148,7 @@ class BackgroundModeExt {
|
||||
/**
|
||||
* Exclude the app from the recent tasks list.
|
||||
*/
|
||||
void excludeFromTaskList() {
|
||||
private void excludeFromTaskList() {
|
||||
ActivityManager am = (ActivityManager) getActivity()
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
|
@ -60,9 +60,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
// Service that keeps the app awake
|
||||
private ForegroundService service;
|
||||
|
||||
// Plugin extensions
|
||||
private BackgroundModeExt ext;
|
||||
|
||||
// Used to (un)bind the service to with the activity
|
||||
private final ServiceConnection connection = new ServiceConnection() {
|
||||
@Override
|
||||
@ -79,14 +76,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Called after plugin construction and fields have been initialized.
|
||||
*/
|
||||
@Override
|
||||
protected void pluginInitialize() {
|
||||
ext = new BackgroundModeExt(cordova, webView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the request.
|
||||
*
|
||||
@ -109,29 +98,16 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
configure(settings, update);
|
||||
}
|
||||
|
||||
else
|
||||
if (action.equalsIgnoreCase("enable")) {
|
||||
enableMode();
|
||||
}
|
||||
|
||||
else
|
||||
if (action.equalsIgnoreCase("disable")) {
|
||||
disableMode();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("optimizations")) {
|
||||
ext.disableWebViewOptimizations();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("background")) {
|
||||
ext.moveToBackground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("foreground")) {
|
||||
ext.moveToForeground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("tasklist")) {
|
||||
ext.excludeFromTaskList();
|
||||
else {
|
||||
BackgroundExt.execute(action, cordova, webView);
|
||||
}
|
||||
|
||||
callback.success();
|
||||
|
Loading…
Reference in New Issue
Block a user