建立窗口的过程涉及的 IPC (Binder)

[ContextImpl](https://github.com/ziv-zh/platform_frameworks_base/blob/master/core/java/android/app/ContextImpl.java  )java

 

[ActivityThread] (https://github.com/ziv-zh/platform_frameworks_base/blob/master/core/java/android/app/ContextImpl.java )android

 

Binder 相关

[IBinder](https://github.com/ziv-zh/platform_frameworks_base/blob/master/core/java/android/os/IBinder.java ) binder 服务端(Stub)要实现的git

 

[Binder](https://github.com/ziv-zh/platform_frameworks_base/blob/master/core/java/android/os/Binder.java )默认的 binder 服务端实现github

 

[IInterface](https://github.com/ziv-zh/platform_frameworks_base/blob/master/core/java/android/os/IInterface.java )binder 客户端(proxy)要实现的,通常 aidl 自动生成session

 

关于 token:

token 都是 IBinder 对象,用于 IPC ;app

 

1,Activity(和 Window,若是 window 对象关联了一个 activity 的话) 中的 token 是由 AmS 建立的 ActivityRecord,用于 activity 通知 AmS 状态变化;ide

 

2,ViewRoot 的 W 对象 mWindow,是有 ViewRoot 建立的,传递给 WmS,用于 WmS 通知 ViewRoot 状态变化:this

WmS --> mWindow(Binder) --> mHandler(ViewRootHandler), 移步下面 " ViewRootImpl 的 mWindow 是如何传递给 WmS 的"spa

 

3,各类 XXXManager(好比WindowManager、ActivityManager)是一种 service 的封装,内部有对应service 的 Binder 对象;rest

 

 

[IWindow.aidl]: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/IWindow.aidl  API back to a client window that the Window Manager uses to inform it of interesting things happening.

[IWindowSession.aidl] https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/IWindow.aidl  System private per-application interface to the window manager.

 

 

 ViewRootImpl 的mWindow(W,Binder 的子类) 是如何传递给 WmS 的

1,Activity 建立完会建立 Window,并给 window 设置 contentView,这个过程当中会建立 decorView;

2,Activity 准备好通知 AmS,AmS 判断状态后最终调用 Activity 的 makeVisible(..),此方法中调用 WindowManager(会调用WindowManagerGlobal的 addView) 的 addView(..)展现 view;

3,在 addView 方法中进行检查并并建立 ViewRootImpl 对象,最终调用 ViewRootImpl 的 setView 方法,在此方法中会把 mWindow 这个 binder  经过 mWindowSession 注册给 WmS;

 

ps: mWindowSession 是 WmS 提供的一个 Binder 对象(因此使用时要处理 RemoteException),从 WindowManagerGlobal 的方法中能够看出;

ps: mAttachInfo 是在 ViewRootImpl 的构造方法中建立的,并在 setView 方法中设置一些其余的变量(好比 mRootView),

而且在 ViewRootImpl 的 'void dispatchAttachedToWindow(AttachInfo info, int visibility)' 方法把 mAttatchedInfo 传递给 view;

 

ViewRootImpl.java:

final IWindowSession mWindowSession;
...
final W mWindow;
...
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
    synchronized (this) {
    ...
    try {
        mOrigWindowType = mWindowAttributes.type;
        mAttachInfo.mRecomputeGlobalAttributes = true;
        collectViewAttributes();
        res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                getHostVisibility(), mDisplay.getDisplayId(),
                mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                mAttachInfo.mOutsets, mInputChannel);
    } catch (RemoteException e) {
    ...
    
}

 

WindowManagerGlobal.java:

    ...
    
    public static IWindowManager getWindowManagerService() {
        synchronized (WindowManagerGlobal.class) {
            if (sWindowManagerService == null) {
                sWindowManagerService = IWindowManager.Stub.asInterface(
                        ServiceManager.getService("window"));
                try {
                    sWindowManagerService = getWindowManagerService();
                    ValueAnimator.setDurationScale(sWindowManagerService.getCurrentAnimatorScale());
                } catch (RemoteException e) {
                    Log.e(TAG, "Failed to get WindowManagerService, cannot set animator scale", e);
                }
            }
            return sWindowManagerService;
        }
    }
    
    public static IWindowSession getWindowSession() {
        synchronized (WindowManagerGlobal.class) {
            if (sWindowSession == null) {
                try {
                    InputMethodManager imm = InputMethodManager.getInstance();
                    IWindowManager windowManager = getWindowManagerService();
                    sWindowSession = windowManager.openSession(
                            new IWindowSessionCallback.Stub() {
                                @Override
                                public void onAnimatorScaleChanged(float scale) {
                                    ValueAnimator.setDurationScale(scale);
                                }
                            },
                            imm.getClient(), imm.getInputContext());
                } catch (RemoteException e) {
                    Log.e(TAG, "Failed to open window session", e);
                }
            }
            return sWindowSession;
        }
    }
   
    
    public void addView(View view, ViewGroup.LayoutParams params,
        Display display, Window parentWindow) {
            ...
        
            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);
                }
        
                int index = findViewLocked(view, false);
                if (index >= 0) {
                    if (mDyingViews.contains(view)) {
                        // Don't wait for MSG_DIE to make it's way through root's queue.
                        mRoots.get(index).doDie();
                    } else {
                        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++) {
                        if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
                            panelParentView = mViews.get(i);
                        }
                    }
                }
        
                root = new ViewRootImpl(view.getContext(), display);
        
                view.setLayoutParams(wparams);
        
                mViews.add(view);
                mRoots.add(root);
                mParams.add(wparams);
            }
        
            // do this last because it fires off messages to start doing things
            try {
                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;
            }
    }
    
    ...
相关文章
相关标签/搜索