summaryrefslogtreecommitdiff
path: root/same-android/src
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-07 15:04:02 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-07 15:04:02 +0100
commitd989fd15232f111bedf79a36c9ec6870357fc26e (patch)
tree9213c26f768797f3084cec2c543543fb03923f9b /same-android/src
parent4e2adf917b0a6112c8d0db1a7afdf937866f4a3d (diff)
Extend SameService.
– Binding to SameService now possible. – Broadcast created for available networks.
Diffstat (limited to 'same-android/src')
-rw-r--r--same-android/src/main/java/com/orbekk/SameControllerActivity.java69
-rw-r--r--same-android/src/main/java/com/orbekk/discovery/SameService.java39
2 files changed, 105 insertions, 3 deletions
diff --git a/same-android/src/main/java/com/orbekk/SameControllerActivity.java b/same-android/src/main/java/com/orbekk/SameControllerActivity.java
index a288789..0a9d387 100644
--- a/same-android/src/main/java/com/orbekk/SameControllerActivity.java
+++ b/same-android/src/main/java/com/orbekk/SameControllerActivity.java
@@ -1,6 +1,7 @@
package com.orbekk;
import java.net.InetAddress;
+import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -9,15 +10,52 @@ import com.orbekk.discovery.Broadcaster;
import com.orbekk.discovery.SameService;
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;
+import android.os.Message;
+import android.os.Messenger;
+import android.os.RemoteException;
import android.view.View;
+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 ServiceConnection sameConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ sameService = new Messenger(service);
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ sameService = null;
+ }
+ };
+
+ private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (SameService.AVAILABLE_NETWORKS_UPDATE.equals(intent.getAction())) {
+ ArrayList<String> networkList = intent.getStringArrayListExtra(
+ SameService.AVAILABLE_NETWORKS);
+ Toast.makeText(SameControllerActivity.this, networkList.toString(),
+ Toast.LENGTH_SHORT).show();
+ }
+ }
+ };
+
public void createNetwork(View unused) {
logger.info("Creating network");
Intent intent = new Intent(this, SameService.class);
@@ -49,6 +87,22 @@ public class SameControllerActivity extends Activity {
public void doneClicked(View unused) {
finish();
}
+
+ public void searchNetworks(View unused) {
+ Toast.makeText(this, "Discovering networks", Toast.LENGTH_SHORT).show();
+ final String[] listItems = new String[]{"First item", "Second item"};
+ ListView list = (ListView)findViewById(R.id.network_list);
+ list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_item,
+ listItems));
+
+ Message message = Message.obtain(null, SameService.DISPLAY_MESSAGE,
+ "Message from Activity!");
+ try {
+ sameService.send(message);
+ } catch (RemoteException e) {
+ logger.error("Failed to send message", e);
+ }
+ }
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -58,7 +112,18 @@ public class SameControllerActivity extends Activity {
System.setProperty("java.net.preferIPv6Addresses", "false");
setContentView(R.layout.controller);
- showIpAddress();
+ showIpAddress();
+
+ Intent intent = new Intent(this, SameService.class);
+ bindService(intent, sameConnection, Context.BIND_AUTO_CREATE);
+ }
+
+ @Override public void onResume() {
+ super.onResume();
+
+ IntentFilter sameServiceUpdates = new IntentFilter(
+ SameService.AVAILABLE_NETWORKS_UPDATE);
+ registerReceiver(broadcastReceiver, sameServiceUpdates);
}
@Override
diff --git a/same-android/src/main/java/com/orbekk/discovery/SameService.java b/same-android/src/main/java/com/orbekk/discovery/SameService.java
index 44f0013..c43d4ff 100644
--- a/same-android/src/main/java/com/orbekk/discovery/SameService.java
+++ b/same-android/src/main/java/com/orbekk/discovery/SameService.java
@@ -1,8 +1,10 @@
package com.orbekk.discovery;
+import java.util.List;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.util.ArrayList;
import java.util.Properties;
import android.app.Service;
@@ -10,6 +12,7 @@ import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
+import android.os.Messenger;
import android.widget.Toast;
import org.slf4j.Logger;
@@ -21,13 +24,22 @@ import com.orbekk.same.SameController;
import com.orbekk.same.config.Configuration;
public class SameService extends Service {
+ public final static int DISPLAY_MESSAGE = 1;
+
+ 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";
+
final static int PORT = 15066;
final static int SERVICE_PORT = 15068;
+
private Logger logger = LoggerFactory.getLogger(getClass());
private Thread discoveryThread = null;
private SameController sameController = null;
private Configuration configuration = null;
+ /** This class should go away >:-/ */
public final class DiscoveryThread extends Thread {
Broadcaster broadcast;
DiscoveryListener listener;
@@ -74,6 +86,31 @@ public class SameService extends Service {
}
};
+
+ class InterfaceHandler extends Handler {
+ @Override public void handleMessage(Message message) {
+ switch (message.what) {
+ case DISPLAY_MESSAGE:
+ Toast.makeText(SameService.this,
+ (String)message.obj, Toast.LENGTH_SHORT)
+ .show();
+
+ Intent intent = new Intent(AVAILABLE_NETWORKS_UPDATE);
+ ArrayList<String> networkList = new ArrayList<String>();
+ networkList.add("FirstNetwork");
+ intent.putStringArrayListExtra(AVAILABLE_NETWORKS,
+ networkList);
+ sendBroadcast(intent);
+
+ break;
+ default:
+ super.handleMessage(message);
+ }
+ }
+ }
+
+ private final Messenger messenger = new Messenger(new InterfaceHandler());
+
private void createNetwork() {
if (discoveryThread == null) {
synchronized (this) {
@@ -124,7 +161,7 @@ public class SameService extends Service {
@Override
public IBinder onBind(Intent intent) {
- return null;
+ return messenger.getBinder();
}
@Override