summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--same-android/res/layout/controller.xml2
-rw-r--r--same-android/src/main/java/com/orbekk/same/SameControllerActivity.java68
-rw-r--r--same-android/src/main/java/com/orbekk/same/SameService.java9
-rw-r--r--same/src/main/java/com/orbekk/same/DiscoveryService.java4
4 files changed, 69 insertions, 14 deletions
diff --git a/same-android/res/layout/controller.xml b/same-android/res/layout/controller.xml
index ad4c286..79974ee 100644
--- a/same-android/res/layout/controller.xml
+++ b/same-android/res/layout/controller.xml
@@ -39,7 +39,7 @@
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:onClick="joinNetwork"
+ android:onClick="joinNetworkUrl"
android:text="Join" />
<Button
diff --git a/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java b/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java
index 31d3e18..c9829a7 100644
--- a/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java
+++ b/same-android/src/main/java/com/orbekk/same/SameControllerActivity.java
@@ -1,5 +1,6 @@
package com.orbekk.same;
+import java.util.List;
import java.util.ArrayList;
import org.slf4j.Logger;
@@ -21,14 +22,18 @@ import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
+import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
+import android.widget.Toast;
public class SameControllerActivity extends Activity {
private Logger logger = LoggerFactory.getLogger(getClass());
private Messenger sameService = null;
+ private List<String> networkNames = new ArrayList<String>();
+ private List<String> networkUrls = new ArrayList<String>();
private ServiceConnection sameConnection = new ServiceConnection() {
@Override
@@ -41,20 +46,37 @@ public class SameControllerActivity extends Activity {
sameService = null;
}
};
-
+
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
- public void onReceive(Context context, Intent intent) {
+ public synchronized void onReceive(Context context, Intent intent) {
if (SameService.AVAILABLE_NETWORKS_UPDATE.equals(intent.getAction())) {
- ArrayList<String> networkList = intent.getStringArrayListExtra(
- SameService.AVAILABLE_NETWORKS);
- ListView list = (ListView)findViewById(R.id.network_list);
- list.setAdapter(new ArrayAdapter<String>(
- SameControllerActivity.this,
- R.layout.list_text_item, networkList));
+ networkNames = intent.getStringArrayListExtra(
+ SameService.AVAILABLE_NETWORKS);
+ networkUrls = intent.getStringArrayListExtra(
+ SameService.NETWORK_URLS);
+ updateNetworkList();
}
}
};
+
+ private AdapterView.OnItemClickListener networkListClickListener =
+ new AdapterView.OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ Toast.makeText(SameControllerActivity.this,
+ "Join network " + networkNames.get(position),
+ Toast.LENGTH_SHORT).show();
+ }
+ };
+
+ private void updateNetworkList() {
+ ListView list = (ListView)findViewById(R.id.network_list);
+ list.setAdapter(new ArrayAdapter<String>(
+ SameControllerActivity.this,
+ R.layout.list_text_item, networkNames));
+ }
+
public void createNetwork(View unused) {
Message message = Message.obtain(null, SameService.CREATE_NETWORK);
@@ -66,14 +88,31 @@ public class SameControllerActivity extends Activity {
}
}
- public void joinNetwork(View unused) {
- logger.info("Joining network");
+ public void joinNetworkUrl(View unused) {
+ String masterUrl = "";
Intent intent = new Intent(this, SameService.class);
intent.setAction("join");
- // InetAddress address = new Broadcaster(this).getBroadcastAddress();
EditText t = (EditText)findViewById(R.id.master_service_url);
- intent.putExtra("masterUrl", t.getText().toString());
- startService(intent);
+ masterUrl = t.getText().toString();
+ if (!masterUrl.startsWith("http://")) {
+ masterUrl = "http://" + masterUrl;
+ }
+ if (!masterUrl.endsWith("/MasterService.json")) {
+ masterUrl += "/MasterService.json";
+ }
+ joinNetwork(masterUrl);
+ }
+
+ private void joinNetwork(String masterUrl) {
+ logger.info("joinNetwork({})", masterUrl);
+ Message message = Message.obtain(null, SameService.JOIN_NETWORK,
+ masterUrl);
+ try {
+ sameService.send(message);
+ } catch (RemoteException e) {
+ logger.error("Failed to send message", e);
+ throw new RuntimeException(e);
+ }
}
private void showIpAddress() {
@@ -105,6 +144,9 @@ public class SameControllerActivity extends Activity {
setContentView(R.layout.controller);
showIpAddress();
+
+ ListView networkList = (ListView)findViewById(R.id.network_list);
+ networkList.setOnItemClickListener(networkListClickListener);
}
@Override public void onResume() {
diff --git a/same-android/src/main/java/com/orbekk/same/SameService.java b/same-android/src/main/java/com/orbekk/same/SameService.java
index 3237e61..a960065 100644
--- a/same-android/src/main/java/com/orbekk/same/SameService.java
+++ b/same-android/src/main/java/com/orbekk/same/SameService.java
@@ -24,11 +24,14 @@ public class SameService extends Service {
public final static int DISPLAY_MESSAGE = 1;
public final static int SEARCH_NETWORKS = 2;
public final static int CREATE_NETWORK = 3;
+ public final static int JOIN_NETWORK = 4;
public final static String AVAILABLE_NETWORKS_UPDATE =
"com.orbekk.same.SameService.action.AVAILABLE_NETWORKS_UPDATE";
public final static String AVAILABLE_NETWORKS =
"com.orbekk.same.SameService.action.AVAILABLE_NETWORKS";
+ public final static String NETWORK_URLS =
+ "com.orbekk.same.SameService.action.NETWORK_URLS";
final static int SERVICE_PORT = 15068;
final static int DISCOVERY_PORT = 15066;
@@ -50,6 +53,8 @@ public class SameService extends Service {
Intent intent = new Intent(AVAILABLE_NETWORKS_UPDATE);
intent.putStringArrayListExtra(AVAILABLE_NETWORKS,
networkNames);
+ intent.putStringArrayListExtra(NETWORK_URLS,
+ networkUrls);
sendBroadcast(intent);
}
};
@@ -70,6 +75,10 @@ public class SameService extends Service {
logger.info("CREATE_NETWORK");
create();
break;
+ case JOIN_NETWORK:
+ logger.info("JOIN_NETWORK");
+ String masterUrl = (String)message.obj;
+ sameController.getClient().joinNetwork(masterUrl);
default:
super.handleMessage(message);
}
diff --git a/same/src/main/java/com/orbekk/same/DiscoveryService.java b/same/src/main/java/com/orbekk/same/DiscoveryService.java
index 0415316..d9af9bb 100644
--- a/same/src/main/java/com/orbekk/same/DiscoveryService.java
+++ b/same/src/main/java/com/orbekk/same/DiscoveryService.java
@@ -22,6 +22,10 @@ public class DiscoveryService extends Thread {
logger.info("DiscoveryService starting.");
while (!Thread.interrupted()) {
DatagramPacket packet = broadcastListener.listen();
+ if (packet == null) {
+ // An error or interrupt occurred.
+ continue;
+ }
String content = new String(packet.getData(), 0, packet.getLength());
String[] words = content.split(" ");