手动下载彩信

2019-05-26  本文已影响0人  坠叶飘香
下载彩信.png 手动下载彩信

1.给下载按钮设置点击事件

packages\apps\Mms\src\com\android\mms\ui\MessageListItem.java

mDownloadButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    mDownloadingLabel.setVisibility(View.VISIBLE);
    mDownloadButton.setVisibility(View.GONE);
    Intent intent = new Intent(mContext, TransactionService.class);
    intent.putExtra(TransactionBundle.URI, mMessageItem.mMessageUri.toString());
    intent.putExtra(TransactionBundle.TRANSACTION_TYPE, Transaction.RETRIEVE_TRANSACTION);
    mContext.startService(intent);

    DownloadManager.getInstance().markState(mMessageItem.mMessageUri, DownloadManager.STATE_PRE_DOWNLOADING);
  }
});

2.onStartCommand

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

public int onStartCommand(Intent intent, int flags, int startId) {
  if (intent != null) {
    Message msg = mServiceHandler.obtainMessage(EVENT_NEW_INTENT);
    msg.arg1 = startId;
    msg.obj = intent;
    mServiceHandler.sendMessage(msg);
  }
  return Service.START_NOT_STICKY;
}

3.handleMessage

EVENT_NEW_INTENT
packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

private final class ServiceHandler extends Handler {
  public void handleMessage(Message msg) {
    Transaction transaction = null;
    switch (msg.what) {
      case EVENT_NEW_INTENT:
        onNewIntent((Intent)msg.obj, msg.arg1);
        break;
    }
  }
}

4.onNewIntent

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

public void onNewIntent(Intent intent, int serviceId) {
  if (ACTION_ONALARM.equals(action) || ACTION_ENABLE_AUTO_RETRIEVE.equals(action) ||
                (intent.getExtras() == null)) {
    
  } else{
    TransactionBundle args = new TransactionBundle(intent.getExtras());
    launchTransaction(serviceId, args, noNetwork);
  }
}

5.launchTransaction

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

private void launchTransaction(int serviceId, TransactionBundle txnBundle, boolean noNetwork) {
  if (noNetwork) {
    Log.w(TAG, "launchTransaction: no network error!");
    onNetworkUnavailable(serviceId, txnBundle.getTransactionType());
    return;
  }
  Message msg = mServiceHandler.obtainMessage(EVENT_TRANSACTION_REQUEST);
  msg.arg1 = serviceId;
  msg.obj = txnBundle;

  mServiceHandler.sendMessage(msg);
}

6.handleMessage

EVENT_TRANSACTION_REQUEST
packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java
private final class ServiceHandler extends Handler {
public void handleMessage(Message msg) {
Transaction transaction = null;
switch (msg.what) {
case EVENT_TRANSACTION_REQUEST:
int serviceId = msg.arg1;
try {
switch (transactionType) {
case Transaction.RETRIEVE_TRANSACTION:
transaction = new RetrieveTransaction(TransactionService.this, serviceId,transactionSettings, args.getUri());
break;
}

      if (!processTransaction(transaction)) {
        transaction = null;
        return;
      }
    } finally{
      if (transaction == null) {
        endMmsConnectivity();
        stopSelf(serviceId);
      }
    }
    return;
}

}
}

7.processTransaction

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

private boolean processTransaction(Transaction transaction) throws IOException {
  transaction.attach(TransactionService.this);
  transaction.process();
}

8.process

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

public void process() {
  new Thread(this, "RetrieveTransaction").start();
}

9.run

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

public void run() {
  try {
    // Change the downloading state of the M-Notification.ind.
    DownloadManager.getInstance().markState(mUri, DownloadManager.STATE_DOWNLOADING);

    // Send GET request to MMSC and retrieve the response data.
    //下载彩信
    byte[] resp = getPdu(mContentLocation);

    // Parse M-Retrieve.conf
    RetrieveConf retrieveConf = (RetrieveConf) new PduParser(resp, PduParserUtil.shouldParseContentDisposition()).parse();

    // Store M-Retrieve.conf into Inbox
    //下载的彩信存入数据库
    PduPersister persister = PduPersister.getPduPersister(mContext);
    msgUri = persister.persist(retrieveConf, Inbox.CONTENT_URI, true,
                        MessagingPreferenceActivity.getIsGroupMmsEnabled(mContext), null);

    // Delete the corresponding M-Notification.ind.
    //删除通知
    SqliteWrapper.delete(mContext, mContext.getContentResolver(), mUri, null, null);

    sendAcknowledgeInd(retrieveConf);  //M-Acknowledge.ind
  } finally {
    if (mTransactionState.getState() != TransactionState.SUCCESS) {
      mTransactionState.setState(TransactionState.FAILED);
      mTransactionState.setContentUri(mUri);
      Log.e(TAG, "Retrieval failed.");
    }
    notifyObservers();
  }
}

10.sendAcknowledgeInd

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

private void sendAcknowledgeInd(RetrieveConf rc) throws MmsException, IOException {
  // Send M-Acknowledge.ind to MMSC if required.
  // If the Transaction-ID isn't set in the M-Retrieve.conf, it means
  // the MMS proxy-relay doesn't require an ACK.
  byte[] tranId = rc.getTransactionId();
  if (tranId != null) {
    // Create M-Acknowledge.ind
    AcknowledgeInd acknowledgeInd = new AcknowledgeInd(
    PduHeaders.CURRENT_MMS_VERSION, tranId);

    // insert the 'from' address per spec
    String lineNumber = MessageUtils.getLocalNumber();
    acknowledgeInd.setFrom(new EncodedStringValue(lineNumber));

    // Pack M-Acknowledge.ind and send it
    if(MmsConfig.getNotifyWapMMSC()) {
      sendPdu(new PduComposer(mContext, acknowledgeInd).make(), mContentLocation);
    } else {
      sendPdu(new PduComposer(mContext, acknowledgeInd).make());
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读