From f30157ee6fe1aa1875f34db4c90a12a7a13981ab Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 28 Jul 2020 16:58:10 +0200 Subject: [PATCH] Fixed send thread --- .../eu/faircode/email/ConnectionHelper.java | 25 +++++++++++++++++++ .../java/eu/faircode/email/ServiceSend.java | 18 ++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 57f0bb6a09..c0cafca692 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -304,6 +304,31 @@ public class ConnectionHelper { return true; } + static Network getActiveNetwork(Context context) { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm == null) + return null; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + return cm.getActiveNetwork(); + + NetworkInfo ani = cm.getActiveNetworkInfo(); + if (ani == null) + return null; + + Network[] networks = cm.getAllNetworks(); + for (Network network : networks) { + NetworkInfo ni = cm.getNetworkInfo(network); + if (ni == null) + continue; + if (ni.getType() == ani.getType() && + ni.getSubtype() == ani.getSubtype()) + return network; + } + + return null; + } + static boolean isIoError(Throwable ex) { while (ex != null) { if (isMaxConnections(ex.getMessage()) || diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index 0576c6f2da..56d1e24299 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -31,6 +31,8 @@ import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkRequest; +import android.os.Handler; +import android.os.Looper; import android.os.PowerManager; import android.text.TextUtils; @@ -65,12 +67,12 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND; public class ServiceSend extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener { private TupleUnsent lastUnsent = null; - private Network lastActive = null; private boolean lastSuitable = false; private PowerManager.WakeLock wlOutbox; private TwoStateOwner owner = new TwoStateOwner("send"); private List handling = new ArrayList<>(); + private static ExecutorService executor = Helper.getBackgroundExecutor(1, "send"); private static final int PI_SEND = 1; @@ -274,6 +276,20 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar }; private void checkConnectivity() { + if (Looper.myLooper() == Looper.getMainLooper()) + _checkConnectivity(); + else { + Log.e(new Throwable("Not on main thread")); + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + _checkConnectivity(); + } + }); + } + } + + private void _checkConnectivity() { boolean suitable = ConnectionHelper.getNetworkState(this).isSuitable(); if (lastSuitable != suitable) { lastSuitable = suitable;