Android Telephony分析(二) ---- RegistrantList详解

前言

本文主要讲解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.创建RegistrantList-->监听通话挂断
    protected final RegistrantList mDisconnectRegistrants
            = new RegistrantList();

    //对应地也会封装好增加Registrant/删除Registrant的方法,
    //然后提供register/unregister方法给外界调用,
    @Override
    public void registerForDisconnect(Handler h, int what, Object obj) {
        checkCorrectThread(h);
        //3.增加一个Registrant
        mDisconnectRegistrants.addUnique(h, what, obj);
    }

    @Override
    public void unregisterForDisconnect(Handler h) {
        //根据Handler删除Registrant
        mDisconnectRegistrants.remove(h);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在RegistrantList的内部:

public class RegistrantList
{
    //用一个列表来存储所有的Registrant。
    //有点好奇源码中为什么不用泛型?:ArrayList<Registrant> registrants = new ArrayList<Registrant>(); 
    ArrayList   registrants = new ArrayList();// of Registrant

    public synchronized void
    add(Handler h, int what, Object obj)
    {   //创建一个Registrant
        add(new Registrant(h, what, obj));
    }

    public synchronized void
    addUnique(Handler h, int what, Object obj)
    {
        // if the handler is already in the registrant list, remove it
        remove(h);
        //4.创建一个Registrant
        add(new Registrant(h, what, obj));        
    }

    public synchronized void
    add(Registrant r)
    {
        removeCleared();
        //5.把Registrant添加到registrants列表中
        registrants.add(r);
    }

    public synchronized void
    remove(Handler h)
    {
        //遍历列表中的Registrant
        for (int i = 0, s = registrants.size() ; i < s ; i++) {
            Registrant  r = (Registrant) registrants.get(i);
            Handler     rh;

            rh = r.getHandler();

            /* Clean up both the requested registrant and
             * any now-collected registrants
             */
            if (rh == null || rh == h) {
                //清空Registrant的内容
                r.clear();
            }
        }

        removeCleared();
    }

    public synchronized void
    removeCleared()
    {
        for (int i = registrants.size() - 1; i >= 0 ; i--) {
            Registrant  r = (Registrant) registrants.get(i);

            if (r.refH == null) {
                //移除Handler为空的Registrant
                registrants.remove(i);
            }
        }
    }
  • 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中

    //2.注册监听某个消息
    getPhone().registerForDisconnect(mHandler, MSG_DISCONNECT, null);

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                 case MSG_DISCONNECT:
                    //10.最终代码流程会走到这里,对之前监听的消息作进一步处理
                    break;
            }
        }
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2.3 发送通知

当收到通话挂断的消息后,先通知RegistrantList

    /*package*/ void
    notifyDisconnect(Connection cn) {
        //6.开始通知所有注册到mDisconnectRegistrants中的Registrant
        mDisconnectRegistrants.notifyResult(cn);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

在RegistrantList的内部:

    public /*synchronized*/ void
    notifyResult(Object result)
    {
        internalNotifyRegistrants (result, null);
    }

    private synchronized void
    internalNotifyRegistrants (Object result, Throwable exception)
    {  //7.遍历registrants列表
       for (int i = 0, s = registrants.size(); i < s ; i++) {
            Registrant  r = (Registrant) registrants.get(i);
            //8.通知Registrant
            r.internalNotifyRegistrant(result, exception);
       }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

来到Registrant里:

    internalNotifyRegistrant (Object result, Throwable exception)
    {   //得到Handler对象
        Handler h = getHandler();

        if (h == null) {
            clear();
        } else {
            Message msg = Message.obtain();

            msg.what = what;

            msg.obj = new AsyncResult(userObj, result, exception);
            //9.通过回调,给Handler发送Message
            h.sendMessage(msg);
        }
    }
  • 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为例,假如我们不知道第二小节的内容,我们分析到这里:

    mDisconnectRegistrants.notifyResult(cn);
  • 1

先搜索mDisconnectRegistrants,找到registerXXX的方法;

    public void registerForDisconnect(Handler h, int what, Object obj) {
        checkCorrectThread(h);
        mDisconnectRegistrants.addUnique(h, what, obj);
    }
  • 1
  • 2
  • 3
  • 4

再搜索registerXXX方法,找到调用register方法的地方;

getPhone().registerForDisconnect(mHandler, MSG_DISCONNECT, null);
  • 1

最后搜索”MSG_DISCONNECT”,找到handleMessage()方法对MSG_DISCONNECT消息的处理,所以下一步代码流程就应该是走这里了。




原文地址:http://blog.csdn.net/linyongan/article/details/52036225

猜你喜欢

转载自blog.csdn.net/f2006116/article/details/78037231
今日推荐