IMAP Folder speedup tuning

This commit is contained in:
2026-11-09 20:11:31 +02:00
parent d0b26db9ee
commit b14209a5ae
14 changed files with 216 additions and 18 deletions
@@ -464,6 +464,7 @@ internal object AndroidMailService {
return saveAttachmentBytes(context, filename, contentType, Base64.decode(encoded, Base64.DEFAULT))
}
@Synchronized
fun flushOutbox(account: JSONObject) {
val accountId = account.optString("id")
val queued = AndroidAccountVault.pendingMailOperations(accountId)
@@ -15,6 +15,7 @@ import com.getcapacitor.PluginMethod
import com.getcapacitor.annotation.CapacitorPlugin
import java.util.Locale
import java.util.UUID
import java.util.concurrent.Executors
import org.json.JSONArray
import org.json.JSONObject
import javax.mail.internet.InternetAddress
@@ -24,6 +25,8 @@ import javax.crypto.Cipher
class EpMailPlugin : Plugin() {
private var syncReceiver: BroadcastReceiver? = null
private var wipeDialogShowing = false
private val mailboxExecutor = Executors.newFixedThreadPool(3)
private val outboxExecutor = Executors.newSingleThreadExecutor()
override fun load() {
AndroidAccountVault.initialize(context)
@@ -58,6 +61,8 @@ class EpMailPlugin : Plugin() {
}
override fun handleOnDestroy() {
mailboxExecutor.shutdown()
outboxExecutor.shutdown()
syncReceiver?.let {
try { context.unregisterReceiver(it) } catch (_: IllegalArgumentException) {}
}
@@ -522,10 +527,31 @@ class EpMailPlugin : Plugin() {
val selected = filterFolder?.takeIf { it in setOf("Inbox", "Sent", "Archive", "Trash", "Starred") } ?: requested
val folder = if (selected in setOf("Inbox", "Sent", "Archive", "Trash", "Starred", "Snoozed") || AndroidMailService.isCustomFolder(selected)) selected else "Inbox"
val offset = (call.getInt("offset") ?: 0).coerceAtLeast(0)
AndroidMailService.flushOutbox(account)
call.resolve(AndroidMailService.sync(account, folder, offset, JSONObject(filters.toString())).toJSObject())
mailboxExecutor.execute {
try {
call.resolve(AndroidMailService.sync(account, folder, offset, JSONObject(filters.toString())).toJSObject())
} catch (error: Exception) {
call.reject(error.message ?: "Could not sync the mailbox.")
return@execute
}
retryOutbox(account)
}
} catch (error: Exception) {
call.reject(error.message ?: "Could not sync the mailbox.")
}
}
private fun retryOutbox(account: JSONObject) {
try {
outboxExecutor.execute {
try {
AndroidMailService.flushOutbox(account)
} catch (error: Exception) {
android.util.Log.e("EpMailOutbox", "Could not retry queued mail operations", error)
}
}
} catch (error: java.util.concurrent.RejectedExecutionException) {
android.util.Log.w("EpMailOutbox", "Could not schedule queued mail retry", error)
}
}