原文鏈接:https://www.cnblogs.com/tiger-wang-ms/p/6517048.html
三腥例、handleResumeActivity()流程
在文章開頭貼出的第一段AcitityThread.handleLauncherActivity()方法的代碼中期虾,執(zhí)行完performLaunchAcitity()創(chuàng)建好Acitivity后,便會(huì)執(zhí)行到handleResumeActivity()方法休雌,該方法代碼如下。
final void handleResumeActivity(IBinder token,
? ? ? ? ? ? boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
? ? ? ? ...// TODO Push resumeArgs into the activity for consideration
? ? ? ? // 該方法執(zhí)行過(guò)程中會(huì)調(diào)用到Acitity的onResume()方法肝断,返回的ActivityClientRecord對(duì)象描述的即是創(chuàng)建好的Activity r = performResumeActivity(token, clearHide, reason);
? ? ? ? if (r != null) {
? ? ? ? ? ? final Activity a = r.activity;//返回之前創(chuàng)建的Acitivty
? ? ? ? ? ? if (localLOGV) Slog.v(
? ? ? ? ? ? ? ? TAG, "Resume " + r + " started activity: " +
? ? ? ? ? ? ? ? a.mStartedActivity + ", hideForNow: " + r.hideForNow
? ? ? ? ? ? ? ? + ", finished: " + a.mFinished);
? ? ? ? ? ? final int forwardBit = isForward ?
? ? ? ? ? ? ? ? ? ? WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
? ? ? ? ? ? // If the window hasn't yet been added to the window manager,
? ? ? ? ? ? // and this guy didn't finish itself or start another activity,
? ? ? ? ? ? // then go ahead and add the window. // 判斷該Acitivity是否可見(jiàn)挑辆,mStartedAcitity記錄的是一個(gè)Activity是否還處于啟動(dòng)狀態(tài) // 如果還處于啟動(dòng)狀態(tài)則mStartedAcitity為true例朱,表示該activity還未啟動(dòng)好,則該Activity還不可見(jiàn) boolean willBeVisible = !a.mStartedActivity; // 如果啟動(dòng)的組建不是全屏的鱼蝉,mStartedActivity也會(huì)是true洒嗤,此時(shí)依然需要willBeVisible為true以下的if邏輯就是針對(duì)這種情況的校正
? ? ? ? ? ? if (!willBeVisible) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
? ? ? ? ? ? ? ? ? ? ? ? ? ? a.getActivityToken());
? ? ? ? ? ? ? ? } catch (RemoteException e) {
? ? ? ? ? ? ? ? ? ? throw e.rethrowFromSystemServer();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (r.window == null && !a.mFinished && willBeVisible) {
? ? ? ? ? ? ? ? r.window = r.activity.getWindow();
? ? ? ? ? ? ? ? View decor = r.window.getDecorView();
? ? ? ? ? ? ? ? decor.setVisibility(View.INVISIBLE);
? ? ? ? ? ? ? ? ViewManager wm = a.getWindowManager();
? ? ? ? ? ? ? ? WindowManager.LayoutParams l = r.window.getAttributes();
? ? ? ? ? ? ? ? a.mDecor = decor;
? ? ? ? ? ? ? ? l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
? ? ? ? ? ? ? ? l.softInputMode |= forwardBit; //PreserverWindow,一般指主題換了或者configuration變了情況下的Acitity快速重啟機(jī)制
? ? ? ? ? ? ? ? if (r.mPreserveWindow) {
? ? ? ? ? ? ? ? ? ? a.mWindowAdded = true;
? ? ? ? ? ? ? ? ? ? r.mPreserveWindow = false;
? ? ? ? ? ? ? ? ? ? // Normally the ViewRoot sets up callbacks with the Activity
? ? ? ? ? ? ? ? ? ? // in addView->ViewRootImpl#setView. If we are instead reusing
? ? ? ? ? ? ? ? ? ? // the decor view we have to notify the view root that the
? ? ? ? ? ? ? ? ? ? // callbacks may have changed.
? ? ? ? ? ? ? ? ? ? ViewRootImpl impl = decor.getViewRootImpl();
? ? ? ? ? ? ? ? ? ? if (impl != null) {
? ? ? ? ? ? ? ? ? ? ? ? impl.notifyChildRebuilt();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (a.mVisibleFromClient && !a.mWindowAdded) {
? ? ? ? ? ? ? ? ? ? a.mWindowAdded = true; //調(diào)用了WindowManagerImpl的addView方法
? ? ? ? ? ? ? ? ? ? wm.addView(decor, l);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ...
? ? }
重點(diǎn)來(lái)看wm.addView()方法魁亦,該方法中的decor參數(shù)為Acitity對(duì)應(yīng)的Window中的視圖DecorView渔隶,wm為在創(chuàng)建PhoneWindow是創(chuàng)建的WindowManagerImpl對(duì)象,該對(duì)象的addView方法實(shí)際調(diào)用到到是單例對(duì)象WindowManagerGlobal的addView方法(前文有提到)洁奈。在看addView代碼前间唉,我先來(lái)看看WindowManagerGlobal對(duì)象成員變量。
private static WindowManagerGlobal sDefaultWindowManager;
? ? private static IWindowManager sWindowManagerService;
? ? private static IWindowSession sWindowSession;
? ? private final Object mLock = new Object();
? ? private final ArrayList<View> mViews = new ArrayList<View>();
? ? private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
? ? private final ArrayList<WindowManager.LayoutParams> mParams =
? ? ? ? ? ? new ArrayList<WindowManager.LayoutParams>();
? ? private final ArraySet<View> mDyingViews = new ArraySet<View>();
三個(gè)成員變量mViews利术、mRoots和mParams分別是類型為View呈野、ViewRootImpl和WindowManager.LayoutParams的數(shù)組。這里有這樣的邏輯關(guān)系印叁,每個(gè)View都對(duì)應(yīng)著唯一的一個(gè)ViewRootImpl和WindowManager.LayoutRarams被冒,即是1:1:1的關(guān)系。這三個(gè)數(shù)組長(zhǎng)度始終保持一致轮蜕,并且在同一個(gè)位置上存放的是互相關(guān)聯(lián)的View昨悼、ViewRootImpl和WindowManager.LayoutParams對(duì)象。此外還有一個(gè)成員變量mDyView跃洛,保存的則是已經(jīng)不需要但還未被系統(tǒng)會(huì)收到View率触。
View與LayoutParams比較好理解,那ViewRootImpl對(duì)象的作用是什么呢汇竭?首先WindowManagerImpl是作為管理類葱蝗,就像主管一樣,根據(jù)Acitity和Window的調(diào)用請(qǐng)求细燎,找到合適的做事的人两曼;DecorView本身是FrameworkLayout,本事是一個(gè)View找颓,所表示的是一種靜態(tài)的結(jié)構(gòu)合愈;所以這里就需要一個(gè)真正做事的人,那就是ViewRootImpl類的工作击狮》鹞觯總結(jié)來(lái)講ViewRootImpl的功能如下
1. 完成了繪制過(guò)程。在ViewRootImpl類中彪蓬,實(shí)現(xiàn)了perfromMeasure()寸莫、performDraw()、performLayout()等繪制相關(guān)的方法档冬。
2. 與系統(tǒng)服務(wù)進(jìn)行交互膘茎,例如與AcitityManagerSerivice桃纯,DisplayService、AudioService等進(jìn)行通信披坏,保證了Acitity相關(guān)功能等正常運(yùn)轉(zhuǎn)态坦。
3. 觸屏事件等分發(fā)邏輯的實(shí)現(xiàn)
接下來(lái)我們進(jìn)入WindowManagerGlobal.addView()方法的代碼。
public void addView(View view, ViewGroup.LayoutParams params,
? ? ? ? ? ? Display display, Window parentWindow) {
? ? ? ? ...
? ? ? ? ViewRootImpl root;
? ? ? ? View panelParentView = null;
? ? ? ? synchronized (mLock) { ...
? ? ? ? ? ? // If this is a panel window, then find the window it is being
? ? ? ? ? ? // attached to for future reference. // 如果當(dāng)前添加的是一個(gè)子視圖棒拂,則還需要找他他的父視圖 //這里我們分析的是添加DecorView的邏輯伞梯,沒(méi)有父視圖,故不會(huì)走到這里帚屉,panelParentView為null if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
? ? ? ? ? ? ? ? ? ? wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
? ? ? ? ? ? ? ? final int count = mViews.size();
? ? ? ? ? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? ? ? ? ? if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
? ? ? ? ? ? ? ? ? ? ? ? panelParentView = mViews.get(i);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? root = new ViewRootImpl(view.getContext(), display);
? ? ? ? ? ? view.setLayoutParams(wparams);
//保存互相對(duì)應(yīng)的View谜诫、ViewRootImpl、WindowManager.LayoutParams到數(shù)組中
? ? ? ? ? ? mViews.add(view);
? ? ? ? ? ? mRoots.add(root);
? ? ? ? ? ? mParams.add(wparams);
? ? ? ? ? ? // do this last because it fires off messages to start doing things
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? root.setView(view, wparams, panelParentView);
? ? ? ? ? ? } catch (RuntimeException e) {
? ? ? ? ? ? ? ? // BadTokenException or InvalidDisplayException, clean up.
? ? ? ? ? ? ? ? if (index >= 0) {
? ? ? ? ? ? ? ? ? ? removeViewLocked(index, true);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? throw e;
? ? ? ? ? ? }
? ? ? ? }
? ? }
關(guān)注代碼中加粗的兩個(gè)方法攻旦,首先會(huì)創(chuàng)建一個(gè)ViewRootImpl對(duì)象喻旷,然后調(diào)用ViewRootImpl.setView方法,其中panelParentView在addView參數(shù)為DecorView是為null牢屋。進(jìn)入ViewRootImpl.setView()代碼且预。?
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
? ? ? ? synchronized (this) {
? ? ? ? ? ? if (mView == null) { //初始化成員變量mView、mWindowAttraibutes //mAttachInfo是View類的一個(gè)內(nèi)部類AttachInfo類的對(duì)象? ? ? ? ? ? ? ? //該類的主要作用就是儲(chǔ)存一組當(dāng)View attach給它的父Window的時(shí)候Activity各種屬性的信息? ? ? ? ? ? ? ? mView = view;
? ? ? ? ? ? ? ? mAttachInfo.mDisplayState = mDisplay.getState();
? ? ? ? ? ? ? ? mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);
? ? ? ? ? ? ? ? mViewLayoutDirectionInitial = mView.getRawLayoutDirection();
? ? ? ? ? ? ? ? mFallbackEventHandler.setView(view);
? ? ? ? ? ? ? ? mWindowAttributes.copyFrom(attrs);
? ? ? ? ? ? ? ? ... //繼續(xù)初始化一些變量伟阔,包含針對(duì)panelParentView不為null時(shí)的父窗口的一些處理
mAdded = true;
? ? ? ? ? ? ? ? // Schedule the first layout -before- adding to the window
? ? ? ? ? ? ? ? // manager, to make sure we do the relayout before receiving
? ? ? ? ? ? ? ? // any other events from the system. // 這里調(diào)用異步刷新請(qǐng)求辣之,最終會(huì)調(diào)用performTraversals方法來(lái)完成View的繪制 requestLayout();
? ? ? ? ? ? ? ? if ((mWindowAttributes.inputFeatures
? ? ? ? ? ? ? ? ? ? ? ? & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
? ? ? ? ? ? ? ? ? ? mInputChannel = new InputChannel();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? mForceDecorViewVisibility = (mWindowAttributes.privateFlags
? ? ? ? ? ? ? ? ? ? ? ? & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0;
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? mOrigWindowType = mWindowAttributes.type;
? ? ? ? ? ? ? ? ? ? mAttachInfo.mRecomputeGlobalAttributes = true;
? ? ? ? ? ? ? ? ? ? collectViewAttributes();
? ? ? ? ? ? ? ? ? ? res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
? ? ? ? ? ? ? ? ? ? ? ? ? ? getHostVisibility(), mDisplay.getDisplayId(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
? ? ? ? ? ? ? ? ? ? ? ? ? ? mAttachInfo.mOutsets, mInputChannel);
? ? ? ? ? ? ? ? } catch (RemoteException e) {
? ? ? ? ? ? ? ? ? ? mAdded = false;
? ? ? ? ? ? ? ? ? ? mView = null;
? ? ? ? ? ? ? ? ? ? mAttachInfo.mRootView = null;
? ? ? ? ? ? ? ? ? ? mInputChannel = null;
? ? ? ? ? ? ? ? ? ? mFallbackEventHandler.setView(null);
? ? ? ? ? ? ? ? ? ? unscheduleTraversals();
? ? ? ? ? ? ? ? ? ? setAccessibilityFocus(null, null);
? ? ? ? ? ? ? ? ? ? throw new RuntimeException("Adding window failed", e);
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? if (restore) {
? ? ? ? ? ? ? ? ? ? ? ? attrs.restore();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ...
? ? ? ? ? ? }
? ? ? ? }
? ? }
相關(guān)變量初始化完成后掰伸,便會(huì)將mAdded設(shè)置為true皱炉,表示ViewRootImpl與setView傳入的View參數(shù)已經(jīng)做好了關(guān)聯(lián)。之后便會(huì)調(diào)用requestLayout()方法來(lái)請(qǐng)求一次異步刷新狮鸭,該方法后來(lái)又會(huì)調(diào)用到performTraversals()方法來(lái)完成view到繪制工作合搅。注意到這里雖然完成了繪制的工作,但是我們創(chuàng)建Activity的源頭是AMS中發(fā)起的歧蕉,我們從一開始創(chuàng)建Acitivity到相對(duì)應(yīng)的Window灾部、DecorView這一大套對(duì)象時(shí),還并未與AMS進(jìn)程進(jìn)行反饋惯退。所以之后便會(huì)調(diào)用mWindowSession.addToDisplay()方法會(huì)執(zhí)行IPC的跨進(jìn)程通信赌髓,最終調(diào)用到AMS中的addWindow方法來(lái)在系統(tǒng)進(jìn)程中執(zhí)行相關(guān)加載Window的操作。