收信息

接收到短信.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);
}

你可能感兴趣的:(收信息)