diff --git a/src/org/kde/kdeconnect/Backends/BaseLink.java b/src/org/kde/kdeconnect/Backends/BaseLink.java index 4e389602..9bfcbdc0 100644 --- a/src/org/kde/kdeconnect/Backends/BaseLink.java +++ b/src/org/kde/kdeconnect/Backends/BaseLink.java @@ -30,14 +30,28 @@ import java.util.ArrayList; public abstract class BaseLink { + public enum ConnectionStarted { + Locally, Remotely; + }; + + public interface PackageReceiver { + void onPackageReceived(NetworkPackage np); + } + private final BaseLinkProvider linkProvider; private final String deviceId; private final ArrayList receivers = new ArrayList<>(); protected PrivateKey privateKey; - protected BaseLink(String deviceId, BaseLinkProvider linkProvider) { + protected ConnectionStarted connectionSource; // If the other device sent me a broadcast, + // I should not close the connection with it + // because it's probably trying to find me and + // potentially ask for pairing. + + protected BaseLink(String deviceId, BaseLinkProvider linkProvider, ConnectionStarted connectionSource) { this.linkProvider = linkProvider; this.deviceId = deviceId; + this.connectionSource = connectionSource; } public String getDeviceId() { @@ -52,9 +66,8 @@ public abstract class BaseLink { return linkProvider; } - - public interface PackageReceiver { - void onPackageReceived(NetworkPackage np); + public ConnectionStarted getConnectionSource() { + return connectionSource; } public void addPackageReceiver(PackageReceiver pr) { diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java index d1706670..d23f8cfd 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java @@ -57,8 +57,8 @@ public class LanLink extends BaseLink { session.close(true); } - public LanLink(IoSession session, String deviceId, BaseLinkProvider linkProvider) { - super(deviceId, linkProvider); + public LanLink(IoSession session, String deviceId, BaseLinkProvider linkProvider, ConnectionStarted connectionSource) { + super(deviceId, linkProvider, connectionSource); this.session = session; } diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java index 833a9d22..5af0fa64 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java @@ -37,6 +37,7 @@ import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.nio.NioDatagramAcceptor; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BaseLinkProvider; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.NetworkPackage; @@ -134,7 +135,7 @@ public class LanLinkProvider extends BaseLinkProvider { //Log.i("KDE/LanLinkProvider", "Identity package received from " + np.getString("deviceName")); - LanLink link = new LanLink(session, np.getString("deviceId"), LanLinkProvider.this); + LanLink link = new LanLink(session, np.getString("deviceId"), LanLinkProvider.this, BaseLink.ConnectionStarted.Locally); nioSessions.put(session.getId(),link); //Log.e("KDE/LanLinkProvider","nioSessions.size(): " + nioSessions.size()); addLink(np, link); @@ -195,7 +196,7 @@ public class LanLinkProvider extends BaseLinkProvider { final IoSession session = ioFuture.getSession(); Log.i("KDE/LanLinkProvider", "Connection successful: " + session.isConnected()); - final LanLink link = new LanLink(session, identityPackage.getString("deviceId"), LanLinkProvider.this); + final LanLink link = new LanLink(session, identityPackage.getString("deviceId"), LanLinkProvider.this, BaseLink.ConnectionStarted.Remotely); new Thread(new Runnable() { @Override public void run() { diff --git a/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java b/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java index add92f85..00d53b14 100644 --- a/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java +++ b/src/org/kde/kdeconnect/Backends/LoopbackBackend/LoopbackLink.java @@ -30,7 +30,7 @@ import java.security.PublicKey; public class LoopbackLink extends BaseLink { public LoopbackLink(BaseLinkProvider linkProvider) { - super("loopback", linkProvider); + super("loopback", linkProvider, ConnectionStarted.Remotely); } @Override diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index b3f658df..c99925a1 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -172,7 +172,7 @@ public class BackgroundService extends Service { Log.i("KDE/onConnectionLost", "removeLink, deviceId: " + link.getDeviceId()); if (d != null) { d.removeLink(link); - if (!d.isReachable() && !d.isPaired()) { + if (!d.isReachable() && !d.isPaired() && (link.getConnectionSource() == BaseLink.ConnectionStarted.Locally)) { //Log.e("onConnectionLost","Removing connection device because it was not paired"); devices.remove(link.getDeviceId()); d.removePairingCallback(devicePairingCallback);