summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-14 13:22:54 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-14 13:22:54 +0100
commitebf3d3451e1a04e4f8b0c762fe6c0297884983e5 (patch)
tree5d480dcf4151f846d12e43293685cfa7b341fb68 /same-android/src/main/java/com/orbekk
parent990f1cfeb54d7ef83a1ee465c95f53ce803c1b13 (diff)
same-android: Implement network joining.
Diffstat (limited to 'same-android/src/main/java/com/orbekk')
-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
2 files changed, 64 insertions, 13 deletions
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);
}