From 9d4c55a2511f76c8098172a2676eb3207c2baaa9 Mon Sep 17 00:00:00 2001 From: ZhaoPengyuan Date: Fri, 14 Aug 2020 10:01:14 +0800 Subject: [PATCH] ams:stop app only if there are no tasks when remove task, stop the app only if there are no tasks --- .../server/am/ActivityManagerService.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index a178f3f4..bbf80fb3 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -9018,6 +9018,21 @@ public final class ActivityManagerService extends ActivityManagerNative // TASK MANAGEMENT // ========================================================= + public int getAppTasksCnt(String callingPackage) { + final int N = mRecentTasks.size(); + int cnt = 0; + for (int i = 0; i < N; i++) { + TaskRecord tr = mRecentTasks.get(i); + Intent intent = tr.getBaseIntent(); + if (intent == null || + !callingPackage.equals(intent.getComponent().getPackageName())) { + continue; + } + cnt += 1; + } + return cnt; + } + @Override public List getAppTasks(String callingPackage) { int callingUid = Binder.getCallingUid(); @@ -9670,11 +9685,12 @@ public final class ActivityManagerService extends ActivityManagerNative final TaskRecord tr = mStackSupervisor.anyTaskForIdLocked( taskId, !RESTORE_FROM_RECENTS, INVALID_STACK_ID); if (tr != null) { - tr.removeTaskActivitiesLocked(); - cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents); ComponentName component = tr.getBaseIntent().getComponent(); final String pkgName = component.getPackageName(); - if (validatePkgName(pkgName)) { + tr.removeTaskActivitiesLocked(); + cleanUpRemovedTaskLocked(tr, killProcess, removeFromRecents); + int cnt = getAppTasksCnt(pkgName); + if (cnt == 0 && validatePkgName(pkgName)) { String cmd = "am force-stop " + pkgName; try { Runtime.getRuntime().exec(cmd); @@ -19083,7 +19099,7 @@ public final class ActivityManagerService extends ActivityManagerNative if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { intent = new Intent(Intent.ACTION_LOCALE_CHANGED); intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); - if (initLocale || !mProcessesReady) { + if (initLocale || !mProcessesReady) { intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); } broadcastIntentLocked(null, null, intent, -- Gitee