Android繪制流程窗口啟動(dòng)流程分析(下)

原文鏈接: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的操作。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末催跪,一起剝皮案震驚了整個(gè)濱河市锁蠕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌懊蒸,老刑警劉巖荣倾,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異骑丸,居然都是意外死亡舌仍,警方通過(guò)查閱死者的電腦和手機(jī)妒貌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)铸豁,“玉大人灌曙,你說(shuō)我怎么就攤上這事〗诮妫” “怎么了平匈?”我有些...
    開封第一講書人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)藏古。 經(jīng)常有香客問(wèn)我增炭,道長(zhǎng),這世上最難降的妖魔是什么拧晕? 我笑而不...
    開封第一講書人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任隙姿,我火速辦了婚禮,結(jié)果婚禮上厂捞,老公的妹妹穿的比我還像新娘输玷。我一直安慰自己,他們只是感情好靡馁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開白布欲鹏。 她就那樣靜靜地躺著,像睡著了一般臭墨。 火紅的嫁衣襯著肌膚如雪赔嚎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評(píng)論 1 307
  • 那天胧弛,我揣著相機(jī)與錄音尤误,去河邊找鬼。 笑死结缚,一個(gè)胖子當(dāng)著我的面吹牛损晤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播红竭,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼尤勋,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了茵宪?” 一聲冷哼從身側(cè)響起最冰,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎眉厨,沒(méi)想到半個(gè)月后锌奴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡憾股,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年鹿蜀,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了箕慧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡茴恰,死狀恐怖颠焦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情往枣,我是刑警寧澤伐庭,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站分冈,受9級(jí)特大地震影響圾另,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雕沉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一集乔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧坡椒,春花似錦扰路、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至丈攒,卻和暖如春哩罪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背肥印。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工识椰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绝葡,地道東北人深碱。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像藏畅,于是被迫代替她去往敵國(guó)和親敷硅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355