2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
|
2015-05-29 16:23:47 -07:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
|
2015-02-27 16:57:32 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
2014-11-12 11:35:54 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
|
2014-11-12 11:15:05 -08:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import static org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
|
|
|
|
|
|
|
public class PushMediaSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-06-24 18:26:51 -07:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static final String TAG = PushMediaSendJob.class.getSimpleName();
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Inject transient TextSecureMessageSenderFactory messageSenderFactory;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private final long messageId;
|
|
|
|
|
|
|
|
public PushMediaSendJob(Context context, long messageId, String destination) {
|
2015-03-11 14:23:45 -07:00
|
|
|
super(context, constructParameters(context, destination));
|
2014-11-08 11:35:58 -08:00
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
2015-01-15 13:35:35 -08:00
|
|
|
MmsDatabase mmsDatabase = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
mmsDatabase.markAsSending(messageId);
|
|
|
|
mmsDatabase.markAsPush(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-11 20:27:34 -08:00
|
|
|
public void onSend(MasterSecret masterSecret)
|
2015-01-15 13:35:35 -08:00
|
|
|
throws RetryLaterException, MmsException, NoSuchMessageException,
|
2015-03-03 11:44:49 -08:00
|
|
|
UndeliverableMessageException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2015-10-12 18:25:05 -07:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(masterSecret, messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
try {
|
2015-03-11 14:23:45 -07:00
|
|
|
deliver(masterSecret, message);
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
2015-10-12 18:25:05 -07:00
|
|
|
database.markAsSent(messageId);
|
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (InsecureFallbackApprovalException ifae) {
|
|
|
|
Log.w(TAG, ifae);
|
|
|
|
database.markAsPendingInsecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
2015-03-11 14:23:45 -07:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context));
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (UntrustedIdentityException uie) {
|
2015-01-15 13:35:35 -08:00
|
|
|
Log.w(TAG, uie);
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, uie.getE164Number(), false);
|
|
|
|
long recipientId = recipients.getPrimaryRecipient().getRecipientId();
|
|
|
|
|
|
|
|
database.addMismatchedIdentity(messageId, recipientId, uie.getIdentityKey());
|
2014-11-08 11:35:58 -08:00
|
|
|
database.markAsSentFailed(messageId);
|
2015-01-15 13:35:35 -08:00
|
|
|
database.markAsPush(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof RequirementNotMetException) return true;
|
2014-11-11 19:57:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
private void deliver(MasterSecret masterSecret, OutgoingMediaMessage message)
|
2015-03-11 14:23:45 -07:00
|
|
|
throws RetryLaterException, InsecureFallbackApprovalException, UntrustedIdentityException,
|
2015-01-02 15:43:28 -08:00
|
|
|
UndeliverableMessageException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2015-07-06 17:36:49 -07:00
|
|
|
TextSecureMessageSender messageSender = messageSenderFactory.create();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
try {
|
2015-10-12 18:25:05 -07:00
|
|
|
TextSecureAddress address = getPushAddress(message.getRecipients().getPrimaryRecipient().getNumber());
|
|
|
|
List<Attachment> scaledAttachments = scaleAttachments(masterSecret, MediaConstraints.PUSH_CONSTRAINTS, message.getAttachments());
|
|
|
|
List<TextSecureAttachment> attachmentStreams = getAttachmentsFor(masterSecret, scaledAttachments);
|
|
|
|
TextSecureDataMessage mediaMessage = TextSecureDataMessage.newBuilder()
|
|
|
|
.withBody(message.getBody())
|
|
|
|
.withAttachments(attachmentStreams)
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
messageSender.sendMessage(address, mediaMessage);
|
|
|
|
} catch (InvalidNumberException | UnregisteredUserException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new InsecureFallbackApprovalException(e);
|
2015-03-03 11:44:49 -08:00
|
|
|
} catch (IOException e) {
|
2014-11-08 11:35:58 -08:00
|
|
|
Log.w(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new RetryLaterException(e);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|