2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 21:55:10 +00:00

Management for device disconnection and initial implementation of the device list

This commit is contained in:
Albert Vaca
2013-07-31 19:10:51 +02:00
parent 4930a019d4
commit b9c013630b
6 changed files with 117 additions and 47 deletions

View File

@@ -149,10 +149,11 @@ public class BackgroundService extends Service {
@Override @Override
public void onConnectionLost(BaseComputerLink link) { public void onConnectionLost(BaseComputerLink link) {
Log.e("BackgroundService","onConnectionLost");
Device d = devices.get(link.getDeviceId()); Device d = devices.get(link.getDeviceId());
if (d != null) { if (d != null) {
d.removeLink(link); 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; return Service.START_STICKY;
} }
private void startDiscovery() { public void startDiscovery() {
Log.i("StartDiscovery","Registering connection receivers"); Log.i("BackgroundService","StartDiscovery");
for (BaseLinkProvider a : linkProviders) { 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(); registerPackageInterfacesFromSettings();
registerLinkProviders(); registerLinkProviders();
//Link Providers need to be already registered
addConnectionListener(deviceListener);
startDiscovery(); startDiscovery();
} }
public void restart() { public void restart() {
devices.clear(); devices.clear();
stopDiscovery();
startDiscovery(); startDiscovery();
} }
@@ -234,5 +253,4 @@ public class BackgroundService extends Service {
c.startService(serviceIntent); c.startService(serviceIntent);
} }
} }

View File

@@ -53,12 +53,13 @@ public class Device {
}); });
} }
public void removeLink(BaseComputerLink link) { public void removeLink(BaseComputerLink link) {
links.remove(link); links.remove(link);
} }
public int countLinkedDevices() {
return links.size();
}
//Send and receive interfaces //Send and receive interfaces

View File

@@ -17,28 +17,25 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class AvahiTcpLinkProvider implements BaseLinkProvider { public class AvahiTcpLinkProvider extends BaseLinkProvider {
String serviceType = "_kdeconnect._tcp"; String serviceType = "_kdeconnect._tcp";
Context context;
NsdManager mNsdManager; NsdManager mNsdManager;
HashMap<InetAddress, TcpComputerLink> visibleComputers = new HashMap<InetAddress, TcpComputerLink>();
Context ctx;
private NsdManager.DiscoveryListener oldListener = null; private NsdManager.DiscoveryListener oldListener = null;
HashMap<String, TcpComputerLink> visibleComputers = new HashMap<String, TcpComputerLink>();
public AvahiTcpLinkProvider(Context context) { public AvahiTcpLinkProvider(Context context) {
this.context = context;
mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE); mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);
ctx = context;
} }
@Override @Override
public void reachComputers(final ConnectionReceiver cr) { public void onStart() {
visibleComputers.clear(); if (oldListener != null) return;
if (oldListener != null) mNsdManager.stopServiceDiscovery(oldListener);
Log.e("AvahiTcpLinkProvider", "Discovering computers..."); Log.e("AvahiTcpLinkProvider", "Discovering computers...");
@@ -50,7 +47,7 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider {
} }
@Override @Override
public void onServiceResolved(NsdServiceInfo serviceInfo) { public void onServiceResolved(final NsdServiceInfo serviceInfo) {
Log.e("AvahiTcpLinkProvider", "Resolve Succeeded. " + serviceInfo); Log.e("AvahiTcpLinkProvider", "Resolve Succeeded. " + serviceInfo);
try { try {
@@ -69,13 +66,13 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider {
String name = np.getString("deviceName"); String name = np.getString("deviceName");
link.setDeviceId(id); link.setDeviceId(id);
link.sendPackage(NetworkPackage.createIdentityPackage(ctx)); link.sendPackage(NetworkPackage.createIdentityPackage(context));
if (visibleComputers.containsKey(host)) { if (visibleComputers.containsKey(serviceInfo.toString())) {
//Remove old connection to same host, probably down //Remove old connection to same host, probably down
cr.onConnectionLost(visibleComputers.get(host)); connectionLost(visibleComputers.get(serviceInfo.getServiceName()));
} }
visibleComputers.put(host,link); visibleComputers.put(serviceInfo.getServiceName(),link);
cr.onConnectionAccepted(id,name,link); connectionAccepted(id,name,link);
link.removePackageReceiver(this); link.removePackageReceiver(this);
} }
@@ -113,10 +110,11 @@ public class AvahiTcpLinkProvider implements BaseLinkProvider {
} }
@Override @Override
public void onServiceLost(NsdServiceInfo service) { public void onServiceLost(NsdServiceInfo serviceInfo) {
Log.e("AvahiTcpLinkProvider", "service lost" + service); Log.e("AvahiTcpLinkProvider", "Service lost: " + serviceInfo.getServiceName());
TcpComputerLink link = visibleComputers.remove(service.getHost()); TcpComputerLink link = visibleComputers.remove(serviceInfo.getServiceName());
if (link != null) cr.onConnectionLost(link); if (link != null) connectionLost(link);
else Log.e("AvahiTcpLinkProvider","Host unknown! (?)");
} }
@Override @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 @Override
public int getPriority() { public int getPriority() {
return 101; return 101;

View File

@@ -1,17 +1,47 @@
package org.kde.connect.LinkProviders; package org.kde.connect.LinkProviders;
import android.util.Log;
import org.kde.connect.ComputerLinks.BaseComputerLink; import org.kde.connect.ComputerLinks.BaseComputerLink;
public interface BaseLinkProvider { import java.util.ArrayList;
public abstract class BaseLinkProvider {
ArrayList<ConnectionReceiver> connectionReceivers = new ArrayList<ConnectionReceiver>();
public interface ConnectionReceiver { public interface ConnectionReceiver {
public void onConnectionAccepted(String deviceId, String name, BaseComputerLink link); public void onConnectionAccepted(String deviceId, String name, BaseComputerLink link);
public void onConnectionLost(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 //To override
public void reachComputers(ConnectionReceiver cr); public abstract void onStart();
public int getPriority(); public abstract void onStop();
public String getName();
public abstract int getPriority();
public abstract String getName();
} }

View File

@@ -3,6 +3,7 @@ package org.kde.connect;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@@ -10,6 +11,8 @@ import android.view.View.OnClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; 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.connect.PackageInterfaces.PingPackageInterface;
import org.kde.kdeconnect.R; import org.kde.kdeconnect.R;
@@ -46,7 +49,7 @@ public class MainActivity extends Activity {
@Override @Override
public void onServiceStart(BackgroundService service) { public void onServiceStart(BackgroundService service) {
PingPackageInterface pi = (PingPackageInterface) service.getPackageInterface(PingPackageInterface.class); 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 @Override
public void onClick(View view) { public void onServiceStart(final BackgroundService service) {
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
final Runnable updateComputerList = new Runnable() {
@Override @Override
public void onServiceStart(final BackgroundService service) { public void run() {
runOnUiThread(new Runnable() { Log.e("MainActivity","updateComputerList");
@Override String[] listContent = service.getVisibleDevices().toArray(new String[0]);
public void run() { ListView list = (ListView)findViewById(R.id.listView1);
String[] listContent = service.getVisibleDevices().toArray(new String[0]); list.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listContent));
ListView list = (ListView)findViewById(R.id.listView1); }
list.setAdapter(new ArrayAdapter<String>(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();
} }
}); });
} }

View File

@@ -9,10 +9,9 @@
tools:context=".MainActivity" tools:context=".MainActivity"
android:orientation="vertical"> android:orientation="vertical">
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Restart service"/> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Restart avahi"/>
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send ping"/>
<Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mpris controls"/> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mpris controls"/>
<Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="List paired computers"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send ping to all"/>
<ListView <ListView
android:layout_width="fill_parent" android:layout_width="fill_parent"