2014-11-08 19:35:58 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.database.documents.NetworkFailure;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
|
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2015-04-07 21:16:05 +00:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
|
2015-05-29 23:23:47 +00:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
|
2015-02-28 00:57:32 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
2014-11-12 19:35:54 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2015-05-29 23:23:47 +00:00
|
|
|
import org.whispersystems.textsecure.internal.push.TextSecureProtos.GroupContext;
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import static org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
|
|
|
|
|
|
|
public class PushGroupSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2015-06-25 01:26:51 +00:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
private static final String TAG = PushGroupSendJob.class.getSimpleName();
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
@Inject transient TextSecureMessageSenderFactory messageSenderFactory;
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
private final long messageId;
|
2015-01-15 21:35:35 +00:00
|
|
|
private final long filterRecipientId;
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
public PushGroupSendJob(Context context, long messageId, String destination, long filterRecipientId) {
|
2014-11-08 19:35:58 +00:00
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withGroupId(destination)
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRetryCount(5)
|
|
|
|
.create());
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
this.messageId = messageId;
|
|
|
|
this.filterRecipientId = filterRecipientId;
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
2015-01-15 21:35:35 +00:00
|
|
|
DatabaseFactory.getMmsDatabase(context)
|
|
|
|
.markAsSending(messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-15 21:35:35 +00:00
|
|
|
public void onSend(MasterSecret masterSecret)
|
2015-03-03 19:44:49 +00:00
|
|
|
throws MmsException, IOException, NoSuchMessageException
|
2015-01-15 21:35:35 +00:00
|
|
|
{
|
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-01-15 21:35:35 +00:00
|
|
|
deliver(masterSecret, message, filterRecipientId);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
2015-10-13 01:25:05 +00:00
|
|
|
database.markAsSent(messageId);
|
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
2015-04-07 21:16:05 +00:00
|
|
|
} catch (InvalidNumberException | RecipientFormattingException | UndeliverableMessageException e) {
|
2014-11-08 19:35:58 +00:00
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
} catch (EncapsulatedExceptions e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 21:35:35 +00:00
|
|
|
List<NetworkFailure> failures = new LinkedList<>();
|
|
|
|
|
|
|
|
for (NetworkFailureException nfe : e.getNetworkExceptions()) {
|
|
|
|
Recipient recipient = RecipientFactory.getRecipientsFromString(context, nfe.getE164number(), false).getPrimaryRecipient();
|
|
|
|
failures.add(new NetworkFailure(recipient.getRecipientId()));
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (UntrustedIdentityException uie : e.getUntrustedIdentityExceptions()) {
|
2015-01-15 21:35:35 +00:00
|
|
|
Recipient recipient = RecipientFactory.getRecipientsFromString(context, uie.getE164Number(), false).getPrimaryRecipient();
|
|
|
|
database.addMismatchedIdentity(messageId, recipient.getRecipientId(), uie.getIdentityKey());
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
database.addFailures(messageId, failures);
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof IOException) return true;
|
2014-11-12 03:57:53 +00:00
|
|
|
return false;
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 03:57:53 +00:00
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
private void deliver(MasterSecret masterSecret, OutgoingMediaMessage message, long filterRecipientId)
|
2015-04-07 21:16:05 +00:00
|
|
|
throws IOException, RecipientFormattingException, InvalidNumberException,
|
|
|
|
EncapsulatedExceptions, UndeliverableMessageException
|
2014-11-08 19:35:58 +00:00
|
|
|
{
|
2015-07-07 00:36:49 +00:00
|
|
|
TextSecureMessageSender messageSender = messageSenderFactory.create();
|
2015-10-13 01:25:05 +00:00
|
|
|
byte[] groupId = GroupUtil.getDecodedId(message.getRecipients().getPrimaryRecipient().getNumber());
|
2014-11-08 19:35:58 +00:00
|
|
|
Recipients recipients = DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, false);
|
2015-10-13 01:25:05 +00:00
|
|
|
List<TextSecureAttachment> attachments = getAttachmentsFor(masterSecret, message.getAttachments());
|
2015-02-28 00:57:32 +00:00
|
|
|
List<TextSecureAddress> addresses;
|
2015-01-15 21:35:35 +00:00
|
|
|
|
|
|
|
if (filterRecipientId >= 0) addresses = getPushAddresses(filterRecipientId);
|
|
|
|
else addresses = getPushAddresses(recipients);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
if (message.isGroup()) {
|
|
|
|
OutgoingGroupMediaMessage groupMessage = (OutgoingGroupMediaMessage) message;
|
|
|
|
GroupContext groupContext = groupMessage.getGroupContext();
|
|
|
|
TextSecureAttachment avatar = attachments.isEmpty() ? null : attachments.get(0);
|
|
|
|
TextSecureGroup.Type type = groupMessage.isGroupQuit() ? TextSecureGroup.Type.QUIT : TextSecureGroup.Type.UPDATE;
|
|
|
|
TextSecureGroup group = new TextSecureGroup(type, groupId, groupContext.getName(), groupContext.getMembersList(), avatar);
|
|
|
|
TextSecureDataMessage groupDataMessage = new TextSecureDataMessage(message.getSentTimeMillis(), group, null, null);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
messageSender.sendMessage(addresses, groupDataMessage);
|
2014-11-08 19:35:58 +00:00
|
|
|
} else {
|
2015-05-29 23:23:47 +00:00
|
|
|
TextSecureGroup group = new TextSecureGroup(groupId);
|
2015-10-13 01:25:05 +00:00
|
|
|
TextSecureDataMessage groupMessage = new TextSecureDataMessage(message.getSentTimeMillis(), group, attachments, message.getBody());
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
messageSender.sendMessage(addresses, groupMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:57:32 +00:00
|
|
|
private List<TextSecureAddress> getPushAddresses(Recipients recipients) throws InvalidNumberException {
|
|
|
|
List<TextSecureAddress> addresses = new LinkedList<>();
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
for (Recipient recipient : recipients.getRecipientsList()) {
|
2015-03-03 19:44:49 +00:00
|
|
|
addresses.add(getPushAddress(recipient.getNumber()));
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:57:32 +00:00
|
|
|
private List<TextSecureAddress> getPushAddresses(long filterRecipientId) throws InvalidNumberException {
|
|
|
|
List<TextSecureAddress> addresses = new LinkedList<>();
|
2015-03-03 19:44:49 +00:00
|
|
|
addresses.add(getPushAddress(RecipientFactory.getRecipientForId(context, filterRecipientId, false).getNumber()));
|
2015-01-15 21:35:35 +00:00
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|