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;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-11-08 11:35:58 -08:00
|
|
|
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;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2014-11-08 11:35:58 -08:00
|
|
|
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;
|
2017-08-14 18:11:13 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-11-12 11:14:45 -08:00
|
|
|
import java.io.FileNotFoundException;
|
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;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2017-09-15 22:38:53 -07:00
|
|
|
@Inject transient SignalServiceMessageSender messageSender;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private final long messageId;
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public PushMediaSendJob(Context context, long messageId, Address 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() {
|
2017-01-06 09:19:58 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-01 19:22:48 -08:00
|
|
|
public void onPushSend()
|
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
|
|
|
{
|
2016-08-15 20:23:56 -07:00
|
|
|
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2018-01-24 19:17:44 -08:00
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
try {
|
2018-01-24 19:17:44 -08:00
|
|
|
deliver(message);
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, true);
|
2015-10-12 18:25:05 -07:00
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
2016-08-15 20:23:56 -07:00
|
|
|
|
|
|
|
if (message.getExpiresIn() > 0 && !message.isExpirationUpdate()) {
|
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
expirationManager.scheduleDeletion(messageId, true, message.getExpiresIn());
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (InsecureFallbackApprovalException ifae) {
|
|
|
|
Log.w(TAG, ifae);
|
|
|
|
database.markAsPendingInsecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
2017-11-21 11:54:18 -08:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context, false));
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (UntrustedIdentityException uie) {
|
2015-01-15 13:35:35 -08:00
|
|
|
Log.w(TAG, uie);
|
2017-07-26 09:59:15 -07:00
|
|
|
database.addMismatchedIdentity(messageId, Address.fromSerialized(uie.getE164Number()), uie.getIdentityKey());
|
2014-11-08 11:35:58 -08:00
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2015-10-30 11:56:05 -07:00
|
|
|
if (exception instanceof RetryLaterException) 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);
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
private void deliver(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
|
|
|
{
|
2017-08-01 08:56:00 -07:00
|
|
|
if (message.getRecipient() == null) {
|
2015-10-26 11:02:23 -07:00
|
|
|
throw new UndeliverableMessageException("No destination address.");
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
try {
|
2017-08-01 08:56:00 -07:00
|
|
|
SignalServiceAddress address = getPushAddress(message.getRecipient().getAddress());
|
2017-05-08 15:32:59 -07:00
|
|
|
MediaConstraints mediaConstraints = MediaConstraints.getPushMediaConstraints();
|
2018-03-19 11:22:39 -07:00
|
|
|
List<Attachment> scaledAttachments = scaleAndStripExifFromAttachments(mediaConstraints, message.getAttachments());
|
2018-01-24 19:17:44 -08:00
|
|
|
List<SignalServiceAttachment> attachmentStreams = getAttachmentsFor(scaledAttachments);
|
2017-08-22 10:44:04 -07:00
|
|
|
Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceDataMessage mediaMessage = SignalServiceDataMessage.newBuilder()
|
|
|
|
.withBody(message.getBody())
|
|
|
|
.withAttachments(attachmentStreams)
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
2016-08-15 20:23:56 -07:00
|
|
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
2017-08-14 18:11:13 -07:00
|
|
|
.withProfileKey(profileKey.orNull())
|
2016-08-15 20:23:56 -07:00
|
|
|
.asExpirationUpdate(message.isExpirationUpdate())
|
2016-03-23 10:34:41 -07:00
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
messageSender.sendMessage(address, mediaMessage);
|
2017-07-26 09:59:15 -07:00
|
|
|
} catch (UnregisteredUserException e) {
|
2014-11-08 11:35:58 -08:00
|
|
|
Log.w(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new InsecureFallbackApprovalException(e);
|
2015-11-12 11:14:45 -08:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
throw new UndeliverableMessageException(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|