androidの短彩信的接收流程深入分析(framework)

短彩信的接收流程


涉及的文件

[plain]  view plain copy
  1. com.android.internal.telephony/Ril.java  
  2. com.android.internal.telephony/SMSDispatcher  
  3. com.android.internal.telephony/CommandsInterface  
  4. com.android.internal.telephony/GsmSMSDispatcher  
  5. com.android.internal.telephony/CdmanSMSDispatcher  
  6. com.android.internal.telephony/ImsSMSDispatcher  
  7. hardware/ril/libril/ril.cpp  
  8. com.android.mms.transaction/PrivilegedSmsReceiver  

流程分析

时序图

 

android ril java层接收短息的流程

1)监听底层上报的数据

在Ril.java中定义了一个receive的框架:当接收到短信时,底层首先通过rild将接收到的短信通过socket传送给Ril.java,那大家都知道这个短信接收是一个不定时的,所以就必须有一个监听器一直监视这个socket一旦有短信就触发其相应的操作。那Ril.java是否有定义这样的一个监听器或者类似的功能了?(补充:这涉及一个Modem端与rild的通信,rild是一个守护进程,是整个android ril层的入口点,

通过查看代码我们可以发现RILReceiver的内部类:

[html]  view plain copy
  1. class RILReceiver implements Runnable {  
  2.         byte[] buffer;  
  3.         RILReceiver() {  
  4.             buffer = new byte[RIL_MAX_COMMAND_BYTES];  
  5.         }  
  6.         public void  
  7.         run() {  
  8.             int retryCount = 0;  
  9.             String rilSocket = "rild";  
  10.   
  11.             try {for (;;) {  
  12.                 LocalSocket s = null;  
  13.                 LocalSocketAddress l;  
  14.   
  15.                 boolean multiRild = SystemProperties.getBoolean("ro.multi.rild", false);  
  16.   
  17.                 if (mInstanceId == 0 || multiRild == false) {  
  18.                     rilSocket = SOCKET_NAME_RIL;  
  19.                 } else {  
  20.                     rilSocket = SOCKET_NAME_RIL1;  
  21.                 }  
  22.                 try {  
  23.                     s = new LocalSocket();  
  24.                     l = new LocalSocketAddress(rilSocket,  
  25.                             LocalSocketAddress.Namespace.RESERVED);  
  26.                     s.connect(l);  
  27.                 }   
  28.                    if (retryCount == 8) {  
  29.                         Log.e (LOG_TAG,  
  30.                             "Couldn't find '" + rilSocket  
  31.                             + "' socket after " + retryCount  
  32.                             + " times, continuing to retry silently");  
  33.                     } else if (retryCount > 0 && retryCount < 8) {  
  34.                         Log.i (LOG_TAG,  
  35.                             "Couldn't find '" + rilSocket  
  36.                             + "' socket; retrying after timeout");  
  37.                     }  
  38.                     try {  
  39.                         Thread.sleep(SOCKET_OPEN_RETRY_MILLIS);  
  40.                     } catch (InterruptedException er) {  
  41.                     }  
  42.                     retryCount++;  
  43.                     continue;  
  44.                 }  
  45.                 retryCount = 0;  
  46.                 mSocket = s;  
  47.                 Log.i(LOG_TAG, "Connected to '" + rilSocket + "' socket");  
  48.                 int length = 0;  
  49.                 try {  
  50.                     InputStream is = mSocket.getInputStream();  
  51.                     for (;;) {  
  52.                         Parcel p;  
  53.                         length = readRilMessage(is, buffer);  
  54.                         if (length < 0) {  
  55.                             // End-of-stream reached  
  56.                             break;  
  57.                         }  
  58.                         p = Parcel.obtain();  
  59.                         p.unmarshall(buffer, 0, length);  
  60.                         p.setDataPosition(0);  
  61. processResponse(p);  
  62.               p.recycle(); }  

2)对底层上报的数据进行处理从上面的代码看出这个线程一直和守护进程rild进行sorcket通信,并获取守护进程上报的数据。可以看出做了很多的工作但是最重要的是processResponse()方法向上汇报数据,下面是其处理代码:

[html]  view plain copy
  1. <p>  
  2.      private void  
  3.     processResponse (Parcel p) {  
  4.         int type;  
  5.   
  6.         type = p.readInt();  
  7.   
  8.         if (type == RESPONSE_UNSOLICITED) {  
  9.             processUnsolicited (p);  
  10.         } else if (type == RESPONSE_SOLICITED) {  
  11.             processSolicited (p);  
  12.         }  
  13.   
  14.         releaseWakeLockIfDone();  
  15.     }</p>  
可以看出在上报数据进行了分类处理,RESPONSE_UNSOLICITED表示接收到数据就直接上报的类型,主动上报,如网络状态和短信、来电等等。RESPONSE_SOLICITED是必须先请求然后才响应的类型。当然这里是短信接收肯定会走前者。processUnsolicited该方法会根据当前的请求的类型,如果是短信则是RIL_UNSOL_RESPONSE_NEW_SMS,以下是其调用的代码;
[html]  view plain copy
  1. case RIL_UNSOL_RESPONSE_NEW_SMS: {  
  2.                if (RILJ_LOGD) unsljLog(response);  
  3.   
  4.                // FIXME this should move up a layer  
  5.                String a[] = new String[2];  
  6.   
  7.                a[1] = (String)ret;  
  8.   
  9.                SmsMessage sms;  
  10.   
  11.                sms = SmsMessage.newFromCMT(a);  
  12.                if (mSMSRegistrant != null) {  
  13.                    mSMSRegistrant  
  14.                        .notifyRegistrant(new AsyncResult(null, sms, null));  
  15.                }  
  16.            break;  
  17.            }  

3)追溯该方法,mSMSRegistrant对象的创建过程mSMSRegistrant是BaseCommands的成员变量,且在调用setOnNewSMS()方法来赋值的,BaseCommands是干什么的有的童鞋会问,我们看一下Ril.java的继承关系就知道了,Ril.java是它的子类。了解了这些童鞋肯定会问那谁又调用了setOnNewSMS方法?以下是该流程的时序图。

 最后发现该方法设置handler的源头是在GsmSMSDispatcher类里,但最后会调用SmsDispatcher的handMessage方法。原因是GsmSMSDispatcher是SmsDispatcher的子类而且GsmSMSDispatcher没有复写handMessage方法,所以接收到消息后肯定由父类的handMessage方法来处理。

到此为止android的ril java层走完了,剩余的就交给中间层慢慢去做。

注意:

1)这是2.3的代码,对比一下4.0的代码可以发现GsmSMSDispatcher复写了handMessage的方法,自己会去处理,但是仅限于EVENT_NEW_SMS_STATUS_REPORT、EVENT_NEW_BROADCAST_SMS、EVENT_WRITE_SMS_COMPLETE其余的事件仍由父类SmsDispatcher来完成。

2)4.0中没有setOnNewSMS该方法,替换成了setOnNewGsmSms、setOnNewCdmaSms方法,对应于不同类型的phone做自己的注册。

framework SMSDispatcher接收短信后的处理流程

中间层SMSDispatcher处理流程:

1)该类做了一件重要的事:

给CommandInterface设置handler的处理方法,就是当接收到短信后触发mSMSRegistrant.notifyRegistrant(new AsyncResult(null, sms, null));方法

然后回调调用之前传入的handler,接着在handler里面处理短信消息。

2)handler处理接收到的短信消息:

[html]  view plain copy
  1. @Override     
  2.     public void handleMessage(Message msg) {  
  3.   
  4.         AsyncResult ar;  
  5.   
  6.         switch (msg.what) {  
  7.   
  8.         case EVENT_NEW_SMS:  
  9.   
  10.             // A new SMS has been received by the device  
  11.   
  12.             if (Config.LOGD) {  
  13.   
  14.                 Log.d(TAG, "New SMS Message Received");  
  15.   
  16.             }  
  17.   
  18.             SmsMessage sms;  
  19.   
  20.             ar = (AsyncResult) msg.obj;  
  21.   
  22.             if (ar.exception != null) {  
  23.   
  24.                 Log.e(TAG, "Exception processing incoming SMS. Exception:"     
  25.                         + ar.exception);  
  26.   
  27.                 return;  
  28.   
  29.             }  
  30.   
  31.             sms = (SmsMessage) ar.result;  
  32.   
  33.             try {  
  34.   
  35.                 int result = dispatchMessage(sms.mWrappedSmsMessage);  
  36.   
  37.                 if (result != Activity.RESULT_OK) {  
  38.   
  39.                     // RESULT_OK means that message was broadcast for app(s) to     
  40.                     // handle.  
  41.   
  42.                     // Any other result, we should ack here.  
  43.   
  44.                     boolean handled = (result == Intents.RESULT_SMS_HANDLED);  
  45.   
  46.                     notifyAndAcknowledgeLastIncomingSms(handled, result, null);  
  47.   
  48.                 }  
  49.   
  50.             } catch (RuntimeException ex) {  
  51.   
  52.                 Log.e(TAG, "Exception dispatching message", ex);  
  53.   
  54.                 notifyAndAcknowledgeLastIncomingSms(false,     
  55.                         Intents.RESULT_SMS_GENERIC_ERROR, null);  
  56.   
  57.             }  
  58.   
  59.             break;  
  60.   
  61.         }  
  62.   
  63.     }  

 说明:

int result = dispatchMessage(sms.mWrappedSmsMessage);

该段会通过子类(GsmSMSDispatcher)的dispatchMessage方法处理。
经一系列的判断处理最后普通短信将交给dispatchPdus(pdus);这个方法处理。

[html]  view plain copy
  1. protected void dispatchPdus(byte[][] pdus) {  
  2. Intent intent = new Intent(Intents.SMS_RECEIVED_ACTION);  
  3. intent.putExtra("pdus", pdus);  
  4. dispatch(intent, "android.permission.RECEIVE_SMS");  
  5. }  
  6.   
  7. void dispatch(Intent intent, String permission) {  
  8. // Hold a wake lock for WAKE_LOCK_TIMEOUT seconds, enough to give any  
  9. // receivers time to take their own wake locks.  
  10. mWakeLock.acquire(WAKE_LOCK_TIMEOUT);  
  11. mContext.sendOrderedBroadcast(intent, permission, mResultReceiver,  
  12. this, Activity.RESULT_OK, null, null);  
  13. }  

3)我们可以看出这个方法将短信通过顺序广播播放出去(action是SMS_RECEIVED_ACTION),无论广播是否被中断最后都会调用mResultReceiver,这里会将已读或未读的状态告诉给对方。如果短信广播中间没有受到終止。然后短信app中PrivilegedSmsReceiver广播接收器接收到广播后会调用onReceiveWithPrivilege方法,由于PrivilegedSmsReceiver继承与SmsReceiver,所以会调用父类的该方法。其代码:

[html]  view plain copy
  1. protected void onReceiveWithPrivilege(Context context, Intent intent, boolean privileged) {
  2.         // If 'privileged' is false, it means that the intent was delivered to the base  
  3.         // no-permissions receiver class.  If we get an SMS_RECEIVED message that way, it  
  4.         // means someone has tried to spoof the message by delivering it outside the normal
  5.         // permission-checked route, so we just ignore it.  
  6.         if (!privileged && (intent.getAction().equals(Intents.SMS_RECEIVED_ACTION)  
  7.                 || intent.getAction().equals(Intents.SMS_CB_RECEIVED_ACTION))) {  
  8.             return;  
  9.         }  
  10.   
  11.         intent.setClass(context, SmsReceiverService.class);  
  12.         intent.putExtra("result", getResultCode());  
  13.         beginStartingService(context, intent);  
  14.     }  

最后交由SmsReceiverService该service去处理,到此为止已经到应用层Mms。

彩信的接收
  可能有些童鞋看到这明白了短信的接收可能会问到那彩信的接收又会使什么样的了,为了解惑所以在此简单描述彩信接收的过程,由于很多和短信相似。这个过程了就画一个时序图个大家,注意:这直到应用层,应用层的具体实现会有专门的文章来讲述。在这以GSM为例,以下是其时序图:
            

说明:大家可以看出和短信的接收来他们的区别在于dispatchMessage方法会根据smsHeader.portAddrs来判断当前是彩信还是短信,然后调用对应的方法。

总结

   这里没有去分析rild是怎么和ril.java 建立socket通信,也没有讲rild怎么和moderm端进行通信的,这些我会继续研究,希望尽早分享给大家。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值