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;
|
2016-08-16 03:23:56 +00:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
|
|
|
import org.whispersystems.signalservice.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;
|
|
|
|
|
2016-12-20 17:55:52 +00:00
|
|
|
import static org.thoughtcrime.securesms.dependencies.SignalCommunicationModule.SignalMessageSenderFactory;
|
2014-11-12 03:57:53 +00:00
|
|
|
|
|
|
|
public class PushTextSendJob 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 = PushTextSendJob.class.getSimpleName();
|
|
|
|
|
2016-12-20 17:55:52 +00:00
|
|
|
@Inject transient SignalMessageSenderFactory messageSenderFactory;
|
2014-11-12 03:57:53 +00:00
|
|
|
|
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
|
2016-12-21 17:49:01 +00:00
|
|
|
public void onAdded() {}
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
@Override
|
2017-01-06 17:19:58 +00:00
|
|
|
public void onPushSend(MasterSecret masterSecret) throws NoSuchMessageException, RetryLaterException {
|
2016-08-16 03:23:56 +00:00
|
|
|
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
|
|
|
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-07-07 00:36:49 +00:00
|
|
|
deliver(record);
|
2016-12-21 17:49:01 +00:00
|
|
|
database.markAsSent(messageId, true);
|
2015-03-11 21:23:45 +00:00
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
if (record.getExpiresIn() > 0) {
|
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
|
|
|
|
}
|
|
|
|
|
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-07-07 00:36:49 +00:00
|
|
|
private void deliver(SmsMessageRecord message)
|
2015-03-11 21:23:45 +00:00
|
|
|
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException
|
2014-11-08 19:35:58 +00:00
|
|
|
{
|
|
|
|
try {
|
2016-03-23 17:34:41 +00:00
|
|
|
SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getNumber());
|
|
|
|
SignalServiceMessageSender messageSender = messageSenderFactory.create();
|
|
|
|
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
|
|
|
.withTimestamp(message.getDateSent())
|
|
|
|
.withBody(message.getBody().getBody())
|
2016-08-16 03:23:56 +00:00
|
|
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
2016-03-23 17:34:41 +00:00
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|