Refactoring

pull/190/head
M66B 5 years ago
parent 11a79c9e41
commit acbc601b56

@ -24,10 +24,12 @@ import android.app.NotificationChannel;
import android.app.NotificationChannelGroup; import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceManager;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@ -149,6 +151,13 @@ public class EntityAccount extends EntityOrder implements Serializable {
return "imap.gmail.com".equalsIgnoreCase(host); return "imap.gmail.com".equalsIgnoreCase(host);
} }
boolean isTransient(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
return (!enabled || this.ondemand || (pollInterval > 0 && !this.poll_exempted));
}
String getProtocol() { String getProtocol() {
switch (protocol) { switch (protocol) {
case TYPE_IMAP: case TYPE_IMAP:

@ -391,8 +391,7 @@ public class FragmentAccounts extends FragmentBase {
EntityAccount account = db.account().getAccount(folder.account); EntityAccount account = db.account().getAccount(folder.account);
if (account != null && !"connected".equals(account.state)) { if (account != null && !"connected".equals(account.state)) {
now = false; now = false;
if (enabled && !account.ondemand && if (!account.isTransient(context))
(pollInterval == 0 || account.poll_exempted))
force = true; force = true;
} }
} }

@ -398,8 +398,7 @@ public class FragmentFolders extends FragmentBase {
EntityAccount account = db.account().getAccount(folder.account); EntityAccount account = db.account().getAccount(folder.account);
if (account != null && !"connected".equals(account.state)) { if (account != null && !"connected".equals(account.state)) {
now = false; now = false;
if (enabled && !account.ondemand && if (!account.isTransient(context))
(pollInterval == 0 || account.poll_exempted))
force = true; force = true;
} }
} }

@ -1483,8 +1483,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
EntityAccount account = db.account().getAccount(folder.account); EntityAccount account = db.account().getAccount(folder.account);
if (account != null && !"connected".equals(account.state)) { if (account != null && !"connected".equals(account.state)) {
now = false; now = false;
if (enabled && !account.ondemand && folder.synchronize && if (!account.isTransient(context))
(pollInterval == 0 || account.poll_exempted))
force = true; force = true;
} }
} }

@ -234,7 +234,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.d("### evaluating " + current); Log.d("### evaluating " + current);
if (current.accountState.shouldRun(current.enabled)) if (current.accountState.shouldRun(current.enabled))
runService = true; runService = true;
if (!isTransient(current.accountState) && if (!current.accountState.isTransient(ServiceSynchronize.this) &&
("connected".equals(current.accountState.state) || current.accountState.backoff_until != null)) ("connected".equals(current.accountState.state) || current.accountState.backoff_until != null))
accounts++; accounts++;
if (current.accountState.synchronize) if (current.accountState.synchronize)
@ -973,7 +973,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (e.getMessageType() == StoreEvent.NOTICE) { if (e.getMessageType() == StoreEvent.NOTICE) {
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + message); EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + message);
if ("Still here".equals(message) && !isTransient(account)) { if ("Still here".equals(message) &&
!account.isTransient(ServiceSynchronize.this)) {
long now = new Date().getTime(); long now = new Date().getTime();
if (now - start < STILL_THERE_THRESHOLD) if (now - start < STILL_THERE_THRESHOLD)
optimizeAccount(account, message); optimizeAccount(account, message);
@ -1892,7 +1893,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
} else { } else {
// Cancel transient sync operations // Cancel transient sync operations
if (isTransient(account)) { if (account.isTransient(this)) {
List<EntityOperation> syncs = db.operation().getOperations(account.id, EntityOperation.SYNC); List<EntityOperation> syncs = db.operation().getOperations(account.id, EntityOperation.SYNC);
if (syncs != null) { if (syncs != null) {
for (EntityOperation op : syncs) { for (EntityOperation op : syncs) {
@ -1943,13 +1944,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
} }
private boolean isTransient(EntityAccount account) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
return (!enabled || account.ondemand || (pollInterval > 0 && !account.poll_exempted));
}
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);

Loading…
Cancel
Save