2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Merge branch 'stable'

This commit is contained in:
Aleix Pol 2015-09-09 00:46:53 +02:00
commit 4fa0a89691
6 changed files with 75 additions and 22 deletions

View File

@ -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);
}

View File

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

View File

@ -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);
}
}

View File

@ -57,6 +57,25 @@ public class BackgroundService extends Service {
private final ConcurrentHashMap<String, Device> 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();
}

View File

@ -873,4 +873,9 @@ public class Device implements BaseLink.PackageReceiver {
pluginsChangedListeners.remove(listener);
}
public void disconnect() {
for(BaseLink link : links) {
link.disconnect();
}
}
}

View File

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