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