More code refactoring
This commit is contained in:
parent
9eb7f9f755
commit
4cd3302853
@ -82,7 +82,7 @@
|
|||||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
src="src/android/BackgroundModeExt.java"
|
src="src/android/BackgroundExt.java"
|
||||||
target-dir="src/de/appplant/cordova/plugin/background" />
|
target-dir="src/de/appplant/cordova/plugin/background" />
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
|
@ -35,7 +35,7 @@ import java.lang.ref.WeakReference;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
class BackgroundModeExt {
|
class BackgroundExt {
|
||||||
|
|
||||||
// Weak reference to the cordova interface passed by the plugin
|
// Weak reference to the cordova interface passed by the plugin
|
||||||
private final WeakReference<CordovaInterface> cordova;
|
private final WeakReference<CordovaInterface> cordova;
|
||||||
@ -49,15 +49,44 @@ class BackgroundModeExt {
|
|||||||
* @param cordova The cordova interface.
|
* @param cordova The cordova interface.
|
||||||
* @param webView The cordova web view.
|
* @param webView The cordova web view.
|
||||||
*/
|
*/
|
||||||
BackgroundModeExt(CordovaInterface cordova, CordovaWebView webView) {
|
private BackgroundExt(CordovaInterface cordova, CordovaWebView webView) {
|
||||||
this.cordova = new WeakReference<CordovaInterface>(cordova);
|
this.cordova = new WeakReference<CordovaInterface>(cordova);
|
||||||
this.webView = new WeakReference<CordovaWebView>(webView);
|
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.
|
* Move app to background.
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
@ -67,7 +96,7 @@ class BackgroundModeExt {
|
|||||||
/**
|
/**
|
||||||
* Move app to foreground.
|
* Move app to foreground.
|
||||||
*/
|
*/
|
||||||
void moveToForeground() {
|
private void moveToForeground() {
|
||||||
Activity app = getActivity();
|
Activity app = getActivity();
|
||||||
String pkgName = app.getPackageName();
|
String pkgName = app.getPackageName();
|
||||||
|
|
||||||
@ -75,8 +104,8 @@ class BackgroundModeExt {
|
|||||||
.getPackageManager()
|
.getPackageManager()
|
||||||
.getLaunchIntentForPackage(pkgName);
|
.getLaunchIntentForPackage(pkgName);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
app.startActivity(intent);
|
app.startActivity(intent);
|
||||||
}
|
}
|
||||||
@ -84,7 +113,7 @@ class BackgroundModeExt {
|
|||||||
/**
|
/**
|
||||||
* Enable GPS position tracking while in background.
|
* Enable GPS position tracking while in background.
|
||||||
*/
|
*/
|
||||||
void disableWebViewOptimizations() {
|
private void disableWebViewOptimizations() {
|
||||||
Thread thread = new Thread(){
|
Thread thread = new Thread(){
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
@ -119,7 +148,7 @@ class BackgroundModeExt {
|
|||||||
/**
|
/**
|
||||||
* Exclude the app from the recent tasks list.
|
* Exclude the app from the recent tasks list.
|
||||||
*/
|
*/
|
||||||
void excludeFromTaskList() {
|
private void excludeFromTaskList() {
|
||||||
ActivityManager am = (ActivityManager) getActivity()
|
ActivityManager am = (ActivityManager) getActivity()
|
||||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||||
|
|
@ -60,9 +60,6 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
// Service that keeps the app awake
|
// Service that keeps the app awake
|
||||||
private ForegroundService service;
|
private ForegroundService service;
|
||||||
|
|
||||||
// Plugin extensions
|
|
||||||
private BackgroundModeExt ext;
|
|
||||||
|
|
||||||
// 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
|
||||||
@ -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.
|
* Executes the request.
|
||||||
*
|
*
|
||||||
@ -109,29 +98,16 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
|
|
||||||
configure(settings, update);
|
configure(settings, update);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (action.equalsIgnoreCase("enable")) {
|
if (action.equalsIgnoreCase("enable")) {
|
||||||
enableMode();
|
enableMode();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (action.equalsIgnoreCase("disable")) {
|
if (action.equalsIgnoreCase("disable")) {
|
||||||
disableMode();
|
disableMode();
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if (action.equalsIgnoreCase("optimizations")) {
|
BackgroundExt.execute(action, cordova, webView);
|
||||||
ext.disableWebViewOptimizations();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action.equalsIgnoreCase("background")) {
|
|
||||||
ext.moveToBackground();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action.equalsIgnoreCase("foreground")) {
|
|
||||||
ext.moveToForeground();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action.equalsIgnoreCase("tasklist")) {
|
|
||||||
ext.excludeFromTaskList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callback.success();
|
callback.success();
|
||||||
|
Loading…
Reference in New Issue
Block a user