Run lifecycle events on main thread

pull/153/head
M66B 6 years ago
parent 09f35a1230
commit cddfb344da

@ -828,6 +828,7 @@ public class ServiceSynchronize extends LifecycleService {
folders.put(folder, null); folders.put(folder, null);
final TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name); final TwoStateOwner cowner = new TwoStateOwner(ServiceSynchronize.this, folder.name);
cowner.start();
new Handler(getMainLooper()).post(new Runnable() { new Handler(getMainLooper()).post(new Runnable() {
@Override @Override
@ -914,7 +915,6 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
}); });
cowner.start();
} }
}); });
@ -1000,13 +1000,8 @@ 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
new Handler(getMainLooper()).post(new Runnable() { for (TwoStateOwner owner : cowners)
@Override owner.destroy();
public void run() {
for (TwoStateOwner owner : cowners)
owner.stop();
}
});
// Update state // Update state
EntityLog.log(this, account.name + " closing"); EntityLog.log(this, account.name + " closing");

@ -1,5 +1,8 @@
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;
@ -10,15 +13,17 @@ 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
TwoStateOwner(String aname) { TwoStateOwner(String aname) {
name = aname; name = aname;
// Initialize registry // Initialize
registry = new LifecycleRegistry(this); registry = new LifecycleRegistry(this);
registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); handler = new Handler(Looper.getMainLooper());
transition(Lifecycle.State.CREATED);
// Logging // Logging
registry.addObserver(new LifecycleObserver() { registry.addObserver(new LifecycleObserver() {
@ -45,11 +50,11 @@ public class TwoStateOwner implements LifecycleOwner {
} }
void start() { void start() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_START); transition(Lifecycle.State.STARTED);
} }
void stop() { void stop() {
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); transition(Lifecycle.State.CREATED);
} }
void restart() { void restart() {
@ -58,13 +63,35 @@ 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.INITIALIZED)) if (!state.equals(Lifecycle.State.CREATED))
registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); registry.markState(Lifecycle.State.CREATED);
if (state.equals(Lifecycle.State.STARTED))
registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); // transition to created
if (!state.equals(Lifecycle.State.DESTROYED)) if (!state.equals(Lifecycle.State.DESTROYED))
registry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); 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

Loading…
Cancel
Save