从SystemServer开始:
?
frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
public static void main(String[] args) {
//启动流程:1
new SystemServer().run();
}
private void run() {
......
startBootstrapServices(t); //此处会初始化ActivityTaskManagerService
startCoreServices(t);
//启动流程:2
startOtherServices(t);//此处进入会调用systemReady()函数,进入到ActivityManagerService
......
}
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
//此处的startService会进入SystemServiceManager.java, 最终调用service.onStart()
//ActivityTaskManagerService.java在onStart函数中会调用
//LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
}
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
......
//启动流程:3
//由此进入Launcher的启动流程
mActivityManagerService.systemReady(()->{
......
try {
startSystemUi(context, windowManagerF);//启动SystemUI,题外话
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
......
});
}
}
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
public ActivityTaskManagerService mActivityTaskManager;
public ActivityTaskManagerInternal mAtmInternal;
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
......
mActivityTaskManager = atm;
//initialize会创建ActivityStackSupervisor和ActivityStartController
mActivityTaskManager.initialize(......);
//addService在ActivityTaskManagerService.start()函数中
mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
......
}
public void setWindowManager(WindowManagerService wm) {
mActivityTaskManager.setWindowManager(wm);
}
public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
......
if (bootingSystemUser) {
//启动流程:4,
//调用ActivityTaskManagerService.LocalService.startHomeOnAllDisplays
mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
}
......
}
//启动流程第十六步会通过Handler调用此函数
public final class LocalService extends ActivityManagerInternal {
public void startProcess(String processName, ApplicationInfo info, boolean knownToBeDead,
boolean isTop, String hostingType, ComponentName hostingName) {
//启动流程:17, 进入startProcessLocked
startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */,
new HostingRecord(hostingType, hostingName, isTop),
}
}
final ProcessRecord startProcessLocked(String processName,
ApplicationInfo info, boolean knownToBeDead, int intentFlags,
HostingRecord hostingRecord, int zygotePolicyFlags, boolean allowWhileBooting,
boolean isolated, boolean keepIfLarge) {
//启动流程:18,
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
hostingRecord, zygotePolicyFlags, allowWhileBooting, isolated, 0 /* isolatedUid */,
keepIfLarge, null /* ABI override */, null /* entryPoint */,
null /* entryPointArgs */, null /* crashHandler */);
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
final ActivityTaskManagerInternal mInternal;
RootWindowContainer mRootWindowContainer;
ActivityStackSupervisor mStackSupervisor;
public ActivityTaskManagerService(Context context) {
mInternal = new LocalService();
}
//此函数在ActivityManagerService.setWindowManager()函数中调用
public void setWindowManager(WindowManagerService wm) {
//启动流程:五中使用的字段在此初始化
mRootWindowContainer = wm.mRoot;
}
ActivityStartController getActivityStartController() {
return mActivityStartController;
}
public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController, Looper looper){
......
mStackSupervisor = createStackSupervisor();//初始化ActivityStackSupervisor
mActivityStartController = new ActivityStartController(this);//初始化ActivityStartController
......
}
private void start() {
//addService, 启动流程4中用到的mAtmInternal字段来源于此
LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
}
public static final class Lifecycle extends SystemService {
@Override
public void onStart() {
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
mService.start();//此处调用到上面的start()函数,addService
}
}
//启动流程第十五步会调回到此函数
void startProcessAsync(ActivityRecord activity, boolean knownToBeDead, boolean isTop,
String hostingType) {
//启动流程:16, 进入 ActivityManagerService.java -> LocalService.startProcess()
final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess,
mAmInternal, activity.processName, activity.info.applicationInfo, knownToBeDead,
isTop, hostingType, activity.intent.getComponent());
mH.sendMessage(m);
}
final class LocalService extends ActivityTaskManagerInternal {
//启动流程:5
//调用RootWindowContainer.startHomeOnAllDisplays()
public boolean startHomeOnAllDisplays(int userId, String reason) {
return mRootWindowContainer.startHomeOnAllDisplays(userId, reason);
}
}
}
frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
class RootWindowContainer extends WindowContainer<DisplayContent> implements DisplayManager.DisplayListener {
ActivityTaskManagerService mService;
RootWindowContainer(WindowManagerService service) {
mService = service.mAtmService;
}
boolean startHomeOnAllDisplays(int userId, String reason) {
//启动流程:6
startHomeOnDisplay(userId, reason, displayId);
}
//经过一系统的重构,最终进入
boolean startHomeOnTaskDisplayArea(int userId, String reason, TaskDisplayArea taskDisplayArea, boolean allowInstrumenting, boolean fromHomeKey) {
......
homeIntent = mService.getHomeIntent();
//Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] }
aInfo = resolveHomeActivity(userId, homeIntent);
//ActivityInfo{312dfb8 com.android.launcher.Launcer}
......
//启动流程:7, 进入ActivityStartController.startHomeActivity()
mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason, taskDisplayArea);
}
boolean resumeFocusedStacksTopActivities( ActivityStack targetStack,......){
//启动流程:13,8~12在下面, 调用ActivityStack.java ->resumeTopActivityUncheckedLocked
result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityStartController.java
public class ActivityStartController {
void startHomeActivity(......){
......
//启动流程:8,此处会进入到 ActivityStarter.java->execute()
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)
.setOutActivity(tmpOutRecord)
.setCallingUid(0)
.setActivityInfo(aInfo)
.setActivityOptions(options.toBundle())
.execute();
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
class ActivityStarter {
private final RootWindowContainer mRootWindowContainer;
ActivityStarter(..., ActivityTaskManagerService service,...){
mRootWindowContainer = service.mRootWindowContainer;
}
int execute() {
//启动流程:9, 进入下面executeReques函数
executeRequest();
}
private int executeRequest(Request request) {
//启动流程:10, 进入下面startActivityUnchecked
mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
request.voiceInteractor, startFlags, true /* doResume */,......);
}
private int startActivityUnchecked(......) {
//启动流程:11, 进入下面startActivityInner
result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options,......);
}
int startActivityInner(......) {
//启动流程:12, 再次RootWindowContainer.java ->resumeFocusedStacksTopActivities
mRootWindowContainer.resumeFocusedStacksTopActivities( mTargetStack, mStartActivity, mOptions);
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityStack.java
class ActivityStack extends Task {
final ActivityStackSupervisor mStackSupervisor;
//在Task的构造函数中初始化给mStackSupervisor 赋值
Task(ActivityTaskManagerService atmService,......){
mStackSupervisor = atmService.mStackSupervisor;
}
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
//启动流程:14, 进入下面resumeTopActivityInnerLocked
result = resumeTopActivityInnerLocked(prev, options);
}
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
//启动流程:15, 进入ActivityStackSupervisor.java -> startSpecificActivity
mStackSupervisor.startSpecificActivity(next, true, true);
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
public class ActivityStackSupervisor implements RecentTasks.Callbacks {
final ActivityTaskManagerService mService;
public ActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
mService = service;
......
}
void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) {
//启动流程:15, 再次回到ActivityTaskManagerService.java -> startProcessAsync
mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
}
}
frameworks/base/services/core/java/com/android/server/am/ProcessList.java
public final class ProcessList {
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
boolean knownToBeDead, int intentFlags, HostingRecord hostingRecord,
int zygotePolicyFlags, boolean allowWhileBooting, boolean isolated, int isolatedUid,
boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs,
Runnable crashHandler) {
//启动流程:19
final boolean success =
startProcessLocked(app, hostingRecord, zygotePolicyFlags, abiOverride);
}
//经过一系列的重载, 调用此startProcessLocked
boolean startProcessLocked(HostingRecord hostingRecord, String entryPoint, ProcessRecord app,
int uid, int[] gids, int runtimeFlags, int zygotePolicyFlags, int mountExternal,
String seInfo, String requiredAbi, String instructionSet, String invokeWith,
long startTime) {
final Process.ProcessStartResult startResult = startProcess(hostingRecord,
entryPoint, app,
uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo,
requiredAbi, instructionSet, invokeWith, startTime);
handleProcessStartedLocked(app, startResult.pid, startResult.usingWrapper,
startSeq, false);
}
private Process.ProcessStartResult startProcess(HostingRecord hostingRecord, String entryPoint,
ProcessRecord app, int uid, int[] gids, int runtimeFlags, int zygotePolicyFlags,
int mountExternal, String seInfo, String requiredAbi, String instructionSet,
String invokeWith, long startTime) {
if (hostingRecord.usesWebviewZygote()) {
} else if (hostingRecord.usesAppZygote()) {
} else {
//启动流程:20 ,启动Launcher进程
startResult = Process.start(entryPoint,
app.processName, uid, uid, gids, runtimeFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
app.info.dataDir, invokeWith, app.info.packageName, zygotePolicyFlags,
isTopApp, app.mDisabledCompatChanges, pkgDataInfoMap,
whitelistedAppDataInfoMap, bindMountAppsData, bindMountAppStorageDirs,
new String[]{PROC_START_SEQ_IDENT + app.startSeq});
}
}
}
|