mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-30 21:45:20 +00:00
7a023b9fdc
Closes #2717 // FREEBIE
107 lines
3.6 KiB
Java
107 lines
3.6 KiB
Java
package org.thoughtcrime.securesms.jobs;
|
|
|
|
import android.content.Context;
|
|
import android.telephony.SmsMessage;
|
|
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;
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
|
import org.thoughtcrime.securesms.sms.IncomingEncryptedMessage;
|
|
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()
|
|
.withWakeLock(true)
|
|
.create());
|
|
|
|
this.pdus = pdus;
|
|
}
|
|
|
|
@Override
|
|
public void onAdded() {}
|
|
|
|
@Override
|
|
public void onRun() {
|
|
Optional<IncomingTextMessage> message = assembleMessageFragments(pdus);
|
|
|
|
if (message.isPresent()) {
|
|
Pair<Long, Long> messageAndThreadId = storeMessage(message.get());
|
|
MessageNotifier.updateNotification(context, KeyCachingService.getMasterSecret(context), messageAndThreadId.second);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onCanceled() {
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean onShouldRetry(Exception exception) {
|
|
return false;
|
|
}
|
|
|
|
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);
|
|
database.markAsLegacyVersion(messageAndThreadId.first);
|
|
} else if (masterSecret == null) {
|
|
messageAndThreadId = database.insertMessageInbox(MasterSecretUtil.getAsymmetricMasterSecret(context, null), message);
|
|
|
|
ApplicationContext.getInstance(context)
|
|
.getJobManager()
|
|
.add(new SmsDecryptJob(context, messageAndThreadId.first));
|
|
} else {
|
|
messageAndThreadId = database.insertMessageInbox(masterSecret, message);
|
|
}
|
|
|
|
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()))
|
|
{
|
|
return Optional.<IncomingTextMessage>of(new IncomingEncryptedMessage(message, message.getMessageBody()));
|
|
} else {
|
|
return Optional.of(message);
|
|
}
|
|
}
|
|
}
|