mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 23:12:55 +00:00
Replace AsyncTask by HandlerThread
Saving accounts and identities take too long on the foreground
This commit is contained in:
parent
7492402332
commit
80307e6836
1 changed files with 24 additions and 10 deletions
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Run on main thread
|
||||||
|
new Handler(context.getMainLooper()).post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Result result) {
|
public void run() {
|
||||||
deliver(args, result);
|
deliver(args, result);
|
||||||
}
|
}
|
||||||
}.execute(args);
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deliver(Bundle args, Result result) {
|
private void deliver(Bundle args, Result result) {
|
||||||
|
|
Loading…
Reference in a new issue