diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt index 77fcb0a555..97306b797b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt @@ -106,6 +106,8 @@ import org.thoughtcrime.securesms.groups.GroupId import org.thoughtcrime.securesms.jobmanager.Job import org.thoughtcrime.securesms.jobmanager.impl.DataRestoreConstraint import org.thoughtcrime.securesms.jobs.ArchiveAttachmentBackfillJob +import org.thoughtcrime.securesms.jobs.ArchiveThumbnailBackfillJob +import org.thoughtcrime.securesms.jobs.ArchiveThumbnailUploadJob import org.thoughtcrime.securesms.jobs.AvatarGroupsV2DownloadJob import org.thoughtcrime.securesms.jobs.BackupDeleteJob import org.thoughtcrime.securesms.jobs.BackupMessagesJob @@ -594,6 +596,7 @@ object BackupRepository { } if (SignalStore.backup.archiveUploadState?.backupPhase == ArchiveUploadProgressState.BackupPhase.Message && AppDependencies.jobManager.find { it.factoryKey == BackupMessagesJob.KEY }.isEmpty()) { + Log.w(TAG, "Found a situation where message backup was in progress, but there's no active BackupMessageJob! Re-enqueueing.") SignalStore.backup.archiveUploadState = null BackupMessagesJob.enqueue() return @@ -605,11 +608,18 @@ object BackupRepository { if (!AppDependencies.jobManager.areQueuesEmpty(UploadAttachmentToArchiveJob.QUEUES)) { if (SignalStore.backup.archiveUploadState?.state == ArchiveUploadProgressState.State.None) { + Log.w(TAG, "Found a situation where attachment uploads are in progress, but the progress state was None! Fixing.") ArchiveUploadProgress.onAttachmentSectionStarted(SignalDatabase.attachments.getPendingArchiveUploadBytes()) } return } + if (AppDependencies.jobManager.areQueuesEmpty(ArchiveThumbnailUploadJob.QUEUES) && SignalDatabase.attachments.areAnyThumbnailsPendingUpload()) { + Log.w(TAG, "Found a situation where there's no thumbnail jobs in progress, but thumbnails are in the pending upload state! Clearing the pending state and re-enqueueing.") + SignalDatabase.attachments.clearArchiveThumbnailTransferStateForInProgressItems() + AppDependencies.jobManager.add(ArchiveThumbnailBackfillJob()) + } + val pendingBytes = SignalDatabase.attachments.getPendingArchiveUploadBytes() if (pendingBytes == 0L) { return diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt index 18b088692b..8bc36d7902 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt @@ -1105,6 +1105,17 @@ class AttachmentTable( .run() } + /** + * Resets the [ARCHIVE_THUMBNAIL_TRANSFER_STATE] of any attachments that are currently in-progress of uploading. + */ + fun clearArchiveThumbnailTransferStateForInProgressItems(): Int { + return writableDatabase + .update(TABLE_NAME) + .values(ARCHIVE_THUMBNAIL_TRANSFER_STATE to ArchiveTransferState.NONE.value) + .where("$ARCHIVE_THUMBNAIL_TRANSFER_STATE = ?", ArchiveTransferState.UPLOAD_IN_PROGRESS.value) + .run() + } + /** * Marks eligible attachments as offloaded based on their received at timestamp, their last restore time, * presence of thumbnail if media, and the full file being available in the archive. @@ -1177,6 +1188,13 @@ class AttachmentTable( .readToSingleLong() } + fun areAnyThumbnailsPendingUpload(): Boolean { + return readableDatabase + .exists(TABLE_NAME) + .where("$ARCHIVE_THUMBNAIL_TRANSFER_STATE = ?", ArchiveTransferState.UPLOAD_IN_PROGRESS.value) + .run() + } + /** * Clears out the incrementalMac for the specified [attachmentId], as well as any other attachments that share the same ([remoteKey], [plaintextHash]) pair (if present). */