问题现象:
由于设备有两个Display设备,分为主屏和副屏,正常情况下startActivity如果不指定displayId的话,都会默认显示在主屏上,但是在某些情况下launcher启动的应用Activity会显示到副屏上面去。
根据现象来分析,初步判断问题原因是startActivity的流程出现异常导致的,那么就需要从startActivity流程开始查找原因。
先看framework\base\services\core\java\com\android\server\am\ActivityStarter.java中的:
```
int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
? ? ? ? ? ?...) {
? ? ? ? ...
? ? ? ? mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
? ? ? ? ? ? ? ? aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
? ? ? ? ? ? ? ? callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
? ? ? ? ? ? ? ? options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
? ? ? ? ? ? ? ? inTask);
? ? ? ? ...
? ? }
```
一步一步往下查之后看到startActivityUnchecked函数中有对task和stack的变更与displayId相关的操作,重点看这个地方:
// Note: This method should only be called from {@link startActivity}.
? ? private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
? ? ? ? ? ? IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
? ? ? ? ? ? int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
? ? ? ? ? ? ActivityRecord[] outActivity) {
...
// Should this be considered a new task?
? ? ? ? int result = START_SUCCESS;
? ? ? ? if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
? ? ? ? ? ? ? ? && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
? ? ? ? ? ? newTask = true;
? ? ? ? ? ? String packageName= mService.mContext.getPackageName();
? ? ? ? ? ? if (mPerf != null) {
? ? ? ? ? ? ? ? mStartActivity.perfActivityBoostHandler =
? ? ? ? ? ? ? ? ? ? mPerf.perfHint(BoostFramework.VENDOR_HINT_FIRST_LAUNCH_BOOST, packageName, -1, BoostFramework.Launch.BOOST_V1);
? ? ? ? ? ? }
? ? ? ? ? ? result = setTaskFromReuseOrCreateNewTask(?
? ? ? ? ? ? ? ? ? ? taskToAffiliate, preferredLaunchStackId, topStack);
? ? ? ? } else if (mSourceRecord != null) {
? ? ? ? ? ? result = setTaskFromSourceRecord(); //在添加打印后发现出现问题的时候是走的这个函数进行task与stack操作的
? ? ? ? } else if (mInTask != null) {
? ? ? ? ? ? result = setTaskFromInTask();
? ? ? ? } else {
? ? ? ? ? ? // This not being started from an existing activity, and not part of a new task...
? ? ? ? ? ? // just put it in the top task, though these days this case should never happen.
? ? ? ? ? ? setTaskToCurrentTopOrCreateNewTask();
? ? ? ? }
...
}
那么就看setTaskFromSourceRecord这个函数干了些什么:
private int setTaskFromSourceRecord() {
...
? ? ? ? // We only want to allow changing stack in two cases:
? ? ? ? // 1. If the target task is not the top one. Otherwise we would move the launching task to
? ? ? ? //? ? the other side, rather than show two side by side.
? ? ? ? // 2. If activity is not allowed on target display.
? ? ? ? final int targetDisplayId = mTargetStack != null ? mTargetStack.mDisplayId
? ? ? ? ? ? ? ? : sourceStack.mDisplayId;
????????final boolean moveStackAllowed = sourceStack.topTask() != sourceTask
? ? ? ? ? ? ? ? || !mStartActivity.canBeLaunchedOnDisplay(targetDisplayId);
????????if (moveStackAllowed) {
? ? ? ? ? ? mTargetStack = getLaunchStack(mStartActivity, mLaunchFlags, mStartActivity.getTask(),
? ? ? ? ? ? ? ? ? ? mOptions);
? ? ? ? ? ? // If target stack is not found now - we can't just rely on the source stack, as it may
? ? ? ? ? ? // be not suitable. Let's check other displays.
? ? ? ? ? ? if (mTargetStack == null && targetDisplayId != sourceStack.mDisplayId) {
? ? ? ? ? ? ? ? // Can't use target display, lets find a stack on the source display.
? ? ? ? ? ? ? ? mTargetStack = mService.mStackSupervisor.getValidLaunchStackOnDisplay(
? ? ? ? ? ? ? ? ? ? ? ? sourceStack.mDisplayId, mStartActivity);
? ? ? ? ? ? }
? ? ? ? ? ? if (mTargetStack == null) {
? ? ? ? ? ? ? ? // There are no suitable stacks on the target and source display(s). Look on all
? ? ? ? ? ? ? ? // displays.
? ? ? ? ? ? ? ? mTargetStack = mService.mStackSupervisor.getNextValidLaunchStackLocked(
? ? ? ? ? ? ? ? ? ? ? ? mStartActivity, -1/* currentFocus */);
? ? ? ? ? ? }
? ? ? ? }
}
根据注释来看,关键就是这个moveStackAllowed了,打印log发现出现问题的时候moveStackAllowed?为true,所以会对mTargetStack?进行重新赋值的操作,在重新赋值操作过程中,由于当前topTask所在的stack是副屏的,所以会进入mService.mStackSupervisor.getNextValidLaunchStackLocked查找非-1的displayId对应的ActivityStack来赋值。
/**
? ? * Get next valid stack for launching provided activity in the system. This will search across
? ? * displays and stacks in last-focused order for a focusable and visible stack, except those
? ? * that are on a currently focused display.
? ? *
? ? * @param r The activity that is being launched.
? ? * @param currentFocus The display that previously had focus and thus needs to be ignored when
? ? *? ? ? ? ? ? ? ? ? ? searching for the next candidate.
? ? * @return Next valid {@link ActivityStack}, null if not found.
? ? */
? ? ActivityStack getNextValidLaunchStackLocked(@NonNull ActivityRecord r, int currentFocus) {
? ? ? ? mWindowManager.getDisplaysInFocusOrder(mTmpOrderedDisplayIds);
? ? ? ? for (int i = mTmpOrderedDisplayIds.size() - 1; i >= 0; --i) {
? ? ? ? ? ? final int displayId = mTmpOrderedDisplayIds.get(i);
? ? ? ? ? ? if (displayId == currentFocus) {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? final ActivityStack stack = getValidLaunchStackOnDisplay(displayId, r);
? ? ? ? ? ? if (stack != null) {
? ? ? ? ? ? ? ? return stack;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
从getNextValidLaunchStackLocked内部可以看到按照displayId由小到大的顺序依次添加到mTmpOrderedDisplayIds这个列表中去,然后再由大到小依次遍历mTmpOrderedDisplayIds非-1的displayId对应的ActivityStack,由于设备的主屏和副屏的displayId分别是0、1,所以最先查到的就是displayId为1的ActivityStack,这样activity就会显示到副屏对应的ActivityStack上,从而导致默认启动的activity在副屏上显示的问题。
知道原因就好办了,直接在setTaskFromSourceRecord中添加判断:
if (mTargetStack == null) {
? ? ? ? ? ? ? ? // There are no suitable stacks on the target and source display(s). Look on all
? ? ? ? ? ? ? ? // displays.
? ? ? ? ? ? ? ? ActivityStack topStack = mSupervisor.mFocusedStack;
? ? ? ? ? ? ? ? int currentFocusDisplayId = -1;
? ? ? ? ? ? ? ? //如果topStack对应的displayId不等于startActivity传入的targetDisplayId,那么就在getNextValidLaunchStackLocked中传入topStack.mDisplayId排除掉对应的topStack就行了。
? ? ? ? ? ? ? ? if (topStack != null && topStack.mDisplayId != targetDisplayId) {
? ? ? ? ? ? ? ? ? ? currentFocusDisplayId = topStack.mDisplayId;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? mTargetStack = mService.mStackSupervisor.getNextValidLaunchStackLocked(
? ? ? ? ? ? ? ? ? ? ? ? mStartActivity, currentFocusDisplayId /* currentFocus */);
? ? ? ? ? ? }