From 08601f84d111a5762c4ec758fc93ceef5a5cfb8f Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 28 Dec 2020 13:58:54 +0100 Subject: [PATCH] Get quota before closing connections --- .../eu/faircode/email/ServiceSynchronize.java | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index e7812a6886..25f0f0014f 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -112,6 +112,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences private MediatorState liveAccountNetworkState = new MediatorState(); private static final long PURGE_DELAY = 60 * 1000L; // milliseconds + private static final long QUOTA_INTERVAL = 5 * 60 * 1000L; // milliseconds private static final long QUIT_DELAY = 5 * 1000L; // milliseconds private static final long STILL_THERE_THRESHOLD = 3 * 60 * 1000L; // milliseconds static final int DEFAULT_POLL_INTERVAL = 0; // minutes @@ -1360,12 +1361,46 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences forced = true; Runnable purge = new Runnable() { + private Long lastQuota = null; + @Override public void run() { executor.submit(new Runnable() { @Override public void run() { try { + // Get quota + if (iservice.hasCapability("QUOTA")) { + long now = new Date().getTime(); + if (lastQuota == null || lastQuota + QUOTA_INTERVAL < now) { + lastQuota = now; + + try { + // https://tools.ietf.org/id/draft-melnikov-extra-quota-00.html + Quota[] quotas = ((IMAPStore) iservice.getStore()).getQuota("INBOX"); + if (quotas != null) { + long usage = 0; + long limit = 0; + for (Quota quota : quotas) + if (quota.resources != null) + for (Quota.Resource resource : quota.resources) { + Log.i("Quota " + resource.name + " " + resource.usage + "/" + resource.limit); + if ("STORAGE".equalsIgnoreCase(resource.name)) { + usage += resource.usage * 1024; + limit = Math.max(limit, resource.limit * 1024); + } + } + db.account().setAccountQuota(account.id, usage, limit); + } + } catch (MessagingException ex) { + Log.w(ex); + db.account().setAccountQuota(account.id, null, null); + } + } + } else + db.account().setAccountQuota(account.id, null, null); + + // Close cached connections Log.i(account.name + " Empty connection pool"); ((IMAPStore) istore).emptyConnectionPool(false); } catch (Throwable ex) { @@ -1667,32 +1702,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences db.account().setAccountConnected(account.id, account.last_connected); db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle)); - // Get quota - if (iservice.hasCapability("QUOTA")) - try { - // https://tools.ietf.org/id/draft-melnikov-extra-quota-00.html - Quota[] quotas = ((IMAPStore) iservice.getStore()).getQuota("INBOX"); - if (quotas != null) { - long usage = 0; - long limit = 0; - for (Quota quota : quotas) - if (quota.resources != null) - for (Quota.Resource resource : quota.resources) { - Log.i("Quota " + resource.name + " " + resource.usage + "/" + resource.limit); - if ("STORAGE".equalsIgnoreCase(resource.name)) { - usage += resource.usage * 1024; - limit = Math.max(limit, resource.limit * 1024); - } - } - db.account().setAccountQuota(account.id, usage, limit); - } - } catch (MessagingException ex) { - Log.w(ex); - db.account().setAccountQuota(account.id, null, null); - } - else - db.account().setAccountQuota(account.id, null, null); - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel("receive:" + account.id, 1); nm.cancel("alert:" + account.id, 1);