|
|
@ -26,19 +26,36 @@ import androidx.lifecycle.LifecycleOwner;
|
|
|
|
import androidx.lifecycle.LifecycleRegistry;
|
|
|
|
import androidx.lifecycle.LifecycleRegistry;
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
|
|
// This class can be used as an externally controlled standalone or child life cycle owner
|
|
|
|
// This class can be used as an externally controlled standalone or child life cycle owner
|
|
|
|
|
|
|
|
|
|
|
|
public class TwoStateOwner implements LifecycleOwner {
|
|
|
|
public class TwoStateOwner implements LifecycleOwner {
|
|
|
|
private String name;
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
private boolean owned = true;
|
|
|
|
private Object condition;
|
|
|
|
private Object condition;
|
|
|
|
private LifecycleRegistry registry;
|
|
|
|
private LifecycleRegistry registry;
|
|
|
|
|
|
|
|
private long created;
|
|
|
|
|
|
|
|
private long changed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static DateFormat DTF = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<TwoStateOwner> list = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static List<TwoStateOwner> getList() {
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
created = new Date().getTime();
|
|
|
|
create();
|
|
|
|
create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -49,23 +66,32 @@ public class TwoStateOwner implements LifecycleOwner {
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
public void onDestroyed() {
|
|
|
|
public void onDestroyed() {
|
|
|
|
Log.d("LifeCycle " + name + " parent destroyed");
|
|
|
|
Log.i(this + " parent destroyed");
|
|
|
|
|
|
|
|
owned = false;
|
|
|
|
destroy();
|
|
|
|
destroy();
|
|
|
|
|
|
|
|
owner.getLifecycle().removeObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void create() {
|
|
|
|
private void create() {
|
|
|
|
// Initialize
|
|
|
|
if (owned) {
|
|
|
|
registry = new LifecycleRegistry(this);
|
|
|
|
// Initialize
|
|
|
|
registry.addObserver(new LifecycleObserver() {
|
|
|
|
registry = new LifecycleRegistry(this);
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
registry.addObserver(new LifecycleObserver() {
|
|
|
|
public void onAny() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
Log.d("LifeCycle " + name + " state=" + registry.getCurrentState() + " " + registry);
|
|
|
|
public void onAny() {
|
|
|
|
}
|
|
|
|
Log.i(this + " " + registry);
|
|
|
|
});
|
|
|
|
changed = new Date().getTime();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
setState(Lifecycle.State.CREATED);
|
|
|
|
setState(Lifecycle.State.CREATED);
|
|
|
|
|
|
|
|
synchronized (list) {
|
|
|
|
|
|
|
|
list.add(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
|
|
|
Log.i(this + " not owned");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
void start() {
|
|
|
@ -103,6 +129,9 @@ public class TwoStateOwner implements LifecycleOwner {
|
|
|
|
if (!state.equals(Lifecycle.State.CREATED))
|
|
|
|
if (!state.equals(Lifecycle.State.CREATED))
|
|
|
|
setState(Lifecycle.State.CREATED);
|
|
|
|
setState(Lifecycle.State.CREATED);
|
|
|
|
setState(Lifecycle.State.DESTROYED);
|
|
|
|
setState(Lifecycle.State.DESTROYED);
|
|
|
|
|
|
|
|
synchronized (list) {
|
|
|
|
|
|
|
|
list.remove(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -131,4 +160,13 @@ public class TwoStateOwner implements LifecycleOwner {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
return "TwoStateOwner " + name +
|
|
|
|
|
|
|
|
" state=" + registry.getCurrentState() +
|
|
|
|
|
|
|
|
" owned=" + owned +
|
|
|
|
|
|
|
|
" created=" + DTF.format(created) +
|
|
|
|
|
|
|
|
" change=" + DTF.format(changed);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|