From ff7ca84277d1f2e87d9cdbc1409e2733e510d1fb Mon Sep 17 00:00:00 2001 From: Louis Chang Date: Wed, 14 Aug 2024 03:58:57 +0000 Subject: Sets the launch display id for Context associated with a display Sets the launch display id while starting activity from a Context that has associated with a display, unless the launch target is already set by the caller. Bug: 359387748 Test: atest MultiDisplayImeTests Test: atest CtsWindowManagerDeviceMultiDisplay Flag: EXEMPT bugfix Change-Id: I29a4530b324b7c1f1182a51e58404e27862f1974 --- core/java/android/app/ActivityOptions.java | 10 ++++++++++ core/java/android/app/ContextImpl.java | 30 +++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/core/java/android/app/ActivityOptions.java b/core/java/android/app/ActivityOptions.java index 65acd49d44fa..91aa225039a4 100644 --- a/core/java/android/app/ActivityOptions.java +++ b/core/java/android/app/ActivityOptions.java @@ -1587,6 +1587,16 @@ public class ActivityOptions extends ComponentOptions { } } + /** @hide */ + public static boolean hasLaunchTargetContainer(ActivityOptions options) { + return options.getLaunchDisplayId() != INVALID_DISPLAY + || options.getLaunchTaskDisplayArea() != null + || options.getLaunchTaskDisplayAreaFeatureId() != FEATURE_UNDEFINED + || options.getLaunchRootTask() != null + || options.getLaunchTaskId() != -1 + || options.getLaunchTaskFragmentToken() != null; + } + /** * Gets whether the activity is to be launched into LockTask mode. * @return {@code true} if the activity is to be launched into LockTask mode. diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 7e2a580bec73..90fba2962a23 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -1160,7 +1160,7 @@ class ContextImpl extends Context { } mMainThread.getInstrumentation().execStartActivity( getOuterContext(), mMainThread.getApplicationThread(), null, - (Activity) null, intent, -1, options); + (Activity) null, intent, -1, applyLaunchDisplayIfNeeded(options)); } /** @hide */ @@ -1170,8 +1170,8 @@ class ContextImpl extends Context { ActivityTaskManager.getService().startActivityAsUser( mMainThread.getApplicationThread(), getOpPackageName(), getAttributionTag(), intent, intent.resolveTypeIfNeeded(getContentResolver()), - null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options, - user.getIdentifier()); + null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, + applyLaunchDisplayIfNeeded(options), user.getIdentifier()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -1194,7 +1194,8 @@ class ContextImpl extends Context { } return mMainThread.getInstrumentation().execStartActivitiesAsUser( getOuterContext(), mMainThread.getApplicationThread(), null, - (Activity) null, intents, options, userHandle.getIdentifier()); + (Activity) null, intents, applyLaunchDisplayIfNeeded(options), + userHandle.getIdentifier()); } @Override @@ -1208,7 +1209,26 @@ class ContextImpl extends Context { } mMainThread.getInstrumentation().execStartActivities( getOuterContext(), mMainThread.getApplicationThread(), null, - (Activity) null, intents, options); + (Activity) null, intents, applyLaunchDisplayIfNeeded(options)); + } + + private Bundle applyLaunchDisplayIfNeeded(@Nullable Bundle options) { + if (!isAssociatedWithDisplay()) { + // return if this Context has no associated display. + return options; + } + + final ActivityOptions activityOptions; + if (options != null) { + activityOptions = ActivityOptions.fromBundle(options); + if (ActivityOptions.hasLaunchTargetContainer(activityOptions)) { + // return if the options already has launching target. + return options; + } + } else { + activityOptions = ActivityOptions.makeBasic(); + } + return activityOptions.setLaunchDisplayId(getAssociatedDisplayId()).toBundle(); } @Override -- cgit v1.2.3-59-g8ed1b