Home | History | Annotate | Download | only in transaction
      1 package com.android.mms.transaction;
      2 
      3 import java.util.ArrayList;
      4 
      5 import android.app.PendingIntent;
      6 import android.content.Context;
      7 import android.content.Intent;
      8 import android.database.sqlite.SQLiteException;
      9 import android.net.Uri;
     10 import android.provider.Telephony.Mms;
     11 import android.telephony.SmsManager;
     12 import android.util.Log;
     13 
     14 import com.android.mms.LogTag;
     15 import com.android.mms.MmsConfig;
     16 import com.google.android.mms.MmsException;
     17 import android.provider.Telephony.Sms;
     18 import com.android.mms.ui.MessageUtils;
     19 
     20 public class SmsSingleRecipientSender extends SmsMessageSender {
     21 
     22     private final boolean mRequestDeliveryReport;
     23     private String mDest;
     24     private Uri mUri;
     25 
     26     public SmsSingleRecipientSender(Context context, String dest, String msgText, long threadId,
     27             boolean requestDeliveryReport, Uri uri) {
     28         super(context, null, msgText, threadId);
     29         mRequestDeliveryReport = requestDeliveryReport;
     30         mDest = dest;
     31         mUri = uri;
     32     }
     33 
     34     public boolean sendMessage(long token) throws MmsException {
     35         if (mMessageText == null) {
     36             // Don't try to send an empty message, and destination should be just
     37             // one.
     38             throw new MmsException("Null message body or have multiple destinations.");
     39         }
     40         SmsManager smsManager = SmsManager.getDefault();
     41         ArrayList<String> messages = null;
     42         if ((MmsConfig.getEmailGateway() != null) &&
     43                 (Mms.isEmailAddress(mDest) || MessageUtils.isAlias(mDest))) {
     44             String msgText;
     45             msgText = mDest + " " + mMessageText;
     46             mDest = MmsConfig.getEmailGateway();
     47             messages = smsManager.divideMessage(msgText);
     48         } else {
     49            messages = smsManager.divideMessage(mMessageText);
     50            // remove spaces from destination number (e.g. "801 555 1212" -> "8015551212")
     51            mDest = mDest.replaceAll(" ", "");
     52         }
     53         int messageCount = messages.size();
     54 
     55         if (messageCount == 0) {
     56             // Don't try to send an empty message.
     57             throw new MmsException("SmsMessageSender.sendMessage: divideMessage returned " +
     58                     "empty messages. Original message is \"" + mMessageText + "\"");
     59         }
     60 
     61         boolean moved = Sms.moveMessageToFolder(mContext, mUri, Sms.MESSAGE_TYPE_OUTBOX, 0);
     62         if (!moved) {
     63             throw new MmsException("SmsMessageSender.sendMessage: couldn't move message " +
     64                     "to outbox: " + mUri);
     65         }
     66 
     67         ArrayList<PendingIntent> deliveryIntents =  new ArrayList<PendingIntent>(messageCount);
     68         ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messageCount);
     69         for (int i = 0; i < messageCount; i++) {
     70             if (mRequestDeliveryReport) {
     71                 // TODO: Fix: It should not be necessary to
     72                 // specify the class in this intent.  Doing that
     73                 // unnecessarily limits customizability.
     74                 deliveryIntents.add(PendingIntent.getBroadcast(
     75                         mContext, 0,
     76                         new Intent(
     77                                 MessageStatusReceiver.MESSAGE_STATUS_RECEIVED_ACTION,
     78                                 mUri,
     79                                 mContext,
     80                                 MessageStatusReceiver.class),
     81                         0));
     82             }
     83             Intent intent  = new Intent(SmsReceiverService.MESSAGE_SENT_ACTION,
     84                     mUri,
     85                     mContext,
     86                     SmsReceiver.class);
     87             if (i == messageCount -1) {
     88                 intent.putExtra(SmsReceiverService.EXTRA_MESSAGE_SENT_SEND_NEXT, true);
     89             }
     90             sentIntents.add(PendingIntent.getBroadcast(
     91                     mContext, 0, intent, 0));
     92         }
     93         try {
     94             smsManager.sendMultipartTextMessage(mDest, mServiceCenter, messages, sentIntents, deliveryIntents);
     95         } catch (Exception ex) {
     96             throw new MmsException("SmsMessageSender.sendMessage: caught " + ex +
     97                     " from SmsManager.sendTextMessage()");
     98         }
     99         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
    100             log("sendMessage: address=" + mDest + ", threadId=" + mThreadId +
    101                     ", uri=" + mUri + ", msgs.count=" + messageCount);
    102         }
    103         return false;
    104     }
    105 
    106     private void log(String msg) {
    107         Log.d(LogTag.TAG, "[SmsSingleRecipientSender] " + msg);
    108     }
    109 }
    110