//存储所有Window对应的View privatefinal ArrayList<View> mViews = new ArrayList<View>(); //所有Window对应的ViewRootImpl privatefinal ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>(); //所有Window对应的布局参数 LayoutParams privatefinal ArrayList<WindowManager.LayoutParams> mParams = new ArrayList<WindowManager.LayoutParams>(); //存储那些正在被删除的对象 privatefinal ArraySet<View> mDyingViews = new ArraySet<View>();
publicvoid addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) { //检测参数是否合法 if (view == null) { throw new IllegalArgumentException("view must not be null"); } if (display == null) { throw new IllegalArgumentException("display must not be null"); } if (!(params instanceof WindowManager.LayoutParams)) { throw new IllegalArgumentException("Params must be WindowManager.LayoutParams"); }
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; //如果是子Window还需要调整参数 if (parentWindow != null) { parentWindow.adjustLayoutParamsForSubWindow(wparams); } else {
// ../android/view/WindowManagerGlobal.java publicvoid updateViewLayout(View view, ViewGroup.LayoutParams params) { if (view == null) { throw new IllegalArgumentException("view must not be null"); } if (!(params instanceof WindowManager.LayoutParams)) { throw new IllegalArgumentException("Params must be WindowManager.LayoutParams"); }
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params; //更新View的LayoutParams view.setLayoutParams(wparams);
synchronized (mLock) { int index = findViewLocked(view, true); ViewRootImpl root = mRoots.get(index); mParams.remove(index); mParams.add(index, wparams); root.setLayoutParams(wparams, false); } }
// ../core/java/com/android/server/wm/Session.java final WindowManagerService mService; publicint relayout(IWindow window, int seq, WindowManager.LayoutParams attrs, int requestedWidth, int requestedHeight, int viewFlags, int flags, Rect outFrame, Rect outOverscanInsets, Rect outContentInsets, Rect outVisibleInsets, Rect outStableInsets, Rect outsets, Rect outBackdropFrame, MergedConfiguration mergedConfiguration, Surface outSurface) {
int res = mService.relayoutWindow(this, window, seq, attrs, requestedWidth, requestedHeight, viewFlags, flags, outFrame, outOverscanInsets, outContentInsets, outVisibleInsets, outStableInsets, outsets, outBackdropFrame, mergedConfiguration, outSurface);
// ../android/view/WindowManagerGlobal.java publicvoid removeView(View view, boolean immediate) { if (view == null) { throw new IllegalArgumentException("view must not be null"); }
synchronized (mLock) { //找到需要删除的View索引 int index = findViewLocked(view, true); View curView = mRoots.get(index).getView(); removeViewLocked(index, immediate); if (curView == view) { return; }
throw new IllegalStateException("Calling with view " + view + " but the ViewAncestor is attached to " + curView); } }
if (view != null) { InputMethodManager imm = InputMethodManager.getInstance(); if (imm != null) { imm.windowDismissed(mViews.get(index).getWindowToken()); } } //在die 中执行删除Window操作 boolean deferred = root.die(immediate); if (view != null) { view.assignParent(null); if (deferred) { //存储即将删除的View mDyingViews.add(view); } } }
../android/view/ViewRootImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
boolean die(boolean immediate/*是否同步执行删除*/) { // Make sure we do execute immediately if we are in the middle of a traversal or the damage // done by dispatchDetachedFromWindow will cause havoc on return. if (immediate && !mIsInTraversal) { //删除对应Window doDie(); return false; }
if (!mIsDrawing) { destroyHardwareRenderer(); } else { Log.e(mTag, "Attempting to destroy the window while drawing!\n" + " window=" + this + ", title=" + mWindowAttributes.getTitle()); } mHandler.sendEmptyMessage(MSG_DIE); return true; }