From b35173cc4c9cab138168c2df6d6331837d0cde12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 26 Jan 2012 19:34:25 +0100 Subject: Clean up Android code. --- .../java/com/orbekk/discovery/SameService.java | 32 ++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'same-android/src') diff --git a/same-android/src/main/java/com/orbekk/discovery/SameService.java b/same-android/src/main/java/com/orbekk/discovery/SameService.java index 154239f..7ca9778 100644 --- a/same-android/src/main/java/com/orbekk/discovery/SameService.java +++ b/same-android/src/main/java/com/orbekk/discovery/SameService.java @@ -12,6 +12,7 @@ import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orbekk.same.ClientApp; import com.orbekk.same.MasterApp; public class SameService extends Service { @@ -52,11 +53,30 @@ public class SameService extends Service { } }; + private void createNetwork() { + if (discoveryThread == null) { + synchronized (this) { + discoveryThread = new DiscoveryThread(); + discoveryThread.start(); + } + } + new Thread() { + @Override public void run() { + new MasterApp().run(PORT+1); + } + }.start(); + } + private void sendBroadcastDiscovery() { byte[] data = "Discover".getBytes(); new Broadcast(this).sendBroadcast(data, PORT); } + private void joinNetwork() { + sendBroadcastDiscovery(); + new ClientApp().run(PORT+2, "ClientNetwork", null); + } + @Override public IBinder onBind(Intent intent) { return null; @@ -66,14 +86,10 @@ public class SameService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "service start: " + intent.getAction(), Toast.LENGTH_SHORT).show(); - if (discoveryThread == null) { - synchronized (this) { - discoveryThread = new DiscoveryThread(); - discoveryThread.start(); - } - } - if (intent.getAction().equals("join")) { - sendBroadcastDiscovery(); + if (intent.getAction().equals("create")) { + createNetwork(); + } else if (intent.getAction().equals("join")) { + joinNetwork(); } return START_STICKY; } -- cgit v1.2.3