2019-03-06 16:36:20 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
2019-03-17 20:31:24 +00:00
|
|
|
import androidx.lifecycle.LifecycleObserver;
|
2019-03-06 16:36:20 +00:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
import androidx.lifecycle.LifecycleRegistry;
|
2019-03-17 20:31:24 +00:00
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
2019-03-06 16:36:20 +00:00
|
|
|
|
|
|
|
public class TwoStateOwner implements LifecycleOwner {
|
2019-03-27 13:24:58 +00:00
|
|
|
private String name;
|
2019-03-06 16:36:20 +00:00
|
|
|
private LifecycleRegistry registry;
|
|
|
|
|
2019-03-27 12:27:51 +00:00
|
|
|
// https://developer.android.com/topic/libraries/architecture/lifecycle#lc
|
|
|
|
|
2019-03-27 13:24:58 +00:00
|
|
|
TwoStateOwner(String aname) {
|
|
|
|
name = aname;
|
2019-04-13 06:02:01 +00:00
|
|
|
create();
|
2019-03-06 16:36:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 13:24:58 +00:00
|
|
|
TwoStateOwner(LifecycleOwner owner, String aname) {
|
|
|
|
this(aname);
|
2019-03-27 13:32:04 +00:00
|
|
|
|
|
|
|
// Destroy when parent destroyed
|
2019-03-17 20:31:24 +00:00
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
public void onDestroyed() {
|
2019-03-27 13:24:58 +00:00
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("LifeCycle " + name + " parent destroyed");
|
2019-03-25 19:20:23 +00:00
|
|
|
destroy();
|
2019-03-17 20:31:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-13 06:02:01 +00:00
|
|
|
private void create() {
|
|
|
|
// Initialize
|
|
|
|
registry = new LifecycleRegistry(this);
|
|
|
|
registry.addObserver(new LifecycleObserver() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
public void onAny() {
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("LifeCycle " + name + " state=" + registry.getCurrentState() + " " + registry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
registry.markState(Lifecycle.State.CREATED);
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
void start() {
|
2019-03-30 19:26:09 +00:00
|
|
|
registry.markState(Lifecycle.State.STARTED);
|
2019-03-06 16:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop() {
|
2019-03-30 19:26:09 +00:00
|
|
|
registry.markState(Lifecycle.State.CREATED);
|
2019-03-06 16:36:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 20:31:24 +00:00
|
|
|
void restart() {
|
2019-03-25 19:20:23 +00:00
|
|
|
stop();
|
2019-03-17 20:31:24 +00:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2019-04-13 06:02:01 +00:00
|
|
|
void recreate() {
|
|
|
|
destroy();
|
|
|
|
create();
|
|
|
|
}
|
|
|
|
|
2019-03-25 19:20:23 +00:00
|
|
|
void destroy() {
|
2019-03-27 13:32:04 +00:00
|
|
|
Lifecycle.State state = registry.getCurrentState();
|
2019-03-30 15:09:06 +00:00
|
|
|
if (!state.equals(Lifecycle.State.CREATED))
|
|
|
|
registry.markState(Lifecycle.State.CREATED);
|
2019-03-27 13:32:04 +00:00
|
|
|
if (!state.equals(Lifecycle.State.DESTROYED))
|
2019-03-30 15:09:06 +00:00
|
|
|
registry.markState(Lifecycle.State.DESTROYED);
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:36:20 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public Lifecycle getLifecycle() {
|
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
}
|