|  |  |  | @ -19,15 +19,27 @@ package org.thoughtcrime.securesms.sms; | 
		
	
		
			
				|  |  |  |  | import android.content.Context; | 
		
	
		
			
				|  |  |  |  | import android.content.Intent; | 
		
	
		
			
				|  |  |  |  | import android.util.Log; | 
		
	
		
			
				|  |  |  |  | import android.util.Pair; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.database.EncryptingSmsDatabase; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.database.MmsDatabase; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.database.MmsSmsColumns; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.mms.IncomingMediaMessage; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.mms.OutgoingMediaMessage; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.recipients.Recipients; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.util.TextSecurePreferences; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.util.Util; | 
		
	
		
			
				|  |  |  |  | import org.whispersystems.textsecure.crypto.MasterSecret; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.database.DatabaseFactory; | 
		
	
		
			
				|  |  |  |  | import org.thoughtcrime.securesms.service.SendReceiveService; | 
		
	
		
			
				|  |  |  |  | import org.whispersystems.textsecure.push.IncomingPushMessage; | 
		
	
		
			
				|  |  |  |  | import org.whispersystems.textsecure.push.PushMessageProtos; | 
		
	
		
			
				|  |  |  |  | import org.whispersystems.textsecure.util.InvalidNumberException; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import java.util.List; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import ws.com.google.android.mms.MmsException; | 
		
	
		
			
				|  |  |  |  | import ws.com.google.android.mms.pdu.RetrieveConf; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | public class MessageSender { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -35,12 +47,23 @@ public class MessageSender { | 
		
	
		
			
				|  |  |  |  |                           OutgoingTextMessage message, long threadId, | 
		
	
		
			
				|  |  |  |  |                           boolean forceSms) | 
		
	
		
			
				|  |  |  |  |   { | 
		
	
		
			
				|  |  |  |  |     if (threadId == -1) | 
		
	
		
			
				|  |  |  |  |     EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if (threadId == -1) { | 
		
	
		
			
				|  |  |  |  |       threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(message.getRecipients()); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     List<Long> messageIds = DatabaseFactory.getEncryptingSmsDatabase(context) | 
		
	
		
			
				|  |  |  |  |         .insertMessageOutbox(masterSecret, threadId, message, forceSms); | 
		
	
		
			
				|  |  |  |  |     List<Long> messageIds = database.insertMessageOutbox(masterSecret, threadId, message, forceSms); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if (!forceSms && isSelfSend(context, message.getRecipients())) { | 
		
	
		
			
				|  |  |  |  |       for (long messageId : messageIds) { | 
		
	
		
			
				|  |  |  |  |         database.markAsSent(messageId); | 
		
	
		
			
				|  |  |  |  |         database.markAsPush(messageId); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         Pair<Long, Long> messageAndThreadId = database.copyMessageInbox(messageId); | 
		
	
		
			
				|  |  |  |  |         database.markAsPush(messageAndThreadId.first); | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  |     } else { | 
		
	
		
			
				|  |  |  |  |       for (long messageId : messageIds) { | 
		
	
		
			
				|  |  |  |  |         Log.w("SMSSender", "Got message id for new message: " + messageId); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -49,6 +72,7 @@ public class MessageSender { | 
		
	
		
			
				|  |  |  |  |         intent.putExtra("message_id", messageId); | 
		
	
		
			
				|  |  |  |  |         context.startService(intent); | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     return threadId; | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
	
		
			
				
					|  |  |  | @ -58,18 +82,27 @@ public class MessageSender { | 
		
	
		
			
				|  |  |  |  |                           long threadId, boolean forceSms) | 
		
	
		
			
				|  |  |  |  |       throws MmsException | 
		
	
		
			
				|  |  |  |  |   { | 
		
	
		
			
				|  |  |  |  |     if (threadId == -1) | 
		
	
		
			
				|  |  |  |  |     MmsDatabase database = DatabaseFactory.getMmsDatabase(context); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if (threadId == -1) { | 
		
	
		
			
				|  |  |  |  |       threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(message.getRecipients(), message.getDistributionType()); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     long messageId = DatabaseFactory.getMmsDatabase(context) | 
		
	
		
			
				|  |  |  |  |                                     .insertMessageOutbox(masterSecret, message, threadId, forceSms); | 
		
	
		
			
				|  |  |  |  |     long messageId = database.insertMessageOutbox(masterSecret, message, threadId, forceSms); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if (!forceSms && isSelfSend(context, message.getRecipients())) { | 
		
	
		
			
				|  |  |  |  |       database.markAsSent(messageId, "self-send".getBytes(), 0); | 
		
	
		
			
				|  |  |  |  |       database.markAsPush(messageId); | 
		
	
		
			
				|  |  |  |  |       long newMessageId = database.copyMessageInbox(masterSecret, messageId); | 
		
	
		
			
				|  |  |  |  |       database.markAsPush(newMessageId); | 
		
	
		
			
				|  |  |  |  |     } else { | 
		
	
		
			
				|  |  |  |  |       Intent intent  = new Intent(SendReceiveService.SEND_MMS_ACTION, null, | 
		
	
		
			
				|  |  |  |  |                                   context, SendReceiveService.class); | 
		
	
		
			
				|  |  |  |  |       intent.putExtra("message_id", messageId); | 
		
	
		
			
				|  |  |  |  |       intent.putExtra("thread_id", threadId); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |       context.startService(intent); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     return threadId; | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
	
		
			
				
					|  |  |  | @ -91,4 +124,22 @@ public class MessageSender { | 
		
	
		
			
				|  |  |  |  |     context.startService(intent); | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |   private static boolean isSelfSend(Context context, Recipients recipients) { | 
		
	
		
			
				|  |  |  |  |     try { | 
		
	
		
			
				|  |  |  |  |       if (!TextSecurePreferences.isPushRegistered(context)) { | 
		
	
		
			
				|  |  |  |  |         return false; | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |       if (!recipients.isSingleRecipient()) { | 
		
	
		
			
				|  |  |  |  |         return false; | 
		
	
		
			
				|  |  |  |  |       } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |       String e164number = Util.canonicalizeNumber(context, recipients.getPrimaryRecipient().getNumber()); | 
		
	
		
			
				|  |  |  |  |       return TextSecurePreferences.getLocalNumber(context).equals(e164number); | 
		
	
		
			
				|  |  |  |  |     } catch (InvalidNumberException e) { | 
		
	
		
			
				|  |  |  |  |       Log.w("MessageSender", e); | 
		
	
		
			
				|  |  |  |  |       return false; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |   } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | } | 
		
	
	
		
			
				
					|  |  |  | 
 |