Get quota before closing connections

This commit is contained in:
M66B 2020-12-28 13:58:54 +01:00
parent ddc6a3750d
commit 08601f84d1
1 changed files with 35 additions and 26 deletions

View File

@ -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);