2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 13:17:43 +00:00

KdeService was not compiling

This commit is contained in:
Albert Vaca 2013-06-18 03:05:32 +02:00
parent dd2a545e0d
commit c02b1e3af4
11 changed files with 93 additions and 75 deletions

View File

@ -1,18 +0,0 @@
package org.kde.connect;
import android.net.nsd.NsdServiceInfo;
/**
* Created by vaka on 6/10/13.
*/
public interface Announcer {
public interface ConnexionReceiver {
public void onPair(ComputerLink p);
}
public boolean startAnnouncing(ConnexionReceiver cr);
public void stopAnnouncing();
}

View File

@ -1,21 +1,17 @@
package org.kde.connect; package org.kde.connect.Announcers;
import android.content.Context; import android.content.Context;
import android.net.nsd.NsdManager; import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo; import android.net.nsd.NsdServiceInfo;
import android.util.Log; import android.util.Log;
import org.kde.connect.Announcer; import org.kde.connect.ComputerLinks.BaseComputerLink;
import org.kde.connect.ComputerLink; import org.kde.connect.ComputerLinks.UdpComputerLink;
import org.kde.connect.Types.NetworkPackage; import org.kde.connect.Types.NetworkPackage;
import org.kde.connect.UdpComputerLink;
import java.lang.Override; import java.lang.Override;
/** public class AvahiAnnouncer extends BaseAnnouncer {
* Created by vaka on 6/10/13.
*/
public class AvahiAnnouncer implements Announcer {
UdpComputerLink computerLink; UdpComputerLink computerLink;
@ -52,16 +48,19 @@ public class AvahiAnnouncer implements Announcer {
} }
}; };
ComputerLink.PackageReceiver mPackageReceiver = new ComputerLink.PackageReceiver() { BaseComputerLink.PackageReceiver mPackageReceiver = new BaseComputerLink.PackageReceiver() {
@Override @Override
public void onPackageReceived(NetworkPackage np) { public void onPackageReceived(NetworkPackage np) {
Log.e("Avahi announcer","packageReceived");
connexionReceiver.onPair(new UdpComputerLink());
/*
if (np.getType() == NetworkPackage.Type.ID_REQUEST) { if (np.getType() == NetworkPackage.Type.ID_REQUEST) {
Log.i("Avahi announcer","ID_REQUEST"); Log.i("Avahi announcer","ID_REQUEST");
} else if (np.getType() == NetworkPackage.Type.PAIR_REQUEST) { } else if (np.getType() == NetworkPackage.Type.PAIR_REQUEST) {
Log.i("Avahi announcer","PAIR_REQUEST"); Log.i("Avahi announcer","PAIR_REQUEST");
} else { } else {
Log.i("Avahi announcer","Not paired, ignoring package "+np.getType()); Log.i("Avahi announcer","Not paired, ignoring package "+np.getType());
} }*/
} }
}; };
@ -84,6 +83,7 @@ public class AvahiAnnouncer implements Announcer {
@Override @Override
public boolean startAnnouncing(ConnexionReceiver cr) { public boolean startAnnouncing(ConnexionReceiver cr) {
super.startAnnouncing(cr);
Log.i("AvahiAnnouncer","startAnnouncing"); Log.i("AvahiAnnouncer","startAnnouncing");
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener); mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
computerLink.addPackageReceiver(mPackageReceiver); computerLink.addPackageReceiver(mPackageReceiver);
@ -92,6 +92,7 @@ public class AvahiAnnouncer implements Announcer {
@Override @Override
public void stopAnnouncing() { public void stopAnnouncing() {
super.stopAnnouncing();
mNsdManager.unregisterService(mRegistrationListener); mNsdManager.unregisterService(mRegistrationListener);
computerLink.removePackageReceiver(mPackageReceiver); computerLink.removePackageReceiver(mPackageReceiver);
} }

View File

@ -0,0 +1,23 @@
package org.kde.connect.Announcers;
import org.kde.connect.ComputerLinks.BaseComputerLink;
public class BaseAnnouncer {
public interface ConnexionReceiver {
public void onPair(BaseComputerLink p);
}
protected ConnexionReceiver connexionReceiver;
//To override
public boolean startAnnouncing(ConnexionReceiver cr) {
connexionReceiver = cr;
return true;
}
public void stopAnnouncing() {
connexionReceiver = null;
}
}

View File

@ -7,6 +7,9 @@ import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import org.kde.connect.Announcers.BaseAnnouncer;
import org.kde.connect.Announcers.AvahiAnnouncer;
import org.kde.connect.ComputerLinks.BaseComputerLink;
import org.kde.connect.PackageEmitters.BasePackageEmitter; import org.kde.connect.PackageEmitters.BasePackageEmitter;
import org.kde.connect.PackageEmitters.CallPackageEmitter; import org.kde.connect.PackageEmitters.CallPackageEmitter;
import org.kde.connect.PackageEmitters.PingPackageEmitter; import org.kde.connect.PackageEmitters.PingPackageEmitter;
@ -19,18 +22,22 @@ import java.util.ArrayList;
public class BackgroundService extends Service { public class BackgroundService extends Service {
SharedPreferences settings; SharedPreferences settings;
ArrayList<Announcer> announcers = new ArrayList<Announcer>();
ArrayList<ComputerLink> computerLinks = new ArrayList<ComputerLink>(); ArrayList<BaseAnnouncer> mBaseAnnouncers = new ArrayList<BaseAnnouncer>();
ArrayList<BaseComputerLink> mBaseComputerLinks = new ArrayList<BaseComputerLink>();
ArrayList<BasePackageEmitter> emitters = new ArrayList<BasePackageEmitter>(); ArrayList<BasePackageEmitter> emitters = new ArrayList<BasePackageEmitter>();
ArrayList<BasePackageReceiver> receivers = new ArrayList<BasePackageReceiver>(); ArrayList<BasePackageReceiver> receivers = new ArrayList<BasePackageReceiver>();
PingPackageEmitter pingEmitter; PingPackageEmitter pingEmitter;
private void addComputerLink(ComputerLink cl) { private void addComputerLink(BaseComputerLink cl) {
computerLinks.add(cl); Log.i("BackgroundService","addComputerLink");
mBaseComputerLinks.add(cl);
Log.i("BackgroundService","sending ping after connection");
NetworkPackage p = new NetworkPackage(System.currentTimeMillis()); NetworkPackage p = new NetworkPackage(System.currentTimeMillis());
p.setType(NetworkPackage.Type.PING); p.setType(NetworkPackage.Type.PING);
@ -43,7 +50,6 @@ public class BackgroundService extends Service {
emitters.add(new CallPackageEmitter(getApplicationContext())); emitters.add(new CallPackageEmitter(getApplicationContext()));
} }
pingEmitter = new PingPackageEmitter(getApplicationContext());
if (settings.getBoolean("emit_ping", true)) { if (settings.getBoolean("emit_ping", true)) {
emitters.add(pingEmitter); emitters.add(pingEmitter);
} }
@ -55,14 +61,20 @@ public class BackgroundService extends Service {
} }
} }
public void registerAnnouncers() {
if (settings.getBoolean("announce_avahi", true)) {
mBaseAnnouncers.add(new AvahiAnnouncer(this));
}
}
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("BackgroundService","Starting"); Log.e("BackgroundService","Starting");
for (Announcer a : announcers) { for (BaseAnnouncer a : mBaseAnnouncers) {
a.startAnnouncing(new Announcer.ConnexionReceiver() { a.startAnnouncing(new BaseAnnouncer.ConnexionReceiver() {
@Override @Override
public void onPair(ComputerLink cl) { public void onPair(BaseComputerLink cl) {
addComputerLink(cl); addComputerLink(cl);
} }
}); });
@ -79,12 +91,13 @@ public class BackgroundService extends Service {
public void onCreate() { public void onCreate() {
Log.e("BackgroundService","Creating"); Log.e("BackgroundService","Creating");
registerEmitters();
registerReceivers();
settings = getSharedPreferences("KdeConnect", 0); settings = getSharedPreferences("KdeConnect", 0);
announcers.add(new AvahiAnnouncer(this)); pingEmitter = new PingPackageEmitter(getApplicationContext());
registerEmitters();
registerReceivers();
registerAnnouncers();
super.onCreate(); super.onCreate();
} }

View File

@ -1,11 +1,11 @@
package org.kde.connect; package org.kde.connect.ComputerLinks;
import org.kde.connect.Types.NetworkPackage; import org.kde.connect.Types.NetworkPackage;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class ComputerLink { public abstract class BaseComputerLink {
public interface PackageReceiver { public interface PackageReceiver {
public void onPackageReceived(NetworkPackage np); public void onPackageReceived(NetworkPackage np);
@ -20,6 +20,7 @@ public abstract class ComputerLink {
receivers.remove(pr); receivers.remove(pr);
} }
//Should be called from a background thread listening to packages
protected void packageReceived(NetworkPackage np) { protected void packageReceived(NetworkPackage np) {
for(PackageReceiver pr : receivers) { for(PackageReceiver pr : receivers) {
pr.onPackageReceived(np); pr.onPackageReceived(np);
@ -28,8 +29,6 @@ public abstract class ComputerLink {
//TO OVERRIDE //TO OVERRIDE
//Should set up a listener that calls packageReceived(NetworkPackage)
public abstract void startListening();
//Should be async //Should be async
public abstract void sendPackage(NetworkPackage np); public abstract void sendPackage(NetworkPackage np);

View File

@ -1,6 +1,5 @@
package org.kde.connect; package org.kde.connect.ComputerLinks;
import android.net.nsd.NsdServiceInfo;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
@ -10,17 +9,19 @@ import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import org.kde.connect.ComputerLink;
public class UdpComputerLink extends ComputerLink { public class UdpComputerLink extends BaseComputerLink {
final int UDP_PORT = 10601; final int UDP_PORT = 10601;
final String IP = "192.168.1.48"; final String IP = "192.168.1.48";
final int BUFFER_SIZE = 5*1024; final int BUFFER_SIZE = 5*1024;
public UdpComputerLink() {
new AsyncTask<Void,Void,Void>() {
@Override @Override
public void startListening() { protected Void doInBackground(Void... voids) {
try { try {
Log.e("UdpComputerLink","Waiting for udp datagrams");
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
DatagramSocket socket = new DatagramSocket(UDP_PORT); DatagramSocket socket = new DatagramSocket(UDP_PORT);
while(true){ while(true){
@ -32,6 +33,9 @@ public class UdpComputerLink extends ComputerLink {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
}
}.execute();
} }
@Override @Override

View File

@ -7,6 +7,7 @@ import android.content.ServiceConnection;
import android.net.nsd.NsdServiceInfo; import android.net.nsd.NsdServiceInfo;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -23,6 +24,7 @@ public class MainActivity extends Activity {
findViewById(R.id.button1).setOnClickListener(new OnClickListener() { findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Log.i("MainActivity", "Button1");
Intent serviceIntent = new Intent(MainActivity.this, BackgroundService.class); Intent serviceIntent = new Intent(MainActivity.this, BackgroundService.class);
startService(serviceIntent); startService(serviceIntent);
@ -43,6 +45,7 @@ public class MainActivity extends Activity {
findViewById(R.id.button2).setOnClickListener(new OnClickListener() { findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Log.i("MainActivity","Button2");
if (service != null) service.sendPing(); if (service != null) service.sendPing();
} }
}); });

View File

@ -1,24 +1,24 @@
package org.kde.connect.PackageEmitters; package org.kde.connect.PackageEmitters;
import org.kde.connect.ComputerLink; import org.kde.connect.ComputerLinks.BaseComputerLink;
import org.kde.connect.Types.NetworkPackage; import org.kde.connect.Types.NetworkPackage;
import java.util.ArrayList; import java.util.ArrayList;
public class BasePackageEmitter { public class BasePackageEmitter {
private ArrayList<ComputerLink> computerLinks = new ArrayList<ComputerLink>(); private ArrayList<BaseComputerLink> mBaseComputerLinks = new ArrayList<BaseComputerLink>();
public void addComputerLink(ComputerLink cl) { public void addComputerLink(BaseComputerLink cl) {
computerLinks.add(cl); mBaseComputerLinks.add(cl);
} }
protected int countLinkedComputers() { protected int countLinkedComputers() {
return computerLinks.size(); return mBaseComputerLinks.size();
} }
protected void sendPackage(NetworkPackage np) { protected void sendPackage(NetworkPackage np) {
for(ComputerLink cl : computerLinks) { for(BaseComputerLink cl : mBaseComputerLinks) {
cl.sendPackage(np); cl.sendPackage(np);
} }
} }

View File

@ -5,7 +5,6 @@ import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
import org.kde.connect.ComputerLink;
import org.kde.connect.Types.NetworkPackage; import org.kde.connect.Types.NetworkPackage;

View File

@ -1,11 +1,8 @@
package org.kde.connect.PackageEmitters; package org.kde.connect.PackageEmitters;
import android.content.Context; import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
import org.kde.connect.ComputerLink;
import org.kde.connect.Types.NetworkPackage; import org.kde.connect.Types.NetworkPackage;
@ -17,7 +14,7 @@ public class PingPackageEmitter extends BasePackageEmitter {
} }
public void sendPing() { public void sendPing() {
Log.i("PingPackageEmitter", "sendPing to "+countLinkedComputers()); Log.e("PingPackageEmitter", "sendPing to "+countLinkedComputers());
NetworkPackage lastPackage = new NetworkPackage(System.currentTimeMillis()); NetworkPackage lastPackage = new NetworkPackage(System.currentTimeMillis());

View File

@ -1,10 +1,7 @@
package org.kde.connect.PackageReceivers; package org.kde.connect.PackageReceivers;
import org.kde.connect.ComputerLink;
import org.kde.connect.Types.NetworkPackage; import org.kde.connect.Types.NetworkPackage;
import java.util.ArrayList;
public interface BasePackageReceiver { public interface BasePackageReceiver {
public void receivePackage(NetworkPackage np); public void receivePackage(NetworkPackage np);