mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 05:37:43 +00:00
Management for device disconnection and initial implementation of the device list
This commit is contained in:
parent
4930a019d4
commit
b9c013630b
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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<InetAddress, TcpComputerLink> visibleComputers = new HashMap<InetAddress, TcpComputerLink>();
|
||||
|
||||
Context ctx;
|
||||
private NsdManager.DiscoveryListener oldListener = null;
|
||||
|
||||
HashMap<String, TcpComputerLink> visibleComputers = new HashMap<String, TcpComputerLink>();
|
||||
|
||||
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;
|
||||
|
@ -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<ConnectionReceiver> connectionReceivers = new ArrayList<ConnectionReceiver>();
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
@ -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<String>(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<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();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -9,10 +9,9 @@
|
||||
tools:context=".MainActivity"
|
||||
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/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send ping"/>
|
||||
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Restart avahi"/>
|
||||
<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
|
||||
android:layout_width="fill_parent"
|
||||
|
Loading…
x
Reference in New Issue
Block a user