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