From 1df218cb0cb92ac8374241f342b1b6a0a5fe9e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Mon, 13 Feb 2012 19:30:23 +0100 Subject: Use special broadcast in android program. --- .../src/main/java/com/orbekk/SameService.java | 31 ++++++++++++++++------ .../android/net/AndroidBroadcasterFactory.java | 20 ++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java (limited to 'same-android/src/main/java') diff --git a/same-android/src/main/java/com/orbekk/SameService.java b/same-android/src/main/java/com/orbekk/SameService.java index 0b88182..dd943cb 100644 --- a/same-android/src/main/java/com/orbekk/SameService.java +++ b/same-android/src/main/java/com/orbekk/SameService.java @@ -21,14 +21,13 @@ import org.slf4j.LoggerFactory; import com.orbekk.same.DiscoveryListener; import com.orbekk.same.NetworkNotificationListener; import com.orbekk.same.SameController; +import com.orbekk.same.android.net.AndroidBroadcasterFactory; import com.orbekk.same.android.net.Broadcaster; import com.orbekk.same.config.Configuration; -/** - * TODO: Remove junk code and use BroadcastInterface stuff. - */ public class SameService extends Service { public final static int DISPLAY_MESSAGE = 1; + public final static int SEARCH_NETWORKS = 2; public final static String AVAILABLE_NETWORKS_UPDATE = "com.orbekk.same.SameService.action.AVAILABLE_NETWORKS_UPDATE"; @@ -81,6 +80,16 @@ public class SameService extends Service { } } + private NetworkNotificationListener networkListener = + new NetworkNotificationListener() { + @Override + public void notifyNetwork(String networkName, String masterUrl) { + Message message = Message.obtain(); + message.obj = "notifyNetwork(" + networkName + ")"; + toastHandler.sendMessage(message); + } + }; + private Handler toastHandler = new Handler() { @Override public void handleMessage(Message message) { Toast.makeText(SameService.this, @@ -105,7 +114,10 @@ public class SameService extends Service { intent.putStringArrayListExtra(AVAILABLE_NETWORKS, networkList); sendBroadcast(intent); - + break; + case SEARCH_NETWORKS: + logger.info("SEARCH_NETWORKS"); + sameController.searchNetworks(); break; default: super.handleMessage(message); @@ -170,20 +182,23 @@ public class SameService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - System.setProperty("http.keepAlive", "false"); - Toast.makeText(this, "service start: " + intent.getAction(), - Toast.LENGTH_SHORT).show(); + logger.info("onStartCommand()"); + if (sameController == null) { initializeConfiguration(); - sameController = SameController.create(configuration); + sameController = SameController.create( + new AndroidBroadcasterFactory(this), + configuration); try { sameController.start(); + sameController.getClient().setNetworkListener(networkListener); } catch (Exception e) { logger.error("Failed to start server", e); return START_STICKY; } } + // TODO: Move this to the bound interface. if (intent.getAction().equals("create")) { createNetwork(); } else if (intent.getAction().equals("join")) { diff --git a/same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java b/same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java new file mode 100644 index 0000000..3d2bdef --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java @@ -0,0 +1,20 @@ +package com.orbekk.same.android.net; + +import android.content.Context; + +import com.orbekk.net.BroadcasterFactory; +import com.orbekk.net.BroadcasterInterface; + +public class AndroidBroadcasterFactory implements BroadcasterFactory { + Context context; + + public AndroidBroadcasterFactory(Context context) { + this.context = context; + } + + @Override + public BroadcasterInterface create() { + return new Broadcaster(context); + } + +} -- cgit v1.2.3