MyException - 我的异常网
当前位置:我的异常网» 移动开发 » Activity的ViewRoot的创造过程(三)

Activity的ViewRoot的创造过程(三)

www.MyException.Cn  网友分享于:2013-11-19  浏览:0次
Activity的ViewRoot的创建过程(三)
page7
在这篇文章里, 我们分析一下W类的构造过程. W类是定义在ViewRootImpl类中的一个内部类, W类的定义如下:
    static class W extends IWindow.Stub
    由此可见,  W本质上是一个Binder本地对象, 其实这是会传给WindowManagerService的, WindowManagerService就是通过W来通知Activity对象执行一些操作.

    W的构造函数如下:
    W(ViewRootImpl viewAncestor) {
        mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
        mWindowSession = viewAncestor.mWindowSession;
    }
    由此可见, W拿着ViewRootImpl和WindowSession.
page8
我们分析一下ViewRootImpl的setView函数的实现:
1     public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
2         synchronized (this) {
3             if (mView == null) {
4                 mView = view;
5                 mViewLayoutDirectionInitial = mView.getRawLayoutDirection();
6                 mFallbackEventHandler.setView(view);
7                 mWindowAttributes.copyFrom(attrs);
8                 attrs = mWindowAttributes;
9                 // Keep track of the actual window flags supplied by the client.
10                 mClientWindowLayoutFlags = attrs.flags;
11
12                 setAccessibilityFocus(null, null);
13
14                 if (view instanceof RootViewSurfaceTaker) {
15                     mSurfaceHolderCallback =
16                             ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
17                     if (mSurfaceHolderCallback != null) {
18                         mSurfaceHolder = new TakenSurfaceHolder();
19                         mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
20                     }
21                 }
22
23                 CompatibilityInfo compatibilityInfo = mCompatibilityInfo.get();
24                 mTranslator = compatibilityInfo.getTranslator();
25
26                 // If the application owns the surface, don't enable hardware acceleration
27                 if (mSurfaceHolder == null) {
28                     enableHardwareAcceleration(mView.getContext(), attrs);
29                 }
30
31                 boolean restore = false;
32                 if (mTranslator != null) {
33                     mSurface.setCompatibilityTranslator(mTranslator);
34                     restore = true;
35                     attrs.backup();
36                     mTranslator.translateWindowLayout(attrs);
37                 }
38                 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
39
40                 if (!compatibilityInfo.supportsScreen()) {
41                     attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
42                     mLastInCompatMode = true;
43                 }
44
45                 mSoftInputMode = attrs.softInputMode;
46                 mWindowAttributesChanged = true;
47                 mWindowAttributesChangesFlag = WindowManager.LayoutParams.EVERYTHING_CHANGED;
48                 mAttachInfo.mRootView = view;
49                 mAttachInfo.mScalingRequired = mTranslator != null;
50                 mAttachInfo.mApplicationScale =
51                         mTranslator == null ? 1.0f : mTranslator.applicationScale;
52                 if (panelParentView != null) {
53                     mAttachInfo.mPanelParentWindowToken
54                             = panelParentView.getApplicationWindowToken();
55                 }
56                 mAdded = true;
57                 int res; /* = WindowManagerImpl.ADD_OKAY; */
58
59                 // Schedule the first layout -before- adding to the window
60                 // manager, to make sure we do the relayout before receiving
61                 // any other events from the system.
62                 requestLayout();
63                 if ((mWindowAttributes.inputFeatures
64                         & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
65                     mInputChannel = new InputChannel();
66                 }
67                 try {
68                     mOrigWindowType = mWindowAttributes.type;
69                     mAttachInfo.mRecomputeGlobalAttributes = true;
70                     collectViewAttributes();
71                     res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
72                             getHostVisibility(), mDisplay.getDisplayId(),
73                             mAttachInfo.mContentInsets, mInputChannel);
74                 } catch (RemoteException e) {
75                     mAdded = false;
76                     mView = null;
77                     mAttachInfo.mRootView = null;
78                     mInputChannel = null;
79                     mFallbackEventHandler.setView(null);
80                     unscheduleTraversals();
81                     setAccessibilityFocus(null, null);
82                     throw new RuntimeException("Adding window failed", e);
83                 } finally {
84                     if (restore) {
85                         attrs.restore();
86                     }
87                 }
88                
89                 if (mTranslator != null) {
90                     mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
91                 }
92                 mPendingContentInsets.set(mAttachInfo.mContentInsets);
93                 mPendingVisibleInsets.set(0, 0, 0, 0);
94                 if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow);
95                 if (res < WindowManagerGlobal.ADD_OKAY) {
96                     mAttachInfo.mRootView = null;
97                     mAdded = false;
98                     mFallbackEventHandler.setView(null);
99                     unscheduleTraversals();
100                     setAccessibilityFocus(null, null);
101                     switch (res) {
102                         case WindowManagerGlobal.ADD_BAD_APP_TOKEN:
103                         case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:
104                             throw new WindowManager.BadTokenException(
105                                 "Unable to add window -- token " + attrs.token
106                                 + " is not valid; is your activity running?");
107                         case WindowManagerGlobal.ADD_NOT_APP_TOKEN:
108                             throw new WindowManager.BadTokenException(
109                                 "Unable to add window -- token " + attrs.token
110                                 + " is not for an application");
111                         case WindowManagerGlobal.ADD_APP_EXITING:
112                             throw new WindowManager.BadTokenException(
113                                 "Unable to add window -- app for token " + attrs.token
114                                 + " is exiting");
115                         case WindowManagerGlobal.ADD_DUPLICATE_ADD:
116                             throw new WindowManager.BadTokenException(
117                                 "Unable to add window -- window " + mWindow
118                                 + " has already been added");
119                         case WindowManagerGlobal.ADD_STARTING_NOT_NEEDED:
120                             // Silently ignore -- we would have just removed it
121                             // right away, anyway.
122                             return;
123                         case WindowManagerGlobal.ADD_MULTIPLE_SINGLETON:
124                             throw new WindowManager.BadTokenException(
125                                 "Unable to add window " + mWindow +
126                                 " -- another window of this type already exists");
127                         case WindowManagerGlobal.ADD_PERMISSION_DENIED:
128                             throw new WindowManager.BadTokenException(
129                                 "Unable to add window " + mWindow +
130                                 " -- permission denied for this window type");
131                         case WindowManagerGlobal.ADD_INVALID_DISPLAY:
132                             throw new WindowManager.InvalidDisplayException(
133                                 "Unable to add window " + mWindow +
134                                 " -- the specified display can not be found");
135                     }
136                     throw new RuntimeException(
137                         "Unable to add window -- unknown error code " + res);
138                 }
139
140                 if (view instanceof RootViewSurfaceTaker) {
141                     mInputQueueCallback =
142                         ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
143                 }
144                 if (mInputChannel != null) {
145                     if (mInputQueueCallback != null) {
146                         mInputQueue = new InputQueue(mInputChannel);
147                         mInputQueueCallback.onInputQueueCreated(mInputQueue);
148                     } else {
149                         mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,
150                                 Looper.myLooper());
151                     }
152                 }
153
154                 view.assignParent(this);
155                 mAddedTouchMode = (res & WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE) != 0;
156                 mAppVisible = (res & WindowManagerGlobal.ADD_FLAG_APP_VISIBLE) != 0;
157
158                 if (mAccessibilityManager.isEnabled()) {
159                     mAccessibilityInteractionConnectionManager.ensureConnection();
160                 }
161
162                 if (view.getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
163                     view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
164                 }
165             }
166         }
167     }
168
第2行(ViewRootImpl->setView)会加锁
第3行(ViewRootImpl->setView)会保证一个ViewRootImpl只对应一个View
第4行(ViewRootImpl->setView)将成员变量mView设置成DecorView

第62行(ViewRootImpl->setView)会调用requestLayout()函数来进行一次layout, 而这是在加入到WindowManagerService管理之前进行的, 关于这部分的分析可以参考View的刷新部分的分析.
第71-73行(ViewRootImpl->setView)会调用WindowSession的addToDisplay函数将mWindow传给WindowManagerService, 关于这部分的分析可以参考Activity和WindowManagerService连接系列的文章.
page9
    我们来看一下PhoneWidnow的getDecorView函数的实现:
    public final View getDecorView() {
        if (mDecor == null) {
            installDecor();
        }
        return mDecor;
    }
    getDecorView函数如果发现mDecor还没有初始化过, 就会调用installDecor函数来构造一个DecorView

文章评论

不懂技术不要对懂技术的人说这很容易实现
不懂技术不要对懂技术的人说这很容易实现
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
2013年中国软件开发者薪资调查报告
2013年中国软件开发者薪资调查报告
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
程序员和编码员之间的区别
程序员和编码员之间的区别
一个程序员的时间管理
一个程序员的时间管理
科技史上最臭名昭著的13大罪犯
科技史上最臭名昭著的13大罪犯
老程序员的下场
老程序员的下场
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
Java 与 .NET 的平台发展之争
Java 与 .NET 的平台发展之争
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
中美印日四国程序员比较
中美印日四国程序员比较
旅行,写作,编程
旅行,写作,编程
代码女神横空出世
代码女神横空出世
10个调试和排错的小建议
10个调试和排错的小建议
总结2014中国互联网十大段子
总结2014中国互联网十大段子
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
十大编程算法助程序员走上高手之路
十大编程算法助程序员走上高手之路
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
我是如何打败拖延症的
我是如何打败拖延症的
那些争议最大的编程观点
那些争议最大的编程观点
如何成为一名黑客
如何成为一名黑客
每天工作4小时的程序员
每天工作4小时的程序员
 程序员的样子
程序员的样子
编程语言是女人
编程语言是女人
漫画:程序员的工作
漫画:程序员的工作
Web开发人员为什么越来越懒了?
Web开发人员为什么越来越懒了?
为什么程序员都是夜猫子
为什么程序员都是夜猫子
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
聊聊HTTPS和SSL/TLS协议
聊聊HTTPS和SSL/TLS协议
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
那些性感的让人尖叫的程序员
那些性感的让人尖叫的程序员
程序员必看的十大电影
程序员必看的十大电影
为啥Android手机总会越用越慢?
为啥Android手机总会越用越慢?
程序员的鄙视链
程序员的鄙视链
2013年美国开发者薪资调查报告
2013年美国开发者薪资调查报告
Google伦敦新总部 犹如星级庄园
Google伦敦新总部 犹如星级庄园
程序员都该阅读的书
程序员都该阅读的书
程序员应该关注的一些事儿
程序员应该关注的一些事儿
初级 vs 高级开发者 哪个性价比更高?
初级 vs 高级开发者 哪个性价比更高?
我的丈夫是个程序员
我的丈夫是个程序员
Java程序员必看电影
Java程序员必看电影
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有