From f40a9f96df96e732cfe543162b3fd37270798a5c Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 30 Mar 2019 06:39:26 +0000 Subject: [PATCH] Fixed lifecycle threading --- .../eu/faircode/email/ServiceSynchronize.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index e816f0b3f0..b5de9f38a4 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -508,7 +508,7 @@ public class ServiceSynchronize extends LifecycleService { final Map folders = new HashMap<>(); List idlers = new ArrayList<>(); - List owners = new ArrayList<>(); + final List cowners = new ArrayList<>(); try { // Listen for store events istore.addStoreListener(new StoreListener() { @@ -841,12 +841,12 @@ public class ServiceSynchronize extends LifecycleService { } else folders.put(folder, null); - final TwoStateOwner owner = new TwoStateOwner(ServiceSynchronize.this, folder.name); + final TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name); new Handler(getMainLooper()).post(new Runnable() { @Override public void run() { - db.operation().liveOperations(folder.id).observe(owner, new Observer>() { + db.operation().liveOperations(folder.id).observe(cowner, new Observer>() { private List handling = new ArrayList<>(); private final ExecutorService folderExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); private final PowerManager.WakeLock wlFolder = pm.newWakeLock( @@ -928,11 +928,11 @@ public class ServiceSynchronize extends LifecycleService { } } }); + cowner.start(); } }); - owner.start(); - owners.add(owner); + cowners.add(cowner); } // Keep alive alarm receiver @@ -1014,10 +1014,15 @@ public class ServiceSynchronize extends LifecycleService { db.account().setAccountError(account.id, Helper.formatThrowable(ex)); } finally { // Stop watching for operations - for (TwoStateOwner owner : owners) - owner.stop(); - owners.clear(); + new Handler(getMainLooper()).post(new Runnable() { + @Override + public void run() { + for (TwoStateOwner owner : cowners) + owner.stop(); + } + }); + // Update state EntityLog.log(this, account.name + " closing"); db.account().setAccountState(account.id, "closing"); for (EntityFolder folder : folders.keySet()) @@ -1041,6 +1046,7 @@ public class ServiceSynchronize extends LifecycleService { state.join(idler); idlers.clear(); + // Update state for (EntityFolder folder : folders.keySet()) if (folder.synchronize && !folder.poll) db.folder().setFolderState(folder.id, null);