@ -16,32 +16,31 @@
* /
package org.thoughtcrime.securesms.sms ;
import java.util.Iterator ;
import android.content.Context ;
import android.content.Intent ;
import android.telephony.PhoneNumberUtils ;
import android.util.Log ;
import org.thoughtcrime.securesms.crypto.KeyUtil ;
import org.thoughtcrime.securesms.crypto.MasterSecret ;
import org.thoughtcrime.securesms.database.DatabaseFactory ;
import org.thoughtcrime.securesms.mms.SlideDeck ;
import org.thoughtcrime.securesms.mms.TextSlide ;
import org.thoughtcrime.securesms.recipients.Recipient ;
import org.thoughtcrime.securesms.recipients.Recipients ;
import org.thoughtcrime.securesms.service.MmsSender ;
import org.thoughtcrime.securesms.service.SendReceiveService ;
import org.thoughtcrime.securesms.service.SmsSender ;
import ws.com.google.android.mms.ContentType ;
import ws.com.google.android.mms.MmsException ;
import ws.com.google.android.mms.pdu.EncodedStringValue ;
import ws.com.google.android.mms.pdu.PduBody ;
import ws.com.google.android.mms.pdu.SendReq ;
import android.content.Context ;
import android.content.Intent ;
import android.telephony.PhoneNumberUtils ;
import android.util.Log ;
public class MessageSender {
public static long sendMms ( Context context , MasterSecret masterSecret , Recipients recipients ,
long threadId , SlideDeck slideDeck , String message , boolean isSecure ) throws MmsException
long threadId , SlideDeck slideDeck , String message , boolean forcePlaintext )
throws MmsException
{
if ( threadId = = - 1 )
threadId = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( recipients ) ;
@ -49,42 +48,55 @@ public class MessageSender {
if ( message . trim ( ) . length ( ) > 0 )
slideDeck . addSlide ( new TextSlide ( context , message ) ) ;
SendReq sendRequest = new SendReq ( ) ;
String [ ] recipientsArray = recipients . toNumberStringArray ( ) ;
EncodedStringValue [ ] encodedNumbers = EncodedStringValue . encodeStrings ( recipientsArray ) ;
PduBody body = slideDeck . toPduBody ( ) ;
SendReq sendRequest = new SendReq ( ) ;
PduBody body = slideDeck . toPduBody ( ) ;
sendRequest . setTo ( encodedNumbers ) ;
sendRequest . setDate ( System . currentTimeMillis ( ) / 1000L ) ;
sendRequest . setBody ( body ) ;
sendRequest . setContentType ( ContentType . MULTIPART_MIXED . getBytes ( ) ) ;
long messageId = DatabaseFactory . getEncryptingMmsDatabase ( context , masterSecret ) . insertMessageSent ( sendRequest , threadId , isSecure ) ;
Intent intent = new Intent ( SendReceiveService . SEND_MMS_ACTION , null , context , SendReceiveService . class ) ;
intent . putExtra ( "message_id" , messageId ) ;
context . startService ( intent ) ;
Recipients secureRecipients = recipients . getSecureSessionRecipients ( context ) ;
Recipients insecureRecipients = recipients . getInsecureSessionRecipients ( context ) ;
for ( Recipient secureRecipient : secureRecipients . getRecipientsList ( ) ) {
sendMms ( context , new Recipients ( secureRecipient ) , masterSecret ,
sendRequest , threadId , ! forcePlaintext ) ;
}
if ( ! insecureRecipients . isEmpty ( ) ) {
sendMms ( context , insecureRecipients , masterSecret , sendRequest , threadId , false ) ;
}
return threadId ;
}
public static long send ( Context context , MasterSecret masterSecret , Recipients recipients ,
long threadId , String message , boolean isSecure )
long threadId , String message , boolean forcePlaintext)
{
if ( threadId = = - 1 )
threadId = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( recipients ) ;
Iterator < Recipient > i = recipients . getRecipientsList ( ) . iterator ( ) ;
while ( i . hasNext ( ) ) {
Recipient recipient = i . next ( ) ;
for ( Recipient recipient : recipients . getRecipientsList ( ) ) {
boolean isSecure = KeyUtil . isSessionFor ( context , recipient ) & & ! forcePlaintext ;
long messageId ;
if ( ! isSecure ) messageId = DatabaseFactory . getEncryptingSmsDatabase ( context ) . insertMessageSent ( masterSecret , PhoneNumberUtils . formatNumber ( recipient . getNumber ( ) ) , threadId , message , System . currentTimeMillis ( ) ) ;
else messageId = DatabaseFactory . getEncryptingSmsDatabase ( context ) . insertSecureMessageSent ( masterSecret , PhoneNumberUtils . formatNumber ( recipient . getNumber ( ) ) , threadId , message , System . currentTimeMillis ( ) ) ;
if ( ! isSecure ) {
messageId = DatabaseFactory . getEncryptingSmsDatabase ( context )
. insertMessageSent ( masterSecret ,
PhoneNumberUtils . formatNumber ( recipient . getNumber ( ) ) ,
threadId , message , System . currentTimeMillis ( ) ) ;
} else {
messageId = DatabaseFactory . getEncryptingSmsDatabase ( context )
. insertSecureMessageSent ( masterSecret ,
PhoneNumberUtils . formatNumber ( recipient . getNumber ( ) ) ,
threadId , message , System . currentTimeMillis ( ) ) ;
}
Log . w ( "SMSSender" , "Got message id for new message: " + messageId ) ;
Intent intent = new Intent ( SendReceiveService . SEND_SMS_ACTION , null , context , SendReceiveService . class ) ;
Intent intent = new Intent ( SendReceiveService . SEND_SMS_ACTION , null ,
context , SendReceiveService . class ) ;
intent . putExtra ( "message_id" , messageId ) ;
context . startService ( intent ) ;
}
@ -92,4 +104,22 @@ public class MessageSender {
return threadId ;
}
private static void sendMms ( Context context , Recipients recipients , MasterSecret masterSecret ,
SendReq sendRequest , long threadId , boolean secure )
throws MmsException
{
String [ ] recipientsArray = recipients . toNumberStringArray ( ) ;
EncodedStringValue [ ] encodedNumbers = EncodedStringValue . encodeStrings ( recipientsArray ) ;
sendRequest . setTo ( encodedNumbers ) ;
long messageId = DatabaseFactory . getEncryptingMmsDatabase ( context , masterSecret )
. insertMessageSent ( sendRequest , threadId , secure ) ;
Intent intent = new Intent ( SendReceiveService . SEND_MMS_ACTION , null ,
context , SendReceiveService . class ) ;
intent . putExtra ( "message_id" , messageId ) ;
context . startService ( intent ) ;
}
}