序
原生的分屏功能是在多任务中,点击应用图标选择分屏,在选择多任务中的其他应用进行分屏
代码分析思路
进入上分屏代码分析
我们有两种分析方法,一种是通过Split screen
文字查找对应的onClick事件跟踪代码流程
另一种是通过,点击进入到上分屏之后,Toast弹出的文字Tap another app to use split screen
来分析
推荐使用第二种方式,如果使用第一种从点击事件开始跟踪比较复杂且容易出错,但是第二种是在操作结束的地方才会有这个打印,我们只需找到打印这个Toast的地方加堆栈即可。
这里我们以这里点击进入到分屏后,Toast弹出的文字Tap another app to use split screen
来分析,在多任务中点击Split screen
的流程。
字符串一般都在XML文件中,我们在代码中找下
我们找到toast_split_select_app
,看看其有没有在java文件中调用
发现java文件中没有这个string,推测是在XML文件中套娃了
我们找到了split_instructions_view.xml
方法其是在SplitInstructionsView.java
中getSplitInstructionsView
方法里面添加的,我们在这里添加堆栈打印,就能得到进入上分屏的流程。
Launcher: at com.android.quickstep.views.SplitInstructionsView.getSplitInstructionsView(SplitInstructionsView.java:79)
Launcher: at com.android.quickstep.views.RecentsView.createInitialSplitSelectAnimation(RecentsView.java:3196)
Launcher: at com.android.quickstep.views.RecentsView.createTaskDismissAnimation(RecentsView.java:3493)
Launcher: at com.android.quickstep.views.RecentsView.createSplitSelectInitAnimation(RecentsView.java:4636)
Launcher: at com.android.launcher3.uioverrides.RecentsViewStateController.handleSplitSelectionState(RecentsViewStateController.java:139)
Launcher: at com.android.launcher3.uioverrides.RecentsViewStateController.setStateWithAnimationInternal(RecentsViewStateController.java:109)
Launcher: at com.android.launcher3.uioverrides.BaseRecentsViewStateController.setStateWithAnimation(BaseRecentsViewStateController.java:91)
Launcher: at com.android.launcher3.uioverrides.BaseRecentsViewStateController.setStateWithAnimation(BaseRecentsViewStateController.java:61)
Launcher: at com.android.launcher3.statemanager.StateManager.createAnimationToNewWorkspaceInternal(StateManager.java:350)
Launcher: at com.android.launcher3.statemanager.StateManager.goToStateAnimated(StateManager.java:285)
Launcher: at com.android.launcher3.statemanager.StateManager.goToState(StateManager.java:273)
Launcher: at com.android.launcher3.statemanager.StateManager.goToState(StateManager.java:152)
Launcher: at com.android.launcher3.statemanager.StateManager.goToState(StateManager.java:145)
Launcher: at com.android.quickstep.views.LauncherRecentsView.initiateSplitSelect(LauncherRecentsView.java:210)
Launcher: at com.android.quickstep.views.TaskView.initiateSplitSelect(TaskView.java:1767)
Launcher: at com.android.quickstep.TaskShortcutFactory$SplitSelectSystemShortcut.onClick(TaskShortcutFactory.java:124)
Launcher: at com.android.quickstep.views.TaskMenuView$$ExternalSyntheticLambda3.onClick(Unknown Source:2)
Launcher: at android.view.View.performClick(View.java:7659)
Launcher: at android.view.View.performClickInternal(View.java:7636)
Launcher: at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
Launcher: at android.view.View$PerformClick.run(View.java:30156)
Launcher: at android.os.Handler.handleCallback(Handler.java:958)
Launcher: at android.os.Handler.dispatchMessage(Handler.java:99)
Launcher: at android.os.Looper.loopOnce(Looper.java:205)
Launcher: at android.os.Looper.loop(Looper.java:294)
Launcher: at android.app.ActivityThread.main(ActivityThread.java:8177)
Launcher: at java.lang.reflect.Method.invoke(Native Method)
Launcher: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
Launcher: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
进入下分屏代码分析
进入下分屏的代码如何查找呢?
从现象上看,进入上分屏后,在多任务界面点击任意的Task,然后进入到分屏。因此可以尝试从下面两点进行分析:
1.最近任务界面有对Task的onClick事件进行监听
2.进入分屏时,涉及窗口相关变化,因此桌面进程一定会进行跨进程通信
关于第二点先进行排除,因为当前无法知道,桌面是如何跨进程通信到系统侧
因此我们需要从第一点进行排查,从第一点中,我们可以获得如下关键词
关键词:桌面进程、Task、onClick、分屏(split screen)
组织一下上述关键词就是,在桌面里面有对Task进行onClick操作之后会触发分屏(split screen)相关的方法。
在源码根目录下我们可以进入到桌面相关的目录cd packages/apps/Launcher3
运行命令find . -iname "*task*" | xargs grep -irnozP " onClick.*[\s\S]*?split" > codeFind.txt
查找相关代码,简单解释下这个语句:
find . -iname "*task*"
模糊查询Task相关文件xargs grep -irnozP " onClick.*[\s\S]*?split"
拼接,使用正则表达式查询包含onClick到split的句子
grep参数说明: -P: 启用Perl正则表达式模式;-z: 会把整个文本当成一个字符串,允许多行匹配,即使匹配跨越了换行符的行;-o: 只输出匹配的部分;-n:显示行数;-r: 递归查询; -i: 忽略大小写。
正则表达式说明:onClick.*[\s\S]*?split
,onClick.*
:包含以【空格】onClick开头的单词;[\s\S]*?
:多行匹配,\s
匹配任何空白字符,包括空格、制表符、换页符等等,\S
匹配任何非空白字符;split
:以split结尾的单词。
最终我们输出一个名为codeFind.txt
的文件,我们之后需要在这个文件中再次筛选出我们要找的代码。
(目前没有发现别的更高效的方法,如果有别的方法可以在评论区留言)
最终我们找到
代码路径:packages/apps/Launcher3/quickstep/src/com/android/quickstep/views/TaskView.java
private void onClick(View view) {
if (getTask() == null) {
return;
}
if (confirmSecondSplitSelectApp()) {
return;
}
launchTasks();
mActivity.getStatsLogManager().logger().withItemInfo(getItemInfo())
.log(LAUNCHER_TASK_LAUNCH_TAP);
}
跟踪confirmSecondSplitSelectApp()
并走读后续流程。
整体流程
层级结构
#1 DefaultTaskDisplayArea type=undefined mode=fullscreen override-mode=fullscreen requested-bounds=[0,0][0,0] bounds=[0,0][1440,2960]
#1 Task=3 type=standard mode=fullscreen override-mode=fullscreen requested-bounds=[0,0][0,0] bounds=[0,0][1440,2960]
#1 Task=4 type=standard mode=multi-window override-mode=multi-window requested-bounds=[0,1498][1440,2960] bounds=[0,1498][1440,2960]
#0 Task=55 type=standard mode=multi-window override-mode=undefined requested-bounds=[0,0][0,0] bounds=[0,1498][1440,2960]
#0 ActivityRecord{c8a85d8 u0 com.android.dialer/.main.impl.MainActivity t55} type=standard mode=multi-window override-mode=undefined requested-bounds=[0,0][0,0] bounds=[0,1498][1440,2960]
#0 17b29c2 com.android.dialer/com.android.dialer.main.impl.MainActivity type=standard mode=multi-window override-mode=undefined requested-bounds=[0,0][0,0] bounds=[0,1498][1440,2960]
#0 Task=5 type=standard mode=multi-window override-mode=multi-window requested-bounds=[0,0][1440,1463] bounds=[0,0][1440,1463]
#0 Task=61 type=standard mode=multi-window override-mode=undefined requested-bounds=[0,0][0,0] bounds=[0,0][1440,1463]
#0 ActivityRecord{a029103 u0 com.android.messaging/.ui.conversationlist.ConversationListActivity t61} type=standard mode=multi-window override-mode=undefined requested-bounds=[0,0][0,0] bounds=[0,0][1440,1463]
#0 d3dd038 com.android.messaging/com.android.messaging.ui.conversationlist.ConversationListActivity type=standard mode=multi-window override-mode=undefined requested-bounds=[0,0][0,0] bounds=[0,0][1440,1463]
对应如下树状图
各模块责任与堆栈
Launcher
1.确定分屏上部分,做好相关显示动画,等待选择下分屏
Launcher: at com.android.quickstep.views.RecentsView.createInitialSplitSelectAnimation(RecentsView.java:3196)
Launcher: at com.android.quickstep.views.RecentsView.createTaskDismissAnimation(RecentsView.java:3493)
Launcher: at com.android.quickstep.views.RecentsView.createSplitSelectInitAnimation(RecentsView.java:4636)
Launcher: at com.android.launcher3.uioverrides.RecentsViewStateController.handleSplitSelectionState(RecentsViewStateController.java:139)
Launcher: at com.android.launcher3.uioverrides.RecentsViewStateController.setStateWithAnimationInternal(RecentsViewStateController.java:109)
Launcher: at com.android.launcher3.uioverrides.BaseRecentsViewStateController.setStateWithAnimation(BaseRecentsViewStateController.java:91)
Launcher: at com.android.launcher3.uioverrides.BaseRecentsViewStateController.setStateWithAnimation(BaseRecentsViewStateController.java:61)
Launcher: at com.android.launcher3.statemanager.StateManager.createAnimationToNewWorkspaceInternal(StateManager.java:350)
Launcher: at com.android.launcher3.statemanager.StateManager.goToStateAnimated(StateManager.java:285)
Launcher: at com.android.launcher3.statemanager.StateManager.goToState(StateManager.java:273)
Launcher: at com.android.launcher3.statemanager.StateManager.goToState(StateManager.java:152)
Launcher: at com.android.launcher3.statemanager.StateManager.goToState(StateManager.java:145)
Launcher: at com.android.quickstep.views.LauncherRecentsView.initiateSplitSelect(LauncherRecentsView.java:210)
Launcher: at com.android.quickstep.views.TaskView.initiateSplitSelect(TaskView.java:1767)
Launcher: at com.android.quickstep.TaskShortcutFactory$SplitSelectSystemShortcut.onClick(TaskShortcutFactory.java:124)
Launcher: at com.android.quickstep.views.TaskMenuView$$ExternalSyntheticLambda3.onClick(Unknown Source:2)
Launcher: at android.view.View.performClick(View.java:7659)
Launcher: at android.view.View.performClickInternal(View.java:7636)
Launcher: at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
Launcher: at android.view.View$PerformClick.run(View.java:30156)
Launcher: at android.os.Handler.handleCallback(Handler.java:958)
Launcher: at android.os.Handler.dispatchMessage(Handler.java:99)
Launcher: at android.os.Looper.loopOnce(Looper.java:205)
Launcher: at android.os.Looper.loop(Looper.java:294)
Launcher: at android.app.ActivityThread.main(ActivityThread.java:8177)
Launcher: at java.lang.reflect.Method.invoke(Native Method)
Launcher: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
Launcher: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
2.确定选择下分屏,播放相关动画,调用SytemUI接口进入到真正分屏
Launcher: at com.android.quickstep.views.RecentsView.confirmSplitSelect(RecentsView.java:4716)
Launcher: at com.android.quickstep.views.TaskView.confirmSecondSplitSelectApp(TaskView.java:762)
Launcher: at com.android.quickstep.views.TaskView.onClick(TaskView.java:746)
Launcher: at com.android.quickstep.views.TaskView.$r8$lambda$2dErZAYzRAWboZR0vn31kIz_HGY(Unknown Source:0)
Launcher: at com.android.quickstep.views.TaskView$$ExternalSyntheticLambda2.onClick(Unknown Source:2)
Launcher: at android.view.View.performClick(View.java:7659)
Launcher: at android.view.View.performClickInternal(View.java:7636)
Launcher: at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
Launcher: at android.view.View$PerformClick.run(View.java:30156)
Launcher: at android.os.Handler.handleCallback(Handler.java:958)
Launcher: at android.os.Handler.dispatchMessage(Handler.java:99)
Launcher: at android.os.Looper.loopOnce(Looper.java:205)
Launcher: at android.os.Looper.loop(Looper.java:294)
Launcher: at android.app.ActivityThread.main(ActivityThread.java:8177)
Launcher: at java.lang.reflect.Method.invoke(Native Method)
Launcher: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
Launcher: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
pendingAnimation.addEndListener(aBoolean -> {
mSplitSelectStateController.launchSplitTasks(
aBoolean1 -> RecentsView.this.resetFromSplitSelectionState());
InteractionJankMonitorWrapper.end(InteractionJankMonitorWrapper.CUJ_SPLIT_SCREEN_ENTER);
});
Launcher: at com.android.quickstep.util.SplitSelectStateController.launchTasksRefactored(SplitSelectStateController.java:412)
Launcher: at com.android.quickstep.util.SplitSelectStateController.launchTasks(SplitSelectStateController.java:329)
Launcher: at com.android.quickstep.util.SplitSelectStateController.launchSplitTasks(SplitSelectStateController.java:258)
Launcher: at com.android.quickstep.views.RecentsView.lambda$confirmSplitSelect$34(RecentsView.java:4718)
Launcher: at com.android.quickstep.views.RecentsView.$r8$lambda$uSHMT-0i9azkpL6YW9McOOFyjVQ(Unknown Source:0)
Launcher: at com.android.quickstep.views.RecentsView$$ExternalSyntheticLambda26.accept(Unknown Source:4)
Launcher: at com.android.launcher3.anim.AnimatorListeners$EndStateCallbackWrapper.onAnimationEnd(AnimatorListeners.java:80)
Launcher: at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:711)
Launcher: at android.animation.Animator$AnimatorCaller$$ExternalSyntheticLambda1.call(Unknown Source:4)
Launcher: at android.animation.Animator.callOnList(Animator.java:669)
Launcher: at android.animation.Animator.notifyListeners(Animator.java:608)
Launcher: at android.animation.Animator.notifyEndListeners(Animator.java:633)
Launcher: at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1306)
Launcher: at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1566)
Launcher: at android.animation.ValueAnimator.pulseAnimationFrame(ValueAnimator.java:1582)
Launcher: at android.animation.AnimatorSet.pulseFrame(AnimatorSet.java:1314)
Launcher: at android.animation.AnimatorSet.handleAnimationEvents(AnimatorSet.java:1297)
Launcher: at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1197)
Launcher: at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:328)
Launcher: at android.animation.AnimationHandler.-$$Nest$mdoAnimationFrame(Unknown Source:0)
Launcher: at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:86)
Launcher: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1337)
Launcher: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1348)
Launcher: at android.view.Choreographer.doCallbacks(Choreographer.java:952)
Launcher: at android.view.Choreographer.doFrame(Choreographer.java:878)
Launcher: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1322)
Launcher: at android.os.Handler.handleCallback(Handler.java:958)
Launcher: at android.os.Handler.dispatchMessage(Handler.java:99)
Launcher: at android.os.Looper.loopOnce(Looper.java:205)
Launcher: at android.os.Looper.loop(Looper.java:294)
Launcher: at android.app.ActivityThread.main(ActivityThread.java:8177)
Launcher: at java.lang.reflect.Method.invoke(Native Method)
Launcher: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
Launcher: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
SystemUiProxy调用ISplitScreen.aidl进行跨进程通信到SystemUI进程
SytemUI
SplitScreenController实现了ISplitScreen.aidl
SplitScreen: at android.window.WindowOrganizer.startNewTransition(WindowOrganizer.java:94)
SplitScreen: at com.android.wm.shell.transition.Transitions.startTransition(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:1121)
SplitScreen: at com.android.wm.shell.splitscreen.SplitScreenTransitions.startEnterTransition(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:345)
SplitScreen: at com.android.wm.shell.splitscreen.StageCoordinator.startWithTask(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:701)
SplitScreen: at com.android.wm.shell.splitscreen.StageCoordinator.startTasks(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:623)
SplitScreen: at com.android.wm.shell.splitscreen.SplitScreenController$ISplitScreenImpl.lambda$startTasks$11(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:1157)
SplitScreen: at com.android.wm.shell.splitscreen.SplitScreenController$ISplitScreenImpl.$r8$lambda$UuHBVv3bzz4CDOdyAqlv4kaRHgY(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:0)
SplitScreen: at com.android.wm.shell.splitscreen.SplitScreenController$ISplitScreenImpl$$ExternalSyntheticLambda14.accept(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:0)
SplitScreen: at com.android.wm.shell.common.ExecutorUtils.lambda$executeRemoteCallWithTaskPermission$1(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:60)
SplitScreen: at com.android.wm.shell.common.ExecutorUtils.$r8$lambda$s8eUOdyrqpqzzyFwAMGxO-MaCg4(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:0)
SplitScreen: at com.android.wm.shell.common.ExecutorUtils$$ExternalSyntheticLambda1.run(go/retraceme d8ee0e3eecdad3cdc77c111ff5591ebe26372c47d62ba59187c5ecf96366b561:0)
SplitScreen: at android.os.Handler.handleCallback(Handler.java:958)
SplitScreen: at android.os.Handler.dispatchMessage(Handler.java:99)
SplitScreen: at android.os.Looper.loopOnce(Looper.java:205)
SplitScreen: at android.os.Looper.loop(Looper.java:294)
SplitScreen: at android.os.HandlerThread.run(HandlerThread.java:67)
1.对上分屏的Task进行启动
2.设置好对应的bound到configration中
3.对分屏的RootTask进行排位到最顶部
4.对下分屏的Task进行启动
对Task的操作均会保存到WindowContainerTransaction对象中,最后通过WindowOrganizer调用IWindowOrganizerController.aidl进行跨进程通信到SystemServer进程进程Task操作
注:SplitLayout初始化分屏的分割线的布局的流程,从代码上看,如果shell动画开关是打开的,那么该流程在1和2之间执行;反之,则会在4之后,即窗口relayout流程中通过shell动画流程调用。
SystemServer
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.applyTransaction(WindowOrganizerController.java:597)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.applyTransaction(WindowOrganizerController.java:484)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.applyTransaction(WindowOrganizerController.java:502)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.lambda$startTransition$3(WindowOrganizerController.java:314)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.$r8$lambda$v_6sOG-rerjgO2wQxXrUcgpnWaU(WindowOrganizerController.java:0)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController$$ExternalSyntheticLambda3.onCollectStarted(R8$$SyntheticClass:0)
WindowOrganizerController: at com.android.server.wm.TransitionController.startCollectOrQueue(TransitionController.java:1306)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.startTransition(WindowOrganizerController.java:310)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.startNewTransition(WindowOrganizerController.java:271)
WindowOrganizerController: at android.window.IWindowOrganizerController$Stub.onTransact(IWindowOrganizerController.java:245)
WindowOrganizerController: at com.android.server.wm.WindowOrganizerController.onTransact(WindowOrganizerController.java:188)
WindowOrganizerController: at android.os.Binder.execTransactInternal(Binder.java:1344)
WindowOrganizerController: at android.os.Binder.execTransact(Binder.java:1275)
WindowOrganizerController实现了IWindowOrganizerController.aidl
其接收SystemUI中的命令来对Task进行不同的操作
applyTransaction中对Task区域大小bounds变化以及RootTask的reorder和startTask等操作进行了处理