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