diff --git a/src/org/kde/kdeconnect/Backends/BaseLink.java b/src/org/kde/kdeconnect/Backends/BaseLink.java index 53407f07..4e389602 100644 --- a/src/org/kde/kdeconnect/Backends/BaseLink.java +++ b/src/org/kde/kdeconnect/Backends/BaseLink.java @@ -71,8 +71,11 @@ public abstract class BaseLink { } } + public void disconnect() { + linkProvider.connectionLost(this); + } + //TO OVERRIDE, should be sync public abstract void sendPackage(NetworkPackage np,Device.SendPackageStatusCallback callback); public abstract void sendPackageEncrypted(NetworkPackage np,Device.SendPackageStatusCallback callback, PublicKey key); - } diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java index b39d9631..039a3eda 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java @@ -43,7 +43,13 @@ public class LanLink extends BaseLink { private IoSession session = null; + @Override public void disconnect() { + closeSocket(); + super.disconnect(); + } + + public void closeSocket() { if (session == null) { Log.e("KDE/LanLink", "Not yet connected"); return; diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java index 0cb1ee3c..7f43c984 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java @@ -80,7 +80,7 @@ public class LanLinkProvider extends BaseLinkProvider { nioSessions.remove(id); //Log.i("KDE/LanLinkProvider", "nioSessions.size(): " + nioSessions.size() + " (-)"); try { - brokenLink.disconnect(); + brokenLink.closeSocket(); } catch (Exception e) { e.printStackTrace(); Log.e("KDE/LanLinkProvider", "Exception. Already disconnected?"); @@ -244,7 +244,7 @@ public class LanLinkProvider extends BaseLinkProvider { connectionAccepted(identityPackage, link); if (oldLink != null) { Log.i("KDE/LanLinkProvider","Removing old connection to same device"); - oldLink.disconnect(); + oldLink.closeSocket(); connectionLost(oldLink); } } diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java index 7b126633..a5e8d047 100644 --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -57,6 +57,25 @@ public class BackgroundService extends Service { private final ConcurrentHashMap devices = new ConcurrentHashMap<>(); + private boolean discoveryEnabled = false; + + public void setDiscoveryEnabled(boolean b) { + if (discoveryEnabled == b) + return; + + discoveryEnabled = b; + + if (b) { + onNetworkChange(); + } else { + cleanDevices(); + } + } + + public boolean isDiscoveryEnabled() { + return discoveryEnabled; + } + private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() { @Override public void incomingRequest() { @@ -114,6 +133,12 @@ public class BackgroundService extends Service { return devices.get(id); } + private void cleanDevices() { + for(Device d : devices.values()) { + d.disconnect(); + } + } + private final BaseLinkProvider.ConnectionReceiver deviceListener = new BaseLinkProvider.ConnectionReceiver() { @Override public void onConnectionReceived(final NetworkPackage identityPackage, final BaseLink link) { @@ -130,8 +155,10 @@ public class BackgroundService extends Service { } else { Log.i("KDE/BackgroundService", "addLink,unknown device: " + deviceId); device = new Device(BackgroundService.this, identityPackage, link); - devices.put(deviceId, device); - device.addPairingCallback(devicePairingCallback); + if (isDiscoveryEnabled() || device.isPaired()) { + devices.put(deviceId, device); + device.addPairingCallback(devicePairingCallback); + } } onDeviceListChanged(); @@ -159,20 +186,6 @@ public class BackgroundService extends Service { return devices; } - public void startDiscovery() { - Log.i("KDE/BackgroundService","StartDiscovery"); - for (BaseLinkProvider a : linkProviders) { - a.onStart(); - } - } - - public void stopDiscovery() { - Log.i("KDE/BackgroundService","StopDiscovery"); - for (BaseLinkProvider a : linkProviders) { - a.onStop(); - } - } - public void onNetworkChange() { Log.i("KDE/BackgroundService","OnNetworkChange"); for (BaseLinkProvider a : linkProviders) { @@ -210,7 +223,7 @@ public class BackgroundService extends Service { IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); registerReceiver(new KdeConnectBroadcastReceiver(), filter); - Log.i("KDE/BackgroundService","Service not started yet, initializing..."); + Log.i("KDE/BackgroundService", "Service not started yet, initializing..."); initializeRsaKeys(); loadRememberedDevicesFromSettings(); @@ -218,7 +231,11 @@ public class BackgroundService extends Service { //Link Providers need to be already registered addConnectionListener(deviceListener); - startDiscovery(); + + Log.i("KDE/BackgroundService", "StartDiscovery"); + for (BaseLinkProvider a : linkProviders) { + a.onStart(); + } } @@ -280,7 +297,10 @@ public class BackgroundService extends Service { @Override public void onDestroy() { Log.i("KDE/BackgroundService", "Destroying"); - stopDiscovery(); + Log.i("KDE/BackgroundService", "StopDiscovery"); + for (BaseLinkProvider a : linkProviders) { + a.onStop(); + } super.onDestroy(); } diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index ba2a1fdc..5fb1bc87 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -873,4 +873,9 @@ public class Device implements BaseLink.PackageReceiver { pluginsChangedListeners.remove(listener); } + public void disconnect() { + for(BaseLink link : links) { + link.disconnect(); + } + } } diff --git a/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java b/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java index 92baf4ad..84a37aad 100644 --- a/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/MaterialActivity.java @@ -187,9 +187,28 @@ public class MaterialActivity extends AppCompatActivity { @Override protected void onResume() { super.onResume(); + + BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + service.setDiscoveryEnabled(true); + } + }); + updateComputerList(); } + @Override + public void onPause() { + super.onPause(); + BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + service.setDiscoveryEnabled(false); + } + }); + } + //TODO: Make it accept two parameters, a constant with the type of screen and the device id in //case the screen is for a device, or even three parameters and the third one be the plugin id? //This way we can keep adding more options with null plugin id (eg: about)