diff --git a/AndroidClient.iml b/Android.iml similarity index 85% rename from AndroidClient.iml rename to Android.iml index 5561ede6..286bd256 100644 --- a/AndroidClient.iml +++ b/Android.iml @@ -1,5 +1,5 @@ - + diff --git a/KdeConnect/src/main/AndroidManifest.xml b/KdeConnect/src/main/AndroidManifest.xml index 247f95f3..79f9355a 100644 --- a/KdeConnect/src/main/AndroidManifest.xml +++ b/KdeConnect/src/main/AndroidManifest.xml @@ -4,9 +4,8 @@ android:versionCode="1" android:versionName="1.0" > - + announcers = new ArrayList(); + ArrayList computerLinks = new ArrayList(); ArrayList receivers = new ArrayList(); - DesktopCommunication dc = new DesktopCommunication(); + + private void addComputerLink(ComputerLink cl) { + + computerLinks.add(cl); + + if (settings.getBoolean("listenCalls", true)) { + receivers.add(new PhoneCallReceiver(getApplicationContext(), cl)); + } + + NetworkPackage p = new NetworkPackage(System.currentTimeMillis()); + p.setType(NetworkPackage.Type.PING); + cl.sendPackage(p); + + } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("BackgroundService","Starting"); - SharedPreferences settings = getSharedPreferences("KdeConnect", 0); - - if (settings.getBoolean("listenCalls", true)) { - receivers.add(new PhoneCallReceiver(getApplicationContext(), dc)); + for (Announcer a : announcers) { + a.startAnnouncing(new Announcer.ConnexionReceiver() { + @Override + public void onPair(ComputerLink cl) { + addComputerLink(cl); + } + }); } - NetworkPackage p = new NetworkPackage(System.currentTimeMillis()); - p.setType(NetworkPackage.Type.PING); - dc.asyncSend(p.toString()); - return Service.START_STICKY; } @Override public void onCreate() { Log.e("BackgroundService","Creating"); + + settings = getSharedPreferences("KdeConnect", 0); + + announcers.add(new AvahiAnnouncer(this)); + super.onCreate(); } diff --git a/KdeConnect/src/main/java/org/kde/connect/ComputerLink.java b/KdeConnect/src/main/java/org/kde/connect/ComputerLink.java new file mode 100644 index 00000000..6c305eb8 --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/ComputerLink.java @@ -0,0 +1,38 @@ +package org.kde.connect; + +import org.kde.connect.Types.NetworkPackage; + +import java.util.ArrayList; + + +public abstract class ComputerLink { + + public interface PackageReceiver { + public void onPackageReceived(NetworkPackage np); + } + + ArrayList receivers = new ArrayList(); + + public void addPackageReceiver(PackageReceiver pr) { + receivers.add(pr); + } + public void removePackageReceiver(PackageReceiver pr) { + receivers.remove(pr); + } + + protected void packageReceived(NetworkPackage np) { + for(PackageReceiver pr : receivers) { + pr.onPackageReceived(np); + } + } + + //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/MainActivity.java b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java index d94e277f..37177865 100644 --- a/KdeConnect/src/main/java/org/kde/connect/MainActivity.java +++ b/KdeConnect/src/main/java/org/kde/connect/MainActivity.java @@ -10,20 +10,6 @@ import android.view.View.OnClickListener; public class MainActivity extends Activity { - //http://developer.android.com/training/connect-devices-wirelessly/nsd.html - public void registerService(int port) { - // Create the NsdServiceInfo object, and populate it. - NsdServiceInfo serviceInfo = new NsdServiceInfo(); - - // The name is subject to change based on conflicts - // with other services advertised on the same network. - serviceInfo.setServiceName("NsdChat"); - serviceInfo.setServiceType("_http._tcp."); - serviceInfo.setPort(port); - //.... - } - - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java new file mode 100644 index 00000000..ccb6ff01 --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/BaseReceiver.java @@ -0,0 +1,6 @@ +package org.kde.connect.PackageEmitters; + +public interface BaseReceiver { + + +} diff --git a/KdeConnect/src/main/java/org/kde/connect/Receivers/PhoneCallReceiver.java b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PhoneCallReceiver.java similarity index 84% rename from KdeConnect/src/main/java/org/kde/connect/Receivers/PhoneCallReceiver.java rename to KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PhoneCallReceiver.java index 998cba80..a4056073 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Receivers/PhoneCallReceiver.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageEmitters/PhoneCallReceiver.java @@ -1,17 +1,17 @@ -package org.kde.connect.Receivers; +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.DesktopCommunication; +import org.kde.connect.ComputerLink; import org.kde.connect.Types.NetworkPackage; public class PhoneCallReceiver implements BaseReceiver { - public PhoneCallReceiver(final Context ctx, final DesktopCommunication dc) { + public PhoneCallReceiver(final Context ctx, final ComputerLink dc) { Log.i("PhoneCallReceiver", "Registered"); @@ -34,7 +34,7 @@ public class PhoneCallReceiver implements BaseReceiver { lastPackage.setType(NetworkPackage.Type.RING); lastPackage.setBody(incomingNumber); - dc.asyncSend(lastPackage.toString()); + dc.sendPackage(lastPackage); break; @@ -42,7 +42,7 @@ public class PhoneCallReceiver implements BaseReceiver { if (lastPackage != null) { lastPackage.cancel(); - dc.asyncSend(lastPackage.toString()); + dc.sendPackage(lastPackage); lastPackage = null; } @@ -53,7 +53,7 @@ public class PhoneCallReceiver implements BaseReceiver { if (lastState != TelephonyManager.CALL_STATE_IDLE && lastPackage != null) { lastPackage.cancel(); - dc.asyncSend(lastPackage.toString()); + dc.sendPackage(lastPackage); if (lastState == TelephonyManager.CALL_STATE_RINGING) { @@ -64,7 +64,7 @@ public class PhoneCallReceiver implements BaseReceiver { lastPackage.setType(NetworkPackage.Type.MISSED); lastPackage.setBody(incomingNumber); - dc.asyncSend(lastPackage.toString()); + dc.sendPackage(lastPackage); } diff --git a/KdeConnect/src/main/java/org/kde/connect/Receivers/BaseReceiver.java b/KdeConnect/src/main/java/org/kde/connect/Receivers/BaseReceiver.java deleted file mode 100644 index a155162c..00000000 --- a/KdeConnect/src/main/java/org/kde/connect/Receivers/BaseReceiver.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.kde.connect.Receivers; - -import android.content.Context; - -public interface BaseReceiver { - - -} diff --git a/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java b/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java index b69e81de..283adc41 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java +++ b/KdeConnect/src/main/java/org/kde/connect/Types/NetworkPackage.java @@ -1,5 +1,9 @@ package org.kde.connect.Types; +import android.content.Context; +import android.telephony.TelephonyManager; +import android.util.Log; + import org.json.JSONObject; import java.util.Calendar; @@ -9,6 +13,8 @@ public class NetworkPackage { public enum Type { UNKNOWN, + ID_REQUEST, + PAIR_REQUEST, RING, MISSED, SMS, @@ -26,8 +32,9 @@ public class NetworkPackage { private boolean mIsCancel; private JSONObject mExtras; //JSON - public NetworkPackage(long id) { + public NetworkPackage(long id/*, Context context*/) { mId = id; + // final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); mDeviceId = 42; mTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000L; mType = Type.UNKNOWN; @@ -99,4 +106,15 @@ public class NetworkPackage { return sb.toString(); } + + + static public NetworkPackage fromString(String s/*, Context context*/) { + Log.i("NetworkPackage.fromString",s); + + NetworkPackage np = new NetworkPackage(123456789 /*, context*/); + np.mType = Type.PAIR_REQUEST; + return np; + } + + } diff --git a/KdeConnect/src/main/java/org/kde/connect/DesktopCommunication.java b/KdeConnect/src/main/java/org/kde/connect/UdpComputerLink.java similarity index 51% rename from KdeConnect/src/main/java/org/kde/connect/DesktopCommunication.java rename to KdeConnect/src/main/java/org/kde/connect/UdpComputerLink.java index b4512973..128394f7 100644 --- a/KdeConnect/src/main/java/org/kde/connect/DesktopCommunication.java +++ b/KdeConnect/src/main/java/org/kde/connect/UdpComputerLink.java @@ -1,18 +1,42 @@ package org.kde.connect; +import android.net.nsd.NsdServiceInfo; import android.os.AsyncTask; import android.util.Log; +import org.kde.connect.Types.NetworkPackage; + +import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; +import org.kde.connect.ComputerLink; -public class DesktopCommunication { +public class UdpComputerLink extends ComputerLink { - final int UDP_PORT = 10600; + final int UDP_PORT = 10601; final String IP = "192.168.1.48"; + final int BUFFER_SIZE = 5*1024; - public void asyncSend(final String messageStr) { + @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)); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void sendPackage(NetworkPackage np) { + final String messageStr = np.toString(); new AsyncTask(){ @Override protected Void doInBackground(Void... voids) { @@ -32,6 +56,4 @@ public class DesktopCommunication { } }.execute(); } - - }