From 3e1ac2bef9283e2d6e66e11c69091095364bc95a Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 19 Apr 2019 16:49:32 +0200 Subject: [PATCH] Simplify simple task --- .../java/eu/faircode/email/SimpleTask.java | 91 +++++-------------- 1 file changed, 24 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/SimpleTask.java b/app/src/main/java/eu/faircode/email/SimpleTask.java index 50f55a6a04..e628370771 100644 --- a/app/src/main/java/eu/faircode/email/SimpleTask.java +++ b/app/src/main/java/eu/faircode/email/SimpleTask.java @@ -42,12 +42,7 @@ import java.util.concurrent.Executors; // public abstract class SimpleTask implements LifecycleObserver { - private LifecycleOwner owner; - private boolean paused; - private boolean destroyed; - private Bundle args; - private String name; - private Result stored; + private Handler handler = new Handler(); private static ExecutorService executor = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcessors(), Helper.backgroundThreadFactory); @@ -72,57 +67,19 @@ public abstract class SimpleTask implements LifecycleObserver { } } - @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) - public void onResume() { - Log.i("Resume task " + this); - paused = false; - if (stored != null) { - Log.i("Deferred delivery task " + this); - deliver(args, stored); - stored = null; - } - } - - @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) - public void onPause() { - Log.i("Pause task " + this); - paused = true; - } - - @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) - public void onDestroyed() { - Log.i("Destroy task " + this); - owner.getLifecycle().removeObserver(this); - owner = null; - destroyed = true; - args = null; - stored = null; - } - - private void run(final Context context, LifecycleOwner owner, final Bundle args, String name) { - this.owner = owner; - this.paused = false; - this.destroyed = false; - this.args = null; - this.name = name; - this.stored = null; - - owner.getLifecycle().addObserver(this); - + private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) { try { onPreExecute(args); } catch (Throwable ex) { Log.e(ex); } - final Handler handler = new Handler(); - // Run in background thread executor.submit(new Runnable() { + private Result result = new Result(); + @Override public void run() { - final Result result = new Result(); - try { result.data = onExecute(context, args); } catch (Throwable ex) { @@ -134,25 +91,32 @@ public abstract class SimpleTask implements LifecycleObserver { handler.post(new Runnable() { @Override public void run() { - deliver(args, result); + Lifecycle.State state = owner.getLifecycle().getCurrentState(); + if (state.isAtLeast(Lifecycle.State.RESUMED)) { + Log.i("Deliver task " + name); + deliver(result, args); + } else if (!state.equals(Lifecycle.State.DESTROYED)) + owner.getLifecycle().addObserver(new LifecycleObserver() { + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + public void onResume() { + deliver(result, args); + Log.i("Resume task " + name); + owner.getLifecycle().removeObserver(this); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + public void onDestroyed() { + Log.i("Destroy task " + name); + owner.getLifecycle().removeObserver(this); + } + }); } }); } }); } - private void deliver(Bundle args, Result result) { - if (destroyed) - return; - - if (paused) { - Log.i("Deferring delivery task " + this); - this.args = args; - this.stored = result; - return; - } - - Log.i("Delivery task " + this); + private void deliver(Result result, Bundle args) { try { onPostExecute(args); } catch (Throwable ex) { @@ -166,7 +130,6 @@ public abstract class SimpleTask implements LifecycleObserver { } catch (Throwable ex) { Log.e(ex); } - onDestroyed(); } } @@ -187,10 +150,4 @@ public abstract class SimpleTask implements LifecycleObserver { Throwable ex; Object data; } - - @NonNull - @Override - public String toString() { - return (name == null ? super.toString() : name); - } }