mirror of https://github.com/M66B/FairEmail.git
Fixed lifecycle concurrency
This commit is contained in:
parent
6071606a63
commit
4089e802c4
|
@ -491,6 +491,9 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
while (state.running()) {
|
||||
Log.i(account.name + " run");
|
||||
|
||||
Handler handler = new Handler(getMainLooper());
|
||||
final List<TwoStateOwner> cowners = new ArrayList<>();
|
||||
|
||||
// Debug
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
|
||||
|
@ -508,7 +511,6 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
|
||||
final Map<EntityFolder, IMAPFolder> folders = new HashMap<>();
|
||||
List<Thread> idlers = new ArrayList<>();
|
||||
final List<TwoStateOwner> cowners = new ArrayList<>();
|
||||
try {
|
||||
// Listen for store events
|
||||
istore.addStoreListener(new StoreListener() {
|
||||
|
@ -829,12 +831,13 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
} else
|
||||
folders.put(folder, null);
|
||||
|
||||
final TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name);
|
||||
cowner.start();
|
||||
|
||||
new Handler(getMainLooper()).post(new Runnable() {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name);
|
||||
cowners.add(cowner);
|
||||
cowner.start();
|
||||
|
||||
db.operation().liveOperations(folder.id).observe(cowner, new Observer<List<EntityOperation>>() {
|
||||
private List<Long> handling = new ArrayList<>();
|
||||
private final ExecutorService folderExecutor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
||||
|
@ -919,8 +922,6 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
});
|
||||
}
|
||||
});
|
||||
|
||||
cowners.add(cowner);
|
||||
}
|
||||
|
||||
// Keep alive alarm receiver
|
||||
|
@ -1002,8 +1003,13 @@ public class ServiceSynchronize extends LifecycleService {
|
|||
db.account().setAccountError(account.id, Helper.formatThrowable(ex));
|
||||
} finally {
|
||||
// Stop watching for operations
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (TwoStateOwner owner : cowners)
|
||||
owner.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
// Update state
|
||||
EntityLog.log(this, account.name + " closing");
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
package eu.faircode.email;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
|
@ -13,7 +10,6 @@ import androidx.lifecycle.OnLifecycleEvent;
|
|||
public class TwoStateOwner implements LifecycleOwner {
|
||||
private String name;
|
||||
private LifecycleRegistry registry;
|
||||
private Handler handler;
|
||||
|
||||
// https://developer.android.com/topic/libraries/architecture/lifecycle#lc
|
||||
|
||||
|
@ -22,8 +18,7 @@ public class TwoStateOwner implements LifecycleOwner {
|
|||
|
||||
// Initialize
|
||||
registry = new LifecycleRegistry(this);
|
||||
handler = new Handler(Looper.getMainLooper());
|
||||
transition(Lifecycle.State.CREATED);
|
||||
registry.markState(Lifecycle.State.CREATED);
|
||||
|
||||
// Logging
|
||||
registry.addObserver(new LifecycleObserver() {
|
||||
|
@ -50,11 +45,11 @@ public class TwoStateOwner implements LifecycleOwner {
|
|||
}
|
||||
|
||||
void start() {
|
||||
transition(Lifecycle.State.STARTED);
|
||||
registry.markState(Lifecycle.State.STARTED);
|
||||
}
|
||||
|
||||
void stop() {
|
||||
transition(Lifecycle.State.CREATED);
|
||||
registry.markState(Lifecycle.State.CREATED);
|
||||
}
|
||||
|
||||
void restart() {
|
||||
|
@ -63,18 +58,6 @@ public class TwoStateOwner implements LifecycleOwner {
|
|||
}
|
||||
|
||||
void destroy() {
|
||||
if (Looper.myLooper() == Looper.getMainLooper())
|
||||
_destroy();
|
||||
else
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
_destroy();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _destroy() {
|
||||
Lifecycle.State state = registry.getCurrentState();
|
||||
if (!state.equals(Lifecycle.State.CREATED))
|
||||
registry.markState(Lifecycle.State.CREATED);
|
||||
|
@ -82,18 +65,6 @@ public class TwoStateOwner implements LifecycleOwner {
|
|||
registry.markState(Lifecycle.State.DESTROYED);
|
||||
}
|
||||
|
||||
private void transition(final Lifecycle.State state) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper())
|
||||
registry.markState(state);
|
||||
else
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
registry.markState(state);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Lifecycle getLifecycle() {
|
||||
|
|
Loading…
Reference in New Issue