WindowManger

Window中的DecorView是如何添加到WindowManger呢?
WindowManager的实现类是WindowManagerImpl,WindowManagerImpl调用addview
68
    public void addView(View viewViewGroup.LayoutParams params) {
69
        .addView(viewparams);
70
    }
mGlobal是WindowManagerGlobal对象,mGlobal调用了addView方法
188
189    public void More ...addView(View view, ViewGroup.LayoutParams params,
190            Display display, Window parentWindow) {
191        if (view == null) {
192            throw new IllegalArgumentException("view must not be null");
193        }
194        if (display == null) {
195            throw new IllegalArgumentException("display must not be null");
196        }
197        if (!(params instanceof WindowManager.LayoutParams)) {
198            throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
199        }
200
201        final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
202        if (parentWindow != null) {
203            parentWindow.adjustLayoutParamsForSubWindow(wparams);
204        }
205
206        ViewRootImpl root;
207        View panelParentView = null;
208
209        synchronized (mLock) {
210            // Start watching for system property changes.
211            if (mSystemPropertyUpdater == null) {
212                mSystemPropertyUpdater = new Runnable() {
213                    @Override public void More ...run() {
214                        synchronized (mLock) {
215                            for (int i = mRoots.size() - 1; i >= 0; --i) {
216                                mRoots.get(i).loadSystemProperties();
217                            }
218                        }
219                    }
220                };
221                SystemProperties.addChangeCallback(mSystemPropertyUpdater);
222            }
223
224            int index = findViewLocked(view, false);
225            if (index >= 0) {
226                if (mDyingViews.contains(view)) {
227                    // Don't wait for MSG_DIE to make it's way through root's queue.
228                    mRoots.get(index).doDie();
229                } else {
230                    throw new IllegalStateException("View " + view
231                            + " has already been added to the window manager.");
232                }
233                // The previous removeView() had not completed executing. Now it has.
234            }
235
236            // If this is a panel window, then find the window it is being
237            // attached to for future reference.
238            if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
239                    wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
240                final int count = mViews.size();
241                for (int i = 0; i < count; i++) {
242                    if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
243                        panelParentView = mViews.get(i);
244                    }
245                }
246            }
247
248            root = new ViewRootImpl(view.getContext(), display);
249
250            view.setLayoutParams(wparams);
251
252            mViews.add(view);
253            mRoots.add(root);
254            mParams.add(wparams);
255        }
256
257        // do this last because it fires off messages to start doing things
258        try {
259            root.setView(view, wparams, panelParentView);
260        } catch (RuntimeException e) {
261            // BadTokenException or InvalidDisplayException, clean up.
262            synchronized (mLock) {
263                final int index = findViewLocked(view, false);
264                if (index >= 0) {
265                    removeViewLocked(index, true);
266                }
267            }
268            throw e;
269        }
270    }
271
里面创建了ViewRootImpl对象,并执行了add(view)。

posted @ 2016-03-25 11:08  清澈见底  阅读(153)  评论(0编辑  收藏  举报