diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt index 3bc33c9746..ee5e22e857 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt @@ -332,6 +332,8 @@ class HomeActivity : PassphraseRequiredActionBarActivity(), lifecycleScope.launch(Dispatchers.Main) { val context = this@HomeActivity as Context + DatabaseFactory.getSessionJobDatabase(this@HomeActivity).cancelPendingMessageSendJobs(threadID) + // Send a leave group message if this is an active closed group if (recipient.address.isClosedGroup && DatabaseFactory.getGroupDatabase(context).isActive(recipient.address.toGroupString())) { var isClosedGroup: Boolean diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt index 22c8d48f44..c5e5e102ce 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt @@ -71,6 +71,30 @@ class SessionJobDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa } } + fun cancelPendingMessageSendJobs(threadID: Long) { + val database = databaseHelper.writableDatabase + val attachmentUploadJobKeys = mutableListOf() + database.getAll(sessionJobTable, "$jobType = ?", arrayOf( AttachmentUploadJob.KEY )) { cursor -> + val job = jobFromCursor(cursor) as AttachmentUploadJob? + if (job != null && job.threadID == threadID.toString()) { attachmentUploadJobKeys.add(job.id!!) } + } + val messageSendJobKeys = mutableListOf() + database.getAll(sessionJobTable, "$jobType = ?", arrayOf( MessageSendJob.KEY )) { cursor -> + val job = jobFromCursor(cursor) as MessageSendJob? + if (job != null && job.message.threadID == threadID) { messageSendJobKeys.add(job.id!!) } + } + if (attachmentUploadJobKeys.isNotEmpty()) { + val attachmentUploadJobKeysAsString = attachmentUploadJobKeys.joinToString(", ") + database.delete(sessionJobTable, "${Companion.jobType} = ? AND ${Companion.jobID} IN (?)", + arrayOf( AttachmentUploadJob.KEY, attachmentUploadJobKeysAsString )) + } + if (messageSendJobKeys.isNotEmpty()) { + val messageSendJobKeysAsString = messageSendJobKeys.joinToString(", ") + database.delete(sessionJobTable, "${Companion.jobType} = ? AND ${Companion.jobID} IN (?)", + arrayOf( MessageSendJob.KEY, messageSendJobKeysAsString )) + } + } + fun isJobCanceled(job: Job): Boolean { val database = databaseHelper.readableDatabase var cursor: android.database.Cursor? = null