From 7f9b4fc2b5e10aebf47a2569872a1ef90f82c1d9 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 22 Jul 2019 13:18:52 +0200 Subject: [PATCH] Skip sync on oneshot operation --- .../java/eu/faircode/email/EntityOperation.java | 4 ++-- .../java/eu/faircode/email/FragmentFolders.java | 2 +- .../eu/faircode/email/ServiceSynchronize.java | 15 +++++++++------ .../main/java/eu/faircode/email/WorkerPoll.java | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 2eec559674..b7bf8dd989 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -229,7 +229,7 @@ public class EntityOperation { if (SEND.equals(name)) ServiceSend.start(context); else - ServiceSynchronize.process(context); + ServiceSynchronize.process(context, false); } static void sync(Context context, long fid, boolean foreground) { @@ -258,7 +258,7 @@ public class EntityOperation { if (folder.account == null) // Outbox ServiceSend.start(context); else if (foreground) - ServiceSynchronize.process(context); + ServiceSynchronize.process(context, true); } static void subscribe(Context context, long fid, boolean subscribe) { diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index 53b10d5132..b52a73552d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -334,7 +334,7 @@ public class FragmentFolders extends FragmentBase { if (enabled) ServiceSynchronize.reload(context, "refresh folders"); else - ServiceSynchronize.process(context); + ServiceSynchronize.process(context, true); } db.setTransactionSuccessful(); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 49efbbd7bc..7acb5644f3 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -101,6 +101,7 @@ public class ServiceSynchronize extends LifecycleService { private ExecutorService queue = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); private static boolean booted = false; + private static boolean sync = true; private static boolean oneshot = false; private static final int CONNECT_BACKOFF_START = 8; // seconds @@ -406,7 +407,7 @@ public class ServiceSynchronize extends LifecycleService { if (doStart) { if (clear) db.account().clearAccountConnected(); - start(); + start(!oneshot || sync); } } catch (Throwable ex) { @@ -442,7 +443,7 @@ public class ServiceSynchronize extends LifecycleService { return ((enabled && pollInterval == 0) || oneshot); } - private void start() { + private void start(final boolean sync) { EntityLog.log(this, "Main start"); state = new Core.State(networkState); @@ -486,7 +487,7 @@ public class ServiceSynchronize extends LifecycleService { @Override public void run() { try { - monitorAccount(account, astate); + monitorAccount(account, astate, sync); } catch (Throwable ex) { Log.e(account.name, ex); } @@ -549,7 +550,7 @@ public class ServiceSynchronize extends LifecycleService { stopSelf(); } - private void monitorAccount(final EntityAccount account, final Core.State state) throws NoSuchProviderException { + private void monitorAccount(final EntityAccount account, final Core.State state, final boolean sync) throws NoSuchProviderException { final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock wlAccount = pm.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":account." + account.id); @@ -988,7 +989,8 @@ public class ServiceSynchronize extends LifecycleService { idler.start(); idlers.add(idler); - EntityOperation.sync(this, folder.id, false); + if (sync) + EntityOperation.sync(this, folder.id, false); } else mapFolders.put(folder, null); @@ -1509,11 +1511,12 @@ public class ServiceSynchronize extends LifecycleService { .setAction("reset")); } - static void process(Context context) { + static void process(Context context, boolean sync) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean enabled = prefs.getBoolean("enabled", true); int pollInterval = prefs.getInt("poll_interval", 0); if (!enabled || pollInterval > 0) { + ServiceSynchronize.sync = sync; oneshot = true; ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class) diff --git a/app/src/main/java/eu/faircode/email/WorkerPoll.java b/app/src/main/java/eu/faircode/email/WorkerPoll.java index 6ff3aba64d..bc3a736207 100644 --- a/app/src/main/java/eu/faircode/email/WorkerPoll.java +++ b/app/src/main/java/eu/faircode/email/WorkerPoll.java @@ -42,7 +42,7 @@ public class WorkerPoll extends Worker { @Override public Result doWork() { Log.i("Running " + getName()); - ServiceSynchronize.process(getApplicationContext()); + ServiceSynchronize.process(getApplicationContext(), true); return Result.success(); }