2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 14:15:14 +00:00

Bind only the socket we use for UDP broadcast, not the process

This commit is contained in:
Albert Vaca Cintora
2023-08-30 23:12:01 +02:00
parent f9a3598edf
commit 37db0810aa
6 changed files with 37 additions and 26 deletions

View File

@@ -6,7 +6,10 @@
package org.kde.kdeconnect.Backends;
import android.net.Network;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -49,7 +52,7 @@ public abstract class BaseLinkProvider {
public abstract void onStart();
public abstract void onStop();
public abstract void onNetworkChange();
public abstract void onNetworkChange(@Nullable Network network);
public abstract String getName();
}

View File

@@ -14,10 +14,13 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Network;
import android.os.Parcelable;
import android.util.Base64;
import android.util.Log;
import androidx.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.kde.kdeconnect.Backends.BaseLinkProvider;
import org.kde.kdeconnect.Device;
@@ -107,7 +110,7 @@ public class BluetoothLinkProvider extends BaseLinkProvider {
}
@Override
public void onNetworkChange() {
public void onNetworkChange(@Nullable Network network) {
onStop();
onStart();
}

View File

@@ -8,9 +8,12 @@ package org.kde.kdeconnect.Backends.LanBackend;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Network;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import org.json.JSONException;
@@ -301,7 +304,7 @@ public class LanLinkProvider extends BaseLinkProvider {
});
} catch (IOException e) {
Log.e("LanLinkProvider", "UdpReceive exception", e);
onNetworkChange(); // Trigger a UDP broadcast to try to get them to connect to us instead
onNetworkChange(null); // Trigger a UDP broadcast to try to get them to connect to us instead
}
}
Log.w("UdpListener", "Stopping UDP listener");
@@ -355,7 +358,7 @@ public class LanLinkProvider extends BaseLinkProvider {
throw new RuntimeException("This should not be reachable");
}
private void broadcastUdpIdentityPacket() {
private void broadcastUdpIdentityPacket(@Nullable Network network) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (!preferences.getBoolean(SettingsFragment.KEY_UDP_BROADCAST_ENABLED, true)) {
Log.i("LanLinkProvider", "UDP broadcast is disabled in settings. Skipping.");
@@ -385,12 +388,12 @@ public class LanLinkProvider extends BaseLinkProvider {
return;
}
sendUdpIdentityPacket(ipList);
sendUdpIdentityPacket(ipList, network);
});
}
@WorkerThread
public void sendUdpIdentityPacket(List<InetAddress> ipList) {
public void sendUdpIdentityPacket(List<InetAddress> ipList, @Nullable Network network) {
if (tcpServer == null || !tcpServer.isBound()) {
Log.i("LanLinkProvider", "Won't broadcast UDP packet if TCP socket is not ready yet");
return;
@@ -411,6 +414,14 @@ public class LanLinkProvider extends BaseLinkProvider {
DatagramSocket socket;
try {
socket = new DatagramSocket();
if (network != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
try {
network.bindSocket(socket);
} catch (IOException e) {
Log.w("LanLinkProvider", "Couldn't bind socket to the network");
e.printStackTrace();
}
}
socket.setReuseAddress(true);
socket.setBroadcast(true);
} catch (SocketException e) {
@@ -443,19 +454,19 @@ public class LanLinkProvider extends BaseLinkProvider {
mdnsDiscovery.startDiscovering();
mdnsDiscovery.startAnnouncing();
broadcastUdpIdentityPacket();
broadcastUdpIdentityPacket(null);
}
}
@Override
public void onNetworkChange() {
public void onNetworkChange(@Nullable Network network) {
if (System.currentTimeMillis() < lastBroadcast + delayBetweenBroadcasts) {
Log.i("LanLinkProvider", "onNetworkChange: relax cowboy");
return;
}
lastBroadcast = System.currentTimeMillis();
broadcastUdpIdentityPacket();
broadcastUdpIdentityPacket(network);
mdnsDiscovery.stopDiscovering();
mdnsDiscovery.startDiscovering();
}

View File

@@ -216,7 +216,7 @@ public class MdnsDiscovery {
// Let the LanLinkProvider handle the connection
InetAddress remoteAddress = serviceInfo.getHost();
lanLinkProvider.sendUdpIdentityPacket(Collections.singletonList(remoteAddress));
lanLinkProvider.sendUdpIdentityPacket(Collections.singletonList(remoteAddress), null);
}
};
}

View File

@@ -7,6 +7,9 @@
package org.kde.kdeconnect.Backends.LoopbackBackend;
import android.content.Context;
import android.net.Network;
import androidx.annotation.Nullable;
import org.kde.kdeconnect.Backends.BaseLinkProvider;
@@ -20,7 +23,7 @@ public class LoopbackLinkProvider extends BaseLinkProvider {
@Override
public void onStart() {
onNetworkChange();
onNetworkChange(null);
}
@Override
@@ -28,7 +31,7 @@ public class LoopbackLinkProvider extends BaseLinkProvider {
}
@Override
public void onNetworkChange() {
public void onNetworkChange(@Nullable Network network) {
LoopbackLink link = new LoopbackLink(context, this);
onConnectionReceived(link);
}

View File

@@ -25,6 +25,7 @@ import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LiveData;
@@ -84,14 +85,14 @@ public class BackgroundService extends Service {
// linkProviders.add(new BluetoothLinkProvider(this));
}
public void onNetworkChange() {
public void onNetworkChange(@Nullable Network network) {
if (!initialized) {
Log.d("KDE/BackgroundService", "ignoring onNetworkChange called before the service is initialized");
return;
}
Log.d("KDE/BackgroundService", "onNetworkChange");
for (BaseLinkProvider a : linkProviders) {
a.onNetworkChange();
a.onNetworkChange(network);
}
}
@@ -130,18 +131,8 @@ public class BackgroundService extends Service {
cm.registerNetworkCallback(networkRequestBuilder.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cm.bindProcessToNetwork(network);
} else {
try {
ConnectivityManager.setProcessDefaultNetwork(network);
} catch (IllegalStateException e) {
Log.d("KDE/BackgroundService", "Failed to bind process to network", e);
return;
}
}
connectedToNonCellularNetwork.postValue(true);
onNetworkChange();
onNetworkChange(network);
}
@Override
public void onLost(Network network) {
@@ -285,7 +276,7 @@ public class BackgroundService extends Service {
}
}
if (intent != null && intent.getBooleanExtra("refresh", false)) {
onNetworkChange();
onNetworkChange(null);
}
return Service.START_STICKY;
}