Window 和 WindowManager

kfgu1455 8年前發布 | 4K 次閱讀 安卓開發 Android開發 移動開發

概述

window

  • window 是一個抽象類,具體實現是 PhoneWindow

  • window 也是一個抽象的概念,每個 window 內對應這一個 DecorView 和一個 ViewRootImpl , window 和 DecorView 通過 ViewRootImpl

    聯系。

    WindowManager

    WindowManager 是 外界訪問 Window

    的入口

    <!-- more -->

    WindowManagerService

    window 的具體實現位于 WindowManagerService 中, WindowManager 和 WindowManagerService

    的交互是一個 IPC 的過程,

源碼分析

window 的添加、刪除、更新 view 均是通過 WindowManager 來完成的,而 WindowManager 繼承了 ViewManager 的接口

/** 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>

      對應的三個方法,繼續看 WindowManager 對應的具體實現類, WindowManagerImple

      @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>

      可以看到,具體的方法均交給 WindowManagerGlobal 來處理,而 WindowManagerGlobal 則以工程模式提供了自己的實例

      private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

      看看``中比較重要的幾個成員變量

      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>

      添加過程

      具體分析 WindowManagerGlobal 的 addview

      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> 

setview 的方法比較長,看其中比較重要的幾個方法

requestLayout();

@Override
    public void requestLayout() {
        if (!mHandlingLayoutInLayoutRequest) {
            checkThread();
            mLayoutRequested = true;
            scheduleTraversals();
        }
    }

scheduleTraversals 是 view 開始繪制的入口

繼續往下看 window 的添加過程

mOrigWindowType = mWindowAttributes.type;
                    mAttachInfo.mRecomputeGlobalAttributes = true;
                    collectViewAttributes();
                    res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(),
                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                            mAttachInfo.mOutsets, mInputChannel);

IWindowSession 是一個 binder 對象。最終會調用到 windowManagerService 中的方法

刪除過程

具體分析 WindowManagerGlobal 的 removeview

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);
    }

更新過程

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);
        }
    }

 

參考

http://gold.xitu.io/entry/571338c7c4c9710054cea455

http://www.jianshu.com/p/687010ccad66

 

來自:http://www.jianshu.com/p/e75312330efc

 

 本文由用戶 kfgu1455 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!