Added 2state debugging

This commit is contained in:
M66B 2021-09-22 18:06:50 +02:00
parent d86a5dcdc0
commit 0ff0b19b00
2 changed files with 51 additions and 12 deletions

View File

@ -2212,9 +2212,10 @@ public class Log {
long size = 0;
File file = attachment.getFile(context);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
for (SimpleTask task : SimpleTask.getList()) {
for (SimpleTask task : SimpleTask.getList())
size += write(os, String.format("%s\r\n", task.toString()));
}
for (TwoStateOwner owner : TwoStateOwner.getList())
size += write(os, String.format("%s\r\n", owner.toString()));
}
db.attachment().setDownloaded(attachment.id, size);

View File

@ -26,19 +26,36 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
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;
// This class can be used as an externally controlled standalone or child life cycle owner
public class TwoStateOwner implements LifecycleOwner {
private String name;
private boolean owned = true;
private Object condition;
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
TwoStateOwner(String aname) {
name = aname;
created = new Date().getTime();
create();
}
@ -49,23 +66,32 @@ public class TwoStateOwner implements LifecycleOwner {
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
Log.d("LifeCycle " + name + " parent destroyed");
Log.i(this + " parent destroyed");
owned = false;
destroy();
owner.getLifecycle().removeObserver(this);
}
});
}
private void create() {
// Initialize
registry = new LifecycleRegistry(this);
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
Log.d("LifeCycle " + name + " state=" + registry.getCurrentState() + " " + registry);
}
});
if (owned) {
// Initialize
registry = new LifecycleRegistry(this);
registry.addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
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() {
@ -103,6 +129,9 @@ public class TwoStateOwner implements LifecycleOwner {
if (!state.equals(Lifecycle.State.CREATED))
setState(Lifecycle.State.CREATED);
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);
}
}