此源码基于 API 30
一:Android main Application和Activity创建
1. ActivityThread extends ClientTransactionHandler
main()->
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq);
Looper.loop();
attach() ->
final IActivityManager mgr = ActivityManager.getService();
try {
mgr.attachApplication(mAppThread, startSeq);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
2. ActivityManagerService
attachApplication()->attachApplicationLocked()->
1.thread.bindApplication() ->sendMessage(H.BIND_APPLICATION, data);
->handleBindApplication()->LoadedApk.makeApplication()
->mActivityThread.mInstrumentation.newApplication();
->Application app = getFactory(context.getPackageName())
.instantiateApplication(cl, className);
app.attach(context);
->mInstrumentation.callApplicationOnCreate(app);
-> Application.onCreate();
2. ActivityTaskManagerService.attachApplication()->
RootWindowContainer.attachApplication()->
startActivityForAttachedApplicationIfNeeded()->
ActivityStackSupervisor .realStartActivityLocked()->
ClientTransaction.obtain()->
clientTransaction.addCallback(LaunchActivityItem); ****
if (andResume) {
lifecycleItem = ResumeActivityItem .obtain(dc.isNextTransitionForward()); ****
} else {
lifecycleItem = PauseActivityItem.obtain();
}
ClientLifecycleManager.scheduleTransaction(clientTransaction)->
transaction.schedule();->
ApplicationThread.scheduleTransaction()->
(ActivityThread | ClientTransactionHandler).scheduleTransaction()->
sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);->
mTransactionExecutor.execute(transaction);->
executeCallbacks(transaction);
executeLifecycleState(transaction);
executeCallbacks()-> LaunchActivityItem .execute()->client.handleLaunchActivity()->
performLaunchActivity()->
activity = mInstrumentation.newActivity();
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
activity.attach() ***
mWindow = new PhoneWindow window view
installDecor();->generateDecor()->new DecorView()
mInstrumentation.callActivityOnCreate(activity, r.state);
activity.performCreate(icicle, persistentState); ****
if (persistentState != null) {
onCreate(icicle, persistentState);
} else {
onCreate(icicle);
}
onCreate() -> dispatchActivityCreated(savedInstanceState);
setState(ON_CREATE);
executeLifecycleState()->ResumeActivityItem.execute()->
ActivityThread. handleResumeActivity()->
performResumeActivity()->
r.activity.performResume() ***
callActivityOnResume()->activity.onResume();->
setState(ON_RESUME)->
//window 添加 DecorView
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
wm.addView(decor, l); ***
//最后显示
activity.makeVisible(); ->mDecor.setVisibility(View.VISIBLE);
//****
ViewManager wm = a.getWindowManager();
mWindowManager = mWindow.getWindowManager();
PhoneWindow.getWindowManager() -> mWindowManager
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
createLocalWindowManager() ->new WindowManagerImpl->
WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance()
addView()->mGlobal.addView
//最终wm.addView -> mGlobal.addView ****
mGlobal.addView()->
1.ViewRootImpl root; 创建
root = new ViewRootImpl(view.getContext(), display);
2.
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
//ViewRootImpl 和DecorView() 关联
3.root. setView(view, wparams, panelParentView);
1.requestLayout()
2.mWindowSession.addToDisplayAsUser():
WindowManagerService .addWindow()
//添加到WindowManagerService 管理
//View 测量 布局 绘制
requestLayout(); ****
1.checkThread();//检查是不是在主线程
2.scheduleTraversals()
//开启同步屏障
mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
//编舞者执行 mTraversalRunnable,
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
mChoreographer
//threadLocal存储
sSfThreadInstance.get()
ThreadLocal<Choreographer> sSfThreadInstance = new ThreadLocal<Choreographer>()
mHandler = new FrameHandler(looper);
//接收底层vsync信号
mDisplayEventReceiver =FrameDisplayEventReceiver
CallbackQueue[]
///放入队列
postCallback()->postCallbackDelayedInternal()->
mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);
if (dueTime <= now) {
//每16ms刷新一次
scheduleFrameLocked(now);->scheduleVsync()->nativeScheduleVsync()
} else {
Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
msg.arg1 = callbackType;
msg.setAsynchronous(true);
mHandler.sendMessageAtTime(msg, dueTime);
}
FrameDisplayEventReceiver
//接收 底层信号
onVsync()->Message msg = Message.obtain(mHandler, this);->
mHandler.sendMessageAtTime(msg,no)
run()->doFrame()->
//取出CallbackQueue里的 最终执行 TraversalRunnable的run方法
->doCallbacks ->CallbackRecord.run() ->TraversalRunnable.run
TraversalRunnable
doTraversal()-
//移除同步屏障
-> mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
->performTraversals();->
performMeasure()->
mView.measure()
performLayout()->
host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
performDraw()->
draw()
ViewRootImpl impl = r.window.getDecorView().getViewRootImpl();
网友评论