原文轉載:http://www.javashuo.com/article/p-mxdqjswj-cv.htmlhtml
在文章開頭貼出的第一段AcitityThread.handleLauncherActivity()方法的代碼中,執行完performLaunchAcitity()建立好Acitivity後,便會執行到handleResumeActivity()方法,該方法代碼以下。
數組
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) { ...// TODO Push resumeArgs into the activity for consideration // 該方法執行過程當中會調用到Acitity的onResume()方法,返回的ActivityClientRecord對象描述的便是建立好的Activity r = performResumeActivity(token, clearHide, reason); if (r != null) { final Activity a = r.activity;//返回以前建立的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是否可見,mStartedAcitity記錄的是一個Activity是否還處於啓動狀態 // 若是還處於啓動狀態則mStartedAcitity爲true,表示該activity還未啓動好,則該Activity還不可見 boolean willBeVisible = !a.mStartedActivity; // 若是啓動的組建不是全屏的,mStartedActivity也會是true,此時依然須要willBeVisible爲true如下的if邏輯就是針對這種狀況的校訂 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快速重啓機制 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; //調用了WindowManagerImpl的addView方法 wm.addView(decor, l); } ... }
重點來看wm.addView()方法,該方法中的decor參數爲Acitity對應的Window中的視圖DecorView,wm爲在建立PhoneWindow是建立的WindowManagerImpl對象,該對象的addView方法實際調用到到是單例對象WindowManagerGlobal的addView方法(前文有提到)。在看addView代碼前,我先來看看WindowManagerGlobal對象成員變量。異步
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>();
三個成員變量mViews、mRoots和mParams分別是類型爲View、ViewRootImpl和WindowManager.LayoutParams的數組。這裏有這樣的邏輯關係,每一個View都對應着惟一的一個ViewRootImpl和WindowManager.LayoutRarams,便是1:1:1的關係。這三個數組長度始終保持一致,而且在同一個位置上存放的是互相關聯的View、ViewRootImpl和WindowManager.LayoutParams對象。此外還有一個成員變量mDyView,保存的則是已經不須要但還未被系統會收到View。ide
View與LayoutParams比較好理解,那ViewRootImpl對象的做用是什麼呢?首先WindowManagerImpl是做爲管理類,就像主管同樣,根據Acitity和Window的調用請求,找到合適的作事的人;DecorView自己是FrameworkLayout,本事是一個View,所表示的是一種靜態的結構;因此這裏就須要一個真正作事的人,那就是ViewRootImpl類的工做。總結來說ViewRootImpl的功能以下ui
接下來咱們進入WindowManagerGlobal.addView()方法的代碼。
this
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. // 若是當前添加的是一個子視圖,則還須要找他他的父視圖 //這裏咱們分析的是添加DecorView的邏輯,沒有父視圖,故不會走到這裏,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); //保存互相對應的View、ViewRootImpl、WindowManager.LayoutParams到數組中 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; } } }
關注代碼中加粗的兩個方法,首先會建立一個ViewRootImpl對象,而後調用ViewRootImpl.setView方法,其中panelParentView在addView參數爲DecorView是爲null。進入ViewRootImpl.setView()代碼。
rest
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { synchronized (this) { if (mView == null) { //初始化成員變量mView、mWindowAttraibutes //mAttachInfo是View類的一個內部類AttachInfo類的對象 //該類的主要做用就是儲存一組當View attach給它的父Window的時候Activity各類屬性的信息 mView = view; mAttachInfo.mDisplayState = mDisplay.getState(); mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); mViewLayoutDirectionInitial = mView.getRawLayoutDirection(); mFallbackEventHandler.setView(view); mWindowAttributes.copyFrom(attrs); ... //繼續初始化一些變量,包含針對panelParentView不爲null時的父窗口的一些處理 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. // 這裏調用異步刷新請求,最終會調用performTraversals方法來完成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(); } } ... } } }
相關變量初始化完成後,便會將mAdded設置爲true,表示ViewRootImpl與setView傳入的View參數已經作好了關聯。以後便會調用requestLayout()方法來請求一次異步刷新,該方法後來又會調用到performTraversals()方法來完成view到繪製工做。注意到這裏雖然完成了繪製的工做,可是咱們建立Activity的源頭是AMS中發起的,咱們從一開始建立Acitivity到相對應的Window、DecorView這一大套對象時,還並未與AMS進程進行反饋。因此以後便會調用mWindowSession.addToDisplay()方法會執行IPC的跨進程通訊,最終調用到AMS中的addWindow方法來在系統進程中執行相關加載Window的操做。code
點擊下方連接免費獲取Android進階資料:
https://shimo.im/docs/tXXKHgdjPYj6WT8d/orm