diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 4682f5ec69..20cfdb8a91 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -205,6 +205,7 @@ public class ApplicationContext extends Application implements AppForegroundObse .addNonBlocking(AppDependencies::getBillingApi) .addNonBlocking(this::ensureProfileUploaded) .addNonBlocking(() -> AppDependencies.getExpireStoriesManager().scheduleIfNecessary()) + .addNonBlocking(BackupRepository::maybeFixAnyDanglingAttachmentUploads) .addPostRender(() -> AppDependencies.getDeletedCallEventManager().scheduleIfNecessary()) .addPostRender(() -> RateLimitUtil.retryAllRateLimitedMessages(this)) .addPostRender(this::initializeExpiringMessageManager) 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 80013b7ad7..8c6ae86664 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 @@ -100,6 +100,7 @@ import org.thoughtcrime.securesms.jobmanager.Job import org.thoughtcrime.securesms.jobmanager.impl.DataRestoreConstraint import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint import org.thoughtcrime.securesms.jobmanager.impl.WifiConstraint +import org.thoughtcrime.securesms.jobs.ArchiveAttachmentBackfillJob import org.thoughtcrime.securesms.jobs.AvatarGroupsV2DownloadJob import org.thoughtcrime.securesms.jobs.BackupDeleteJob import org.thoughtcrime.securesms.jobs.BackupMessagesJob @@ -112,6 +113,7 @@ import org.thoughtcrime.securesms.jobs.RestoreAttachmentJob import org.thoughtcrime.securesms.jobs.RestoreOptimizedMediaJob import org.thoughtcrime.securesms.jobs.RetrieveProfileJob import org.thoughtcrime.securesms.jobs.StickerPackDownloadJob +import org.thoughtcrime.securesms.jobs.UploadAttachmentToArchiveJob import org.thoughtcrime.securesms.keyvalue.BackupValues.ArchiveServiceCredentials import org.thoughtcrime.securesms.keyvalue.KeyValueStore import org.thoughtcrime.securesms.keyvalue.SignalStore @@ -613,6 +615,23 @@ object BackupRepository { SignalStore.backup.snoozeDownloadNotifier() } + @JvmStatic + fun maybeFixAnyDanglingAttachmentUploads() { + if (!SignalStore.backup.backsUpMedia || !AppDependencies.jobManager.areQueuesEmpty(UploadAttachmentToArchiveJob.QUEUES)) { + return + } + + val pendingBytes = SignalDatabase.attachments.getPendingArchiveUploadBytes() + if (pendingBytes == 0L) { + return + } + + Log.w(TAG, "There are ${pendingBytes.bytes.toUnitString(maxPlaces = 2)} of attachments that need to be uploaded to the archive, but no jobs for them! Attempting to fix.") + val resetCount = SignalDatabase.attachments.clearArchiveTransferStateForInProgressItems() + Log.w(TAG, "Cleared the archive transfer state of $resetCount attachments.") + AppDependencies.jobManager.add(ArchiveAttachmentBackfillJob()) + } + /** * Whether or not the "Your media will be deleted today" sheet should be displayed. */ diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/backups/remote/RemoteBackupsSettingsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/backups/remote/RemoteBackupsSettingsViewModel.kt index c45ca1df8e..f9dc3ebbd3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/backups/remote/RemoteBackupsSettingsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/backups/remote/RemoteBackupsSettingsViewModel.kt @@ -164,6 +164,10 @@ class RemoteBackupsSettingsViewModel : ViewModel() { } } } + + viewModelScope.launch(Dispatchers.IO) { + BackupRepository.maybeFixAnyDanglingAttachmentUploads() + } } fun setCanBackUpUsingCellular(canBackUpUsingCellular: Boolean) { 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 fdca9a6eae..0a880ecb1e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt @@ -868,6 +868,17 @@ class AttachmentTable( } } + /** + * Resets the [ARCHIVE_TRANSFER_STATE] of any attachments that are currently in-progress of uploading. + */ + fun clearArchiveTransferStateForInProgressItems(): Int { + return writableDatabase + .update(TABLE_NAME) + .values(ARCHIVE_TRANSFER_STATE to ArchiveTransferState.NONE.value) + .where("$ARCHIVE_TRANSFER_STATE IN (${ArchiveTransferState.UPLOAD_IN_PROGRESS.value}, ${ArchiveTransferState.COPY_PENDING.value}, ${ArchiveTransferState.TEMPORARY_FAILURE.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.