java绘ui_ui绘制流程1

----->SystemServer.run()

------------>1.createSystemContext()是其他service启动的context来源

ActivityThread activityThread = ActivityThread.systemMain()创纪了系统进程的ActivityThread

--------------------->Context mSystemContext = activityThread.getSystemContext();

---------------------> mSystemContext = ContextImpl.createSystemContext(this)

---------------------> 最终通过new创建了LoadedApk和ContextImpl

------------>2. 实例化SystemServiceManager 用于服务启动

private Context mSystemContext;

private void run() {

try { ....

// Initialize the system context.

createSystemContext();

// Create the system service manager.

mSystemServiceManager = new SystemServiceManager(mSystemContext);

mSystemServiceManager.setStartInfo(mRuntimeRestart,

mRuntimeStartElapsedTime, mRuntimeStartUptime);

LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);

// Prepare the thread pool for init tasks that can be parallelized

SystemServerInitThreadPool.get();

} finally {

traceEnd(); // InitBeforeStartServices

}

// Start services.

try {

traceBeginAndSlog("StartServices");

startBootstrapServices();

startCoreServices();

startOtherServices();

SystemServerInitThreadPool.shutdown();

} catch (Throwable ex) {

Slog.e("System", "******************************************");

Slog.e("System", "************ Failure starting system services", ex);

throw ex;

} finally {

traceEnd();

}

}

private void createSystemContext() {

ActivityThread activityThread = ActivityThread.systemMain();

mSystemContext = activityThread.getSystemContext();

mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);

final Context systemUiContext = activityThread.getSystemUiContext();

systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);

}

static ContextImpl createSystemContext(ActivityThread mainThread) {

LoadedApk packageInfo = new LoadedApk(mainThread);

ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,

null);

context.setResources(packageInfo.getResources());

context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),

context.mResourcesManager.getDisplayMetrics());

return context;

}

------------>3. startBootstrapServices()

// SystemServer.run()内部

------------------>mActivityManagerService =

mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService();

------------------------>startService(Class serviceClass)

------------------------>最终反射实例调用,因为ActivityManagerService.Lifecycle extends SystemService service.onStart()

------------------------>ActivityManagerService.Lifecycle.onStart()

------------------------>因为构造Lifecycle时内部mService = new ActivityManagerService(context, sAtm);

而方法内部调用了mService.start();

ActivityManagerService 实例化时创建了

ActivityStackSupervisor mStackSupervisor = createStackSupervisor();

------------>2.startOtherServices()

--------------------->mActivityManagerService.systemReady(final Runnable goingCallback, TimingsTraceLog traceLog)

--------------------->startHomeActivityLocked(currentUserId, "systemReady")

mActivityManagerService.systemReady(() -> {....}, BOOT_TIMINGS_TRACE_LOG);

--------------------->mActivityStartController.startHomeActivity(intent, aInfo, myReason);

在mActivityManagerService构造方法中实例化mActivityStartController = new ActivityStartController(this);

class ActivityStarter {

static class DefaultFactory implements Factory {

private final int MAX_STARTER_COUNT = 3;

private ActivityStartController mController;

private ActivityManagerService mService;

private ActivityStackSupervisor mSupervisor;

private ActivityStartInterceptor mInterceptor;

private SynchronizedPool mStarterPool =

new SynchronizedPool<>(MAX_STARTER_COUNT);

DefaultFactory(ActivityManagerService service,

ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {

mService = service;

mSupervisor = supervisor;

mInterceptor = interceptor;

}

@Override

public void setController(ActivityStartController controller) {

mController = controller;

}

@Override

public ActivityStarter obtain() {

ActivityStarter starter = mStarterPool.acquire();

if (starter == null) {

starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);

}

return starter;

}

@Override

public void recycle(ActivityStarter starter) {

starter.reset(true /* clearRequest*/);

mStarterPool.release(starter);

}

}

}

public class ActivityStartController {

ActivityStartController(ActivityManagerService service) {

this(service, service.mStackSupervisor,

new DefaultFactory(service, service.mStackSupervisor,

new ActivityStartInterceptor(service, service.mStackSupervisor)));

}

@VisibleForTesting

ActivityStartController(ActivityManagerService service, ActivityStackSupervisor supervisor,

Factory factory) {

mService = service;

mSupervisor = supervisor;

mHandler = new StartHandler(mService.mHandlerThread.getLooper());

mFactory = factory;

mFactory.setController(this);

mPendingRemoteAnimationRegistry = new PendingRemoteAnimationRegistry(service,

service.mHandler);

}

void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {

mSupervisor.moveHomeStackTaskToTop(reason);

mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)

.setOutActivity(tmpOutRecord)

.setCallingUid(0)

.setActivityInfo(aInfo)

.execute();

mLastHomeActivityStartRecord = tmpOutRecord[0];

if (mSupervisor.inResumeTopActivity) {

// If we are in resume section already, home activity will be initialized, but not

// resumed (to avoid recursive resume) and will stay that way until something pokes it

// again. We need to schedule another resume.

mSupervisor.scheduleResumeTopActivities();

}

}

ActivityStarter obtainStarter(Intent intent, String reason) {

return mFactory.obtain().setIntent(intent).setReason(reason);

}

}

// ActivityManagerService.java的构造方法内部创建

mStackSupervisor = createStackSupervisor();

protected ActivityStackSupervisor createStackSupervisor() {

final ActivityStackSupervisor supervisor = new ActivityStackSupervisor(this, mHandler.getLooper());

supervisor.initialize();

return supervisor;

}

---------------------> mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)

.setOutActivity(tmpOutRecord)

.setCallingUid(0)

.setActivityInfo(aInfo)

.execute();

--------------------------->obtainStarter(Intent intent, String reason)

// ActivityStartController.java

--------------------------->mFactory.obtain().setIntent(intent).setReason(reason)

--------------------------->是ActivityStarter.DefaultFactory的内部类,在ActivityStartController构造方法内部实例化

--------------------------->mFactory.obtain()返回一个ActivityStarter,要么从pool拿要么新建一个,后面跟的方法返回ActivityStarter自己

--------------------->setOutActivity(tmpOutRecord).setCallingUid(0).setActivityInfo(aInfo)也是返回ActivityStarter自己

---------------------> execute() //ActivityStarter.java

---------------------> 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);

//ActivityStarter.java

---------------------> mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,

aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,

callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,

options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,

inTask, allowPendingRemoteAnimationRegistryLookup);

//ActivityStarter.java

// 方法内部创建了ActivityRecord

---------------------> startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,

true /* doResume */, checkedOptions, inTask, outActivity)

//ActivityStarter.java

--------------------->result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,

startFlags, doResume, options, inTask, outActivity);

class ActivityStarter {

private int startActivity(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,

SafeActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,

ActivityRecord[] outActivity, TaskRecord inTask, String reason,

boolean allowPendingRemoteAnimationRegistryLookup) {

...

mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,

aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,

callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,

options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,

inTask, allowPendingRemoteAnimationRegistryLookup);

...

}

private int startActivity(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,

SafeActivityOptions options,

boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,

TaskRecord inTask, boolean allowPendingRemoteAnimationRegistryLookup) {

...

ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,

callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),

resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,

mSupervisor, checkedOptions, sourceRecord);

....

return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,

true /* doResume */, checkedOptions, inTask, outActivity);

}

private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,

IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,

ActivityRecord[] outActivity) {

...

result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,

startFlags, doResume, options, inTask, outActivity);

...

}

private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,

IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,

ActivityRecord[] outActivity) {

setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,

voiceInteractor);

...

// 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;

result = setTaskFromReuseOrCreateNewTask(taskToAffiliate, topStack);

} else if (mSourceRecord != null) {

result = setTaskFromSourceRecord();

} 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();

}

if (result != START_SUCCESS) {

return result;

}

...

if (mDoResume) {

final ActivityRecord topTaskActivity =

mStartActivity.getTask().topRunningActivityLocked();

if (!mTargetStack.isFocusable()

|| (topTaskActivity != null && topTaskActivity.mTaskOverlay

&& mStartActivity != topTaskActivity)) {

// If the activity is not focusable, we can't resume it, but still would like to

// make sure it becomes visible as it starts (this will also trigger entry

// animation). An example of this are PIP activities.

// Also, we don't want to resume activities in a task that currently has an overlay

// as the starting activity just needs to be in the visible paused state until the

// over is removed.

mTargetStack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);

// Go ahead and tell window manager to execute app transition for this activity

// since the app transition will not be triggered through the resume channel.

mService.mWindowManager.executeAppTransition();

} else {

// If the target stack was not previously focusable (previous top running activity

// on that stack was not visible) then any prior calls to move the stack to the

// will not update the focused stack. If starting the new activity now allows the

// task stack to be focusable, then ensure that we now update the focused stack

// accordingly.

if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {

mTargetStack.moveToFront("startActivityUnchecked");

}

mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,

mOptions);

}

} else if (mStartActivity != null) {

mSupervisor.mRecentTasks.add(mStartActivity.getTask());

}

mSupervisor.updateUserStackLocked(mStartActivity.userId, mTargetStack);

mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(), preferredWindowingMode,

preferredLaunchDisplayId, mTargetStack);

return START_SUCCESS;

}

private void setInitialState(ActivityRecord r, ActivityOptions options, TaskRecord inTask,

boolean doResume, int startFlags, ActivityRecord sourceRecord,

IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor) {

reset(false /* clearRequest */);

mStartActivity = r;

mIntent = r.intent;

mOptions = options;

mCallingUid = r.launchedFromUid;

mSourceRecord = sourceRecord;

mVoiceSession = voiceSession;

mVoiceInteractor = voiceInteractor;

mPreferredDisplayId = getPreferedDisplayId(mSourceRecord, mStartActivity, options);

mLaunchParams.reset();

mSupervisor.getLaunchParamsController().calculate(inTask, null /*layout*/, r, sourceRecord,

options, mLaunchParams);

mLaunchMode = r.launchMode;

mLaunchFlags = adjustLaunchFlagsToDocumentMode(

r, LAUNCH_SINGLE_INSTANCE == mLaunchMode,

LAUNCH_SINGLE_TASK == mLaunchMode, mIntent.getFlags());

mLaunchTaskBehind = r.mLaunchTaskBehind

&& !isLaunchModeOneOf(LAUNCH_SINGLE_TASK, LAUNCH_SINGLE_INSTANCE)

&& (mLaunchFlags & FLAG_ACTIVITY_NEW_DOCUMENT) != 0;

sendNewTaskResultRequestIfNeeded();

...

mDoResume = doResume;

...

mNotTop = (mLaunchFlags & FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;

mInTask = inTask;

mStartFlags = startFlags;

...

mNoAnimation = (mLaunchFlags & FLAG_ACTIVITY_NO_ANIMATION) != 0;

}

}

---------------------> mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack,

mStartActivity, mOptions)

// mSupervisor是在AMS中创建最终传入ActivityStarter

// ActivityStackSupervisor.java

// mTargetStack 通过新建TaskRecord创建

// TO do有空细化

public class ActivityStackSupervisor extends ConfigurationContainer implements DisplayListener,

RecentTasks.Callbacks {

boolean resumeFocusedStackTopActivityLocked(

ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {

...

if (targetStack != null && isFocusedStack(targetStack)) {

return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);

}

final ActivityRecord r = mFocusedStack.topRunningActivityLocked();

if (r == null || !r.isState(RESUMED)) {

mFocusedStack.resumeTopActivityUncheckedLocked(null, null);

} else if (r.isState(RESUMED)) {

// Kick off any lingering app transitions form the MoveTaskToFront operation.

mFocusedStack.executeAppTransition(targetOptions);

}

return false;

}

}

---------------------> targetStack.resumeTopActivityInnerLocked(target,

targetOptions)

// ActivityStack.java

class ActivityStack extends ConfigurationContainer

implements StackWindowListener {

@GuardedBy("mService")

private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {

...

final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);

...

// If the most recent activity was noHistory but was only stopped rather

// than stopped+finished because the device went to sleep, we need to make

// sure to finish it as we're making a new activity topmost.

if (shouldSleepActivities() && mLastNoHistoryActivity != null &&

!mLastNoHistoryActivity.finishing) {

if (DEBUG_STATES) Slog.d(TAG_STATES,

"no-history finish of " + mLastNoHistoryActivity + " on new resume");

requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,

null, "resume-no-history", false);

mLastNoHistoryActivity = null;

}

ActivityStack lastStack = mStackSupervisor.getLastStack();

if (next.app != null && next.app.thread != null) {

synchronized(mWindowManager.getWindowManagerLock()) {

...

if (mStackSupervisor.isFocusedStack(this)) {

notUpdated = !mStackSupervisor.ensureVisibilityAndConfig(next, mDisplayId,

true /* markFrozenIfConfigChanged */, false /* deferResume */);

}

if (notUpdated) {

...

if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

return true;

}

try {

final ClientTransaction transaction = ClientTransaction.obtain(next.app.thread,

next.appToken);

...

mService.getLifecycleManager().scheduleTransaction(transaction);

...

} catch (Exception e) {

...

mStackSupervisor.startSpecificActivityLocked(next, true, false);

if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

return true;

}

}

...

} else {

...

if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);

mStackSupervisor.startSpecificActivityLocked(next, true, true);

}

if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

return true;

}

}

---------------------> mStackSupervisor.startSpecificActivityLocked(target,

targetOptions)

// ActivityStackSupervisor.java

void startSpecificActivityLocked(ActivityRecord r,

boolean andResume, boolean checkConfig) {

// Is this activity's application already running?

ProcessRecord app = mService.getProcessRecordLocked(r.processName,

r.info.applicationInfo.uid, true);

getLaunchTimeTracker().setLaunchTime(r);

if (app != null && app.thread != null) {

try {

if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0

|| !"android".equals(r.info.packageName)) {

// Don't add this if it is a platform component that is marked

// to run in multiple processes, because this is actually

// part of the framework so doesn't make sense to track as a

// separate apk in the process.

app.addPackage(r.info.packageName, r.info.applicationInfo.longVersionCode,

mService.mProcessStats);

}

realStartActivityLocked(r, app, andResume, checkConfig);

return;

} catch (RemoteException e) {

Slog.w(TAG, "Exception when starting activity "

+ r.intent.getComponent().flattenToShortString(), e);

}

// If a dead object exception was thrown -- fall through to

// restart the application.

}

mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,

"activity", r.intent.getComponent(), false, false, true);

}

-------->Service.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,

"activity", r.intent.getComponent(), false, false, true)

// ActivityManagerService.java

// 经过一阵startProcessLocked重载方法传递

public class ActivityManagerService extends IActivityManager.Stub

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

@GuardedBy("this")

final ProcessRecord startProcessLocked(String processName,

ApplicationInfo info, boolean knownToBeDead, int intentFlags,

String hostingType, ComponentName hostingName, boolean allowWhileBooting,

boolean isolated, boolean keepIfLarge) {

return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType,

hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,

null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,

null /* crashHandler */);

}

@GuardedBy("this")

final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,

boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,

boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,

String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {

long startTime = SystemClock.elapsedRealtime();

ProcessRecord app;

...

// We don't have to do anything more if:

// (1) There is an existing application record; and

// (2) The caller doesn't think it is dead, OR there is no thread

// object attached to it so we know it couldn't have crashed; and

// (3) There is a pid assigned to it, so it is either starting or

// already running.

....

checkTime(startTime, "startProcess: stepping in to startProcess");

final boolean success = startProcessLocked(app, hostingType, hostingNameStr, abiOverride);

checkTime(startTime, "startProcess: done starting proc!");

return success ? app : null;

}

}

@GuardedBy("this")

private final boolean startProcessLocked(ProcessRecord app,

String hostingType, String hostingNameStr, String abiOverride) {

return startProcessLocked(app, hostingType, hostingNameStr,

false /* disableHiddenApiChecks */, abiOverride);

}

@GuardedBy("this")

private final boolean startProcessLocked(ProcessRecord app, String hostingType,

String hostingNameStr, boolean disableHiddenApiChecks, String abiOverride) {

....

return startProcessLocked(hostingType, hostingNameStr, entryPoint, app, uid, gids,

runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith,

startTime);

...

}

private boolean startProcessLocked(String hostingType, String hostingNameStr, String entryPoint,

ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,

String seInfo, String requiredAbi, String instructionSet, String invokeWith,

long startTime) {

app.pendingStart = true;

app.killedByAm = false;

app.removed = false;

app.killed = false;

final long startSeq = app.startSeq = ++mProcStartSeqCounter;

app.setStartParams(uid, hostingType, hostingNameStr, seInfo, startTime);

if (mConstants.FLAG_PROCESS_START_ASYNC) {

...

mProcStartHandler.post(() -> {

....

final ProcessStartResult startResult = startProcess(app.hostingType, entryPoint,

app, app.startUid, gids, runtimeFlags, mountExternal, app.seInfo,

requiredAbi, instructionSet, invokeWith, app.startTime);

...

});

return true;

} else {

...

final ProcessStartResult startResult = startProcess(hostingType, entryPoint, app,

uid, gids, runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet,

invokeWith, startTime);

...

}

}

----->startProcess(hostingType, entryPoint, app,

uid, gids, runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet,

invokeWith, startTime)

private ProcessStartResult startProcess(String hostingType, String entryPoint,

ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,

String seInfo, String requiredAbi, String instructionSet, String invokeWith,

long startTime) {

try {

....

if (hostingType.equals("webview_service")) {

startResult = startWebView(entryPoint,

app.processName, uid, uid, gids, runtimeFlags, mountExternal,

app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,

app.info.dataDir, null,

new String[] {PROC_START_SEQ_IDENT + app.startSeq});

} else {

startResult = Process.start(entryPoint,

app.processName, uid, uid, gids, runtimeFlags, mountExternal,

app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,

app.info.dataDir, invokeWith,

new String[] {PROC_START_SEQ_IDENT + app.startSeq});

}

...

return startResult;

} finally {

Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

}

}

------------>Process.start(entryPoint,

app.processName, uid, uid, gids, runtimeFlags, mountExternal,

app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,

app.info.dataDir, invokeWith,

new String[] {PROC_START_SEQ_IDENT + app.startSeq})

public static final ProcessStartResult start(final String processClass,

final String niceName,

int uid, int gid, int[] gids,

int runtimeFlags, int mountExternal,

int targetSdkVersion,

String seInfo,

String abi,

String instructionSet,

String appDataDir,

String invokeWith,

String[] zygoteArgs) {

return zygoteProcess.start(processClass, niceName, uid, gid, gids,

runtimeFlags, mountExternal, targetSdkVersion, seInfo,

abi, instructionSet, appDataDir, invokeWith, zygoteArgs);

}

------>zygoteProcess.start(processClass, niceName, uid, gid, gids,

runtimeFlags, mountExternal, targetSdkVersion, seInfo,

abi, instructionSet, appDataDir, invokeWith, zygoteArgs)

------->startViaZygote(processClass, niceName, uid, gid, gids,

runtimeFlags, mountExternal, targetSdkVersion, seInfo,

abi, instructionSet, appDataDir, invokeWith, false /* startChildZygote */,

zygoteArgs)

------->startViaZygote(processClass, niceName, uid, gid, gids,

runtimeFlags, mountExternal, targetSdkVersion, seInfo,

abi, instructionSet, appDataDir, invokeWith, false /* startChildZygote /,

zygoteArgs)

------->zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote)

openZygoteSocketIfNeeded(abi)方法使用socket与ZygoteProcess进程通信,

zygoteSendArgsAndGetResult方法主要写入参数

其中processClass ="android.app.ActivityThread",通过Zygote进程来Fork出一个新的进程,并执行 "android.app.ActivityThread"的main方法*

public class ZygoteProcess {

public final Process.ProcessStartResult start(final String processClass,

final String niceName,

int uid, int gid, int[] gids,

int runtimeFlags, int mountExternal,

int targetSdkVersion,

String seInfo,

String abi,

String instructionSet,

String appDataDir,

String invokeWith,

String[] zygoteArgs) {

try {

return startViaZygote(processClass, niceName, uid, gid, gids,

runtimeFlags, mountExternal, targetSdkVersion, seInfo,

abi, instructionSet, appDataDir, invokeWith, false /* startChildZygote */,

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(final String processClass,

final String niceName,

final int uid, final int gid,

final int[] gids,

int runtimeFlags, int mountExternal,

int targetSdkVersion,

String seInfo,

String abi,

String instructionSet,

String appDataDir,

String invokeWith,

boolean startChildZygote,

String[] extraArgs)

throws ZygoteStartFailedEx {

ArrayList argsForZygote = new ArrayList();

// --runtime-args, --setuid=, --setgid=,

// and --setgroups= must go first

argsForZygote.add("--runtime-args");

argsForZygote.add("--setuid=" + uid);

argsForZygote.add("--setgid=" + gid);

argsForZygote.add("--runtime-flags=" + runtimeFlags);

if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) {

argsForZygote.add("--mount-external-default");

} else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) {

argsForZygote.add("--mount-external-read");

} else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) {

argsForZygote.add("--mount-external-write");

}

argsForZygote.add("--target-sdk-version=" + targetSdkVersion);

// --setgroups is a comma-separated list

if (gids != null && gids.length > 0) {

StringBuilder sb = new StringBuilder();

sb.append("--setgroups=");

int sz = gids.length;

for (int i = 0; i < sz; i++) {

if (i != 0) {

sb.append(',');

}

sb.append(gids[i]);

}

argsForZygote.add(sb.toString());

}

if (niceName != null) {

argsForZygote.add("--nice-name=" + niceName);

}

if (seInfo != null) {

argsForZygote.add("--seinfo=" + seInfo);

}

if (instructionSet != null) {

argsForZygote.add("--instruction-set=" + instructionSet);

}

if (appDataDir != null) {

argsForZygote.add("--app-data-dir=" + appDataDir);

}

if (invokeWith != null) {

argsForZygote.add("--invoke-with");

argsForZygote.add(invokeWith);

}

if (startChildZygote) {

argsForZygote.add("--start-child-zygote");

}

argsForZygote.add(processClass);

if (extraArgs != null) {

for (String arg : extraArgs) {

argsForZygote.add(arg);

}

}

synchronized(mLock) {

return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);

}

}

private static Process.ProcessStartResult zygoteSendArgsAndGetResult(

ZygoteState zygoteState, ArrayList args)

throws ZygoteStartFailedEx {

try {

// Throw early if any of the arguments are malformed. This means we can

// avoid writing a partial response to the zygote.

int sz = args.size();

for (int i = 0; i < sz; i++) {

if (args.get(i).indexOf('\n') >= 0) {

throw new ZygoteStartFailedEx("embedded newlines not allowed");

}

}

/**

* See com.android.internal.os.SystemZygoteInit.readArgumentList()

* Presently the wire format to the zygote process is:

* a) a count of arguments (argc, in essence)

* b) a number of newline-separated argument strings equal to count

*

* After the zygote process reads these it will write the pid of

* the child or -1 on failure, followed by boolean to

* indicate whether a wrapper process was used.

*/

final BufferedWriter writer = zygoteState.writer;

final DataInputStream inputStream = zygoteState.inputStream;

writer.write(Integer.toString(args.size()));

writer.newLine();

for (int i = 0; i < sz; i++) {

String arg = args.get(i);

writer.write(arg);

writer.newLine();

}

writer.flush();

// Should there be a timeout on this?

Process.ProcessStartResult result = new Process.ProcessStartResult();

// Always read the entire result from the input stream to avoid leaving

// bytes in the stream for future process starts to accidentally stumble

// upon.

result.pid = inputStream.readInt();

result.usingWrapper = inputStream.readBoolean();

if (result.pid < 0) {

throw new ZygoteStartFailedEx("fork() failed");

}

return result;

} catch (IOException ex) {

zygoteState.close();

throw new ZygoteStartFailedEx(ex);

}

}

@GuardedBy("mLock")

private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {

Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held");

if (primaryZygoteState == null || primaryZygoteState.isClosed()) {

try {

primaryZygoteState = ZygoteState.connect(mSocket);

} catch (IOException ioe) {

throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);

}

maybeSetApiBlacklistExemptions(primaryZygoteState, false);

maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState);

}

if (primaryZygoteState.matches(abi)) {

return primaryZygoteState;

}

// The primary zygote didn't match. Try the secondary.

if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {

try {

secondaryZygoteState = ZygoteState.connect(mSecondarySocket);

} catch (IOException ioe) {

throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);

}

maybeSetApiBlacklistExemptions(secondaryZygoteState, false);

maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState);

}

if (secondaryZygoteState.matches(abi)) {

return secondaryZygoteState;

}

throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);

}

public static class ZygoteState {

public static ZygoteState connect(LocalSocketAddress address) throws IOException {

DataInputStream zygoteInputStream = null;

BufferedWriter zygoteWriter = null;

final LocalSocket zygoteSocket = new LocalSocket();

try {

zygoteSocket.connect(address);

zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream());

zygoteWriter = new BufferedWriter(new OutputStreamWriter(

zygoteSocket.getOutputStream()), 256);

} catch (IOException ex) {

try {

zygoteSocket.close();

} catch (IOException ignore) {

}

throw ex;

}

String abiListString = getAbiList(zygoteWriter, zygoteInputStream);

Log.i("Zygote", "Process: zygote socket " + address.getNamespace() + "/"

+ address.getName() + " opened, supported ABIS: " + abiListString);

return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter,

Arrays.asList(abiListString.split(",")));

}

}

}

Zygote fork一个Launcher进程的阶段

SystemServer的AMS服务向启动Home Activity发起一个fork请求,Zygote进程通过Linux的fork函数,孵化出一个新的进程。由于Zygote进程在启动时会创建Java虚拟机,因此通过fork而创建的Launcher程序进程可以在内部获取一个Java虚拟机的实例拷贝。fork采用copy-on-write机制,有些类如果不做改变,甚至都不用复制,子进程可以和父进程共享这部分数据,从而省去不少内存的占用。

**[ZygoteInit.java] main()****

说明:**Zygote先fork出SystemServer进程,接着进入循环等待,用来接收Socket发来的消息,用来fork出其他应用进程,比如Launcher

public static void main(String argv[]) {

...

Runnable caller;

....

if (startSystemServer) {

//Zygote Fork出的第一个进程 SystmeServer

Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);

if (r != null) {

r.run();

return;

}

}

...

//循环等待fork出其他的应用进程,比如Launcher

//最终通过调用processOneCommand()来进行进程的处理

caller = zygoteServer.runSelectLoop(abiList);

...

if (caller != null) {

caller.run(); //执行返回的Runnable对象,进入子进程

}

}

[ZygoteConnection.java] processOneCommand()

说明:通过forkAndSpecialize()来fork出Launcher的子进程,并执行handleChildProc,进入子进程的处理

Runnable processOneCommand(ZygoteServer zygoteServer) {

int pid = -1;

...

//Fork子进程,得到一个新的pid

/fork子进程,采用copy on write方式,这里执行一次,会返回两次

///pid=0 表示Zygote fork子进程成功

//pid > 0 表示子进程 的真正的PID

pid = Zygote.forkAndSpecialize(parsedArgs.mUid, parsedArgs.mGid, parsedArgs.mGids,

parsedArgs.mRuntimeFlags, rlimits, parsedArgs.mMountExternal, parsedArgs.mSeInfo,

parsedArgs.mNiceName, fdsToClose, fdsToIgnore, parsedArgs.mStartChildZygote,

parsedArgs.mInstructionSet, parsedArgs.mAppDataDir, parsedArgs.mTargetSdkVersion);

...

if (pid == 0) {

// in child, fork成功,第一次返回的pid = 0

...

return handleChildProc(parsedArgs, descriptors, childPipeFd,

parsedArgs.mStartChildZygote);

} else {

//in parent

...

childPipeFd = null;

handleParentProc(pid, descriptors, serverPipeFd);

return null;

}

}

[ZygoteConnection.java] handleChildProc()

说明:进行子进程的操作,最终获得需要执行的ActivityThread的main()

private Runnable handleChildProc(ZygoteArguments parsedArgs, FileDescriptor[] descriptors,

FileDescriptor pipeFd, boolean isZygote) {

...

if (parsedArgs.mInvokeWith != null) {

...

throw new IllegalStateException("WrapperInit.execApplication unexpectedly returned");

} else {

if (!isZygote) {

// App进程将会调用到这里,执行目标类的main()方法

return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,

parsedArgs.mRemainingArgs, null /* classLoader */);

} else {

return ZygoteInit.childZygoteInit(parsedArgs.mTargetSdkVersion,

parsedArgs.mRemainingArgs, null /* classLoader */);

}

}

}

zygoteInit 进行一些环境的初始化、启动Binder进程等操作:

public static final Runnable zygoteInit(int targetSdkVersion, String[] argv,

ClassLoader classLoader) {

RuntimeInit.commonInit(); //初始化运行环境

ZygoteInit.nativeZygoteInit(); //启动Binder线程池

//调用程序入口函数

return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);

}

把之前传来的"android.app.ActivityThread" 传递给findStaticMain:

protected static Runnable applicationInit(int targetSdkVersion, String[] argv,

ClassLoader classLoader) {

...

// startClass: 如果AMS通过socket传递过来的是 ActivityThread

return findStaticMain(args.startClass, args.startArgs, classLoader);

}

通过反射,拿到ActivityThread的main()方法:

protected static Runnable findStaticMain(String className, String[] argv,

ClassLoader classLoader) {

Class> cl;

try {

cl = Class.forName(className, true, classLoader);

} catch (ClassNotFoundException ex) {

throw new RuntimeException(

"Missing class when invoking static main " + className,

ex);

}

Method m;

try {

m = cl.getMethod("main", new Class[] { String[].class });

} catch (NoSuchMethodException ex) {

throw new RuntimeException(

"Missing static main on " + className, ex);

} catch (SecurityException ex) {

throw new RuntimeException(

"Problem getting static main on " + className, ex);

}

int modifiers = m.getModifiers();

if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {

throw new RuntimeException(

"Main method is not public and static on " + className);

}

return new MethodAndArgsCaller(m, argv);

}

把反射得来的ActivityThread main()入口返回给ZygoteInit的main,通过caller.run()进行调用:

static class MethodAndArgsCaller implements Runnable {

/** method to call */

private final Method mMethod;

/** argument array */

private final String[] mArgs;

public MethodAndArgsCaller(Method method, String[] args) {

mMethod = method;

mArgs = args;

}

//调用ActivityThread的main()

public void run() {

try {

mMethod.invoke(null, new Object[] { mArgs });

} catch (IllegalAccessException ex) {

throw new RuntimeException(ex);

} catch (InvocationTargetException ex) {

Throwable cause = ex.getCause();

if (cause instanceof RuntimeException) {

throw (RuntimeException) cause;

} else if (cause instanceof Error) {

throw (Error) cause;

}

throw new RuntimeException(ex);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值