Merge "Give fg services a shelf life before they go bad." into oc-mr1-dev

This commit is contained in:
Daniel Sandler
2017-10-27 14:39:01 +00:00
committed by Android (Google) Code Review
2 changed files with 13 additions and 4 deletions

View File

@@ -34,6 +34,10 @@ import java.util.Arrays;
*/ */
public class ForegroundServiceControllerImpl public class ForegroundServiceControllerImpl
implements ForegroundServiceController { implements ForegroundServiceController {
// shelf life of foreground services before they go bad
public static final long FG_SERVICE_GRACE_MILLIS = 5000;
private static final String TAG = "FgServiceController"; private static final String TAG = "FgServiceController";
private static final boolean DBG = false; private static final boolean DBG = false;
@@ -72,7 +76,7 @@ public class ForegroundServiceControllerImpl
if (isDungeonNotification(sbn)) { if (isDungeonNotification(sbn)) {
// if you remove the dungeon entirely, we take that to mean there are // if you remove the dungeon entirely, we take that to mean there are
// no running services // no running services
userServices.setRunningServices(null); userServices.setRunningServices(null, 0);
return true; return true;
} else { } else {
// this is safe to call on any notification, not just FLAG_FOREGROUND_SERVICE // this is safe to call on any notification, not just FLAG_FOREGROUND_SERVICE
@@ -94,7 +98,7 @@ public class ForegroundServiceControllerImpl
final Bundle extras = sbn.getNotification().extras; final Bundle extras = sbn.getNotification().extras;
if (extras != null) { if (extras != null) {
final String[] svcs = extras.getStringArray(Notification.EXTRA_FOREGROUND_APPS); final String[] svcs = extras.getStringArray(Notification.EXTRA_FOREGROUND_APPS);
userServices.setRunningServices(svcs); // null ok userServices.setRunningServices(svcs, sbn.getNotification().when);
} }
} else { } else {
userServices.removeNotification(sbn.getPackageName(), sbn.getKey()); userServices.removeNotification(sbn.getPackageName(), sbn.getKey());
@@ -118,9 +122,11 @@ public class ForegroundServiceControllerImpl
*/ */
private static class UserServices { private static class UserServices {
private String[] mRunning = null; private String[] mRunning = null;
private long mServiceStartTime = 0;
private ArrayMap<String, ArraySet<String>> mNotifications = new ArrayMap<>(1); private ArrayMap<String, ArraySet<String>> mNotifications = new ArrayMap<>(1);
public void setRunningServices(String[] pkgs) { public void setRunningServices(String[] pkgs, long serviceStartTime) {
mRunning = pkgs != null ? Arrays.copyOf(pkgs, pkgs.length) : null; mRunning = pkgs != null ? Arrays.copyOf(pkgs, pkgs.length) : null;
mServiceStartTime = serviceStartTime;
} }
public void addNotification(String pkg, String key) { public void addNotification(String pkg, String key) {
if (mNotifications.get(pkg) == null) { if (mNotifications.get(pkg) == null) {
@@ -142,7 +148,9 @@ public class ForegroundServiceControllerImpl
return found; return found;
} }
public boolean isDungeonNeeded() { public boolean isDungeonNeeded() {
if (mRunning != null) { if (mRunning != null
&& System.currentTimeMillis() - mServiceStartTime >= FG_SERVICE_GRACE_MILLIS) {
for (String pkg : mRunning) { for (String pkg : mRunning) {
final ArraySet<String> set = mNotifications.get(pkg); final ArraySet<String> set = mNotifications.get(pkg);
if (set == null || set.size() == 0) { if (set == null || set.size() == 0) {

View File

@@ -287,6 +287,7 @@ public class ForegroundServiceControllerTest extends SysuiTestCase {
final Bundle extras = new Bundle(); final Bundle extras = new Bundle();
if (pkgs != null) extras.putStringArray(Notification.EXTRA_FOREGROUND_APPS, pkgs); if (pkgs != null) extras.putStringArray(Notification.EXTRA_FOREGROUND_APPS, pkgs);
n.extras = extras; n.extras = extras;
n.when = System.currentTimeMillis() - 10000; // ten seconds ago
final StatusBarNotification sbn = makeMockSBN(userid, "android", final StatusBarNotification sbn = makeMockSBN(userid, "android",
SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICES, SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICES,
null, n); null, n);