前言
以前改过一个分屏的需求,跟了一下SystemUI
的代码。在这边做一下记录,方便后续做一些更细化的理解拓展。
预备知识
1、SystemUI
中的EventBus
该EventBus
用于不同类之间的消息传递,至于原理有空在看。这边说说使用吧,很简单。
注册与解注册:
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow(); EventBus.getDefault().register(this);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow(); EventBus.getDefault().unregister(this);
}
发送消息与接收消息,注意这边的方法名xxxEvent
要一致:
EventBus.getDefault().send(new xxxEvent(...));
public final void onBusEvent(xxxEvent event) {
}
1、理清ActivityRecord、TaskRecord、ActivityStack
关系
先上图,这是从博客看到的一张图。
首先一个ActivityRecord
对应着一个Activity
,而Activity可能对应着不同的ActivityRecord
(因为Activity可能被实例化多次)。其次一系列ActivityRecord
存在于TaskRecord
,而一系列TaskRecord
存在于ActivityStack
。ActivityStackSupervisor
是用来管理这些ActivityStack
的。
2.1 ActivityRecord
@frameworks/base/services/core/java/com/android/server/am/ActivityRecord.java
//ams的引用
final ActivityManagerService service; // owner
//token用来和wms交互
final IApplicationToken.Stub appToken; // window manager token
final ActivityInfo info; // all about me
final ApplicationInfo appInfo; // information about activity's app
...
//Activity资源信息
CharSequence nonLocalizedLabel; // the label information from the package mgr.
int labelRes; // the label information from the package mgr.
int icon; // resource identifier of activity's icon.
int logo; // resource identifier of activity's logo.
int theme; // resource identifier of activity's theme.
int realTheme; // actual theme resource we will use, never 0.
int windowFlags; // custom window flags for preview window.
//Activity所在的TaskRecord
TaskRecord task; // the task this is in.
...
//Activity所在进程
ProcessRecord app; // if non-null, hosting application
int mActivityType
ActivityState state; // current state we are in
...
其中ActivityRecord
定义了该ActivityRecord
对应Activity的三种类型:
static final int APPLICATION_ACTIVITY_TYPE = 0;//普通应用类型
static final int HOME_ACTIVITY_TYPE = 1;//桌面类型
static final int RECENTS_ACTIVITY_TYPE = 2;//最近任务类型
ActivityRecord
是在startActivity
的时候创建的。
@frameworks/base/services/core/java/com/android/server/am/ActivityStarter.java
final int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
ActivityRecord[] outActivity, ActivityStackSupervisor.ActivityContainer container,
TaskRecord inTask) {
...
ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
requestCode, componentSpecified, voiceSession != null, mSupervisor, container,
options, sourceRecord);
...
}
总结一下,就是在Activity启动的时候同时创建了ActivityRecord
。同时指定了该ActivityRecord
的类型以及所在的TaskRecord
。
2.2 TaskRecord
@frameworks/base/services/core/java/com/android/server/am/TaskRecord.java
//TaskRecord的唯一标识
final int taskId;
...
//TaskRecord里所有的ActivityRecord信息
final ArrayList<ActivityRecord> mActivities;
//TaskRecord所在的ActivityStack
ActivityStack stack