forked from Github/cordova-plugin-run-in-background
Code refactoring
This commit is contained in:
@@ -22,14 +22,11 @@
|
||||
package de.appplant.cordova.plugin.background;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.view.View;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
@@ -37,9 +34,6 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
public class BackgroundMode extends CordovaPlugin {
|
||||
|
||||
// Event types for callbacks
|
||||
@@ -66,6 +60,9 @@ 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
|
||||
@@ -82,6 +79,14 @@ 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.
|
||||
*
|
||||
@@ -105,22 +110,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
configure(settings, update);
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("disableWebViewOptimizations")) {
|
||||
disableWebViewOptimizations();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("background")) {
|
||||
moveToBackground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("foreground")) {
|
||||
moveToForeground();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("tasklist")) {
|
||||
excludeFromTaskList();
|
||||
}
|
||||
|
||||
if (action.equalsIgnoreCase("enable")) {
|
||||
enableMode();
|
||||
}
|
||||
@@ -129,6 +118,22 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
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();
|
||||
}
|
||||
|
||||
callback.success();
|
||||
|
||||
return true;
|
||||
@@ -167,33 +172,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
stopService();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move app to background.
|
||||
*/
|
||||
private void moveToBackground() {
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
|
||||
intent.addCategory(Intent.CATEGORY_HOME);
|
||||
cordova.getActivity().startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move app to foreground.
|
||||
*/
|
||||
private void moveToForeground() {
|
||||
Context context = cordova.getActivity();
|
||||
String pkgName = context.getPackageName();
|
||||
|
||||
Intent intent = context
|
||||
.getPackageManager()
|
||||
.getLaunchIntentForPackage(pkgName);
|
||||
|
||||
intent.addFlags(
|
||||
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the background mode.
|
||||
*/
|
||||
@@ -305,68 +283,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
isBind = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable GPS position tracking while in background.
|
||||
*/
|
||||
private void disableWebViewOptimizations() {
|
||||
Thread thread = new Thread(){
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
View view = webView.getEngine().getView();
|
||||
|
||||
try {
|
||||
Class<?> xWalkCls = Class.forName(
|
||||
"org.crosswalk.engine.XWalkCordovaView");
|
||||
|
||||
Method onShowMethod =
|
||||
xWalkCls.getMethod("onShow");
|
||||
|
||||
onShowMethod.invoke(view);
|
||||
} catch (Exception e){
|
||||
view.dispatchWindowVisibilityChanged(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (InterruptedException e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude the app from the recent tasks list.
|
||||
*/
|
||||
private void excludeFromTaskList() {
|
||||
ActivityManager am = (ActivityManager) cordova.getActivity()
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
|
||||
if (am == null || Build.VERSION.SDK_INT < 21)
|
||||
return;
|
||||
|
||||
try {
|
||||
Method getAppTasks = am.getClass().getMethod("getAppTasks");
|
||||
List tasks = (List) getAppTasks.invoke(am);
|
||||
|
||||
if (tasks == null || tasks.isEmpty())
|
||||
return;
|
||||
|
||||
ActivityManager.AppTask task = (ActivityManager.AppTask) tasks.get(0);
|
||||
Method setExcludeFromRecents = task.getClass()
|
||||
.getMethod("setExcludeFromRecents", boolean.class);
|
||||
|
||||
setExcludeFromRecents.invoke(task, true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire vent with some parameters inside the web view.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user