From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Christophe Pinelli Date: Tue, 16 May 2023 17:40:02 +0000 Subject: [PATCH] Restrict activity launch when caller is running in the background Test: test on device + atest-src BackgroundActivityLaunchTest#testBackgroundActivityBlockedInStartNextMatchingActivity Bug: 230492947 (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:d8368be4f8fb7019ea24b4798f029301c704092c) Merged-In: I7ae88eb62e435b9a77d2a724c5a953fe1f35b838 Change-Id: I7ae88eb62e435b9a77d2a724c5a953fe1f35b838 --- .../server/wm/ActivityTaskManagerService.java | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java index 5bcc5975604a..7384ca7173d3 100644 --- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java +++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java @@ -1194,28 +1194,37 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub { final long origId = Binder.clearCallingIdentity(); // TODO(b/64750076): Check if calling pid should really be -1. - final int res = getActivityStartController() - .obtainStarter(intent, "startNextMatchingActivity") - .setCaller(r.app.getThread()) - .setResolvedType(r.resolvedType) - .setActivityInfo(aInfo) - .setResultTo(resultTo != null ? resultTo.appToken : null) - .setResultWho(resultWho) - .setRequestCode(requestCode) - .setCallingPid(-1) - .setCallingUid(r.launchedFromUid) - .setCallingPackage(r.launchedFromPackage) - .setRealCallingPid(-1) - .setRealCallingUid(r.launchedFromUid) - .setActivityOptions(options) - .execute(); - Binder.restoreCallingIdentity(origId); - - r.finishing = wasFinishing; - if (res != ActivityManager.START_SUCCESS) { - return false; + try { + if (options == null) { + options = new SafeActivityOptions(ActivityOptions.makeBasic()); + } + // Fixes b/230492947 + // Prevents background activity launch through #startNextMatchingActivity + // An activity going into the background could still go back to the foreground + // if the intent used matches both: + // - the activity in the background + // - a second activity. + options.getOptions(r).setAvoidMoveToFront(); + final int res = getActivityStartController() + .obtainStarter(intent, "startNextMatchingActivity") + .setCaller(r.app.getThread()) + .setResolvedType(r.resolvedType) + .setActivityInfo(aInfo) + .setResultTo(resultTo != null ? resultTo.appToken : null) + .setResultWho(resultWho) + .setRequestCode(requestCode) + .setCallingPid(-1) + .setCallingUid(r.launchedFromUid) + .setCallingPackage(r.launchedFromPackage) + .setRealCallingPid(-1) + .setRealCallingUid(r.launchedFromUid) + .setActivityOptions(options) + .execute(); + r.finishing = wasFinishing; + return res == ActivityManager.START_SUCCESS; + } finally { + Binder.restoreCallingIdentity(origId); } - return true; } }