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;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecureAxolotlStore;
|
|
|
|
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;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.PartParser;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage;
|
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
|
|
|
import org.thoughtcrime.securesms.transport.SecureFallbackApprovalException;
|
|
|
|
import org.whispersystems.libaxolotl.state.AxolotlStore;
|
|
|
|
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;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
|
2014-11-12 11:35:54 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.PushAddress;
|
|
|
|
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;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
2014-12-11 16:10:11 -08:00
|
|
|
super(context, constructParameters(context, destination, true));
|
2014-11-08 11:35:58 -08:00
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onRun(MasterSecret masterSecret)
|
|
|
|
throws RetryLaterException, MmsException, NoSuchMessageException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2014-11-11 19:57:53 -08:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
SendReq message = database.getOutgoingMessage(masterSecret, messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
try {
|
2014-12-03 14:15:54 -08:00
|
|
|
if (deliver(masterSecret, message)) {
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
|
|
|
database.markAsSent(messageId, "push".getBytes(), 0);
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (InsecureFallbackApprovalException ifae) {
|
|
|
|
Log.w(TAG, ifae);
|
|
|
|
database.markAsPendingInsecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
} catch (SecureFallbackApprovalException sfae) {
|
|
|
|
Log.w(TAG, sfae);
|
|
|
|
database.markAsPendingSecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
} catch (UntrustedIdentityException uie) {
|
|
|
|
IncomingIdentityUpdateMessage identityUpdateMessage = IncomingIdentityUpdateMessage.createFor(message.getTo()[0].getString(), uie.getIdentityKey());
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).insertMessageInbox(masterSecret, identityUpdateMessage);
|
|
|
|
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;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-03 14:15:54 -08:00
|
|
|
private boolean deliver(MasterSecret masterSecret, SendReq message)
|
2014-11-08 11:35:58 -08:00
|
|
|
throws RetryLaterException, SecureFallbackApprovalException,
|
|
|
|
InsecureFallbackApprovalException, UntrustedIdentityException
|
|
|
|
{
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2014-11-11 19:57:53 -08:00
|
|
|
TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret);
|
2014-11-08 11:35:58 -08:00
|
|
|
String destination = message.getTo()[0].getString();
|
2014-12-11 16:10:11 -08:00
|
|
|
boolean isSmsFallbackSupported = isSmsFallbackSupported(context, destination, true);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, destination, false);
|
|
|
|
PushAddress address = getPushAddress(recipients.getPrimaryRecipient());
|
2014-12-12 01:03:24 -08:00
|
|
|
List<TextSecureAttachment> attachments = getAttachments(masterSecret, message);
|
2014-11-08 11:35:58 -08:00
|
|
|
String body = PartParser.getMessageText(message.getBody());
|
|
|
|
TextSecureMessage mediaMessage = new TextSecureMessage(message.getSentTimestamp(), attachments, body);
|
|
|
|
|
|
|
|
messageSender.sendMessage(address, mediaMessage);
|
2014-12-03 14:15:54 -08:00
|
|
|
return true;
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (InvalidNumberException | UnregisteredUserException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
if (isSmsFallbackSupported) fallbackOrAskApproval(masterSecret, message, destination);
|
|
|
|
else database.markAsSentFailed(messageId);
|
|
|
|
} catch (IOException | RecipientFormattingException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
if (isSmsFallbackSupported) fallbackOrAskApproval(masterSecret, message, destination);
|
|
|
|
else throw new RetryLaterException(e);
|
|
|
|
}
|
2014-12-03 14:15:54 -08:00
|
|
|
return false;
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void fallbackOrAskApproval(MasterSecret masterSecret, SendReq mediaMessage, String destination)
|
|
|
|
throws SecureFallbackApprovalException, InsecureFallbackApprovalException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Recipient recipient = RecipientFactory.getRecipientsFromString(context, destination, false).getPrimaryRecipient();
|
2014-12-11 16:10:11 -08:00
|
|
|
boolean isSmsFallbackApprovalRequired = isSmsFallbackApprovalRequired(destination, true);
|
2014-11-08 11:35:58 -08:00
|
|
|
AxolotlStore axolotlStore = new TextSecureAxolotlStore(context, masterSecret);
|
|
|
|
|
|
|
|
if (!isSmsFallbackApprovalRequired) {
|
|
|
|
Log.w(TAG, "Falling back to MMS");
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsForcedSms(mediaMessage.getDatabaseMessageId());
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new MmsSendJob(context, messageId));
|
2014-11-12 09:09:55 -08:00
|
|
|
} else if (!axolotlStore.containsSession(recipient.getRecipientId(), PushAddress.DEFAULT_DEVICE_ID)) {
|
2014-11-08 11:35:58 -08:00
|
|
|
Log.w(TAG, "Marking message as pending insecure SMS fallback");
|
|
|
|
throw new InsecureFallbackApprovalException("Pending user approval for fallback to insecure SMS");
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Marking message as pending secure SMS fallback");
|
|
|
|
throw new SecureFallbackApprovalException("Pending user approval for fallback secure to SMS");
|
|
|
|
}
|
|
|
|
} catch (RecipientFormattingException rfe) {
|
|
|
|
Log.w(TAG, rfe);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|