performLaunchActivity的时候获取final int displayId = ActivityClient.getInstance().getDisplayId(r.token),调用createDisplayContext创建context
java
@frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread.performLaunchActivity()
ContextImpl appContext = createBaseContextForActivity(r);
final int displayId = ActivityClient.getInstance().getDisplayId(r.token);
ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.activityInfo, r.token, displayId, r.overrideConfig);
for (int id : dm.getDisplayIds()) {
isplay display = dm.getCompatibleDisplay(id, appContext.getResources());
appContext = (ContextImpl) appContext.createDisplayContext(display);
@frameworks/base/core/java/android/app/ContextImpl.java
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, ContextParams.EMPTY, attributionTag, null, activityInfo.splitName, activityToken, null, 0, classLoader, null);
context.mDisplay = resourcesManager.getAdjustedDisplay(displayId, context.getResources()); //记录Display
@frameworks/base/core/java/android/app/Activity.java
activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, config,r.referrer, r.voiceInteractor, window,);
mWindow = new PhoneWindow(this, window, activityConfigCallback);
@frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java
mContext = context; //PhoneWindow设置mContext为Activity里的context
mWindow.setWindowManager((WindowManager)context.getSystemService(Context.WINDOW_SERVICE), mToken, (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
//context是attach传入的值,使用context创建WindowManager
if (wm == null) { //如果wm为空则创建使用mContext创建WindowManager,mContext是PhoneWindow保存的值
wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
}
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
return new WindowManagerImpl(mContext, parentWindow, mWindowContextToken); //创建WindowManagerImpl,mContext是wm的,wm有两种情况
mContext = context;
mParentWindow = parentWindow;
1. Activity 为例来分析应用窗口的添加过程
@frameworks/base/core/java/android/app/ActivityThread.java
handleResumeActivity
@frameworks/base/core/java/android/view/WindowManagerImpl.java
View decor = r.window.getDecorView();
wm.addView(decor, l);
@frameworks/base/core/java/android/app/ContextImpl.java
getDisplayNoVerify
return mDisplay;
mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow, mContext.getUserId()); //WindowManagerImpl的mContext