Prevent SendJobs from sending already-sent messages.

This is to guard against behavior WorkManager has where it may
re-enqueue a job that has already been completed (if, for instance, it
was preempted).

Fixes #8268
This commit is contained in:
Greyson Parrelli 2018-11-14 11:39:23 -08:00
parent a7e15dc21e
commit d93bfbf693
7 changed files with 40 additions and 0 deletions

View File

@ -1053,6 +1053,17 @@ public class MmsDatabase extends MessagingDatabase {
}
}
public boolean isSent(long messageId) {
SQLiteDatabase database = databaseHelper.getReadableDatabase();
try (Cursor cursor = database.query(TABLE_NAME, new String[] { MESSAGE_BOX }, ID + " = ?", new String[] { String.valueOf(messageId)}, null, null, null)) {
if (cursor != null && cursor.moveToNext()) {
long type = cursor.getLong(cursor.getColumnIndexOrThrow(MESSAGE_BOX));
return Types.isSentType(type);
}
}
return false;
}
/*package*/ void deleteThreads(Set<Long> threadIds) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String where = "";

View File

@ -117,6 +117,10 @@ public interface MmsSmsColumns {
(type & BASE_TYPE_MASK) == BASE_SENDING_TYPE;
}
public static boolean isSentType(long type) {
return (type & BASE_TYPE_MASK) == BASE_SENT_TYPE;
}
public static boolean isPendingSmsFallbackType(long type) {
return (type & BASE_TYPE_MASK) == BASE_PENDING_INSECURE_SMS_FALLBACK ||
(type & BASE_TYPE_MASK) == BASE_PENDING_SECURE_SMS_FALLBACK;

View File

@ -92,6 +92,11 @@ public class MmsSendJob extends SendJob {
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
if (database.isSent(messageId)) {
Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
return;
}
try {
Log.i(TAG, "Sending message: " + messageId);

View File

@ -114,6 +114,11 @@ public class PushGroupSendJob extends PushSendJob implements InjectableType {
List<NetworkFailure> existingNetworkFailures = message.getNetworkFailures();
List<IdentityKeyMismatch> existingIdentityMismatches = message.getIdentityKeyMismatches();
if (database.isSent(messageId)) {
Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
return;
}
try {
Log.i(TAG, "Sending message: " + messageId);

View File

@ -85,6 +85,11 @@ public class PushMediaSendJob extends PushSendJob implements InjectableType {
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
if (database.isSent(messageId)) {
Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
return;
}
try {
Log.i(TAG, "Sending message: " + messageId);

View File

@ -78,6 +78,11 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
SmsMessageRecord record = database.getMessage(messageId);
if (!record.isPending() && !record.isFailed()) {
Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
return;
}
try {
Log.i(TAG, "Sending message: " + messageId);

View File

@ -90,6 +90,11 @@ public class SmsSendJob extends SendJob {
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
SmsMessageRecord record = database.getMessage(messageId);
if (!record.isPending() && !record.isFailed()) {
Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
return;
}
try {
Log.i(TAG, "Sending message: " + messageId + " (attempt " + runAttempt + ")");
deliver(record);