Window 和 WindowManager
kfgu1455
8年前
<h2><strong>概述</strong></h2> <h2><strong>window</strong></h2> <ul> <li> <p>window 是一个抽象类,具体实现是 PhoneWindow</p> </li> <li> <p>window 也是一个抽象的概念,每个 window 内对应这一个 DecorView 和一个 ViewRootImpl , window 和 DecorView 通过 ViewRootImpl</p> <p>联系。</p> <h2><strong>WindowManager</strong></h2> WindowManager 是 外界访问 Window <p>的入口</p> <p><!-- more --></p> <h2><strong>WindowManagerService</strong></h2> window 的具体实现位于 WindowManagerService 中, WindowManager 和 WindowManagerService <p>的交互是一个 IPC 的过程,</p> </li> </ul> <h2><strong>源码分析</strong></h2> <p>window 的添加、删除、更新 view 均是通过 WindowManager 来完成的,而 WindowManager 继承了 ViewManager 的接口</p> <pre> <code class="language-java">/** Interface to let you add and remove child views to an Activity. To get an instance * of this class, call {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. */ public interface ViewManager { /** * Assign the passed LayoutParams to the passed View and add the view to the window. * <p>Throws {@link android.view.WindowManager.BadTokenException} for certain programming * errors, such as adding a second view to a window without removing the first view. * <p>Throws {@link android.view.WindowManager.InvalidDisplayException} if the window is on a * secondary {@link Display} and the specified display can't be found * (see {@link android.app.Presentation}). * @param view The view to be added to this window. * @param params The LayoutParams to assign to view. */ public void addView(View view, ViewGroup.LayoutParams params); public void updateViewLayout(View view, ViewGroup.LayoutParams params); public void removeView(View view); }</code></pre> <p>对应的三个方法,继续看 WindowManager 对应的具体实现类, WindowManagerImple</p> <pre> <code class="language-java">@Override public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { applyDefaultToken(params); mGlobal.addView(view, params, mDisplay, mParentWindow); } @Override public void removeView(View view) { mGlobal.removeView(view, false); } @Override public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { applyDefaultToken(params); mGlobal.updateViewLayout(view, params); }</code></pre> <p>可以看到,具体的方法均交给 WindowManagerGlobal 来处理,而 WindowManagerGlobal 则以工程模式提供了自己的实例</p> <pre> <code class="language-java">private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();</code></pre> <p>看看``中比较重要的几个成员变量</p> <pre> <code class="language-java">private final ArrayList<View> mViews = new ArrayList<View>();//所有 window 对应的 DecorView private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();////所有 window 对应的 ViewRootImpl private final ArrayList<WindowManager.LayoutParams> mParams = new ArrayList<WindowManager.LayoutParams>(); //所有 window 对应的 WindowManager.LayoutParams private final ArraySet<View> mDyingViews = new ArraySet<View>();//所有 window 对应需要删除的 view</code></pre> <h2><strong>添加过程</strong></h2> <p>具体分析 WindowManagerGlobal 的 addview</p> <pre> <code class="language-java">public void 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; if (parentWindow != null) { parentWindow.adjustLayoutParamsForSubWindow(wparams); } else { // If there's no parent, then hardware acceleration for this view is // set from the application's hardware acceleration setting. final Context context = view.getContext(); if (context != null && (context.getApplicationInfo().flags & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) { wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; } } ViewRootImpl root; View panelParentView = null; synchronized (mLock) { // Start watching for system property changes. if (mSystemPropertyUpdater == null) { mSystemPropertyUpdater = new Runnable() { @Override public void run() { synchronized (mLock) { for (int i = mRoots.size() - 1; i >= 0; --i) { mRoots.get(i).loadSystemProperties(); } } } }; SystemProperties.addChangeCallback(mSystemPropertyUpdater); } //通过findViewLocked找到 view 对应的 index int index = findViewLocked(view, false); if (index >= 0) { //如果找到了,说明这个 view 已经被添加进来了 if (mDyingViews.contains(view)) { //这个 view 是需要删除的话,那么不用管直接用doDie 删除 // Don't wait for MSG_DIE to make it's way through root's queue. mRoots.get(index).doDie(); } else { //说明这个 view 已经被添加进来了,抛出异常 throw new IllegalStateException("View " + view + " has already been added to the window manager."); } // The previous removeView() had not completed executing. Now it has. } // If this is a panel window, then find the window it is being // attached to for future reference. if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW && wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) { final int count = mViews.size(); for (int i = 0; i < count; i++) { //找到对应的 window if (mRoots.get(i).mWindow.asBinder() == wparams.token) { panelParentView = mViews.get(i); } } } root = new ViewRootImpl(view.getContext(), display); view.setLayoutParams(wparams); //将得DecorView viewimpl params 都加入到 list 中 mViews.add(view); mRoots.add(root); mParams.add(wparams); } // do this last because it fires off messages to start doing things try { //这个重要,通过 viewrootImpl 的 setview 方法来刷新 view root.setView(view, wparams, panelParentView); } catch (RuntimeException e) { // BadTokenException or InvalidDisplayException, clean up. synchronized (mLock) { final int index = findViewLocked(view, false); if (index >= 0) { removeViewLocked(index, true); } } throw e; } }</code></pre> <p>setview 的方法比较长,看其中比较重要的几个方法</p> <p>requestLayout();</p> <pre> <code class="language-java">@Override public void requestLayout() { if (!mHandlingLayoutInLayoutRequest) { checkThread(); mLayoutRequested = true; scheduleTraversals(); } }</code></pre> <p>scheduleTraversals 是 view 开始绘制的入口</p> <p>继续往下看 window 的添加过程</p> <pre> <code class="language-java">mOrigWindowType = mWindowAttributes.type; mAttachInfo.mRecomputeGlobalAttributes = true; collectViewAttributes(); res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(), mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, mAttachInfo.mOutsets, mInputChannel);</code></pre> <p>IWindowSession 是一个 binder 对象。最终会调用到 windowManagerService 中的方法</p> <h2><strong>删除过程</strong></h2> <p>具体分析 WindowManagerGlobal 的 removeview</p> <pre> <code class="language-java">public void removeView(View view, boolean immediate) { if (view == null) { throw new IllegalArgumentException("view must not be null"); } synchronized (mLock) { //找到对应的 index ,再removeViewLocked 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); } } private void removeViewLocked(int index, boolean immediate) { ViewRootImpl root = mRoots.get(index); View view = root.getView(); if (view != null) { InputMethodManager imm = InputMethodManager.getInstance(); if (imm != null) { imm.windowDismissed(mViews.get(index).getWindowToken()); } } // ViewRootImpl 的 die 方法 boolean deferred = root.die(immediate); if (view != null) { view.assignParent(null); if (deferred) { mDyingViews.add(view); } } } //die 方法比较简单,就是发送了一个 message,最后在处理 message 的时候调用了doDie 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) { doDie(); return false; } if (!mIsDrawing) { destroyHardwareRenderer(); } else { Log.e(TAG, "Attempting to destroy the window while drawing!\n" + " window=" + this + ", title=" + mWindowAttributes.getTitle()); } mHandler.sendEmptyMessage(MSG_DIE); return true; } void doDie() { checkThread(); if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface); synchronized (this) { if (mRemoved) { return; } mRemoved = true; if (mAdded) { //这里做了一些资源的回抽,停动画等 dispatchDetachedFromWindow(); } if (mAdded && !mFirst) { destroyHardwareRenderer(); if (mView != null) { int viewVisibility = mView.getVisibility(); boolean viewVisibilityChanged = mViewVisibility != viewVisibility; if (mWindowAttributesChanged || viewVisibilityChanged) { // If layout params have been changed, first give them // to the window manager to make sure it has the correct // animation info. try { if ((relayoutWindow(mWindowAttributes, viewVisibility, false) & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) { mWindowSession.finishDrawing(mWindow); } } catch (RemoteException e) { } } mSurface.release(); } } mAdded = false; } //在其中清理 那些成员 list 变量 WindowManagerGlobal.getInstance().doRemoveView(this); }</code></pre> <h2><strong>更新过程</strong></h2> <pre> <code class="language-java">public void 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.setLayoutParams(wparams); synchronized (mLock) { //删除旧的 param 更新成新的 param int index = findViewLocked(view, true); ViewRootImpl root = mRoots.get(index); mParams.remove(index); mParams.add(index, wparams); //通过 viewrootImpl 来刷新 view root.setLayoutParams(wparams, false); } }</code></pre> <p> </p> <h2><strong>参考</strong></h2> <p><a href="/misc/goto?guid=4959715864996358274" rel="nofollow,noindex">http://gold.xitu.io/entry/571338c7c4c9710054cea455</a></p> <p><a href="/misc/goto?guid=4959715865101757630" rel="nofollow,noindex">http://www.jianshu.com/p/687010ccad66</a></p> <p> </p> <p>来自:http://www.jianshu.com/p/e75312330efc</p> <p> </p>