Skip to content

Commit

Permalink
Allow app from being excluded from recent list
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Jan 29, 2017
1 parent c2fedef commit 18fc64f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ Override the back button on Android to go to background instead of closing the a
cordova.plugins.backgroundMode.overrideBackButton();
```

### Recent task list
Exclude the app from the recent task list works on Android 5.0+.

```javascript
cordova.plugins.backgroundMode.excludeFromTaskList();
```

### Notification
To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar.

Expand Down
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<preference name="windows-target-version" value="UAP" />
<preference name="uap-target-min-version" value="10.0.14393.0" />
<preference name="Windows.Universal-MinVersion" value="10.0.14393.0" />
<preference name="Windows.Universal" value="10.0.14393.0" />
</config-file>
<resource-file src="appbeep.wma" target="appbeep.wma" />
Expand Down
34 changes: 34 additions & 0 deletions src/android/BackgroundMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ Licensed to the Apache Software Foundation (ASF) under one
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;

Expand All @@ -36,6 +38,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.List;

public class BackgroundMode extends CordovaPlugin {

Expand Down Expand Up @@ -114,6 +117,10 @@ public boolean execute (String action, JSONArray args,
moveToForeground();
}

if (action.equalsIgnoreCase("tasklist")) {
excludeFromTaskList();
}

if (action.equalsIgnoreCase("enable")) {
enableMode();
}
Expand Down Expand Up @@ -333,6 +340,33 @@ public void run() {
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.
*
Expand Down
11 changes: 11 additions & 0 deletions www/background-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ exports.moveToForeground = function () {
}
};

/**
* Exclude the app from the recent tasks list (Android only).
*
* @return [ Void ]
*/
exports.excludeFromTaskList = function () {
if (this._isAndroid) {
cordova.exec(null, null, 'BackgroundMode', 'tasklist', []);
}
};

/**
* Override the back button on Android to go to background
* instead of closing the app.
Expand Down

0 comments on commit 18fc64f

Please sign in to comment.