Commit f381fa89 authored by Hussein's avatar Hussein

P4A-1014 Improve KeySync message deletion.

parent 6c7a5cf8
......@@ -1713,7 +1713,7 @@ public class K9 extends MultiDexApplication {
public void onActivityDestroyed(@NotNull Activity activity) {
--activityCount;
if (activityCount == 0) {
MessagingController.getInstance().deleteConsumedMessages();
KeySyncCleaner.queueAutoConsumeMessages();
pEpSyncProvider.stopSync();
pEpProvider.close();
pEpProvider = null;
......
......@@ -797,7 +797,7 @@ public class MessagingController implements Sync.MessageToSendCallback {
Timber.d("SYNC: About to process pending commands for account %s", account.getDescription());
try {
consumeMessages(context);
consumeMessages(account);
processPendingCommandsSynchronous(account);
} catch (Exception e) {
Timber.e(e, "Failure processing command, but allow message sync attempt");
......@@ -4006,8 +4006,6 @@ public class MessagingController implements Sync.MessageToSendCallback {
account.setRingNotified(false);
deleteConsumedMessages();
sendPendingMessages(account, listener);
try {
......@@ -4941,15 +4939,38 @@ public class MessagingController implements Sync.MessageToSendCallback {
@WorkerThread
public void consumeMessages(final Context context) throws MessagingException {
Timber.e("Delete pEp-auto-consume messages older than 10min");
Timber.e("Delete pEp-auto-consume messages older than %d min for All accounts",
PEpProvider.TIMEOUT / (60 * 1000));
List<Account> accounts = Preferences.getPreferences(context).getAccounts();
for (Account account : accounts) {
List<MessageReference> refs = account.getLocalStore().getAutoConsumeMessageReferences();
deleteMessages(refs, null);
expunge(account, account.getInboxFolderName());
consumeMessages(account);
}
}
@WorkerThread
private void consumeMessages(Account account) throws MessagingException {
Timber.e("Delete pEp-auto-consume messages for account %s::%s", account.getName(), account.getEmail());
List<MessageReference> refs = account.getLocalStore().getAutoConsumeMessageReferences();
actOnMessagesGroupedByAccountAndFolder(
refs, (account1, messageFolder, accountMessages) -> {
for (LocalMessage accountMessage : accountMessages) {
try {
String folderName = accountMessage.getFolder().getName();
deleteMessage(accountMessage, account1, folderName, accountMessage.getFolder());
Folder<? extends Message> remoteFolder = account.getRemoteStore().getFolder(folderName);
remoteFolder.expunge();
} catch (MessagingException e) {
Timber.e(e, "Could not clean pEpEngine sync message");
}
}
});
}
//FIXME: check if really needed
public void deleteConsumedMessages() {
putBackground("deleteConsumedMessages", null, () -> {
try {
......
......@@ -24,7 +24,7 @@ class KeySyncCleaner {
.setRequiresStorageNotLow(false)
.build()
private fun autoConsumeRequest(): WorkRequest {
private fun autoConsumeRequest(): PeriodicWorkRequest {
return PeriodicWorkRequestBuilder<CleanWorker>(15, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag(AUTO_CONSUME_CLEAN_TAG)
......@@ -36,7 +36,10 @@ class KeySyncCleaner {
@JvmStatic
fun queueAutoConsumeMessages() {
WorkManager.getInstance().enqueue(KeySyncCleaner().autoConsumeRequest())
// Just have one cleanup job enqueued
WorkManager.getInstance().enqueueUniquePeriodicWork(AUTO_CONSUME_CLEAN_TAG,
ExistingPeriodicWorkPolicy.KEEP,
KeySyncCleaner().autoConsumeRequest())
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment