前言
本文主要讲解RegistrantList的原理,以及如何快速分析RegistrantList相关的代码流程。
在Telephony模块中,在RIL、Tracker(ServiceStateTracker、CallTracker、DcTracker)、Phone(PhoneBase及其子类)、UICC框架、CallManager等等中都大量使用到的RegistrantList,可见RegistrantList使用范围之广。如果代码流程中使用了RegistrantList,如何分析下一步代码流程走到哪里也是必须掌握的技能。
1. RegistrantList的原理
在讲解RegistrantList之前,先引出观察者模式的概念:
观察者模式:定义对象间的一种一(Subject)对多(Observer)的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并且自动更新。
RegistrantList跟观察者模式有点类似,先创建一个RegistrantList集合,专门关注某种消息;如果有人也对该消息感兴趣的话,那么为他封装一个Registrant对象并且添加到RegistrantList中;当有消息上报时,先通知RegistrantList,接着所有注册到RegistrantList中的Registrant都会被通知到并且更新。
RegistrantList与Registrant的关系如下:
RegistrantList中有:
add()/addUnique()方法,用于增加一个Registrant;
remove()方法,用于删除一个Registrant;
internalNotifyRegistrants()方法,用于通知所有的Registrant。
而Registrant中有:
internalNotifyRegistrants()方法,用于在收到RegistrantList的通知之后,再更新自己的内容。
本文来自 http://blog.csdn.net/linyongan ,转载请务必注明出处。
2. RegistrantList的使用
我们在上一篇文章《Android Telephony分析(一) —- Phone详解 》中曾经说到Phone接口中有大量的register/unregister的方法,为了跟上一篇文章相呼应,那么我们就选PhoneBase.java中监听通话挂断的mDisconnectRegistrants作为例子分析一下吧。
(时序图中的编号对应下面代码注释中的编号)
2.1 创建RegistrantList
在PhoneBase初始化的时候,会创建mDisconnectRegistrants用于监听通话挂断这个事件。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
在RegistrantList的内部:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
2.2 注册监听某个消息
在TelephonyConnection.java中
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.3 发送通知
当收到通话挂断的消息后,先通知RegistrantList
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
在RegistrantList的内部:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
来到Registrant里:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
整个RegistrantList注册Registrant,再到通知Registrant这个过程,也是利用了回调,可以留意时序图中,步骤2~步骤10,折腾了半天,最后还是回到TelephonyConnection中去。
回顾上面第二小节的代码:
TelephonyConnection对PhoneBase说,你帮我留意一下通话挂断这个消息啊!
PhoneBase说,什么时候有通话挂断消息来我也不清楚啊,要不你留个联系方式给我?
TelephonyConnection就传递了Handler和消息类型给PhoneBase,然后自己就去忙其他事了。
PhoneBase通过RegistrantList,把TelephonyConnection的联系方式(Handler和消息类型)传递给了Registrant。
等到通话挂断消息上报时,RegistrantList先通知Registrant,然后Registrant就给TelephonyConnection发消息。
3. 学以致用
代码中使用了RegistrantList,如何快速分析下一步流程走到哪里?
这是一个回调的过程,所以核心就是在哪里注册就在哪里处理,我们要找到调用register方法的地方。
还是以第二小节的mDisconnectRegistrants为例,假如我们不知道第二小节的内容,我们分析到这里:
- 1
- 1
先搜索mDisconnectRegistrants,找到registerXXX的方法;
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
再搜索registerXXX方法,找到调用register方法的地方;
- 1
- 1
最后搜索”MSG_DISCONNECT”,找到handleMessage()方法对MSG_DISCONNECT消息的处理,所以下一步代码流程就应该是走这里了。
原文地址:http://blog.csdn.net/linyongan/article/details/52036225