From 923d9fb07b7b5c617e9cef1a1058153e5272747f Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Fri, 4 Sep 2015 10:56:59 -0700 Subject: [PATCH] fix crash on pending self-sent media fixes #4016 Closes #4017 // FREEBIE --- .../securesms/DatabaseUpgradeActivity.java | 15 ++++++++++----- .../securesms/database/MmsDatabase.java | 4 ++++ .../securesms/database/PartDatabase.java | 2 +- .../securesms/jobs/AttachmentDownloadJob.java | 4 ++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/org/thoughtcrime/securesms/DatabaseUpgradeActivity.java b/src/org/thoughtcrime/securesms/DatabaseUpgradeActivity.java index cbc09e40d4..5c35ffd2b6 100644 --- a/src/org/thoughtcrime/securesms/DatabaseUpgradeActivity.java +++ b/src/org/thoughtcrime/securesms/DatabaseUpgradeActivity.java @@ -34,6 +34,7 @@ import org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.MmsDatabase; import org.thoughtcrime.securesms.database.MmsDatabase.Reader; +import org.thoughtcrime.securesms.database.PartDatabase; import org.thoughtcrime.securesms.database.PushDatabase; import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.jobs.AttachmentDownloadJob; @@ -223,16 +224,20 @@ public class DatabaseUpgradeActivity extends BaseActivity { } private void schedulePendingIncomingParts(Context context) { - MmsDatabase db = DatabaseFactory.getMmsDatabase(context); - List pendingParts = DatabaseFactory.getPartDatabase(context).getPendingParts(); + final PartDatabase partDb = DatabaseFactory.getPartDatabase(context); + final MmsDatabase mmsDb = DatabaseFactory.getMmsDatabase(context); + final List pendingParts = DatabaseFactory.getPartDatabase(context).getPendingParts(); Log.w(TAG, pendingParts.size() + " pending parts."); for (PduPart part : pendingParts) { - final Reader reader = db.readerFor(masterSecret, db.getMessage(part.getMmsId())); + final Reader reader = mmsDb.readerFor(masterSecret, mmsDb.getMessage(part.getMmsId())); final MessageRecord record = reader.getNext(); - if (record != null && !record.isOutgoing() && record.isPush()) { - Log.w(TAG, "queuing new attachment download job for incoming push part."); + if (part.getContentLocation() == null) { + Log.w(TAG, "corrected a pending self-sent media part " + part.getPartId() + "."); + partDb.setTransferState(part.getMmsId(), part.getPartId(), PartDatabase.TRANSFER_PROGRESS_DONE); + } else if (record != null && !record.isOutgoing() && record.isPush()) { + Log.w(TAG, "queuing new attachment download job for incoming push part " + part.getPartId() + "."); ApplicationContext.getInstance(context) .getJobManager() .add(new AttachmentDownloadJob(context, part.getMmsId(), part.getPartId())); diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index 37349524e4..c479b3e52a 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -577,6 +577,10 @@ public class MmsDatabase extends MessagingDatabase { contentValues.put(READ, 1); contentValues.put(DATE_RECEIVED, contentValues.getAsLong(DATE_SENT)); + for (int i = 0; i < request.getBody().getPartsNum(); i++) { + request.getBody().getPart(i).setTransferProgress(PartDatabase.TRANSFER_PROGRESS_DONE); + } + return insertMediaMessage(new MasterSecretUnion(masterSecret), request.getPduHeaders(), request.getBody(), contentValues); } catch (NoSuchMessageException e) { diff --git a/src/org/thoughtcrime/securesms/database/PartDatabase.java b/src/org/thoughtcrime/securesms/database/PartDatabase.java index 17d40fc609..b56cca8698 100644 --- a/src/org/thoughtcrime/securesms/database/PartDatabase.java +++ b/src/org/thoughtcrime/securesms/database/PartDatabase.java @@ -444,7 +444,7 @@ public class PartDatabase extends Database { return part; } - public List getPendingParts() { + public @NonNull List getPendingParts() { final SQLiteDatabase database = databaseHelper.getReadableDatabase(); final List parts = new LinkedList<>(); diff --git a/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java b/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java index a6b45afc00..f8a9020503 100644 --- a/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java +++ b/src/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.java @@ -88,6 +88,7 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable private void retrievePart(MasterSecret masterSecret, PduPart part, long messageId) throws IOException { + PartDatabase database = DatabaseFactory.getPartDatabase(context); File attachmentFile = null; @@ -115,6 +116,8 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable private TextSecureAttachmentPointer createAttachmentPointer(MasterSecret masterSecret, PduPart part) throws InvalidPartException { + if (part.getContentLocation() == null) throw new InvalidPartException("null content location"); + try { AsymmetricMasterSecret asymmetricMasterSecret = MasterSecretUtil.getAsymmetricMasterSecret(context, masterSecret); long id = Long.parseLong(Util.toIsoString(part.getContentLocation())); @@ -153,6 +156,7 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable } private static class InvalidPartException extends Exception { + public InvalidPartException(String s) {super(s);} public InvalidPartException(Exception e) {super(e);} }