Added SimpleTask.isAlive

This commit is contained in:
M66B
2022-04-24 10:37:47 +02:00
parent 491b3cec18
commit 842bf19dd2
3 changed files with 23 additions and 23 deletions

View File

@@ -49,10 +49,10 @@ import java.util.concurrent.Future;
public abstract class SimpleTask<T> implements LifecycleObserver {
private boolean log = true;
private boolean count = true;
private boolean optional = false;
private String name;
private long started;
private boolean destroyed;
private boolean reported;
private Lifecycle.State state;
private Future<?> future;
@@ -79,11 +79,6 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
return this;
}
public SimpleTask<T> setOptional(boolean optional) {
this.optional = optional;
return this;
}
public SimpleTask<T> setExecutor(ExecutorService executor) {
this.localExecutor = executor;
return this;
@@ -153,8 +148,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
LifecycleObserver watcher = new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
EntityLog.log(context, EntityLog.Type.Debug, "Cancelling task=" + name);
cancel(context);
destroyed = true;
owner.getLifecycle().removeObserver(this);
}
};
@@ -197,8 +191,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
// No delivery
cleanup(context);
} else {
if (optional)
owner.getLifecycle().removeObserver(watcher);
owner.getLifecycle().removeObserver(watcher);
if (state.isAtLeast(Lifecycle.State.RESUMED)) {
// Inline delivery
@@ -269,12 +262,15 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
}
});
if (optional)
owner.getLifecycle().addObserver(watcher);
owner.getLifecycle().addObserver(watcher);
updateTaskCount(context);
}
public boolean isAlive() {
return !this.destroyed;
}
void cancel(Context context) {
if (future != null && future.cancel(false)) {
Log.i("Cancelled task=" + name);