美文网首页
Window和WindowManager2:window的添加删

Window和WindowManager2:window的添加删

作者: 81bad73e9053 | 来源:发表于2016-12-05 20:50 被阅读125次

    Window是一个抽象的概念,每一个Window都对应着一个View和一个ViewRootImpl,Window和View通过ViewRootImpl来建立联系,View是Window存在的实体,应用无法直接访问Window,对Window的访问必须通过WindowManager

    1.Window的添加过程

    ViewManager.png

    WindowManager继承自ViewManager

    WindowManager.png

    WindowManger的实现类是WindowMangerImpl

    public final class WindowManagerImpl implements WindowManager {
        private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
        private final Display mDisplay;
        private final Window mParentWindow;
    
        public WindowManagerImpl(Display display) {
            this(display, null);
        }
    
        private WindowManagerImpl(Display display, Window parentWindow) {
            mDisplay = display;
            mParentWindow = parentWindow;
        }
    
        public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
            return new WindowManagerImpl(mDisplay, parentWindow);
        }
    
        public WindowManagerImpl createPresentationWindowManager(Display display) {
            return new WindowManagerImpl(display, mParentWindow);
        }
    
        @Override
        public void addView(View view, ViewGroup.LayoutParams params) {
            mGlobal.addView(view, params, mDisplay, mParentWindow);
        }
    
        @Override
        public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
            mGlobal.updateViewLayout(view, params);
        }
    
        @Override
        public void removeView(View view) {
            mGlobal.removeView(view, false);
        }
    
        @Override
        public void removeViewImmediate(View view) {
            mGlobal.removeView(view, true);
        }
    
        @Override
        public Display getDefaultDisplay() {
            return mDisplay;
        }
    }
    
    

    WindowMangerImpl的功能实际的操作转接给WindowManagerGlobal完成

       private static WindowManagerGlobal sDefaultWindowManager;
        private static IWindowManager sWindowManagerService;
        private static IWindowSession sWindowSession;
    
        private final Object mLock = new Object();
    
        private final ArrayList<View> mViews = new ArrayList<View>();
        private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
        private final ArrayList<WindowManager.LayoutParams> mParams =
                new ArrayList<WindowManager.LayoutParams>();
        private final ArraySet<View> mDyingViews = new ArraySet<View>();
        private boolean mNeedsEglTerminate;
    
        private Runnable mSystemPropertyUpdater;
    
        private WindowManagerGlobal() {
        }
    
        public static WindowManagerGlobal getInstance() {
            synchronized (WindowManagerGlobal.class) {
                if (sDefaultWindowManager == null) {
                    sDefaultWindowManager = new WindowManagerGlobal();
                }
                return sDefaultWindowManager;
            }
        }
    
        public static IWindowManager getWindowManagerService() {
            synchronized (WindowManagerGlobal.class) {
                if (sWindowManagerService == null) {
                    sWindowManagerService = IWindowManager.Stub.asInterface(
                            ServiceManager.getService("window"));
                }
                return sWindowManagerService;
            }
        }
    
        public static IWindowSession getWindowSession() {
            synchronized (WindowManagerGlobal.class) {
                if (sWindowSession == null) {
                    try {
                        InputMethodManager imm = InputMethodManager.getInstance();
                        IWindowManager windowManager = getWindowManagerService();
                        sWindowSession = windowManager.openSession(
                                imm.getClient(), imm.getInputContext());
                        float animatorScale = windowManager.getAnimationScale(2);
                        ValueAnimator.setDurationScale(animatorScale);
                    } catch (RemoteException e) {
                        Log.e(TAG, "Failed to open window session", e);
                    }
                }
                return sWindowSession;
            }
        }
    
        public static IWindowSession peekWindowSession() {
            synchronized (WindowManagerGlobal.class) {
                return sWindowSession;
            }
        }
    
    

    WindowManagerGlobal通过getWindowManagerService获取了一个IWindowManager对象,通过getWindowSession调用windowManager.openSession获取一个IWindowSession对象

    2.Window的删除过程

    3.Window的更新过程

    相关文章

      网友评论

          本文标题:Window和WindowManager2:window的添加删

          本文链接:https://www.haomeiwen.com/subject/zpllmttx.html