定義
ContextImpl
啟動(dòng)activity流程中,在創(chuàng)建Activity之前會(huì)創(chuàng)建activity關(guān)聯(lián)的上下文實(shí)例contextimpl,其內(nèi)部提供訪問(wèn)資源盹舞、服務(wù)等等諸多接口像樊,同時(shí)注冊(cè)并緩存了諸多服務(wù),是用過(guò)SystemServiceRegistry注冊(cè)的予颤,其中注冊(cè)了WINDOW_SERVICE關(guān)聯(lián)的WindowManager的實(shí)現(xiàn)類(lèi)WindowManagerImpl等
Window
每個(gè)Activity都會(huì)綁定一個(gè)窗口window囤官,其實(shí)現(xiàn)類(lèi)是PhoneWindow,它是WindowManager的跟容器
DecorView
DecorView是View視圖樹(shù)的根布局蛤虐,它繼承了FrameLayout党饮,包含三大部分的子View:LineLayout(Activity視圖內(nèi)容容器,其包含兩部分actionBar和一個(gè)id為R.id.content的FrameLayout)驳庭、navigationBarBackground(導(dǎo)航欄)刑顺、statusBarBackround(狀態(tài)欄)
ViewRootImpl
ViewRootImpl是View的根,但它不是View視圖樹(shù)的一部分饲常,它是Window和DecorView的交互橋梁蹲堂,用來(lái)控制View測(cè)量和繪制,同時(shí)它持有WindowSession通過(guò)Binder與WMS通信贝淤,實(shí)現(xiàn)如事件分發(fā)
關(guān)系流程
Activity的attch——》創(chuàng)建PhoneWindow柒竞,同時(shí)設(shè)置WindowManager,將PhoneWindow作為WindowManagerImpl的window——》setContentView——》進(jìn)入PhoneView創(chuàng)建DecorView播聪,并將加載activity的視圖到decorView中——》ActivityThread的handleResumeActivity將decorView設(shè)置為不可見(jiàn)朽基,創(chuàng)建ViewRootImpl,并通過(guò)ViewRootImpl進(jìn)行視圖測(cè)量和繪制
attach
創(chuàng)建創(chuàng)建PhoneWindow犬耻,同時(shí)設(shè)置WindowManager踩晶,將PhoneWindow作為WindowManagerImpl的window
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
//......
mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
//......
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
//......
}
//Window.setWindowManager
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
mAppToken = appToken;
mAppName = appName;
mHardwareAccelerated = hardwareAccelerated
|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
if (wm == null) {
wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
}
//此wm是WindowManager的實(shí)現(xiàn)類(lèi)WindowManagerImpl
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
private WindowManagerImpl(Context context, Window parentWindow) {
mContext = context;
//此parentWindow是PhoneWindow,將PhoneWindow作為WindowManagerImpl的父window
mParentWindow = parentWindow;
}
public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
return new WindowManagerImpl(mContext, parentWindow);
}
setContentView
Activity的setContentView會(huì)調(diào)用PhoneWindow的setContentView:
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
我們接著看PhoneWindow的setContentView方法:
@Override
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
//調(diào)用installDecor創(chuàng)建DecorView
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
//.....
//加載xml轉(zhuǎn)為視圖加載到DecorView視圖樹(shù)中
mLayoutInflater.inflate(layoutResID, mContentParent);
//.....
}
private void installDecor() {
mForceDecorInstall = false;
if (mDecor == null) {
//調(diào)用generateDecor方法創(chuàng)建DecorVIew
mDecor = generateDecor(-1);
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
} else {
mDecor.setWindow(this);
}
if (mContentParent == null) {
//如果DecorView內(nèi)容為空枕磁,則調(diào)用generateLayout生成DecorVIew的布局內(nèi)容
mContentParent = generateLayout(mDecor);
//......
}
protected DecorView generateDecor(int featureId) {
// System process doesn't have application context and in that case we need to directly use
// the context we have. Otherwise we want the application context, so we don't cling to the
//.....
//創(chuàng)建decorView
return new DecorView(context, featureId, this, getAttributes());
}
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
//.......應(yīng)用設(shè)置activity的樣式渡蜻,這里省略.....
mDecor.startChanging();
//layoutResource是DecorView內(nèi)容布局xml文件id
//onResourcesLoaded將布局文件加載到DecorView中
mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
//ID_ANDROID_CONTENT = com.android.internal.R.id.content
//將id為content的FrameLayout作為Activity布局視圖的容器
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
//.......
}
generateLayout主要是設(shè)置activity的樣式,并根據(jù)樣式選擇DecorView的內(nèi)容布局xml文件id,接著調(diào)用DecorVIew的onResourcesLoaded方法添加內(nèi)容布局到DecorView中茸苇,我們接著看DecorView的onResourcesLoaded方法:
void onResourcesLoaded(LayoutInflater inflater, int layoutResource) {
//......
//加載布局文件
final View root = inflater.inflate(layoutResource, null);
if (mDecorCaptionView != null) {
if (mDecorCaptionView.getParent() == null) {
addView(mDecorCaptionView,
new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
//添加布局到DecorView中
mDecorCaptionView.addView(root,
new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT));
} else {
// Put it below the color views.
//添加布局到DecorView中
addView(root, 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
mContentRoot = (ViewGroup) root;
initializeElevation();
}
setContentView主要做的事:
- 使用PhoneWindow創(chuàng)建DecorView
- 設(shè)置activity樣式排苍,本根據(jù)樣式選擇DecorView的內(nèi)容布局xml文件id
- 加載DecorView的布局文件并添加到DecorView中
- 使用id為content的FragmentLayout作為activity的布局視圖的容器
handleResumeActivity
ActivityThread的handleResumeActivity依次調(diào)用performResumeActivity、將DecorView設(shè)置為不可見(jiàn)学密、創(chuàng)建ViewRootImpl淘衙、調(diào)用ViewRootImpl的setView對(duì)DecorView進(jìn)行測(cè)量和繪制:
@Override
public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
String reason) {
//......
final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason);
//......
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
wm.addView(decor, l);
//......
if (r.activity.mVisibleFromClient) {
r.activity.makeVisible();
}
}
handleResumeActivity中wm.addView是進(jìn)行創(chuàng)建ViewRootImpl和調(diào)用ViewRootImpl的關(guān)鍵,其中wm是WindowManagerImpl腻暮,我們繼續(xù)看下WindowManagerImpl的addView方法:
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}
WindowManagerImpl的addView又調(diào)用mGlobal的addView彤守,其中WindowManagerGlobal的實(shí)例,我們繼續(xù)進(jìn)入WindowManagerGlobal的addView方法:
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
//......
synchronized (mLock) {
//......
//創(chuàng)建ViewRootImpl
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// do this last because it fires off messages to start doing things
try {
//調(diào)用ViewRootImpl的setView對(duì)DecorView進(jìn)行測(cè)量和繪制操作
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
if (index >= 0) {
removeViewLocked(index, true);
}
throw e;
}
}
//......
}
WindowManagerGlobal的addView中先創(chuàng)建ViewRootImpl,接著調(diào)用ViewRootImpl的setView對(duì)DecorView進(jìn)行測(cè)量和繪制操作(測(cè)量和繪制流程見(jiàn):Android View繪制流程哭靖。
我們回到ActivityThread的handleResumeActivity的makeVisible部分具垫,調(diào)用的是activity的makeVisible方法,我們接著看activity的makeVisible方法:
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
//將DecorView設(shè)置為可見(jiàn)
mDecor.setVisibility(View.VISIBLE);
}