diff --git a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java index 35625460..0116725a 100644 --- a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java +++ b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java @@ -149,10 +149,11 @@ public class BackgroundService extends Service { @Override public void onConnectionLost(BaseComputerLink link) { + Log.e("BackgroundService","onConnectionLost"); Device d = devices.get(link.getDeviceId()); if (d != null) { d.removeLink(link); - //if (d.countLinkedDevices() == 0) devices.remove(link.getDeviceId); + if (d.countLinkedDevices() == 0) devices.remove(link.getDeviceId()); } } @@ -177,10 +178,24 @@ public class BackgroundService extends Service { return Service.START_STICKY; } - private void startDiscovery() { - Log.i("StartDiscovery","Registering connection receivers"); + public void startDiscovery() { + Log.i("BackgroundService","StartDiscovery"); for (BaseLinkProvider a : linkProviders) { - a.reachComputers(deviceListener); + a.onStart(); + } + } + + public void stopDiscovery() { + Log.i("BackgroundService","StopDiscovery"); + for (BaseLinkProvider a : linkProviders) { + a.onStop(); + } + } + + public void addConnectionListener(BaseLinkProvider.ConnectionReceiver cr) { + Log.i("BackgroundService","Registering connection listener"); + for (BaseLinkProvider a : linkProviders) { + a.addConnectionReceiver(cr); } } @@ -193,12 +208,16 @@ public class BackgroundService extends Service { registerPackageInterfacesFromSettings(); registerLinkProviders(); + + //Link Providers need to be already registered + addConnectionListener(deviceListener); startDiscovery(); } public void restart() { devices.clear(); + stopDiscovery(); startDiscovery(); } @@ -234,5 +253,4 @@ public class BackgroundService extends Service { c.startService(serviceIntent); } - } diff --git a/KdeConnect/src/main/java/org/kde/connect/Device.java b/KdeConnect/src/main/java/org/kde/connect/Device.java index 9b70dc7c..3c24782b 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Device.java +++ b/KdeConnect/src/main/java/org/kde/connect/Device.java @@ -53,12 +53,13 @@ public class Device { }); } - - public void removeLink(BaseComputerLink link) { links.remove(link); } + public int countLinkedDevices() { + return links.size(); + } //Send and receive interfaces diff --git a/KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiTcpLinkProvider.java b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiTcpLinkProvider.java index e5677374..a0a27de5 100644 --- a/KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiTcpLinkProvider.java +++ b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/AvahiTcpLinkProvider.java @@ -17,28 +17,25 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -public class AvahiTcpLinkProvider implements BaseLinkProvider { +public class AvahiTcpLinkProvider extends BaseLinkProvider { String serviceType = "_kdeconnect._tcp"; + Context context; NsdManager mNsdManager; - - HashMap visibleComputers = new HashMap(); - - Context ctx; private NsdManager.DiscoveryListener oldListener = null; + HashMap visibleComputers = new HashMap(); + public AvahiTcpLinkProvider(Context context) { + this.context = context; mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE); - ctx = context; } @Override - public void reachComputers(final ConnectionReceiver cr) { + public void onStart() { - visibleComputers.clear(); - - if (oldListener != null) mNsdManager.stopServiceDiscovery(oldListener); + if (oldListener != null) return; Log.e("AvahiTcpLinkProvider", "Discovering computers..."); @@ -50,7 +47,7 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider { } @Override - public void onServiceResolved(NsdServiceInfo serviceInfo) { + public void onServiceResolved(final NsdServiceInfo serviceInfo) { Log.e("AvahiTcpLinkProvider", "Resolve Succeeded. " + serviceInfo); try { @@ -69,13 +66,13 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider { String name = np.getString("deviceName"); link.setDeviceId(id); - link.sendPackage(NetworkPackage.createIdentityPackage(ctx)); - if (visibleComputers.containsKey(host)) { + link.sendPackage(NetworkPackage.createIdentityPackage(context)); + if (visibleComputers.containsKey(serviceInfo.toString())) { //Remove old connection to same host, probably down - cr.onConnectionLost(visibleComputers.get(host)); + connectionLost(visibleComputers.get(serviceInfo.getServiceName())); } - visibleComputers.put(host,link); - cr.onConnectionAccepted(id,name,link); + visibleComputers.put(serviceInfo.getServiceName(),link); + connectionAccepted(id,name,link); link.removePackageReceiver(this); } @@ -113,10 +110,11 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider { } @Override - public void onServiceLost(NsdServiceInfo service) { - Log.e("AvahiTcpLinkProvider", "service lost" + service); - TcpComputerLink link = visibleComputers.remove(service.getHost()); - if (link != null) cr.onConnectionLost(link); + public void onServiceLost(NsdServiceInfo serviceInfo) { + Log.e("AvahiTcpLinkProvider", "Service lost: " + serviceInfo.getServiceName()); + TcpComputerLink link = visibleComputers.remove(serviceInfo.getServiceName()); + if (link != null) connectionLost(link); + else Log.e("AvahiTcpLinkProvider","Host unknown! (?)"); } @Override @@ -142,6 +140,15 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider { } + @Override + public void onStop() { + + if (oldListener != null) mNsdManager.stopServiceDiscovery(oldListener); + oldListener = null; + visibleComputers.clear(); + + } + @Override public int getPriority() { return 101; diff --git a/KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java index d27e8fb6..8044a3b9 100644 --- a/KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java +++ b/KdeConnect/src/main/java/org/kde/connect/LinkProviders/BaseLinkProvider.java @@ -1,17 +1,47 @@ package org.kde.connect.LinkProviders; +import android.util.Log; + import org.kde.connect.ComputerLinks.BaseComputerLink; -public interface BaseLinkProvider { +import java.util.ArrayList; + +public abstract class BaseLinkProvider { + + ArrayList connectionReceivers = new ArrayList(); public interface ConnectionReceiver { public void onConnectionAccepted(String deviceId, String name, BaseComputerLink link); public void onConnectionLost(BaseComputerLink link); } + public void addConnectionReceiver(ConnectionReceiver cr) { + connectionReceivers.add(cr); + } + + public boolean removeConnectionReceiver(ConnectionReceiver cr) { + return connectionReceivers.remove(cr); + } + + //These two should be called when the provider links to a new computer + protected void connectionAccepted(String deviceId, String name, BaseComputerLink link) { + Log.e("LinkProvider", "connectionAccepted"); + for(ConnectionReceiver cr : connectionReceivers) { + cr.onConnectionAccepted(deviceId,name,link); + } + } + protected void connectionLost(BaseComputerLink link) { + Log.e("LinkProvider", "connectionLost"); + for(ConnectionReceiver cr : connectionReceivers) { + cr.onConnectionLost(link); + } + } + //To override - public void reachComputers(ConnectionReceiver cr); - public int getPriority(); - public String getName(); + public abstract void onStart(); + public abstract void onStop(); + + public abstract int getPriority(); + public abstract String getName(); } diff --git a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java index c5a9c7f5..2ed64c57 100644 --- a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java +++ b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java @@ -3,6 +3,7 @@ package org.kde.connect; import android.app.Activity; import android.content.Intent; import android.os.Bundle; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -10,6 +11,8 @@ import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; +import org.kde.connect.ComputerLinks.BaseComputerLink; +import org.kde.connect.LinkProviders.BaseLinkProvider; import org.kde.connect.PackageInterfaces.PingPackageInterface; import org.kde.kdeconnect.R; @@ -46,7 +49,7 @@ public class MainActivity extends Activity { @Override public void onServiceStart(BackgroundService service) { PingPackageInterface pi = (PingPackageInterface) service.getPackageInterface(PingPackageInterface.class); - pi.sendPing(); + if (pi != null) pi.sendPing(); } }); @@ -67,23 +70,35 @@ public class MainActivity extends Activity { } }); - findViewById(R.id.button4).setOnClickListener(new OnClickListener() { + BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() { @Override - public void onClick(View view) { - BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() { + public void onServiceStart(final BackgroundService service) { + + final Runnable updateComputerList = new Runnable() { @Override - public void onServiceStart(final BackgroundService service) { - runOnUiThread(new Runnable() { - @Override - public void run() { - String[] listContent = service.getVisibleDevices().toArray(new String[0]); - ListView list = (ListView)findViewById(R.id.listView1); - list.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, listContent)); - } - }); + public void run() { + Log.e("MainActivity","updateComputerList"); + String[] listContent = service.getVisibleDevices().toArray(new String[0]); + ListView list = (ListView)findViewById(R.id.listView1); + list.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, listContent)); + } + }; + + service.addConnectionListener(new BaseLinkProvider.ConnectionReceiver() { + @Override + public void onConnectionAccepted(String deviceId, String name, BaseComputerLink link) { + runOnUiThread(updateComputerList); + } + + @Override + public void onConnectionLost(BaseComputerLink link) { + Log.e("MainActivity","onConnectionLost"); + runOnUiThread(updateComputerList); } }); + updateComputerList.run(); + } }); } diff --git a/KdeConnect/src/main/res/layout/activity_main.xml b/KdeConnect/src/main/res/layout/activity_main.xml index b8e10079..3480c962 100644 --- a/KdeConnect/src/main/res/layout/activity_main.xml +++ b/KdeConnect/src/main/res/layout/activity_main.xml @@ -9,10 +9,9 @@ tools:context=".MainActivity" android:orientation="vertical"> -