From 8f2ba5450aafe5898656893c56e6e4957e0707c6 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 11 Jul 2020 17:22:45 +0200 Subject: [PATCH] Limit number of tasks when limiting query threads --- .../main/java/eu/faircode/email/ApplicationEx.java | 4 ++++ .../java/eu/faircode/email/FragmentOptionsMisc.java | 5 ++++- app/src/main/java/eu/faircode/email/SimpleTask.java | 13 ++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 49a0107163..7291ed8de0 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -289,6 +289,10 @@ public class ApplicationEx extends Application { editor.putString("subject_ellipsize", "middle"); if (!prefs.contains("auto_optimize")) editor.putBoolean("auto_optimize", false); + } else if (version < 1253) { + int threads = prefs.getInt("query_threads", 4); + if (threads == 4) + editor.remove("query_threads"); } if (version < BuildConfig.VERSION_CODE) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index ac68f15be6..759afc26ea 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -264,7 +264,10 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swQueries.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { - prefs.edit().putInt("query_threads", checked ? 2 : 4).commit(); // apply won't work here + if (checked) + prefs.edit().putInt("query_threads", 2).commit(); // apply won't work here + else + prefs.edit().remove("query_threads").commit(); // apply won't work here restart(); } }); diff --git a/app/src/main/java/eu/faircode/email/SimpleTask.java b/app/src/main/java/eu/faircode/email/SimpleTask.java index 6849cf3f32..7fd1dd5b44 100644 --- a/app/src/main/java/eu/faircode/email/SimpleTask.java +++ b/app/src/main/java/eu/faircode/email/SimpleTask.java @@ -21,6 +21,7 @@ package eu.faircode.email; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; @@ -33,6 +34,7 @@ import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleService; import androidx.lifecycle.OnLifecycleEvent; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.preference.PreferenceManager; import java.util.ArrayList; import java.util.Date; @@ -54,11 +56,9 @@ public abstract class SimpleTask implements LifecycleObserver { private String name; private Future future; + private static ExecutorService executor = null; private static final List tasks = new ArrayList<>(); - private static final ExecutorService executor = - Helper.getBackgroundExecutor(Runtime.getRuntime().availableProcessors(), "task"); - static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT"; public SimpleTask setLog(boolean log) { @@ -117,6 +117,13 @@ public abstract class SimpleTask implements LifecycleObserver { onException(args, ex); } + if (executor == null) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + int threads = prefs.getInt("query_threads", Runtime.getRuntime().availableProcessors()); + Log.i("Task threads=" + threads); + executor = Helper.getBackgroundExecutor(threads, "task"); + } + future = executor.submit(new Runnable() { private Object data; private long elapse;