Simplify quota

This commit is contained in:
M66B 2020-12-29 18:46:17 +01:00
parent 16fe4d4762
commit 24f895b7d9
1 changed files with 30 additions and 34 deletions

View File

@ -112,7 +112,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private MediatorState liveAccountNetworkState = new MediatorState(); private MediatorState liveAccountNetworkState = new MediatorState();
private static final long PURGE_DELAY = 60 * 1000L; // milliseconds 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 QUIT_DELAY = 5 * 1000L; // milliseconds
private static final long STILL_THERE_THRESHOLD = 3 * 60 * 1000L; // milliseconds private static final long STILL_THERE_THRESHOLD = 3 * 60 * 1000L; // milliseconds
static final int DEFAULT_POLL_INTERVAL = 0; // minutes 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"); EntityLog.log(this, account.name + " connected");
db.account().setAccountMaxSize(account.id, iservice.getMaxSize()); db.account().setAccountMaxSize(account.id, iservice.getMaxSize());
if (istore instanceof IMAPStore)
updateQuota(((IMAPStore) iservice.getStore()), account);
// Listen for folder events // Listen for folder events
iservice.getStore().addFolderListener(new FolderAdapter() { iservice.getStore().addFolderListener(new FolderAdapter() {
@ -1361,8 +1362,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
forced = true; forced = true;
final Runnable purge = new Runnable() { final Runnable purge = new Runnable() {
private Long lastQuota = null;
@Override @Override
public void run() { public void run() {
executor.submit(new Runnable() { executor.submit(new Runnable() {
@ -1371,37 +1370,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try { try {
wlFolder.acquire(); 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 // Close cached connections
Log.i(account.name + " Empty connection pool"); Log.i(account.name + " Empty connection pool");
((IMAPStore) istore).emptyConnectionPool(false); ((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) { private void optimizeAccount(EntityAccount account, String reason) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean auto_optimize = prefs.getBoolean("auto_optimize", false); boolean auto_optimize = prefs.getBoolean("auto_optimize", false);