From ebf3d3451e1a04e4f8b0c762fe6c0297884983e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 14 Feb 2012 13:22:54 +0100 Subject: same-android: Implement network joining. --- .../com/orbekk/same/SameControllerActivity.java | 68 +++++++++++++++++----- .../src/main/java/com/orbekk/same/SameService.java | 9 +++ 2 files changed, 64 insertions(+), 13 deletions(-) (limited to 'same-android/src/main/java') 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 networkNames = new ArrayList(); + private List networkUrls = new ArrayList(); 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 networkList = intent.getStringArrayListExtra( - SameService.AVAILABLE_NETWORKS); - ListView list = (ListView)findViewById(R.id.network_list); - list.setAdapter(new ArrayAdapter( - 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( + 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); } -- cgit v1.2.3