2014-11-08 19:35:58 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
2013-07-17 02:52:02 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.text.TextUtils;
|
2013-07-17 02:52:02 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2013-07-17 02:52:02 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase.DistributionTypes;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2015-05-01 19:03:05 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.CompatMmsConnection;
|
2015-01-02 23:43:28 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
2013-11-19 18:13:24 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsSendResult;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
2014-06-13 23:15:33 +00:00
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
2015-03-12 20:39:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.SmilUtil;
|
2014-12-29 22:01:02 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TelephonyUtil;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2013-07-17 02:52:02 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
2015-10-13 01:25:05 +00:00
|
|
|
import java.util.List;
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
import ws.com.google.android.mms.ContentType;
|
2014-11-08 19:35:58 +00:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
2015-10-13 01:25:05 +00:00
|
|
|
import ws.com.google.android.mms.pdu.CharacterSets;
|
2013-07-17 02:52:02 +00:00
|
|
|
import ws.com.google.android.mms.pdu.EncodedStringValue;
|
2015-10-13 01:25:05 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduBody;
|
2013-07-17 02:52:02 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduComposer;
|
|
|
|
import ws.com.google.android.mms.pdu.PduHeaders;
|
2015-10-13 01:25:05 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
2013-07-17 02:52:02 +00:00
|
|
|
import ws.com.google.android.mms.pdu.SendConf;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
2015-01-12 04:27:34 +00:00
|
|
|
public class MmsSendJob extends SendJob {
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
private static final long serialVersionUID = 0L;
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
private static final String TAG = MmsSendJob.class.getSimpleName();
|
2014-11-03 23:16:04 +00:00
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
private final long messageId;
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
public MmsSendJob(Context context, long messageId) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withGroupId("mms-operation")
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withPersistence()
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
2016-12-21 17:49:01 +00:00
|
|
|
// MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
// database.markAsSending(messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-12 04:27:34 +00:00
|
|
|
public void onSend(MasterSecret masterSecret) throws MmsException, NoSuchMessageException, IOException {
|
2015-10-13 01:25:05 +00:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(masterSecret, messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
try {
|
2015-10-13 01:25:05 +00:00
|
|
|
SendReq pdu = constructSendPdu(masterSecret, message);
|
|
|
|
|
|
|
|
validateDestinations(message, pdu);
|
2014-12-29 22:01:02 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
final byte[] pduBytes = getPduBytes(pdu);
|
2016-02-06 00:10:33 +00:00
|
|
|
final SendConf sendConf = new CompatMmsConnection(context).send(pduBytes, message.getSubscriptionId());
|
2015-10-13 01:25:05 +00:00
|
|
|
final MmsSendResult result = getSendResult(sendConf, pdu);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2016-12-21 17:49:01 +00:00
|
|
|
database.markAsSent(messageId, false);
|
2015-10-13 01:25:05 +00:00
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
2015-05-01 19:03:05 +00:00
|
|
|
} catch (UndeliverableMessageException | IOException e) {
|
2014-11-08 19:35:58 +00:00
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
} catch (InsecureFallbackApprovalException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsPendingInsecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
2013-07-17 02:52:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
2014-11-08 19:35:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
private byte[] getPduBytes(SendReq message)
|
2014-04-01 01:47:24 +00:00
|
|
|
throws IOException, UndeliverableMessageException, InsecureFallbackApprovalException
|
2013-07-17 02:52:02 +00:00
|
|
|
{
|
2015-03-11 21:23:45 +00:00
|
|
|
String number = TelephonyUtil.getManager(context).getLine1Number();
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
message.setBody(SmilUtil.getSmilBody(message.getBody()));
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
if (!TextUtils.isEmpty(number)) {
|
2013-07-17 02:52:02 +00:00
|
|
|
message.setFrom(new EncodedStringValue(number));
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
byte[] pduBytes = new PduComposer(context, message).make();
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
if (pduBytes == null) {
|
|
|
|
throw new UndeliverableMessageException("PDU composition failed, null payload");
|
|
|
|
}
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
return pduBytes;
|
|
|
|
}
|
2015-01-13 19:40:29 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
private MmsSendResult getSendResult(SendConf conf, SendReq message)
|
|
|
|
throws UndeliverableMessageException
|
|
|
|
{
|
|
|
|
if (conf == null) {
|
|
|
|
throw new UndeliverableMessageException("No M-Send.conf received in response to send.");
|
|
|
|
} else if (conf.getResponseStatus() != PduHeaders.RESPONSE_STATUS_OK) {
|
|
|
|
throw new UndeliverableMessageException("Got bad response: " + conf.getResponseStatus());
|
|
|
|
} else if (isInconsistentResponse(message, conf)) {
|
|
|
|
throw new UndeliverableMessageException("Mismatched response!");
|
|
|
|
} else {
|
|
|
|
return new MmsSendResult(conf.getMessageId(), conf.getResponseStatus());
|
2013-07-17 02:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isInconsistentResponse(SendReq message, SendConf response) {
|
2014-11-03 23:16:04 +00:00
|
|
|
Log.w(TAG, "Comparing: " + Hex.toString(message.getTransactionId()));
|
|
|
|
Log.w(TAG, "With: " + Hex.toString(response.getTransactionId()));
|
2013-07-17 02:52:02 +00:00
|
|
|
return !Arrays.equals(message.getTransactionId(), response.getTransactionId());
|
|
|
|
}
|
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
private void validateDestinations(EncodedStringValue[] destinations) throws UndeliverableMessageException {
|
|
|
|
if (destinations == null) return;
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
for (EncodedStringValue destination : destinations) {
|
|
|
|
if (destination == null || !NumberUtil.isValidSmsOrEmail(destination.getString())) {
|
|
|
|
throw new UndeliverableMessageException("Invalid destination: " +
|
|
|
|
(destination == null ? null : destination.getString()));
|
|
|
|
}
|
2014-06-13 23:15:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
private void validateDestinations(OutgoingMediaMessage media, SendReq message) throws UndeliverableMessageException {
|
2014-12-29 22:01:02 +00:00
|
|
|
validateDestinations(message.getTo());
|
|
|
|
validateDestinations(message.getCc());
|
|
|
|
validateDestinations(message.getBcc());
|
2014-06-13 23:15:33 +00:00
|
|
|
|
|
|
|
if (message.getTo() == null && message.getCc() == null && message.getBcc() == null) {
|
|
|
|
throw new UndeliverableMessageException("No to, cc, or bcc specified!");
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (media.isSecure()) {
|
|
|
|
throw new UndeliverableMessageException("Attempt to send encrypted MMS?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private SendReq constructSendPdu(MasterSecret masterSecret, OutgoingMediaMessage message)
|
|
|
|
throws UndeliverableMessageException
|
|
|
|
{
|
2015-10-25 01:36:03 +00:00
|
|
|
SendReq sendReq = new SendReq();
|
|
|
|
PduBody body = new PduBody();
|
|
|
|
List<String> numbers = message.getRecipients().toNumberStringList(true);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2015-10-25 01:36:03 +00:00
|
|
|
for (String number : numbers) {
|
2015-10-13 01:25:05 +00:00
|
|
|
if (message.getDistributionType() == DistributionTypes.CONVERSATION) {
|
2015-10-25 01:36:03 +00:00
|
|
|
sendReq.addTo(new EncodedStringValue(Util.toIsoBytes(number)));
|
2015-10-13 01:25:05 +00:00
|
|
|
} else {
|
2015-10-25 01:36:03 +00:00
|
|
|
sendReq.addBcc(new EncodedStringValue(Util.toIsoBytes(number)));
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq.setDate(message.getSentTimeMillis() / 1000L);
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(message.getBody())) {
|
|
|
|
PduPart part = new PduPart();
|
|
|
|
part.setData(Util.toUtf8Bytes(message.getBody()));
|
|
|
|
part.setCharset(CharacterSets.UTF_8);
|
|
|
|
part.setContentType(ContentType.TEXT_PLAIN.getBytes());
|
|
|
|
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
|
|
|
part.setName(("Text"+System.currentTimeMillis()).getBytes());
|
|
|
|
|
|
|
|
body.addPart(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Attachment> scaledAttachments = scaleAttachments(masterSecret, MediaConstraints.MMS_CONSTRAINTS, message.getAttachments());
|
|
|
|
|
|
|
|
for (Attachment attachment : scaledAttachments) {
|
|
|
|
try {
|
|
|
|
if (attachment.getDataUri() == null) throw new IOException("Assertion failed, attachment for outgoing MMS has no data!");
|
|
|
|
|
|
|
|
PduPart part = new PduPart();
|
|
|
|
part.setData(Util.readFully(PartAuthority.getAttachmentStream(context, masterSecret, attachment.getDataUri())));
|
|
|
|
part.setContentType(Util.toIsoBytes(attachment.getContentType()));
|
|
|
|
part.setContentId((System.currentTimeMillis() + "").getBytes());
|
|
|
|
part.setName((System.currentTimeMillis() + "").getBytes());
|
|
|
|
|
|
|
|
body.addPart(part);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq.setBody(body);
|
|
|
|
return sendReq;
|
2014-06-13 23:15:33 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
private void notifyMediaMessageDeliveryFailed(Context context, long messageId) {
|
|
|
|
long threadId = DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
|
|
|
|
2015-02-11 18:29:05 +00:00
|
|
|
if (recipients != null) {
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
|
|
|
}
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
2013-07-17 02:52:02 +00:00
|
|
|
}
|