Fixed send thread

This commit is contained in:
M66B 2020-07-28 16:58:10 +02:00
parent 69c0c4c0f8
commit f30157ee6f
2 changed files with 42 additions and 1 deletions

View File

@ -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()) ||

View File

@ -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<Long> 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;