From 24f895b7d98cf8e4056e932f1bb2143b68cc39a4 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 29 Dec 2020 18:46:17 +0100 Subject: [PATCH] Simplify quota --- .../eu/faircode/email/ServiceSynchronize.java | 64 +++++++++---------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index b1629d968b..8db11af4e2 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -112,7 +112,6 @@ 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 = 15 * 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 @@ -1054,6 +1053,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences EntityLog.log(this, account.name + " connected"); db.account().setAccountMaxSize(account.id, iservice.getMaxSize()); + if (istore instanceof IMAPStore) + updateQuota(((IMAPStore) iservice.getStore()), account); // Listen for folder events iservice.getStore().addFolderListener(new FolderAdapter() { @@ -1361,8 +1362,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences forced = true; final Runnable purge = new Runnable() { - private Long lastQuota = null; - @Override public void run() { executor.submit(new Runnable() { @@ -1371,37 +1370,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences try { wlFolder.acquire(); - // 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); @@ -1959,6 +1927,34 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } } + private void updateQuota(IMAPStore istore, EntityAccount account) { + DB db = DB.getInstance(this); + try { + if (istore.hasCapability("QUOTA")) { + // https://tools.ietf.org/id/draft-melnikov-extra-quota-00.html + Quota[] quotas = istore.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); + } + } else + db.account().setAccountQuota(account.id, null, null); + } catch (MessagingException ex) { + Log.w(ex); + db.account().setAccountQuota(account.id, null, null); + } + } + private void optimizeAccount(EntityAccount account, String reason) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean auto_optimize = prefs.getBoolean("auto_optimize", false);