2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-07-21 16:11:55 -07:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2014-11-03 15:16:04 -08:00
|
|
|
import android.telephony.SmsMessage;
|
2015-06-09 07:37:20 -07:00
|
|
|
import android.util.Log;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-01-22 13:52:36 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult;
|
2018-01-24 19:17:44 -08:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2018-02-21 18:49:08 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2018-02-26 14:02:12 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.SqlCipherMigrationRequirement;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
2018-02-26 14:02:12 -08:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class SmsReceiveJob extends ContextJob {
|
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
private static final String TAG = SmsReceiveJob.class.getSimpleName();
|
|
|
|
|
2017-07-21 16:11:55 -07:00
|
|
|
private final @Nullable Object[] pdus;
|
2016-02-05 16:10:33 -08:00
|
|
|
private final int subscriptionId;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2017-07-21 16:11:55 -07:00
|
|
|
public SmsReceiveJob(@NonNull Context context, @Nullable Object[] pdus, int subscriptionId) {
|
2014-11-03 15:16:04 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
2015-03-18 14:25:27 -07:00
|
|
|
.withWakeLock(true)
|
2018-02-26 14:02:12 -08:00
|
|
|
.withRequirement(new SqlCipherMigrationRequirement(context))
|
2014-11-03 15:16:04 -08:00
|
|
|
.create());
|
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
this.pdus = pdus;
|
|
|
|
this.subscriptionId = subscriptionId;
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
2018-02-26 14:02:12 -08:00
|
|
|
public void onRun() throws MigrationPendingException {
|
2017-02-17 14:05:22 -08:00
|
|
|
Log.w(TAG, "onRun()");
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
Optional<IncomingTextMessage> message = assembleMessageFragments(pdus, subscriptionId);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
if (message.isPresent() && !isBlocked(message.get())) {
|
2018-01-24 19:17:44 -08:00
|
|
|
Optional<InsertResult> insertResult = storeMessage(message.get());
|
2017-01-22 13:52:36 -08:00
|
|
|
|
|
|
|
if (insertResult.isPresent()) {
|
2018-01-24 19:17:44 -08:00
|
|
|
MessageNotifier.updateNotification(context, insertResult.get().getThreadId());
|
2017-01-22 13:52:36 -08:00
|
|
|
}
|
2015-06-09 07:37:20 -07:00
|
|
|
} else if (message.isPresent()) {
|
|
|
|
Log.w(TAG, "*** Received blocked SMS, ignoring...");
|
2017-02-17 14:05:22 -08:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "*** Failed to assemble message fragments!");
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2018-02-26 14:02:12 -08:00
|
|
|
return exception instanceof MigrationPendingException;
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private boolean isBlocked(IncomingTextMessage message) {
|
|
|
|
if (message.getSender() != null) {
|
2017-08-21 18:32:38 -07:00
|
|
|
Recipient recipient = Recipient.from(context, message.getSender(), false);
|
2017-08-01 08:56:00 -07:00
|
|
|
return recipient.isBlocked();
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 14:02:12 -08:00
|
|
|
private Optional<InsertResult> storeMessage(IncomingTextMessage message) throws MigrationPendingException {
|
2018-01-24 19:17:44 -08:00
|
|
|
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
2018-02-26 14:02:12 -08:00
|
|
|
database.ensureMigration();
|
|
|
|
|
|
|
|
if (TextSecurePreferences.getNeedsSqlCipherMigration(context)) {
|
|
|
|
throw new MigrationPendingException();
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
if (message.isSecureMessage()) {
|
2017-01-22 13:52:36 -08:00
|
|
|
IncomingTextMessage placeholder = new IncomingTextMessage(message, "");
|
|
|
|
Optional<InsertResult> insertResult = database.insertMessageInbox(placeholder);
|
|
|
|
database.markAsLegacyVersion(insertResult.get().getMessageId());
|
|
|
|
|
|
|
|
return insertResult;
|
2014-11-03 15:16:04 -08:00
|
|
|
} else {
|
2018-01-24 19:17:44 -08:00
|
|
|
return database.insertMessageInbox(message);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 16:11:55 -07:00
|
|
|
private Optional<IncomingTextMessage> assembleMessageFragments(@Nullable Object[] pdus, int subscriptionId) {
|
|
|
|
if (pdus == null) {
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
List<IncomingTextMessage> messages = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Object pdu : pdus) {
|
2017-07-26 09:59:15 -07:00
|
|
|
messages.add(new IncomingTextMessage(context, SmsMessage.createFromPdu((byte[])pdu), subscriptionId));
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (messages.isEmpty()) {
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:43:50 -07:00
|
|
|
return Optional.of(new IncomingTextMessage(messages));
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
2018-02-26 14:02:12 -08:00
|
|
|
|
|
|
|
private class MigrationPendingException extends Exception {
|
|
|
|
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|