自己看,如果不对,请指出!
image.pngframework/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
// setp 1 SystemServer启动
new SystemServer().run();
}
private void run() {
// ActivityManagerService内部通过继承自SystemService的静态内部类Lifecycle,创建ActivityManagerService对象
startBootstrapServices();
startCoreServices();
// step 2 这里启动很多服务,包括RuntimeService等等
startOtherServices();
}
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
// 启动AlarmManagerService,AudioService等几十个服务
// step 3 AMS对象调用systemReady(),这里开始启动Launcher流程
mActivityManagerService.systemReady(()->{
// 启动SystemUI,网络管理服务等
});
}
}
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
继承IActivityManager.aidl
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
public static final class Lifecycle extends SystemService {
public Lifecycle(Context context) {
super(context);
// 创建AMS对象,比较重要的点
mService = new ActivityManagerService(context, sAtm);
}
// 用于获取AMS对象
public ActivityManagerService getService() {
return mService;
}
}
public void setWindowManager(WindowManagerService wm) {
synchronized (this) {
// 绑定WindowManager
mWindowManager = wm;
mActivityTaskManager.setWindowManager(wm);
}
}
public void systemReady(final Runnable goingCallback, @NonNull TimingsTraceAndSlog t) {
if (bootingSystemUser) {
// step 4 开始在所有display上启动Launcher显示
// 调用ActivityTaskManagerService.LocalService.startHomeOnAllDisplays
// ActivityTaskManagerService对象由AMS构造函数从SystemServer传入
mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
}
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
继承IActivityTaskManager.aidl
public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
RootActivityContainer(ActivityTaskManagerService service) {
// 传入ActivityTaskManagerService 对象
mService = service;
mStackSupervisor = service.mStackSupervisor;
mStackSupervisor.mRootActivityContainer = this;
}
public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController,
Looper looper) {
// 获取RootActivityContainer对象
mRootActivityContainer = new RootActivityContainer(this);
}
final class LocalService extends ActivityTaskManagerInternal {
// step 5 调用mRootActivityContainer.startHomeOnAllDisplays()
@Override
public boolean startHomeOnAllDisplays(int userId, String reason) {
synchronized (mGlobalLock) {
return mRootActivityContainer.startHomeOnAllDisplays(userId, reason);
}
}
}
}
frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
class RootActivityContainer extends ConfigurationContainer
implements DisplayManager.DisplayListener {
boolean startHomeOnAllDisplays(int userId, String reason) {
boolean homeStarted = false;
// setp 6 遍历所有显示
for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {
final int displayId = mActivityDisplays.get(i).mDisplayId;
homeStarted |= startHomeOnDisplay(userId, reason, displayId);
}
return homeStarted;
}
boolean startHomeOnDisplay(int userId, String reason, int displayId) {
// step 7 每个显示单独启动
return startHomeOnDisplay(userId, reason, displayId, false /* allowInstrumenting */,
false /* fromHomeKey */);
}
boolean startHomeOnDisplay(int userId, String reason, int displayId, boolean allowInstrumenting,
boolean fromHomeKey) {
// step 8 ActivityTaskManagerService获取ActivityStartController调用startHomeActivity
mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason,
displayId);
return true;
}
}
frameworks/base/services/core/java/com/android/server/wm/AcitivityStartController.java
public class ActivityStartController {
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason, int displayId) {
// step 9 通过ActivityStarter对象调用execute
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)
.setOutActivity(tmpOutRecord)
.setCallingUid(0)
.setActivityInfo(aInfo)
.setActivityOptions(options.toBundle())
.execute();
}
ActivityStarter obtainStarter(Intent intent, String reason) {
// 获取ActivityStarter对象
return mFactory.obtain().setIntent(intent).setReason(reason);
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
class ActivityStarter {
int execute() {
// step 10 根据参数启动Acitivity
try {
if (mRequest.mayWait) {
// 这个后面也是调用的startAcitiviy
return startActivityMayWait(mRequest.caller, mRequest.callingUid,
mRequest.callingPackage, mRequest.realCallingPid, mRequest.realCallingUid,
mRequest.intent, mRequest.resolvedType,
mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,
mRequest.resultWho, mRequest.requestCode, mRequest.startFlags,
mRequest.profilerInfo, mRequest.waitResult, mRequest.globalConfig,
mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.userId,
mRequest.inTask, mRequest.reason,
mRequest.allowPendingRemoteAnimationRegistryLookup,
mRequest.originatingPendingIntent, mRequest.allowBackgroundActivityStart);
} else {
return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent,
mRequest.resolvedType, mRequest.activityInfo, mRequest.resolveInfo,
mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,
mRequest.resultWho, mRequest.requestCode, mRequest.callingPid,
mRequest.callingUid, mRequest.callingPackage, mRequest.realCallingPid,
mRequest.realCallingUid, mRequest.startFlags, mRequest.activityOptions,
mRequest.ignoreTargetSecurity, mRequest.componentSpecified,
mRequest.outActivity, mRequest.inTask, mRequest.reason,
mRequest.allowPendingRemoteAnimationRegistryLookup,
mRequest.originatingPendingIntent, mRequest.allowBackgroundActivityStart);
}
} finally {
onExecutionComplete();
}
}
private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity, boolean restrictedBgActivity) {
int result = START_CANCELED;
final ActivityStack startedActivityStack;
try {
mService.mWindowManager.deferSurfaceLayout();
// step 11
result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options, inTask, outActivity, restrictedBgActivity);
} finally {
}
postStartActivityProcessing(r, result, startedActivityStack);
return result;
}
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity, boolean restrictedBgActivity) {
// step 12
mRootActivityContainer.resumeFocusedStacksTopActivities(
mTargetStack, mStartActivity, mOptions);
}
}
frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
boolean resumeFocusedStacksTopActivities(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
if (targetStack != null && (targetStack.isTopStackOnDisplay()
|| getTopDisplayFocusedStack() == targetStack)) {
// step 13
result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
}
}
return result;
}
framework/base/services/core/java/com/android/server/wm/ActivityStack.java
class ActivityStack extends ConfigurationContainer {
ActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
int windowingMode, int activityType, boolean onTop) {
mStackSupervisor = supervisor;
}
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
boolean result = false;
try {
// Protect against recursion.
mInResumeTopActivity = true;
// step 14
result = resumeTopActivityInnerLocked(prev, options);
} finally {
mInResumeTopActivity = false;
}
return result;
}
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
// step 15
mStackSupervisor.startSpecificActivityLocked(next, true, true);
}
}
frameworks/base/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
public class ActivityStackSupervisor implements RecentTasks.Callbacks {
void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
// step 16 调用ActivityTaskManagerService发送message
// 通过抽象类ActivityManagerInternal,最终到达ActivityManagerService.java的LocalService
final Message msg = PooledLambda.obtainMessage(
ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,
r.info.applicationInfo, knownToBeDead, "activity", r.intent.getComponent());
mService.mH.sendMessage(msg);
}
}
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
@VisibleForTesting
public final class LocalService extends ActivityManagerInternal {
@Override
public void startProcess(String processName, ApplicationInfo info,
boolean knownToBeDead, String hostingType, ComponentName hostingName) {
try {
synchronized (ActivityManagerService.this) {
// step 17
startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */,
new HostingRecord(hostingType, hostingName),
false /* allowWhileBooting */, false /* isolated */,
true /* keepIfLarge */);
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
}
}
@GuardedBy("this")
final ProcessRecord startProcessLocked(String processName,
ApplicationInfo info, boolean knownToBeDead, int intentFlags,
HostingRecord hostingRecord, boolean allowWhileBooting,
boolean isolated, boolean keepIfLarge) {
// step 18
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
hostingRecord, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
null /* crashHandler */);
}
}
frameworks/base/services/core/java/com/android/server/am/ProcessList.java
public final class ProcessList {
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) {
// step 19
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 mountExternal,
String seInfo, String requiredAbi, String instructionSet, String invokeWith,
long startTime) {
// step 20
startResult = Process.start(entryPoint,
app.processName, uid, uid, gids, runtimeFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
app.info.dataDir, invokeWith, app.info.packageName,
new String[] {PROC_START_SEQ_IDENT + app.startSeq});
}
}
framework/base/core/java/android/os/Process.java
public class Process {
public static ProcessStartResult start(@NonNull final String processClass,
@Nullable final String niceName,
int uid, int gid, @Nullable int[] gids,
int runtimeFlags,
int mountExternal,
int targetSdkVersion,
@Nullable String seInfo,
@NonNull String abi,
@Nullable String instructionSet,
@Nullable String appDataDir,
@Nullable String invokeWith,
@Nullable String packageName,
@Nullable String[] zygoteArgs) {
// step 21
return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids,
runtimeFlags, mountExternal, targetSdkVersion, seInfo,
abi, instructionSet, appDataDir, invokeWith, packageName,
/*useUsapPool=*/ true, zygoteArgs);
}
}
framework/base/core/java/android/os/ZygoteProcess.java
public class ZygoteProcess {
public final Process.ProcessStartResult start(@NonNull final String processClass,
final String niceName,
int uid, int gid, @Nullable int[] gids,
int runtimeFlags, int mountExternal,
int targetSdkVersion,
@Nullable String seInfo,
@NonNull String abi,
@Nullable String instructionSet,
@Nullable String appDataDir,
@Nullable String invokeWith,
@Nullable String packageName,
boolean useUsapPool,
@Nullable String[] zygoteArgs) {
try {
// step 22
return startViaZygote(processClass, niceName, uid, gid, gids,
runtimeFlags, mountExternal, targetSdkVersion, seInfo,
abi, instructionSet, appDataDir, invokeWith, /*startChildZygote=*/ false,
packageName, useUsapPool, zygoteArgs);
} catch (ZygoteStartFailedEx ex) {
Log.e(LOG_TAG,
"Starting VM process through Zygote failed");
throw new RuntimeException(
"Starting VM process through Zygote failed", ex);
}
}
private Process.ProcessStartResult startViaZygote(@NonNull final String processClass,
@Nullable final String niceName,
final int uid, final int gid,
@Nullable final int[] gids,
int runtimeFlags, int mountExternal,
int targetSdkVersion,
@Nullable String seInfo,
@NonNull String abi,
@Nullable String instructionSet,
@Nullable String appDataDir,
@Nullable String invokeWith,
boolean startChildZygote,
@Nullable String packageName,
boolean useUsapPool,
@Nullable String[] extraArgs)
throws ZygoteStartFailedEx {
synchronized(mLock) {
// The USAP pool can not be used if the application will not use the systems graphics
// driver. If that driver is requested use the Zygote application start path.
// step 23
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),
useUsapPool,
argsForZygote);
}
}
private Process.ProcessStartResult zygoteSendArgsAndGetResult(
ZygoteState zygoteState, boolean useUsapPool, @NonNull ArrayList<String> args)
throws ZygoteStartFailedEx {
// step 24
return attemptZygoteSendArgsAndGetResult(zygoteState, msgStr);
}
private Process.ProcessStartResult attemptZygoteSendArgsAndGetResult(
ZygoteState zygoteState, String msgStr) throws ZygoteStartFailedEx {
try {
final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter;
final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream;
// step 25
zygoteWriter.write(msgStr);
zygoteWriter.flush();
return result;
} catch (IOException ex) {
}
}
}
网友评论