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.MmsSmsColumns;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.mms.PartParser;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
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;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
|
2014-11-12 19:35:54 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.PushAddress;
|
|
|
|
import org.whispersystems.textsecure.internal.push.PushMessageProtos;
|
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
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;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public PushGroupSendJob(Context context, long messageId, String destination) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withGroupId(destination)
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRetryCount(5)
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 03:57:53 +00:00
|
|
|
public void onRun(MasterSecret masterSecret) throws MmsException, IOException, NoSuchMessageException {
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
SendReq message = database.getOutgoingMessage(masterSecret, messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
deliver(masterSecret, message);
|
|
|
|
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
|
|
|
database.markAsSent(messageId, "push".getBytes(), 0);
|
|
|
|
} catch (InvalidNumberException | RecipientFormattingException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
} catch (EncapsulatedExceptions e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
if (!e.getUnregisteredUserExceptions().isEmpty()) {
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (UntrustedIdentityException uie : e.getUntrustedIdentityExceptions()) {
|
|
|
|
IncomingIdentityUpdateMessage identityUpdateMessage = IncomingIdentityUpdateMessage.createFor(message.getTo()[0].getString(), uie.getIdentityKey());
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).insertMessageInbox(masterSecret, identityUpdateMessage);
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private void deliver(MasterSecret masterSecret, SendReq message)
|
|
|
|
throws IOException, RecipientFormattingException, InvalidNumberException, EncapsulatedExceptions
|
|
|
|
{
|
2014-11-12 03:57:53 +00:00
|
|
|
TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret);
|
2014-11-08 19:35:58 +00:00
|
|
|
byte[] groupId = GroupUtil.getDecodedId(message.getTo()[0].getString());
|
|
|
|
Recipients recipients = DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, false);
|
|
|
|
List<PushAddress> addresses = getPushAddresses(recipients);
|
2014-12-12 09:03:24 +00:00
|
|
|
List<TextSecureAttachment> attachments = getAttachments(masterSecret, message);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
if (MmsSmsColumns.Types.isGroupUpdate(message.getDatabaseMessageBox()) ||
|
|
|
|
MmsSmsColumns.Types.isGroupQuit(message.getDatabaseMessageBox()))
|
|
|
|
{
|
|
|
|
String content = PartParser.getMessageText(message.getBody());
|
|
|
|
|
|
|
|
if (content != null && !content.trim().isEmpty()) {
|
|
|
|
PushMessageProtos.PushMessageContent.GroupContext groupContext = PushMessageProtos.PushMessageContent.GroupContext.parseFrom(Base64.decode(content));
|
|
|
|
TextSecureAttachment avatar = attachments.isEmpty() ? null : attachments.get(0);
|
|
|
|
TextSecureGroup.Type type = MmsSmsColumns.Types.isGroupQuit(message.getDatabaseMessageBox()) ? TextSecureGroup.Type.QUIT : TextSecureGroup.Type.UPDATE;
|
|
|
|
TextSecureGroup group = new TextSecureGroup(type, groupId, groupContext.getName(), groupContext.getMembersList(), avatar);
|
|
|
|
TextSecureMessage groupMessage = new TextSecureMessage(message.getSentTimestamp(), group, null, null);
|
|
|
|
|
|
|
|
messageSender.sendMessage(addresses, groupMessage);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String body = PartParser.getMessageText(message.getBody());
|
|
|
|
TextSecureGroup group = new TextSecureGroup(groupId);
|
|
|
|
TextSecureMessage groupMessage = new TextSecureMessage(message.getSentTimestamp(), group, attachments, body);
|
|
|
|
|
|
|
|
messageSender.sendMessage(addresses, groupMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<PushAddress> getPushAddresses(Recipients recipients) throws InvalidNumberException {
|
|
|
|
List<PushAddress> addresses = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Recipient recipient : recipients.getRecipientsList()) {
|
|
|
|
addresses.add(getPushAddress(recipient));
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|