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