/i**
*We have one child*/
public voidsetView(View view, WindowManager.LayoutParams attrs, View panelParentView) {synchronized (this) {if (mView == null) {
mView=view;//获取当前屏幕最新的状态,并且给当前DecorView注册屏幕监听,用来检测灭屏和亮屏
mAttachInfo.mDisplayState =mDisplay.getState();
mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);
mViewLayoutDirectionInitial=mView.getRawLayoutDirection();
mFallbackEventHandler.setView(view);
mWindowAttributes.copyFrom(attrs);if (mWindowAttributes.packageName == null) {
mWindowAttributes.packageName=mBasePackageName;
}
attrs=mWindowAttributes;//Keep track of the actual window flags supplied by the client.
mClientWindowLayoutFlags =attrs.flags;//清除辅助功能的焦点
setAccessibilityFocus(null, null);if (view instanceofRootViewSurfaceTaker) {
mSurfaceHolderCallback=((RootViewSurfaceTaker)view).willYouTakeTheSurface();if (mSurfaceHolderCallback != null) {
mSurfaceHolder= newTakenSurfaceHolder();
mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
}
}//Compute surface insets required to draw at specified Z value.//TODO: Use real shadow insets for a constant max Z.
if (!attrs.hasManualSurfaceInsets) {final int surfaceInset = (int) Math.ceil(view.getZ() * 2);
attrs.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset);
}//获取屏幕兼容模式,一般情况下不考虑 mTranslator是空的
CompatibilityInfo compatibilityInfo =mDisplayAdjustments.getCompatibilityInfo();
mTranslator=compatibilityInfo.getTranslator();//开启硬件加速功能
if (mSurfaceHolder == null) {
enableHardwareAcceleration(attrs);
}boolean restore = false;//Application不考虑兼容模式
if (mTranslator != null) {
mSurface.setCompatibilityTranslator(mTranslator);
restore= true;
attrs.backup();
mTranslator.translateWindowLayout(attrs);
}if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" +attrs);//Application不考虑兼容模式
if (!compatibilityInfo.supportsScreen()) {
attrs.privateFlags|=WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
mLastInCompatMode= true;
}
mSoftInputMode=attrs.softInputMode;
mWindowAttributesChanged= true;
mWindowAttributesChangesFlag=WindowManager.LayoutParams.EVERYTHING_CHANGED;
mAttachInfo.mRootView=view;
mAttachInfo.mScalingRequired= mTranslator != null;
mAttachInfo.mApplicationScale=mTranslator== null ? 1.0f: mTranslator.applicationScale;if (panelParentView != null) {
mAttachInfo.mPanelParentWindowToken=panelParentView.getApplicationWindowToken();
}
mAdded= true;int res; /*= WindowManagerImpl.ADD_OKAY;*/
//在这里开始启用绘制线程,requestLayout会触发整个的绘制过程
requestLayout();if((mWindowAttributes.inputFeatures& WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
mInputChannel= newInputChannel();
}try{
mOrigWindowType=mWindowAttributes.type;
mAttachInfo.mRecomputeGlobalAttributes= true;
collectViewAttributes();//mWindow是ViewRootImpl中的一个静态类,这个类可以用来和WindowSession交互,就等于间接在和WindowManagerService交互//是一个桥梁类
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();
}
}if (mTranslator != null) {
mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
}
mPendingOverscanInsets.set(0, 0, 0, 0);
mPendingContentInsets.set(mAttachInfo.mContentInsets);
mPendingStableInsets.set(mAttachInfo.mStableInsets);
mPendingVisibleInsets.set(0, 0, 0, 0);if (DEBUG_LAYOUT) Log.v(TAG, "Added window " +mWindow);//在这里处理WindowManagerServices的处理结果,在这里你能看到很多熟悉的异常
if (res
mAttachInfo.mRootView= null;
mAdded= false;
mFallbackEventHandler.setView(null);
unscheduleTraversals();
setAccessibilityFocus(null, null);switch(res) {caseWindowManagerGlobal.ADD_BAD_APP_TOKEN:caseWindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:throw newWindowManager.BadTokenException("Unable to add window -- token " +attrs.token+ " is not valid; is your activity running?");caseWindowManagerGlobal.ADD_NOT_APP_TOKEN:throw newWindowManager.BadTokenException("Unable to add window -- token " +attrs.token+ " is not for an application");caseWindowManagerGlobal.ADD_APP_EXITING:throw newWindowManager.BadTokenException("Unable to add window -- app for token " +attrs.token+ " is exiting");caseWindowManagerGlobal.ADD_DUPLICATE_ADD:throw newWindowManager.BadTokenException("Unable to add window -- window " +mWindow+ " has already been added");caseWindowManagerGlobal.ADD_STARTING_NOT_NEEDED://Silently ignore -- we would have just removed it//right away, anyway.
return;caseWindowManagerGlobal.ADD_MULTIPLE_SINGLETON:throw newWindowManager.BadTokenException("Unable to add window " + mWindow +
" -- another window of this type already exists");caseWindowManagerGlobal.ADD_PERMISSION_DENIED:throw newWindowManager.BadTokenException("Unable to add window " + mWindow +
" -- permission denied for this window type");caseWindowManagerGlobal.ADD_INVALID_DISPLAY:throw newWindowManager.InvalidDisplayException("Unable to add window " + mWindow +
" -- the specified display can not be found");caseWindowManagerGlobal.ADD_INVALID_TYPE:throw newWindowManager.InvalidDisplayException("Unable to add window " +mWindow+ " -- the specified window type is not valid");
}throw newRuntimeException("Unable to add window -- unknown error code " +res);
}if (view instanceofRootViewSurfaceTaker) {
mInputQueueCallback=((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
}if (mInputChannel != null) {if (mInputQueueCallback != null) {
mInputQueue= newInputQueue();
mInputQueueCallback.onInputQueueCreated(mInputQueue);
}
mInputEventReceiver= newWindowInputEventReceiver(mInputChannel,
Looper.myLooper());
}//给DecorView绑定mParent参数
view.assignParent(this);
mAddedTouchMode= (res & WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE) != 0;
mAppVisible= (res & WindowManagerGlobal.ADD_FLAG_APP_VISIBLE) != 0;//判断辅助功能是否可用
if(mAccessibilityManager.isEnabled()) {
mAccessibilityInteractionConnectionManager.ensureConnection();
}if (view.getImportantForAccessibility() ==View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
}//Set up the input pipeline.
CharSequence counterSuffix =attrs.getTitle();
mSyntheticInputStage= newSyntheticInputStage();
InputStage viewPostImeStage= newViewPostImeInputStage(mSyntheticInputStage);
InputStage nativePostImeStage= newNativePostImeInputStage(viewPostImeStage,"aq:native-post-ime:" +counterSuffix);
InputStage earlyPostImeStage= newEarlyPostImeInputStage(nativePostImeStage);
InputStage imeStage= newImeInputStage(earlyPostImeStage,"aq:ime:" +counterSuffix);
InputStage viewPreImeStage= newViewPreImeInputStage(imeStage);
InputStage nativePreImeStage= newNativePreImeInputStage(viewPreImeStage,"aq:native-pre-ime:" +counterSuffix);
mFirstInputStage=nativePreImeStage;
mFirstPostImeInputStage=earlyPostImeStage;
mPendingInputEventQueueLengthCounterName= "aq:pending:" +counterSuffix;
}
}
}