summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-13 19:30:23 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-13 19:30:23 +0100
commit1df218cb0cb92ac8374241f342b1b6a0a5fe9e01 (patch)
tree36ceb8fe534dce7b71e967ef35b099d5b73f7d51
parent45390a10a599275a0a7e185d294879138b74c2a3 (diff)
Use special broadcast in android program.
-rw-r--r--same-android/src/main/java/com/orbekk/SameService.java31
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/net/AndroidBroadcasterFactory.java20
2 files changed, 43 insertions, 8 deletions
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);
+ }
+
+}