summaryrefslogtreecommitdiff
path: root/same-android/src/main/java
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-05-10 14:11:33 +0200
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-05-10 14:11:33 +0200
commit7680d58d2ab0d5edc4fcc5214001b3880eb78e8a (patch)
tree16fa7054582daff5ea3014c1e14c11670074f917 /same-android/src/main/java
parent248378a24ced97cd70409dc4bbf8dab46d2a8bfc (diff)
Change network discovery in Android app.
– Activity discovers networks instead of getting SameService to do it.
Diffstat (limited to 'same-android/src/main/java')
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java87
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/SameService.java63
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/widget/NetworkListAdapter.java22
3 files changed, 75 insertions, 97 deletions
diff --git a/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java b/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java
index 8fff198..bbd9031 100644
--- a/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java
+++ b/same-android/src/main/java/com/orbekk/same/android/SameControllerActivity.java
@@ -15,18 +15,16 @@
*/
package com.orbekk.same.android;
-import java.util.ArrayList;
-import java.util.List;
+import java.io.IOException;
+import java.net.UnknownHostException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.app.Activity;
-import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
-import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
@@ -38,15 +36,19 @@ import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
+import android.widget.Toast;
+import com.google.protobuf.RpcCallback;
+import com.orbekk.protobuf.Rpc;
+import com.orbekk.protobuf.RpcChannel;
+import com.orbekk.same.Services;
import com.orbekk.same.android.net.Networking;
import com.orbekk.same.android.widget.NetworkListAdapter;
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 volatile Services.NetworkDirectory networks = null;
private ServiceConnection sameConnection = new ServiceConnection() {
@Override
@@ -60,34 +62,24 @@ public class SameControllerActivity extends Activity {
}
};
- private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
- @Override
- public synchronized void onReceive(Context context, Intent intent) {
- if (SameService.AVAILABLE_NETWORKS_UPDATE.equals(intent.getAction())) {
- 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) {
- String networkName = networkNames.get(position);
- String masterUrl = networkUrls.get(position);
- joinNetwork(masterUrl);
+ Services.MasterState network = networks.getNetwork(position);
+ joinNetwork(network.getMasterLocation());
}
};
private void updateNetworkList() {
+ Services.NetworkDirectory currentNetworks = networks;
+ if (currentNetworks == null) {
+ return;
+ }
ListView list = (ListView)findViewById(R.id.network_list);
list.setAdapter(new NetworkListAdapter(
SameControllerActivity.this,
- R.layout.list_text_item, networkNames, networkUrls));
+ R.layout.list_text_item, currentNetworks));
}
@@ -142,7 +134,47 @@ public class SameControllerActivity extends Activity {
}
public void searchNetworks(View unused) {
- logger.info("SearchNetworks() not working");
+ logger.info("Looking up networks.");
+ final Toast failedToast = Toast.makeText(this, "No networks found!", Toast.LENGTH_SHORT);
+ RpcChannel channel = null;
+ try {
+ channel = RpcChannel.create(
+ SameService.DIRECTORY_HOST, SameService.DIRECTORY_PORT);
+ } catch (UnknownHostException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ failedToast.show();
+ e.printStackTrace();
+ return;
+ }
+ Services.Directory directory = Services.Directory.newStub(channel);
+ if (directory == null) {
+ logger.warn("No discovery service configured.");
+ return;
+ }
+ final Rpc rpc = new Rpc();
+ rpc.setTimeout(10000);
+ RpcCallback<Services.NetworkDirectory> done =
+ new RpcCallback<Services.NetworkDirectory>() {
+ @Override public void run(Services.NetworkDirectory networks) {
+ if (!rpc.isOk()) {
+ failedToast.show();
+ logger.warn("Unable to find networks: {}", rpc.errorText());
+ return;
+ }
+ setAvailableNetworks(networks);
+ }
+ };
+ directory.getNetworks(rpc, Services.Empty.getDefaultInstance(), done);
+ }
+
+ private void setAvailableNetworks(final Services.NetworkDirectory networks) {
+ runOnUiThread(new Runnable() {
+ @Override public void run() {
+ SameControllerActivity.this.networks = networks;
+ updateNetworkList();
+ }
+ });
}
@Override
@@ -157,8 +189,6 @@ public class SameControllerActivity extends Activity {
ListView networkList = (ListView)findViewById(R.id.network_list);
networkList.setOnItemClickListener(networkListClickListener);
-
- updateNetworkList();
}
@Override public void onResume() {
@@ -167,9 +197,7 @@ public class SameControllerActivity extends Activity {
Intent intent = new Intent(this, SameService.class);
bindService(intent, sameConnection, Context.BIND_AUTO_CREATE);
- IntentFilter sameServiceUpdates = new IntentFilter(
- SameService.AVAILABLE_NETWORKS_UPDATE);
- registerReceiver(broadcastReceiver, sameServiceUpdates);
+ searchNetworks(null);
}
@Override public void onStop() {
@@ -177,7 +205,6 @@ public class SameControllerActivity extends Activity {
if (sameService != null) {
unbindService(sameConnection);
}
- unregisterReceiver(broadcastReceiver);
}
@Override
diff --git a/same-android/src/main/java/com/orbekk/same/android/SameService.java b/same-android/src/main/java/com/orbekk/same/android/SameService.java
index f369b3a..8d0d83c 100644
--- a/same-android/src/main/java/com/orbekk/same/android/SameService.java
+++ b/same-android/src/main/java/com/orbekk/same/android/SameService.java
@@ -34,7 +34,6 @@ import android.widget.Toast;
import com.google.protobuf.RpcCallback;
import com.orbekk.protobuf.Rpc;
-import com.orbekk.same.NetworkNotificationListener;
import com.orbekk.same.SameController;
import com.orbekk.same.Services;
import com.orbekk.same.Services.MasterState;
@@ -75,19 +74,12 @@ public class SameService extends Service {
public final static int OPERATION_STATUS_CALLBACK = 9;
public final static int KILL_MASTER = 10;
-
- // TODO: Remove these and use messengers instead of broadcast intents.
- 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 PPORT = 15070;
final static int SERVICE_PORT = 15068;
- final static int DISCOVERY_PORT = 15066;
- final static String DIRECTORY_URL = "flode.pvv.ntnu.no:15072";
+
+ final static String DIRECTORY_HOST = "flode.pvv.ntnu.no";
+ final static int DIRECTORY_PORT = 15072;
private Logger logger = LoggerFactory.getLogger(getClass());
private SameController sameController = null;
@@ -97,22 +89,6 @@ public class SameService extends Service {
private ArrayList<String> networkNames = new ArrayList<String>();
private ArrayList<String> networkUrls = new ArrayList<String>();
- private NetworkNotificationListener networkListener =
- new NetworkNotificationListener() {
- @Override
- public void notifyNetwork(String networkName, String masterUrl) {
- logger.info("notifyNetwork({})", networkName);
- networkNames.add(networkName);
- networkUrls.add(masterUrl);
- Intent intent = new Intent(AVAILABLE_NETWORKS_UPDATE);
- intent.putStringArrayListExtra(AVAILABLE_NETWORKS,
- networkNames);
- intent.putStringArrayListExtra(NETWORK_URLS,
- networkUrls);
- sendBroadcast(intent);
- }
- };
-
class InterfaceHandler extends Handler {
@Override public void handleMessage(Message message) {
switch (message.what) {
@@ -216,32 +192,7 @@ public class SameService extends Service {
}
}
}
-
- private void findNetworks() {
- logger.info("Looking up networks.");
- Services.Directory directory = sameController.getDirectory();
- if (directory == null) {
- logger.warn("No discovery service configured.");
- return;
- }
- final Rpc rpc = new Rpc();
- rpc.setTimeout(10000);
- RpcCallback<Services.NetworkDirectory> done =
- new RpcCallback<Services.NetworkDirectory>() {
- @Override public void run(Services.NetworkDirectory networks) {
- if (!rpc.isOk()) {
- logger.warn("Unable to find networks: {}", rpc.errorText());
- return;
- }
- for (Services.MasterState network : networks.getNetworkList()) {
- networkListener.notifyNetwork(network.getNetworkName(),
- network.getMasterLocation());
- }
- }
- };
- directory.getNetworks(rpc, Services.Empty.getDefaultInstance(), done);
- }
-
+
private void initializeConfiguration() {
Properties properties = new Properties();
String localIp = new Networking(this)
@@ -251,10 +202,9 @@ public class SameService extends Service {
properties.setProperty("pport", ""+PPORT);
properties.setProperty("localIp", localIp);
properties.setProperty("baseUrl", baseUrl);
- properties.setProperty("enableDiscovery", "true");
- properties.setProperty("discoveryPort", ""+DISCOVERY_PORT);
+ properties.setProperty("directoryLocation", DIRECTORY_HOST + ":" +
+ DIRECTORY_PORT);
properties.setProperty("networkName", "AndroidNetwork");
- properties.setProperty("directoryLocation", DIRECTORY_URL);
configuration = new Configuration(properties);
}
@@ -292,7 +242,6 @@ public class SameService extends Service {
sameController.start();
sameController.getClient().getInterface()
.addStateListener(stateListener);
- findNetworks();
} catch (Exception e) {
logger.error("Failed to start server", e);
}
diff --git a/same-android/src/main/java/com/orbekk/same/android/widget/NetworkListAdapter.java b/same-android/src/main/java/com/orbekk/same/android/widget/NetworkListAdapter.java
index 88073f4..446c550 100644
--- a/same-android/src/main/java/com/orbekk/same/android/widget/NetworkListAdapter.java
+++ b/same-android/src/main/java/com/orbekk/same/android/widget/NetworkListAdapter.java
@@ -23,6 +23,9 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
+
+import com.orbekk.same.Services;
+import com.orbekk.same.Services.NetworkDirectory;
import com.orbekk.same.android.R;
/**
@@ -32,15 +35,13 @@ import com.orbekk.same.android.R;
*
* list.setAdapter(new NetworkListAdapter(...));
*/
-public class NetworkListAdapter extends ArrayAdapter<String> {
- private List<String> networkNames;
- private List<String> masterUrls;
+public class NetworkListAdapter extends ArrayAdapter<Services.MasterState> {
+ private NetworkDirectory networks;
public NetworkListAdapter(Context context, int resource_id,
- List<String> networkNames, List<String> masterUrls) {
- super(context, resource_id, networkNames);
- this.networkNames = networkNames;
- this.masterUrls = masterUrls;
+ Services.NetworkDirectory networks) {
+ super(context, resource_id, networks.getNetworkList());
+ this.networks = networks;
}
public View getView(int position, View convertView, ViewGroup parent) {
@@ -48,13 +49,14 @@ public class NetworkListAdapter extends ArrayAdapter<String> {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.network_list_item, null);
}
- if (networkNames.get(position) != null) {
+ Services.MasterState network = networks.getNetwork(position);
+ if (network != null) {
TextView nameText = (TextView)convertView.findViewById(
R.id.network_name_text);
TextView urlText = (TextView)convertView.findViewById(
R.id.master_url_text);
- nameText.setText(networkNames.get(position));
- urlText.setText(masterUrls.get(position));
+ nameText.setText(network.getNetworkName());
+ urlText.setText(network.getMasterLocation());
}
return convertView;
}