diff --git a/plugin.xml b/plugin.xml index e96c2bd..6075035 100644 --- a/plugin.xml +++ b/plugin.xml @@ -82,7 +82,7 @@ target-dir="src/de/appplant/cordova/plugin/background" /> 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(cordova); this.webView = new WeakReference(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); diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 5551ff7..aebb3ca 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -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();