Files
FairEmail/app/src/main/java/eu/faircode/email/SimpleTask.java

196 lines
5.5 KiB
Java
Raw Normal View History

package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
2018-09-02 08:22:04 +00:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.OnLifecycleEvent;
//
// This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
2018-12-31 07:03:48 +00:00
// Make sure to not access any member in any outer scope from onExecute
2018-08-16 11:23:30 +00:00
// Results will not be delivered to destroyed fragments
//
public abstract class SimpleTask<T> implements LifecycleObserver {
2018-08-16 11:23:30 +00:00
private LifecycleOwner owner;
2018-09-14 05:26:21 +00:00
private boolean paused;
private Bundle args;
private Result stored;
2018-11-04 12:54:57 +00:00
private static ExecutorService executor = Executors.newCachedThreadPool(Helper.backgroundThreadFactory);
2018-12-31 07:03:48 +00:00
public void execute(Context context, LifecycleOwner owner, Bundle args) {
run(context, owner, args);
}
2018-12-31 07:03:48 +00:00
public void execute(LifecycleService service, Bundle args) {
run(service, service, args);
}
2018-12-31 07:03:48 +00:00
public void execute(AppCompatActivity activity, Bundle args) {
run(activity, activity, args);
}
2018-12-31 07:03:48 +00:00
public void execute(final Fragment fragment, Bundle args) {
2018-09-04 18:56:56 +00:00
try {
run(fragment.getContext(), fragment.getViewLifecycleOwner(), args);
} catch (IllegalStateException ex) {
2018-12-24 12:27:45 +00:00
Log.w(ex);
2018-09-04 18:56:56 +00:00
}
}
2018-08-16 11:23:30 +00:00
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
2018-12-24 12:27:45 +00:00
Log.i("Start task " + this);
2018-08-16 11:23:30 +00:00
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
2018-12-24 12:27:45 +00:00
Log.i("Stop task " + this);
2018-08-16 11:23:30 +00:00
}
2018-08-12 17:36:41 +00:00
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
2018-12-24 12:27:45 +00:00
Log.i("Resume task " + this);
2018-08-12 17:36:41 +00:00
paused = false;
if (stored != null) {
2018-12-24 12:27:45 +00:00
Log.i("Deferred delivery task " + this);
2018-08-12 17:36:41 +00:00
deliver(args, stored);
stored = null;
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
2018-12-24 12:27:45 +00:00
Log.i("Pause task " + this);
2018-08-12 17:36:41 +00:00
paused = true;
}
2018-08-16 11:23:30 +00:00
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreated() {
2018-12-24 12:27:45 +00:00
Log.i("Created task " + this);
2018-08-16 11:23:30 +00:00
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2018-12-24 12:27:45 +00:00
Log.i("Destroy task " + this);
2018-08-16 11:23:30 +00:00
owner.getLifecycle().removeObserver(this);
owner = null;
2018-08-12 17:36:41 +00:00
paused = true;
args = null;
stored = null;
}
private void run(final Context context, LifecycleOwner owner, final Bundle args) {
2018-08-16 11:23:30 +00:00
this.owner = owner;
2018-09-14 05:26:21 +00:00
this.paused = false;
this.args = null;
this.stored = null;
2018-12-20 20:47:15 +01:00
2018-12-21 08:49:31 +01:00
try {
2018-12-31 07:03:48 +00:00
onPreExecute(args);
2018-12-21 08:49:31 +01:00
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-21 08:49:31 +01:00
}
owner.getLifecycle().addObserver(this);
2018-12-20 20:47:15 +01:00
final Handler handler = new Handler();
// Run in background thread
2018-09-02 08:22:04 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
final Result result = new Result();
try {
2018-12-31 07:03:48 +00:00
result.data = onExecute(context, args);
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
result.ex = ex;
}
// Run on main thread
2018-12-20 20:47:15 +01:00
handler.post(new Runnable() {
@Override
public void run() {
deliver(args, result);
}
});
}
});
}
2018-08-12 17:36:41 +00:00
private void deliver(Bundle args, Result result) {
if (paused) {
2018-12-24 12:27:45 +00:00
Log.i("Deferring delivery task " + this);
2018-08-12 17:36:41 +00:00
this.args = args;
this.stored = result;
} else {
2018-12-24 12:27:45 +00:00
Log.i("Delivery task " + this);
2018-08-16 11:23:30 +00:00
try {
2018-12-31 07:03:48 +00:00
onPostExecute(args);
2018-08-16 11:23:30 +00:00
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-08-16 11:23:30 +00:00
} finally {
2018-12-21 08:49:31 +01:00
try {
2018-12-31 07:03:48 +00:00
if (result.ex == null)
onExecuted(args, (T) result.data);
else
onException(args, result.ex);
2018-12-21 08:49:31 +01:00
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-21 08:49:31 +01:00
}
2018-08-16 11:23:30 +00:00
onDestroyed();
}
2018-08-12 17:36:41 +00:00
}
}
2018-12-31 07:03:48 +00:00
protected void onPreExecute(Bundle args) {
2018-12-21 08:49:31 +01:00
}
2018-12-31 07:03:48 +00:00
protected abstract T onExecute(Context context, Bundle args) throws Throwable;
2018-12-31 07:03:48 +00:00
protected void onExecuted(Bundle args, T data) {
}
2018-12-11 11:31:31 +01:00
protected abstract void onException(Bundle args, Throwable ex);
2018-12-31 07:03:48 +00:00
protected void onPostExecute(Bundle args) {
2018-12-21 08:49:31 +01:00
}
private static class Result {
Throwable ex;
Object data;
}
}