|
|
|
@ -144,6 +144,14 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LifecycleObserver watcher = new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
cancel(context);
|
|
|
|
|
owner.getLifecycle().removeObserver(this);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
future = getExecutor(context).submit(new Runnable() {
|
|
|
|
|
private Object data;
|
|
|
|
|
private long elapsed;
|
|
|
|
@ -181,30 +189,34 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
|
if (state.equals(Lifecycle.State.DESTROYED)) {
|
|
|
|
|
// No delivery
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
|
|
|
|
|
// Inline delivery
|
|
|
|
|
Log.i("Deliver task " + name + " state=" + state + " elapse=" + elapsed + " ms");
|
|
|
|
|
deliver();
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else {
|
|
|
|
|
Log.i("Deferring task " + name + " state=" + state);
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
|
public void onAny() {
|
|
|
|
|
state = owner.getLifecycle().getCurrentState();
|
|
|
|
|
if (state.equals(Lifecycle.State.DESTROYED)) {
|
|
|
|
|
Log.i("Destroyed task " + name);
|
|
|
|
|
owner.getLifecycle().removeObserver(this);
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
|
|
|
|
|
Log.i("Deferred delivery task " + name);
|
|
|
|
|
owner.getLifecycle().removeObserver(this);
|
|
|
|
|
deliver();
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else
|
|
|
|
|
Log.i("Deferring task " + name + " state=" + state);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
owner.getLifecycle().removeObserver(watcher);
|
|
|
|
|
|
|
|
|
|
if (state.isAtLeast(Lifecycle.State.RESUMED)) {
|
|
|
|
|
// Inline delivery
|
|
|
|
|
Log.i("Deliver task " + name + " state=" + state + " elapse=" + elapsed + " ms");
|
|
|
|
|
deliver();
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else {
|
|
|
|
|
Log.i("Deferring task " + name + " state=" + state);
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
|
public void onAny() {
|
|
|
|
|
state = owner.getLifecycle().getCurrentState();
|
|
|
|
|
if (state.equals(Lifecycle.State.DESTROYED)) {
|
|
|
|
|
Log.i("Destroyed task " + name);
|
|
|
|
|
owner.getLifecycle().removeObserver(this);
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
|
|
|
|
|
Log.i("Deferred delivery task " + name);
|
|
|
|
|
owner.getLifecycle().removeObserver(this);
|
|
|
|
|
deliver();
|
|
|
|
|
cleanup(context);
|
|
|
|
|
} else
|
|
|
|
|
Log.i("Deferring task " + name + " state=" + state);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -249,6 +261,8 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(watcher);
|
|
|
|
|
|
|
|
|
|
updateTaskCount(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|