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.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
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.notifications.MessageNotifier;
|
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.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
2014-11-08 21:37:57 +00:00
|
|
|
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
2015-05-29 23:23:47 +00:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
|
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.UnregisteredUserException;
|
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;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
import static org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
|
|
|
|
|
|
|
public class PushTextSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
private static final String TAG = PushTextSendJob.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 PushTextSendJob(Context context, long messageId, String destination) {
|
2015-03-11 21:23:45 +00:00
|
|
|
super(context, constructParameters(context, destination));
|
2014-11-08 19:35:58 +00:00
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
2015-01-15 21:35:35 +00:00
|
|
|
SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
smsDatabase.markAsSending(messageId);
|
|
|
|
smsDatabase.markAsPush(messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-03-03 19:44:49 +00:00
|
|
|
public void onSend(MasterSecret masterSecret) throws NoSuchMessageException, RetryLaterException {
|
2015-03-11 21:23:45 +00:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
SmsMessageRecord record = database.getMessage(masterSecret, messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Log.w(TAG, "Sending message: " + messageId);
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
deliver(masterSecret, record);
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
|
|
|
database.markAsSent(messageId);
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
} catch (InsecureFallbackApprovalException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsPendingInsecureSmsFallback(record.getId());
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipients(), record.getThreadId());
|
2015-03-11 21:23:45 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context));
|
2014-11-08 19:35:58 +00:00
|
|
|
} catch (UntrustedIdentityException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 21:35:35 +00:00
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, e.getE164Number(), false);
|
|
|
|
long recipientId = recipients.getPrimaryRecipient().getRecipientId();
|
|
|
|
|
|
|
|
database.addMismatchedIdentity(record.getId(), recipientId, e.getIdentityKey());
|
2014-11-08 19:35:58 +00:00
|
|
|
database.markAsSentFailed(record.getId());
|
2015-01-15 21:35:35 +00:00
|
|
|
database.markAsPush(record.getId());
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof RetryLaterException) return true;
|
2014-11-12 03:57:53 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
|
|
|
|
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
|
|
|
|
2015-04-13 15:57:20 +00:00
|
|
|
if (threadId != -1 && recipients != null) {
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
|
|
|
}
|
2014-11-12 03:57:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
private void deliver(MasterSecret masterSecret, SmsMessageRecord message)
|
|
|
|
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException
|
2014-11-08 19:35:58 +00:00
|
|
|
{
|
|
|
|
try {
|
2015-03-11 21:23:45 +00:00
|
|
|
TextSecureAddress address = getPushAddress(message.getIndividualRecipient().getNumber());
|
|
|
|
TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret);
|
2015-05-29 23:23:47 +00:00
|
|
|
TextSecureDataMessage textSecureMessage = TextSecureDataMessage.newBuilder()
|
|
|
|
.withTimestamp(message.getDateSent())
|
|
|
|
.withBody(message.getBody().getBody())
|
|
|
|
.asEndSessionMessage(message.isEndSession())
|
|
|
|
.build();
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2014-12-03 22:15:54 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
messageSender.sendMessage(address, textSecureMessage);
|
2014-11-08 19:35:58 +00:00
|
|
|
} catch (InvalidNumberException | UnregisteredUserException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-03-11 21:23:45 +00:00
|
|
|
throw new InsecureFallbackApprovalException(e);
|
2014-11-08 19:35:58 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-03-11 21:23:45 +00:00
|
|
|
throw new RetryLaterException(e);
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|