Activity、Window的關係

首先需要帶着目的去閱讀源碼
這次的目的是:Activity的顯示,Window起了什麼作用?

建議:一邊跟着文章,一邊跟着源碼閱讀,否則會暈

1、activityThread收到handler消息

啓動一個activity,調用performLaunchActivity方法

2、調用activity的attach方法

3、在activity的attach方法

attach方法中爲mWindow賦值
mWindow = new PhoneWindow(this, window, activityConfigCallback);
⚠️關鍵點:Activity和Window的橋樑建立,Activity持有了window對象。

4、開始activity的生命週期

在onCreate方法中,調用setContentView方法,調用getWindow的setContentView方法。getWindow返回的值就是上面創建的phoneWindow。

public void setContentView(@LayoutRes int layoutResID) {
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
}

5、phoneWindow.setContentView()

其中,先創建了decorView,然後創建我們熟
悉的 R.id.content,最後把傳入的layoutResID放入content中,實現了view的創建。這時候,PhoneWindow就把view創建完成。

⚠️關鍵點:setContenView方法,PhoneWindow創建View。Window和View 的橋樑建立。

兩個橋樑已經創建,window就是activity和view的中間節點,activity對view的控制都需要通過window。

6、ActivityThread.handleResumeActivity 調用目標activity的makeVisible方法

void makeVisible() {
    if (!mWindowAdded) {
        ViewManager wm = getWindowManager();
        wm.addView(mDecor, getWindow().getAttributes());
        mWindowAdded = true;
    }
    mDecor.setVisibility(View.VISIBLE);
}

7、getWindowManager()返回的是什麼?

//Activity.class
public WindowManager getWindowManager() {
    return mWindowManager;
}

mWindowManager在哪裏創建?
mWindowManager = mWindow.getWindowManager();
調用window的getWindowManager方法

//Window.class
public WindowManager getWindowManager() {
    return mWindowManager;
}

window的mWindowManager創建

//Window.class
mWindowManager=((WindowManagerImpl)wm).createLocalWindowManager(this);
//WindowManagerImpl.class
public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
    return new WindowManagerImpl(mContext, parentWindow);
}

⚠️關鍵點:所以,activity中使用的getWindowManager就是WindowManagerImpl

WindowManagerImpl實現了WindowManager接口,WindowManager繼承自ViewManager

//ViewManager 接口
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);

所以WindowManagerImpl的主要目的就是對View的管理。

8、現在就是WindowManagerImpl調用了addView方法,把decorView傳入。

//WindowManagerImpl.class
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
    applyDefaultToken(params);
    mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}

這裏出現了mGlobal對象。

private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

這個mGlobal是個單例,WindowManagerImpl對view的操作都是通過WindowManagerGlobal來實現的。

9、WindowManagerGlobal的addView方法,傳入了decorView

//WindowManagerGlobal.class
//addView方法

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

addView方法中,把decorview保存起來,創建了rootViewImpl對象(root),然後調用setView方法。

10、ViewRootImpl的setView方法

res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
        getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,
        mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
        mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);

調用mWindowSession對象的addToDisplay方法,從方法名中也能看出,是把window添加到顯示屏上。
這裏就需要大致找一下mWindowSession是什麼?

11、mWindowSession是什麼?

//ViewRootImpl.class
mWindowSession = WindowManagerGlobal.getWindowSession();

調用WindowManagerGlobal的getWindowSession方法

//WindowManagerGlobal.class
//getWindowSession()方法
sWindowSession = windowManager.openSession(
        new IWindowSessionCallback.Stub() {
            @Override
            public void onAnimatorScaleChanged(float scale) {
                ValueAnimator.setDurationScale(scale);
            }
        },
        imm.getClient(), imm.getInputContext());

sWindowSession是windowManager.openSession方法返回的。這樣,就需要到windowManager中去尋找,windowManager的實現類就是WindowManagerService

//WindowManagerService.class
@Override
public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client,
        IInputContext inputContext) {
    if (client == null) throw new IllegalArgumentException("null client");
    if (inputContext == null) throw new IllegalArgumentException(“null inputContext”);
    Session session = new Session(this, callback, client, inputContext);
    return session;
}

返回的是一個Session對象
我們回到第10步,其中調用了addToDisplay方法,用來添加window。

12、Session的addToDisplay方法

//Session.class
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
        int viewVisibility, int displayId, Rect outFrame, Rect outContentInsets,
        Rect outStableInsets, Rect outOutsets,
        DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel) {
    return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outFrame,
            outContentInsets, outStableInsets, outOutsets, outDisplayCutout, outInputChannel);
}

最終調用mService.addWindow方法。而mService是什麼?
final WindowManagerService mService;
mService就是WindowManagerService。這樣,window就通過IPC的方式,交給了系統服務,進行顯示。

總結:

Activity創建後,window幹了這些事

1、直面用戶的activity創建後,其內部創建了Window對象。
2、調用setContentView後,Window對象把我們自己的xml通過inflate方式生成了View,並添加到了decorView中。
3、都創建完成後,就需要顯示了,觸發了ActivityThread.handleResumeActivity方法,這時,就需要把剛纔生成的window顯示到屏幕上
4、window的一路傳遞,最終交給了WindowManagerService處理進行顯示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章