Added 2state debugging

pull/206/head
M66B 3 years ago
parent d86a5dcdc0
commit 0ff0b19b00

@ -2212,9 +2212,10 @@ public class Log {
long size = 0; long size = 0;
File file = attachment.getFile(context); File file = attachment.getFile(context);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { 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())); 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); db.attachment().setDownloaded(attachment.id, size);

@ -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);
}
} }
Loading…
Cancel
Save