在底层(native层)接收到短信后通过socket发送RIL_UNSOL_RESPONSE_NEW_SMS 响应给RIL。
在RIL(见com/android/internal/telephony/RIL.java)中,启动RILReceiver用于接收处理底层发送的响应,
mReceiver= new RILReceiver();
mReceiverThread= new Thread(mReceiver, "RILReceiver");
mReceiverThread.start();
在RILReceiver中通过socket接收来自native层的响应(Parcel对象):
建立cocket连接
LocalSockets = null;
LocalSocketAddress l;
s= new LocalSocket();
l= new LocalSocketAddress(SOCKET_NAME_RIL,
LocalSocketAddress.Namespace.RESERVED);
s.connect(l);
读取响应:
intlength = 0;
InputStreamis = mSocket.getInputStream();
for(;;) {
Parcel p;
length = readRilMessage(is, buffer);
if(length < 0) {
// End-of-stream reached
break;
}
p= Parcel.obtain();
p.unmarshall(buffer, 0, length);
p.setDataPosition(0);
//处理响应
processResponse(p);
p.recycle();
}
在processResponse处理的响应中分为请求响应与非请求响应,其中接收短信为非请求响应。在RIL中处理的响应很多,这里只讨论对于RIL_UNSOL_RESPONSE_NEW_SMS的响应。在收到RIL_UNSOL_RESPONSE_NEW_SMS响应后processResponse调用responseString方法取得响应消息的内容,使用SmsMessage.newFromCMT方法将内容转化为短消息。使用mGsmSmsRegistrant.notifyRegistrant(newAsyncResult(null, sms, null))方法调用短消息的分发类对短消息进行分发(见android/os/Registrant.java)。
对于mGsmSmsRegistrant.notifyRegistrant短信的分发过程:
在Registrant中notifyRegistrant(AsyncResultar)方法就是向对应的Handler对象发送消息。
publicvoid
notifyRegistrant(AsyncResult ar)
{
internalNotifyRegistrant (ar.result,ar.exception);
}
/*package*/void
internalNotifyRegistrant(Object result, Throwable exception)
{
Handlerh = getHandler();
if(h == null) {
clear();
}else {
Messagemsg = Message.obtain();
msg.what= what;
msg.obj= new AsyncResult(userObj, result, exception);
h.sendMessage(msg);
}
}
而mGsmSmsRegistrant中的Handler对象即时通过RIL的父类中的setOnNewGsmSms方法设置。
在phoneFactory的makeDefaultPhone方法中首先创建RIL实例,然后根据网络模式选择创建GSMPhone或是CDMAPhone。在创建GSMPhone时PhoneBaseSMSDispatcher调用了setOnNewGsmSms方法。
sCommandsInterface= new RIL(context, networkMode, cdmaSubscription);
intphoneType = getPhoneType(networkMode);
if(phoneType == Phone.PHONE_TYPE_GSM) {
sProxyPhone = new PhoneProxy(newGSMPhone(context,
sCommandsInterface,sPhoneNotifier));
}
在 GSMPhone类中:
public
GSMPhone(Context context, CommandsInterface ci, PhoneNotifier notifier,boolean unitTestMode) {
mSMS= new GsmSMSDispatcher(this, mSmsStorageMonitor, mSmsUsageMonitor);
}
在 GsmSMSDispatcher中:
publicGsmSMSDispatcher(PhoneBase phone, SmsStorageMonitor storageMonitor,
SmsUsageMonitorusageMonitor) {
super(phone,storageMonitor, usageMonitor);
Log.v("smstest","GsmSMSDispatcher");
mDataDownloadHandler= new UsimDataDownloadHandler(mCm);
mCm.setOnNewGsmSms(this,EVENT_NEW_SMS, null);
mCm.setOnSmsStatus(this,EVENT_NEW_SMS_STATUS_REPORT, null);
mCm.setOnNewGsmBroadcastSms(this,EVENT_NEW_BROADCAST_SMS, null);
}
其中:mCm就是phoneFactory的makeDefaultPhone方法中创建的sCommandsInterface的引用。
此时,native层中的短信已经传递到了GsmSMSDispatcher中,系统调用handleMessage处理mGsmSmsRegistrant.notifyRegistrant发送的message。
Handlemessage→ SMSDispatcher.handleMessage → GsmSMSDispatcher.dispatchMessage→ SMSDispatcher.dispatchNormalMessage →SMSDispatcher.dispatchPdus
最后dispatchPdus通过“android.provider.Telephony.SMS_RECEIVED”广播将短信息传递给各个感兴趣的app
protectedvoid dispatchPdus(byte[][] pdus) {
Intentintent = new Intent(Intents.SMS_RECEIVED_ACTION);
intent.putExtra("pdus",pdus);
intent.putExtra("format",getFormat());
dispatch(intent,RECEIVE_SMS_PERMISSION);
}
自此,一条普通短信从native层传递到了app层。
参考:
阅读(1199) | 评论(0) | 转发(0) |