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

235 lines
8.4 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/>.
2020-01-05 18:32:53 +01:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
2019-06-09 13:11:33 +02:00
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
2019-01-06 17:44:03 +00:00
import androidx.annotation.NonNull;
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;
2019-06-09 13:11:33 +02:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.ArrayList;
2019-08-22 08:44:22 +02:00
import java.util.HashMap;
import java.util.List;
2019-08-22 08:44:22 +02:00
import java.util.Map;
import java.util.concurrent.ExecutorService;
2020-01-26 16:25:34 +01:00
import java.util.concurrent.Future;
// 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 {
2019-08-22 08:44:22 +02:00
private boolean log = true;
2019-06-13 11:59:35 +02:00
private boolean count = true;
private int executing = 0;
2020-01-26 16:25:34 +01:00
private String name;
private Future<?> future;
private static final List<SimpleTask> tasks = new ArrayList<>();
2019-10-10 13:26:44 +02:00
private static final ExecutorService executor =
Helper.getBackgroundExecutor(Runtime.getRuntime().availableProcessors(), "task");
2019-06-09 13:11:33 +02:00
static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT";
2019-08-22 08:44:22 +02:00
public SimpleTask<T> setLog(boolean log) {
this.log = log;
return this;
}
2019-06-13 11:59:35 +02:00
public SimpleTask<T> setCount(boolean count) {
this.count = count;
return this;
}
2019-03-14 16:19:56 +00:00
public void execute(Context context, LifecycleOwner owner, @NonNull Bundle args, @NonNull String name) {
2019-01-06 17:55:20 +00:00
run(context, owner, args, name);
}
2019-03-14 16:19:56 +00:00
public void execute(LifecycleService service, @NonNull Bundle args, @NonNull String name) {
2019-01-06 17:55:20 +00:00
run(service, service, args, name);
}
2019-03-14 16:19:56 +00:00
public void execute(AppCompatActivity activity, @NonNull Bundle args, @NonNull String name) {
2019-01-06 17:55:20 +00:00
run(activity, activity, args, name);
}
2019-03-14 16:19:56 +00:00
public void execute(final Fragment fragment, @NonNull Bundle args, @NonNull String name) {
2018-09-04 18:56:56 +00:00
try {
2020-03-24 14:25:39 +01:00
if (fragment.getView() != null)
run(fragment.getContext(), fragment.getViewLifecycleOwner(), args, name);
2018-09-04 18:56:56 +00:00
} catch (IllegalStateException ex) {
2020-03-24 14:25:39 +01:00
Log.e(ex);
2018-09-04 18:56:56 +00:00
}
}
2019-04-19 16:49:32 +02:00
private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) {
final Handler handler = new Handler();
2020-01-26 16:25:34 +01:00
this.name = name;
if (owner instanceof TwoStateOwner)
Log.e(new Throwable("SimpleTask/TwoStateOwner"));
// prevent garbage collection
synchronized (tasks) {
tasks.add(this);
2019-06-13 11:59:35 +02:00
if (count)
executing++;
}
2019-06-09 13:11:33 +02:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
2019-06-13 11:59:35 +02:00
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", executing));
2019-06-09 13:11:33 +02: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);
2019-09-22 15:01:59 +02:00
onException(args, ex);
2018-12-21 08:49:31 +01:00
}
2020-01-26 16:25:34 +01:00
future = executor.submit(new Runnable() {
2019-04-19 18:12:54 +02:00
private Object data;
private Throwable ex;
2019-04-19 16:49:32 +02:00
@Override
public void run() {
2019-04-19 18:12:54 +02:00
// Run in background thread
try {
2019-04-19 18:12:54 +02:00
data = onExecute(context, args);
} catch (Throwable ex) {
if (!(ex instanceof IllegalArgumentException))
Log.e(ex);
2019-04-19 18:12:54 +02:00
this.ex = ex;
}
2019-04-19 18:12:54 +02:00
// Run on UI thread
2018-12-20 20:47:15 +01:00
handler.post(new Runnable() {
@Override
public void run() {
2019-04-19 16:49:32 +02:00
Lifecycle.State state = owner.getLifecycle().getCurrentState();
2019-04-19 18:12:54 +02:00
if (state.equals(Lifecycle.State.DESTROYED)) {
// No delivery
2019-06-09 13:11:33 +02:00
cleanup(context);
2019-07-30 22:06:02 +02:00
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
2019-04-19 18:12:54 +02:00
// Inline delivery
2019-12-07 17:02:42 +01:00
Log.i("Deliver task " + name + " state=" + state);
2019-04-19 18:12:54 +02:00
deliver();
2019-06-09 13:11:33 +02:00
cleanup(context);
} else
2019-04-19 16:49:32 +02:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
2019-07-30 17:02:21 +02:00
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
Lifecycle.State state = owner.getLifecycle().getCurrentState();
if (state.equals(Lifecycle.State.DESTROYED)) {
Log.i("Destroyed task " + name);
owner.getLifecycle().removeObserver(this);
cleanup(context);
2019-07-30 22:06:02 +02:00
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
2019-07-30 17:02:21 +02:00
Log.i("Deferred delivery task " + name);
owner.getLifecycle().removeObserver(this);
deliver();
cleanup(context);
} else
2020-01-11 08:39:53 +01:00
Log.i("Deferring task " + name + " state=" + state);
2019-04-19 16:49:32 +02:00
}
});
}
2019-04-19 18:12:54 +02:00
private void deliver() {
try {
onPostExecute(args);
} catch (Throwable ex) {
Log.e(ex);
2019-09-22 15:01:59 +02:00
onException(args, ex);
2019-04-19 18:12:54 +02:00
} finally {
try {
2019-08-22 08:44:22 +02:00
if (ex == null) {
if (log && BuildConfig.BETA_RELEASE) {
Log.i("Crumb " + name);
Map<String, String> crumb = new HashMap<>();
crumb.put("name", name);
Log.breadcrumb("task", crumb);
}
2019-04-19 18:12:54 +02:00
onExecuted(args, (T) data);
2019-08-22 08:44:22 +02:00
} else
2019-04-19 18:12:54 +02:00
onException(args, ex);
} catch (Throwable ex) {
Log.e(ex);
2019-09-22 15:01:59 +02:00
onException(args, ex);
2019-04-19 18:12:54 +02:00
}
}
}
});
}
});
}
2020-01-26 16:25:34 +01:00
void cancel(Context context) {
if (future != null)
if (future.cancel(false)) {
Log.i("Cancelled task=" + name);
cleanup(context);
}
}
2019-06-09 13:11:33 +02:00
private void cleanup(Context context) {
2020-01-26 16:25:34 +01:00
future = null;
synchronized (tasks) {
tasks.remove(this);
2019-06-13 11:59:35 +02:00
if (count)
executing--;
}
2019-06-09 13:11:33 +02:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
2019-06-13 11:59:35 +02:00
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", executing));
Log.i("Remaining tasks=" + tasks.size());
}
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
}
2019-07-24 19:07:07 +02:00
static int getCount() {
return tasks.size();
}
}