Auto optimize no IDLE

This commit is contained in:
M66B 2020-03-10 08:44:47 +01:00
parent 4a8bf5fe19
commit 7ca3415fbd
1 changed files with 30 additions and 23 deletions

View File

@ -848,29 +848,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if ("Still here".equals(message) && !account.ondemand) {
long now = new Date().getTime();
if (now - start > STILL_THERE_THRESHOLD)
return;
boolean auto_optimize = prefs.getBoolean("auto_optimize", false);
if (!auto_optimize)
return;
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
if (pollInterval == 0) {
prefs.edit().putInt("poll_interval", STILL_THERE_POLL_INTERVAL).apply();
try {
db.beginTransaction();
for (EntityAccount a : db.account().getAccounts())
db.account().setAccountPollExempted(a.id, !a.id.equals(account.id));
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(ServiceSynchronize.this, message);
} else if (account.poll_exempted) {
db.account().setAccountPollExempted(account.id, false);
ServiceSynchronize.eval(ServiceSynchronize.this, message);
}
if (now - start < STILL_THERE_THRESHOLD)
optimizeAccount(ServiceSynchronize.this, account, message);
}
} else
try {
@ -964,6 +943,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
final boolean capIdle = iservice.hasCapability("IDLE");
Log.i(account.name + " idle=" + capIdle);
if (!capIdle)
optimizeAccount(ServiceSynchronize.this, account, "IDLE");
db.account().setAccountState(account.id, "connected");
db.account().setAccountError(account.id, null);
@ -1598,6 +1579,32 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
message.contains("User is authenticated but not connected") /* Outlook */));
}
private void optimizeAccount(Context context, EntityAccount account, String reason) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean auto_optimize = prefs.getBoolean("auto_optimize", false);
if (!auto_optimize)
return;
DB db = DB.getInstance(context);
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
if (pollInterval == 0) {
prefs.edit().putInt("poll_interval", STILL_THERE_POLL_INTERVAL).apply();
try {
db.beginTransaction();
for (EntityAccount a : db.account().getAccounts())
db.account().setAccountPollExempted(a.id, !a.id.equals(account.id));
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(ServiceSynchronize.this, "Optimize=" + reason);
} else if (account.poll_exempted) {
db.account().setAccountPollExempted(account.id, false);
ServiceSynchronize.eval(ServiceSynchronize.this, "Optimize=" + reason);
}
}
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {