2012-10-21 17:41:44 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-10-21 17:41:44 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-10-21 17:41:44 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.sms;
|
|
|
|
|
2012-10-21 17:41:44 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-02-17 11:42:51 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.ImageSlide;
|
2013-08-17 18:37:18 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2011-12-20 10:20:44 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2013-04-25 18:59:49 -07:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
2011-12-20 10:20:44 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
|
|
|
import org.thoughtcrime.securesms.mms.TextSlide;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
import java.util.List;
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
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;
|
2014-02-17 11:42:51 -08:00
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
2011-12-20 10:20:44 -08:00
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
|
|
|
public class MessageSender {
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2014-02-17 11:42:51 -08:00
|
|
|
public static long sendGroupAction(Context context, MasterSecret masterSecret, Recipients recipients,
|
|
|
|
long threadId, int groupAction, String groupActionArguments, byte[] avatar)
|
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
if (threadId == -1) {
|
|
|
|
threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
|
|
|
}
|
|
|
|
|
|
|
|
PduBody body = new PduBody();
|
|
|
|
|
|
|
|
if (avatar != null) {
|
|
|
|
PduPart part = new PduPart();
|
|
|
|
part.setData(avatar);
|
|
|
|
part.setContentType(ContentType.IMAGE_PNG.getBytes());
|
|
|
|
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
|
|
|
part.setName(("Image" + System.currentTimeMillis()).getBytes());
|
|
|
|
body.addPart(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
SendReq sendRequest = new SendReq();
|
|
|
|
sendRequest.setDate(System.currentTimeMillis() / 1000L);
|
|
|
|
sendRequest.setBody(body);
|
|
|
|
sendRequest.setContentType(ContentType.MULTIPART_MIXED.getBytes());
|
|
|
|
sendRequest.setGroupAction(groupAction);
|
|
|
|
sendRequest.setGroupActionArguments(groupActionArguments);
|
|
|
|
|
|
|
|
sendMms(context, recipients, masterSecret, sendRequest, threadId,
|
|
|
|
ThreadDatabase.DistributionTypes.CONVERSATION, true);
|
|
|
|
|
|
|
|
return threadId;
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:41:44 -07:00
|
|
|
public static long sendMms(Context context, MasterSecret masterSecret, Recipients recipients,
|
2013-04-25 18:59:49 -07:00
|
|
|
long threadId, SlideDeck slideDeck, String message, int distributionType,
|
2013-05-16 13:48:44 -07:00
|
|
|
boolean secure)
|
2012-10-21 17:41:44 -07:00
|
|
|
throws MmsException
|
2011-12-20 10:20:44 -08:00
|
|
|
{
|
|
|
|
if (threadId == -1)
|
2013-04-25 18:59:49 -07:00
|
|
|
threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients, distributionType);
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
if (message.trim().length() > 0)
|
|
|
|
slideDeck.addSlide(new TextSlide(context, message));
|
2012-10-21 17:41:44 -07:00
|
|
|
|
|
|
|
SendReq sendRequest = new SendReq();
|
|
|
|
PduBody body = slideDeck.toPduBody();
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
sendRequest.setDate(System.currentTimeMillis() / 1000L);
|
|
|
|
sendRequest.setBody(body);
|
|
|
|
sendRequest.setContentType(ContentType.MULTIPART_MIXED.getBytes());
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2013-04-25 18:59:49 -07:00
|
|
|
// Recipients secureRecipients = recipients.getSecureSessionRecipients(context);
|
|
|
|
// Recipients insecureRecipients = recipients.getInsecureSessionRecipients(context);
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2013-04-25 18:59:49 -07:00
|
|
|
// for (Recipient secureRecipient : secureRecipients.getRecipientsList()) {
|
|
|
|
// sendMms(context, new Recipients(secureRecipient), masterSecret,
|
|
|
|
// sendRequest, threadId, !forcePlaintext);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (!insecureRecipients.isEmpty()) {
|
|
|
|
// sendMms(context, insecureRecipients, masterSecret, sendRequest, threadId, false);
|
|
|
|
// }
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2013-05-16 13:48:44 -07:00
|
|
|
sendMms(context, recipients, masterSecret, sendRequest, threadId, distributionType, secure);
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
return threadId;
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
public static long send(Context context, MasterSecret masterSecret,
|
|
|
|
OutgoingTextMessage message, long threadId)
|
2012-10-21 17:41:44 -07:00
|
|
|
{
|
2011-12-20 10:20:44 -08:00
|
|
|
if (threadId == -1)
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(message.getRecipients());
|
2011-12-20 10:20:44 -08:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
List<Long> messageIds = DatabaseFactory.getEncryptingSmsDatabase(context)
|
|
|
|
.insertMessageOutbox(masterSecret, threadId, message);
|
2011-12-20 10:20:44 -08:00
|
|
|
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
for (long messageId : messageIds) {
|
2011-12-20 10:20:44 -08:00
|
|
|
Log.w("SMSSender", "Got message id for new message: " + messageId);
|
2012-10-21 17:41:44 -07:00
|
|
|
|
|
|
|
Intent intent = new Intent(SendReceiveService.SEND_SMS_ACTION, null,
|
|
|
|
context, SendReceiveService.class);
|
2011-12-20 10:20:44 -08:00
|
|
|
intent.putExtra("message_id", messageId);
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
return threadId;
|
|
|
|
}
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2013-10-13 12:53:41 +02:00
|
|
|
public static void resend(Context context, long messageId, boolean isMms)
|
|
|
|
{
|
|
|
|
|
|
|
|
Intent intent;
|
|
|
|
if (isMms) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSending(messageId);
|
|
|
|
intent = new Intent(SendReceiveService.SEND_MMS_ACTION, null,
|
|
|
|
context, SendReceiveService.class);
|
|
|
|
} else {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSending(messageId);
|
|
|
|
intent = new Intent(SendReceiveService.SEND_SMS_ACTION, null,
|
|
|
|
context, SendReceiveService.class);
|
|
|
|
}
|
|
|
|
intent.putExtra("message_id", messageId);
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
|
|
|
|
2012-10-21 17:41:44 -07:00
|
|
|
private static void sendMms(Context context, Recipients recipients, MasterSecret masterSecret,
|
2013-04-25 18:59:49 -07:00
|
|
|
SendReq sendRequest, long threadId, int distributionType, boolean secure)
|
2012-10-21 17:41:44 -07:00
|
|
|
throws MmsException
|
|
|
|
{
|
2014-01-14 00:26:43 -08:00
|
|
|
Log.w("MessageSender", "Distribution type: " + distributionType);
|
|
|
|
|
2013-03-14 15:59:28 -07:00
|
|
|
String[] recipientsArray = recipients.toNumberStringArray(true);
|
2012-10-21 17:41:44 -07:00
|
|
|
EncodedStringValue[] encodedNumbers = EncodedStringValue.encodeStrings(recipientsArray);
|
|
|
|
|
2013-04-25 18:59:49 -07:00
|
|
|
if (recipients.isSingleRecipient()) {
|
2014-01-14 00:26:43 -08:00
|
|
|
Log.w("MessageSender", "Single recipient!?");
|
2013-04-25 18:59:49 -07:00
|
|
|
sendRequest.setTo(encodedNumbers);
|
|
|
|
} else if (distributionType == ThreadDatabase.DistributionTypes.BROADCAST) {
|
2014-01-14 00:26:43 -08:00
|
|
|
Log.w("MessageSender", "Broadcast...");
|
2013-04-25 18:59:49 -07:00
|
|
|
sendRequest.setBcc(encodedNumbers);
|
2014-01-14 00:26:43 -08:00
|
|
|
} else if (distributionType == ThreadDatabase.DistributionTypes.CONVERSATION || distributionType == 0) {
|
|
|
|
Log.w("MessageSender", "Conversation...");
|
|
|
|
sendRequest.setTo(encodedNumbers);
|
2013-04-25 18:59:49 -07:00
|
|
|
}
|
2012-10-21 17:41:44 -07:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
long messageId = DatabaseFactory.getMmsDatabase(context)
|
|
|
|
.insertMessageOutbox(masterSecret, sendRequest, threadId, secure);
|
2012-10-21 17:41:44 -07:00
|
|
|
|
|
|
|
Intent intent = new Intent(SendReceiveService.SEND_MMS_ACTION, null,
|
|
|
|
context, SendReceiveService.class);
|
|
|
|
intent.putExtra("message_id", messageId);
|
2013-11-18 13:16:18 -08:00
|
|
|
intent.putExtra("thread_id", threadId);
|
2012-10-21 17:41:44 -07:00
|
|
|
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|