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;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.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-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
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-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2018-02-26 14:02:12 -08:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import java.io.IOException;
|
2014-11-03 15:16:04 -08:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
2018-11-27 12:34:42 -08:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
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();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_PDUS = "pdus";
|
|
|
|
private static final String KEY_SUBSCRIPTION_ID = "subscription_id";
|
|
|
|
|
|
|
|
private @Nullable Object[] pdus;
|
|
|
|
|
|
|
|
private int subscriptionId;
|
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
public SmsReceiveJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
|
|
|
super(context, workerParameters);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
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()
|
2018-08-09 10:15:43 -04:00
|
|
|
.withSqlCipherRequirement()
|
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
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
String[] encoded = data.getStringArray(KEY_PDUS);
|
|
|
|
pdus = new Object[encoded.length];
|
|
|
|
try {
|
|
|
|
for (int i = 0; i < encoded.length; i++) {
|
|
|
|
pdus[i] = Base64.decode(encoded[i]);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
subscriptionId = data.getInt(KEY_SUBSCRIPTION_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
String[] encoded = new String[pdus.length];
|
|
|
|
for (int i = 0; i < pdus.length; i++) {
|
|
|
|
encoded[i] = Base64.encodeBytes((byte[]) pdus[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataBuilder.putStringArray(KEY_PDUS, encoded)
|
|
|
|
.putInt(KEY_SUBSCRIPTION_ID, subscriptionId)
|
|
|
|
.build();
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2018-02-26 14:02:12 -08:00
|
|
|
public void onRun() throws MigrationPendingException {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "onRun()");
|
2017-02-17 14:05:22 -08:00
|
|
|
|
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
|
|
|
}
|