Simplification

pull/155/head
M66B 6 years ago
parent 47e47fac2a
commit 37de0f0112

@ -37,11 +37,9 @@ import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
//
// This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes // This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
// Make sure to not access any member in any outer scope from onExecute // Make sure to not access any member in any outer scope from onExecute
// Results will not be delivered to destroyed fragments // Results will not be delivered to destroyed fragments
//
public abstract class SimpleTask<T> implements LifecycleObserver { public abstract class SimpleTask<T> implements LifecycleObserver {
private static final List<SimpleTask> tasks = new ArrayList<>(); private static final List<SimpleTask> tasks = new ArrayList<>();
@ -83,70 +81,75 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
Log.e(ex); Log.e(ex);
} }
// Run in background thread
executor.submit(new Runnable() { executor.submit(new Runnable() {
private Result result = new Result(); private Object data;
private Throwable ex;
@Override @Override
public void run() { public void run() {
// Run in background thread
try { try {
result.data = onExecute(context, args); data = onExecute(context, args);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
result.ex = ex; this.ex = ex;
} }
// Run on main thread // Run on UI thread
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
Lifecycle.State state = owner.getLifecycle().getCurrentState(); Lifecycle.State state = owner.getLifecycle().getCurrentState();
if (state.equals(Lifecycle.State.DESTROYED)) if (state.equals(Lifecycle.State.DESTROYED)) {
// No delivery
cleanup(); cleanup();
else if (state.isAtLeast(Lifecycle.State.RESUMED)) { } else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
// Inline delivery
Log.i("Deliver task " + name); Log.i("Deliver task " + name);
deliver(result, args); deliver();
cleanup(); cleanup();
} else } else
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() { public void onResume() {
// Deferred delivery
Log.i("Resume task " + name); Log.i("Resume task " + name);
owner.getLifecycle().removeObserver(this); owner.getLifecycle().removeObserver(this);
deliver(result, args); deliver();
cleanup(); cleanup();
} }
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() { public void onDestroyed() {
// No delivery
Log.i("Destroy task " + name); Log.i("Destroy task " + name);
owner.getLifecycle().removeObserver(this); owner.getLifecycle().removeObserver(this);
cleanup(); cleanup();
} }
}); });
} }
private void deliver() {
try {
onPostExecute(args);
} catch (Throwable ex) {
Log.e(ex);
} finally {
try {
if (ex == null)
onExecuted(args, (T) data);
else
onException(args, ex);
} catch (Throwable ex) {
Log.e(ex);
}
}
}
}); });
} }
}); });
} }
private void deliver(Result result, Bundle args) {
try {
onPostExecute(args);
} catch (Throwable ex) {
Log.e(ex);
} finally {
try {
if (result.ex == null)
onExecuted(args, (T) result.data);
else
onException(args, result.ex);
} catch (Throwable ex) {
Log.e(ex);
}
}
}
private void cleanup() { private void cleanup() {
synchronized (tasks) { synchronized (tasks) {
tasks.remove(this); tasks.remove(this);
@ -166,9 +169,4 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
protected void onPostExecute(Bundle args) { protected void onPostExecute(Bundle args) {
} }
private static class Result {
private Throwable ex;
private Object data;
}
} }

Loading…
Cancel
Save