收信息

接收到短信.png

1.cdmaNewSms

frameworks\opt\telephony\src\java\com\android\internal\telephony\RadioIndication.java

public void cdmaNewSms(int indicationType, CdmaSmsMessage msg) {
  mRil.processIndication(indicationType);

  if (RIL.RILJ_LOGD) mRil.unsljLog(RIL_UNSOL_RESPONSE_CDMA_NEW_SMS);

  mRil.writeMetricsNewSms(SmsSession.Event.Tech.SMS_CDMA, SmsSession.Event.Format.SMS_FORMAT_3GPP2);

  // todo: conversion from CdmaSmsMessage to SmsMessage should be contained in this class so
  // that usage of auto-generated HAL classes is limited to this file
  SmsMessage sms = SmsMessageConverter.newSmsMessageFromCdmaSmsMessage(msg);
  if (mRil.mCdmaSmsRegistrant != null) {
    mRil.mCdmaSmsRegistrant.notifyRegistrant(new AsyncResult(null, sms, null));  //mCdmaSmsRegistrant:Registrant
  }
}

2.notifyRegistrant

frameworks\base\core\java\android\os\Registrant.java

public void notifyRegistrant(AsyncResult ar) {
  internalNotifyRegistrant (ar.result, ar.exception);
}

3.internalNotifyRegistrant

frameworks\base\core\java\android\os\Registrant.java

void internalNotifyRegistrant (Object result, Throwable exception) {
  Handler h = getHandler(); //之前生成Registrant时的handler处理Message
  if (h == null) {
    clear();
  } else {
    Message msg = Message.obtain();
    msg.what = what;    
    msg.obj = new AsyncResult(userObj, result, exception);
    h.sendMessage(msg);
  }
}

4.handleMessage

frameworks\base\core\java\com\android\internal\util\StateMachine.java

private static class SmHandler extends Handler {
  public final void handleMessage(Message msg) {
    msgProcessedState = processMsg(msg);
  }
}

5.processMsg

frameworks\base\core\java\com\android\internal\util\StateMachine.java

private final State processMsg(Message msg) {
  while (!curStateInfo.state.processMessage(msg)) {
  }
}

6.processMessage

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

private class DeliveringState extends State {
  public boolean processMessage(Message msg) {
    switch (msg.what) {
      case EVENT_NEW_SMS:
        handleNewSms((AsyncResult) msg.obj);
        sendMessage(EVENT_RETURN_TO_IDLE);
        return HANDLED;
    }
  }
}

7.handleNewSms

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

private void handleNewSms(AsyncResult ar) {
  if (ar.exception != null) {
    loge("Exception processing incoming SMS: " + ar.exception);
    return;
  }

  int result;
  try {
    SmsMessage sms = (SmsMessage) ar.result;
    result = dispatchMessage(sms.mWrappedSmsMessage);
  } catch (RuntimeException ex) {
    loge("Exception dispatching message", ex);
    result = Intents.RESULT_SMS_GENERIC_ERROR;
  }

  // RESULT_OK means that the SMS will be acknowledged by special handling,
  // e.g. for SMS-PP data download. Any other result, we should ack here.
  if (result != Activity.RESULT_OK) {
    boolean handled = (result == Intents.RESULT_SMS_HANDLED);
    notifyAndAcknowledgeLastIncomingSms(handled, result, null);
  }
}

8.dispatchMessage

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

private int dispatchMessage(SmsMessageBase smsb) {
  return dispatchMessageRadioSpecific(smsb);
}

9.dispatchMessageRadioSpecific

frameworks\opt\telephony\src\java\com\android\internal\telephony\gsm\GsmInboundSmsHandler.java

protected int dispatchMessageRadioSpecific(SmsMessageBase smsb) {
  return dispatchNormalMessage(smsb);
}

10.dispatchNormalMessage

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

protected int dispatchNormalMessage(SmsMessageBase sms) {
  return addTrackerToRawTableAndSendMessage(tracker,
                tracker.getDestPort() == -1 /* de-dup if text message */);
}

11.addTrackerToRawTableAndSendMessage

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

protected int addTrackerToRawTableAndSendMessage(InboundSmsTracker tracker, boolean deDup) {
        switch(addTrackerToRawTable(tracker, deDup)) { //把短信存入raw表
        case Intents.RESULT_SMS_HANDLED:
            sendMessage(EVENT_BROADCAST_SMS, tracker); //发送Message
            return Intents.RESULT_SMS_HANDLED;

        case Intents.RESULT_SMS_DUPLICATED:
            return Intents.RESULT_SMS_HANDLED;

        case Intents.RESULT_SMS_GENERIC_ERROR:
        default:
            return Intents.RESULT_SMS_GENERIC_ERROR;
        }
}

12.processMessage

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

private class DeliveringState extends State {
  public boolean processMessage(Message msg) {
    case EVENT_BROADCAST_SMS:
      // if any broadcasts were sent, transition to waiting state
      InboundSmsTracker inboundSmsTracker = (InboundSmsTracker) msg.obj;
      if (processMessagePart(inboundSmsTracker)) { //如果长短信没有收到完整的信息,还是需要等待剩余信息
        transitionTo(mWaitingState);
      } else {
        // if event is sent from SmsBroadcastUndelivered.broadcastSms(), and
        // processMessagePart() returns false, the state machine will be stuck in
        // DeliveringState until next message is received. Send message to
        // transition to idle to avoid that so that wakelock can be released
        log("No broadcast sent on processing EVENT_BROADCAST_SMS in Delivering " + "state. Return to Idle state");
        sendMessage(EVENT_RETURN_TO_IDLE);
      }
      return HANDLED;
  }
}

13.processMessagePart

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

private boolean processMessagePart(InboundSmsTracker tracker) {
  if (!filterInvoked) {
    dispatchSmsDeliveryIntent(pdus, tracker.getFormat(), destPort, resultReceiver);
  }
  return true;
}

14.dispatchSmsDeliveryIntent

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

private void dispatchSmsDeliveryIntent(byte[][] pdus, String format, int destPort,
            BroadcastReceiver resultReceiver) {
  Intent intent = new Intent();
  intent.putExtra("pdus", pdus);
  intent.putExtra("format", format);

  if (destPort == -1) {
    intent.setAction(Intents.SMS_DELIVER_ACTION);
  } else{
    intent.setAction(Intents.DATA_SMS_RECEIVED_ACTION);
  }
  dispatchIntent(intent, android.Manifest.permission.RECEIVE_SMS,
                AppOpsManager.OP_RECEIVE_SMS, options, resultReceiver, UserHandle.SYSTEM);
}

15.dispatchIntent

frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSmsHandler.java

public void dispatchIntent(Intent intent, String permission, int appOp,
            Bundle opts, BroadcastReceiver resultReceiver, UserHandle user) {
  //发送广播
  mContext.sendOrderedBroadcastAsUser(intent, user, permission, appOp, opts,
                    resultReceiver, getHandler(), Activity.RESULT_OK, null, null);
}

16.上层应用收到广播

packages\apps\Mms\src\com\android\mms\transaction\PrivilegedSmsReceiver.java

public class PrivilegedSmsReceiver extends SmsReceiver {
  public void onReceive(Context context, Intent intent) {
    onReceiveWithPrivilege(context, intent, true);
  }
}

17.启动SmsReceiverService

packages\apps\Mms\src\com\android\mms\transaction\SmsReceiver.java

protected void onReceiveWithPrivilege(Context context, Intent intent, boolean privileged) {
  if (!privileged && intent.getAction().equals(Intents.SMS_DELIVER_ACTION)) {
            return;
  }

  intent.setClass(context, SmsReceiverService.class);
  intent.putExtra("result", getResultCode());
  beginStartingService(context, intent);
}

18.handleMessage

packages\apps\Mms\src\com\android\mms\transaction\SmsReceiverService.java

public void handleMessage(Message msg) {
  if (SMS_DELIVER_ACTION.equals(action)) {
    handleSmsReceived(intent, error);
  }
}

19.handleSmsReceived

packages\apps\Mms\src\com\android\mms\transaction\SmsReceiverService.java

private void handleSmsReceived(Intent intent, int error) {
  SmsMessage[] msgs = Intents.getMessagesFromIntent(intent);
  String format = intent.getStringExtra("format");
  Uri messageUri = insertMessage(this, msgs, error, format); //插入数据库
  if (messageUri != null) { //弹出新信息的通知
    long threadId = MessagingNotification.getSmsThreadId(this, messageUri);
    // Called off of the UI thread so ok to block.
    Log.d(TAG, "handleSmsReceived messageUri: " + messageUri + " threadId: " + threadId);
    MessagingNotification.blockingUpdateNewMessageIndicator(this, threadId, false);
  }
}

20.insertMessage

packages\apps\Mms\src\com\android\mms\transaction\SmsReceiverService.java

private Uri insertMessage(Context context, SmsMessage[] msgs, int error, String format) {
  // Build the helper classes to parse the messages.
  SmsMessage sms = msgs[0];

  if (sms.getMessageClass() == SmsMessage.MessageClass.CLASS_0) {
    displayClassZeroMessage(context, sms, format);    //显示class0信息
    return null;
  } else if (sms.isReplace()) {
    return replaceMessage(context, msgs, error);
  } else {
    return storeMessage(context, msgs, error);
  }
}

21.storeMessage插入数据库

packages\apps\Mms\src\com\android\mms\transaction\SmsReceiverService.java

private Uri storeMessage(Context context, SmsMessage[] msgs, int error) {
  ContentResolver resolver = context.getContentResolver();
  Uri insertedUri = SqliteWrapper.insert(context, resolver, Inbox.CONTENT_URI, values);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,529评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,015评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,409评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,385评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,387评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,466评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,880评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,528评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,727评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,528评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,602评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,302评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,873评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,890评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,132评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,777评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,310评论 2 342

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,689评论 0 3
  • 1.makeDefaultPhones frameworks\opt\telephony\src\java\com...
    坠叶飘香阅读 706评论 0 0
  • 1. 应用进程 从点击拨号按钮流程说起packages/apps/Dialer/src/com/android/d...
    小的橘子阅读 4,094评论 3 1
  • 文/青枫曦雨 谁说相思无处寄 寄一曲红颜残殇高歌 舞一段羞涩娇舞 把相思寄给高空明月 邀君共青舞,饮美酒 如此,可...
    青枫曦雨阅读 351评论 0 2
  • 各位小伙伴,大家晚上好,还记得我们小时候每到大城市,就被金碧辉煌的灯光吸引了、陶醉了,没错,我就是用灯光点亮一座城...
    Sherry秋阅读 154评论 0 0