2014-11-03 23:16:04 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.telephony.SmsMessage;
|
2015-06-09 14:37:20 +00:00
|
|
|
import android.util.Log;
|
2014-11-03 23:16:04 +00:00
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
2015-06-09 14:37:20 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
2015-03-11 21:23:45 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingEncryptedMessage;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class SmsReceiveJob extends ContextJob {
|
|
|
|
|
|
|
|
private static final String TAG = SmsReceiveJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final Object[] pdus;
|
|
|
|
|
|
|
|
public SmsReceiveJob(Context context, Object[] pdus) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
2015-03-18 21:25:27 +00:00
|
|
|
.withWakeLock(true)
|
2014-11-03 23:16:04 +00:00
|
|
|
.create());
|
|
|
|
|
|
|
|
this.pdus = pdus;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() {
|
|
|
|
Optional<IncomingTextMessage> message = assembleMessageFragments(pdus);
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
if (message.isPresent() && !isBlocked(message.get())) {
|
2014-11-03 23:16:04 +00:00
|
|
|
Pair<Long, Long> messageAndThreadId = storeMessage(message.get());
|
|
|
|
MessageNotifier.updateNotification(context, KeyCachingService.getMasterSecret(context), messageAndThreadId.second);
|
2015-06-09 14:37:20 +00:00
|
|
|
} else if (message.isPresent()) {
|
|
|
|
Log.w(TAG, "*** Received blocked SMS, ignoring...");
|
2014-11-03 23:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2014-11-03 23:16:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
private boolean isBlocked(IncomingTextMessage message) {
|
|
|
|
if (message.getSender() != null) {
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, message.getSender(), false);
|
|
|
|
return recipients.isBlocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
private Pair<Long, Long> storeMessage(IncomingTextMessage message) {
|
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
|
|
|
|
|
|
|
|
Pair<Long, Long> messageAndThreadId;
|
|
|
|
|
|
|
|
if (message.isSecureMessage()) {
|
|
|
|
messageAndThreadId = database.insertMessageInbox((MasterSecret)null, message);
|
2015-03-11 21:23:45 +00:00
|
|
|
database.markAsLegacyVersion(messageAndThreadId.first);
|
2014-11-03 23:16:04 +00:00
|
|
|
} else if (masterSecret == null) {
|
|
|
|
messageAndThreadId = database.insertMessageInbox(MasterSecretUtil.getAsymmetricMasterSecret(context, null), message);
|
|
|
|
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new SmsDecryptJob(context, messageAndThreadId.first));
|
|
|
|
} else {
|
2015-03-11 21:23:45 +00:00
|
|
|
messageAndThreadId = database.insertMessageInbox(masterSecret, message);
|
2014-11-03 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return messageAndThreadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Optional<IncomingTextMessage> assembleMessageFragments(Object[] pdus) {
|
|
|
|
List<IncomingTextMessage> messages = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Object pdu : pdus) {
|
|
|
|
messages.add(new IncomingTextMessage(SmsMessage.createFromPdu((byte[])pdu)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (messages.isEmpty()) {
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
|
|
|
|
IncomingTextMessage message = new IncomingTextMessage(messages);
|
|
|
|
|
|
|
|
if (WirePrefix.isEncryptedMessage(message.getMessageBody()) ||
|
|
|
|
WirePrefix.isKeyExchange(message.getMessageBody()) ||
|
|
|
|
WirePrefix.isPreKeyBundle(message.getMessageBody()) ||
|
|
|
|
WirePrefix.isEndSession(message.getMessageBody()))
|
|
|
|
{
|
2015-03-11 21:23:45 +00:00
|
|
|
return Optional.<IncomingTextMessage>of(new IncomingEncryptedMessage(message, message.getMessageBody()));
|
2014-11-03 23:16:04 +00:00
|
|
|
} else {
|
|
|
|
return Optional.of(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|