diff --git a/KdeConnect/src/main/java/org/kde/connect/Announcer.java b/KdeConnect/src/main/java/org/kde/connect/Announcer.java deleted file mode 100644 index cd96c035..00000000 --- a/KdeConnect/src/main/java/org/kde/connect/Announcer.java +++ /dev/null @@ -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(); - -} diff --git a/KdeConnect/src/main/java/org/kde/connect/AvahiAnnouncer.java b/KdeConnect/src/main/java/org/kde/connect/Announcers/AvahiAnnouncer.java similarity index 86% rename from KdeConnect/src/main/java/org/kde/connect/AvahiAnnouncer.java rename to KdeConnect/src/main/java/org/kde/connect/Announcers/AvahiAnnouncer.java index e15310ca..6b9cf305 100644 --- a/KdeConnect/src/main/java/org/kde/connect/AvahiAnnouncer.java +++ b/KdeConnect/src/main/java/org/kde/connect/Announcers/AvahiAnnouncer.java @@ -1,21 +1,17 @@ -package org.kde.connect; +package org.kde.connect.Announcers; import android.content.Context; import android.net.nsd.NsdManager; import android.net.nsd.NsdServiceInfo; import android.util.Log; -import org.kde.connect.Announcer; -import org.kde.connect.ComputerLink; +import org.kde.connect.ComputerLinks.BaseComputerLink; +import org.kde.connect.ComputerLinks.UdpComputerLink; import org.kde.connect.Types.NetworkPackage; -import org.kde.connect.UdpComputerLink; import java.lang.Override; -/** - * Created by vaka on 6/10/13. - */ -public class AvahiAnnouncer implements Announcer { +public class AvahiAnnouncer extends BaseAnnouncer { UdpComputerLink computerLink; @@ -52,16 +48,19 @@ public class AvahiAnnouncer implements Announcer { } }; - ComputerLink.PackageReceiver mPackageReceiver = new ComputerLink.PackageReceiver() { + BaseComputerLink.PackageReceiver mPackageReceiver = new BaseComputerLink.PackageReceiver() { @Override public void onPackageReceived(NetworkPackage np) { + Log.e("Avahi announcer","packageReceived"); + connexionReceiver.onPair(new UdpComputerLink()); + /* if (np.getType() == NetworkPackage.Type.ID_REQUEST) { Log.i("Avahi announcer","ID_REQUEST"); } else if (np.getType() == NetworkPackage.Type.PAIR_REQUEST) { Log.i("Avahi announcer","PAIR_REQUEST"); } else { Log.i("Avahi announcer","Not paired, ignoring package "+np.getType()); - } + }*/ } }; @@ -84,6 +83,7 @@ public class AvahiAnnouncer implements Announcer { @Override public boolean startAnnouncing(ConnexionReceiver cr) { + super.startAnnouncing(cr); Log.i("AvahiAnnouncer","startAnnouncing"); mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener); computerLink.addPackageReceiver(mPackageReceiver); @@ -92,6 +92,7 @@ public class AvahiAnnouncer implements Announcer { @Override public void stopAnnouncing() { + super.stopAnnouncing(); mNsdManager.unregisterService(mRegistrationListener); computerLink.removePackageReceiver(mPackageReceiver); } diff --git a/KdeConnect/src/main/java/org/kde/connect/Announcers/BaseAnnouncer.java b/KdeConnect/src/main/java/org/kde/connect/Announcers/BaseAnnouncer.java new file mode 100644 index 00000000..2220f6e7 --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/Announcers/BaseAnnouncer.java @@ -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; + } + +} diff --git a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java index 93595cb5..c14047f3 100644 --- a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java +++ b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java @@ -7,6 +7,9 @@ import android.os.Binder; import android.os.IBinder; 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.CallPackageEmitter; import org.kde.connect.PackageEmitters.PingPackageEmitter; @@ -19,18 +22,22 @@ import java.util.ArrayList; public class BackgroundService extends Service { SharedPreferences settings; - ArrayList announcers = new ArrayList(); - ArrayList computerLinks = new ArrayList(); + + ArrayList mBaseAnnouncers = new ArrayList(); + ArrayList mBaseComputerLinks = new ArrayList(); ArrayList emitters = new ArrayList(); ArrayList receivers = new ArrayList(); 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()); p.setType(NetworkPackage.Type.PING); @@ -43,7 +50,6 @@ public class BackgroundService extends Service { emitters.add(new CallPackageEmitter(getApplicationContext())); } - pingEmitter = new PingPackageEmitter(getApplicationContext()); if (settings.getBoolean("emit_ping", true)) { 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 public int onStartCommand(Intent intent, int flags, int startId) { Log.e("BackgroundService","Starting"); - for (Announcer a : announcers) { - a.startAnnouncing(new Announcer.ConnexionReceiver() { + for (BaseAnnouncer a : mBaseAnnouncers) { + a.startAnnouncing(new BaseAnnouncer.ConnexionReceiver() { @Override - public void onPair(ComputerLink cl) { + public void onPair(BaseComputerLink cl) { addComputerLink(cl); } }); @@ -79,12 +91,13 @@ public class BackgroundService extends Service { public void onCreate() { Log.e("BackgroundService","Creating"); - registerEmitters(); - registerReceivers(); - settings = getSharedPreferences("KdeConnect", 0); - announcers.add(new AvahiAnnouncer(this)); + pingEmitter = new PingPackageEmitter(getApplicationContext()); + + registerEmitters(); + registerReceivers(); + registerAnnouncers(); super.onCreate(); } diff --git a/KdeConnect/src/main/java/org/kde/connect/ComputerLink.java b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/BaseComputerLink.java similarity index 79% rename from KdeConnect/src/main/java/org/kde/connect/ComputerLink.java rename to KdeConnect/src/main/java/org/kde/connect/ComputerLinks/BaseComputerLink.java index 6c305eb8..b88e9c24 100644 --- a/KdeConnect/src/main/java/org/kde/connect/ComputerLink.java +++ b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/BaseComputerLink.java @@ -1,11 +1,11 @@ -package org.kde.connect; +package org.kde.connect.ComputerLinks; import org.kde.connect.Types.NetworkPackage; import java.util.ArrayList; -public abstract class ComputerLink { +public abstract class BaseComputerLink { public interface PackageReceiver { public void onPackageReceived(NetworkPackage np); @@ -20,6 +20,7 @@ public abstract class ComputerLink { receivers.remove(pr); } + //Should be called from a background thread listening to packages protected void packageReceived(NetworkPackage np) { for(PackageReceiver pr : receivers) { pr.onPackageReceived(np); @@ -28,8 +29,6 @@ public abstract class ComputerLink { //TO OVERRIDE - //Should set up a listener that calls packageReceived(NetworkPackage) - public abstract void startListening(); //Should be async public abstract void sendPackage(NetworkPackage np); diff --git a/KdeConnect/src/main/java/org/kde/connect/UdpComputerLink.java b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java similarity index 55% rename from KdeConnect/src/main/java/org/kde/connect/UdpComputerLink.java rename to KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java index 128394f7..1913fe8f 100644 --- a/KdeConnect/src/main/java/org/kde/connect/UdpComputerLink.java +++ b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/UdpComputerLink.java @@ -1,6 +1,5 @@ -package org.kde.connect; +package org.kde.connect.ComputerLinks; -import android.net.nsd.NsdServiceInfo; import android.os.AsyncTask; import android.util.Log; @@ -10,28 +9,33 @@ import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; 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 String IP = "192.168.1.48"; final int BUFFER_SIZE = 5*1024; - @Override - public void startListening() { - try { - byte[] buffer = new byte[BUFFER_SIZE]; - DatagramSocket socket = new DatagramSocket(UDP_PORT); - while(true){ - DatagramPacket packet = new DatagramPacket(buffer, buffer.length ); - socket.receive(packet); - String s = new String(packet.getData(), 0, packet.getLength()); - packageReceived(NetworkPackage.fromString(s)); + public UdpComputerLink() { + new AsyncTask() { + @Override + protected Void doInBackground(Void... voids) { + try { + Log.e("UdpComputerLink","Waiting for udp datagrams"); + byte[] buffer = new byte[BUFFER_SIZE]; + DatagramSocket socket = new DatagramSocket(UDP_PORT); + while(true){ + DatagramPacket packet = new DatagramPacket(buffer, buffer.length ); + socket.receive(packet); + String s = new String(packet.getData(), 0, packet.getLength()); + packageReceived(NetworkPackage.fromString(s)); + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; } - } catch (IOException e) { - e.printStackTrace(); - } + }.execute(); } @Override diff --git a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java index b5e9ce72..be7949d2 100644 --- a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java +++ b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java @@ -7,6 +7,7 @@ import android.content.ServiceConnection; import android.net.nsd.NsdServiceInfo; import android.os.Bundle; import android.os.IBinder; +import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; @@ -23,6 +24,7 @@ public class MainActivity extends Activity { findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { + Log.i("MainActivity", "Button1"); Intent serviceIntent = new Intent(MainActivity.this, BackgroundService.class); startService(serviceIntent); @@ -43,6 +45,7 @@ public class MainActivity extends Activity { findViewById(R.id.button2).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { + Log.i("MainActivity","Button2"); if (service != null) service.sendPing(); } }); diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java index cbe0211e..51a73bc7 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BasePackageEmitter.java @@ -1,24 +1,24 @@ package org.kde.connect.PackageEmitters; -import org.kde.connect.ComputerLink; +import org.kde.connect.ComputerLinks.BaseComputerLink; import org.kde.connect.Types.NetworkPackage; import java.util.ArrayList; public class BasePackageEmitter { - private ArrayList computerLinks = new ArrayList(); + private ArrayList mBaseComputerLinks = new ArrayList(); - public void addComputerLink(ComputerLink cl) { - computerLinks.add(cl); + public void addComputerLink(BaseComputerLink cl) { + mBaseComputerLinks.add(cl); } protected int countLinkedComputers() { - return computerLinks.size(); + return mBaseComputerLinks.size(); } protected void sendPackage(NetworkPackage np) { - for(ComputerLink cl : computerLinks) { + for(BaseComputerLink cl : mBaseComputerLinks) { cl.sendPackage(np); } } diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java index a813708e..5aa6f2c8 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/CallPackageEmitter.java @@ -5,7 +5,6 @@ import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; -import org.kde.connect.ComputerLink; import org.kde.connect.Types.NetworkPackage; diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java index a3717cff..5a70f4e4 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PingPackageEmitter.java @@ -1,11 +1,8 @@ package org.kde.connect.PackageEmitters; import android.content.Context; -import android.telephony.PhoneStateListener; -import android.telephony.TelephonyManager; import android.util.Log; -import org.kde.connect.ComputerLink; import org.kde.connect.Types.NetworkPackage; @@ -17,7 +14,7 @@ public class PingPackageEmitter extends BasePackageEmitter { } public void sendPing() { - Log.i("PingPackageEmitter", "sendPing to "+countLinkedComputers()); + Log.e("PingPackageEmitter", "sendPing to "+countLinkedComputers()); NetworkPackage lastPackage = new NetworkPackage(System.currentTimeMillis()); diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java index db930c15..8dd1f7d0 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageReceivers/BasePackageReceiver.java @@ -1,10 +1,7 @@ package org.kde.connect.PackageReceivers; -import org.kde.connect.ComputerLink; import org.kde.connect.Types.NetworkPackage; -import java.util.ArrayList; - public interface BasePackageReceiver { public void receivePackage(NetworkPackage np);