Replace AsyncTask by HandlerThread

Saving accounts and identities take too long on the foreground
pull/50/head
M66B 6 years ago
parent 7492402332
commit 80307e6836

@ -20,8 +20,9 @@ package eu.faircode.email;
*/ */
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log; import android.util.Log;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -42,6 +43,15 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private Bundle args = null; private Bundle args = null;
private Result stored = null; private Result stored = null;
static HandlerThread handlerThread;
static Handler handler;
static {
handlerThread = new HandlerThread("SimpleTask");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
public void load(Context context, LifecycleOwner owner, Bundle args) { public void load(Context context, LifecycleOwner owner, Bundle args) {
run(context, owner, args); run(context, owner, args);
} }
@ -82,24 +92,28 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private void run(final Context context, LifecycleOwner owner, final Bundle args) { private void run(final Context context, LifecycleOwner owner, final Bundle args) {
owner.getLifecycle().addObserver(this); owner.getLifecycle().addObserver(this);
new AsyncTask<Object, Object, Result>() { // Run in background thread
handler.post(new Runnable() {
@Override @Override
protected Result doInBackground(Object... params) { public void run() {
Result result = new Result(); final Result result = new Result();
try { try {
result.data = onLoad(context, args); result.data = onLoad(context, args);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
result.ex = ex; result.ex = ex;
} }
return result;
}
@Override // Run on main thread
protected void onPostExecute(Result result) { new Handler(context.getMainLooper()).post(new Runnable() {
deliver(args, result); @Override
public void run() {
deliver(args, result);
}
});
} }
}.execute(args); });
} }
private void deliver(Bundle args, Result result) { private void deliver(Bundle args, Result result) {

Loading…
Cancel
Save