diff options
Diffstat (limited to 'same-android')
-rw-r--r-- | same-android/AndroidManifest.xml | 1 | ||||
-rw-r--r-- | same-android/res/layout/controller.xml | 18 | ||||
-rw-r--r-- | same-android/res/layout/list_text_item.xml | 7 | ||||
-rw-r--r-- | same-android/src/main/java/com/orbekk/SameControllerActivity.java | 69 | ||||
-rw-r--r-- | same-android/src/main/java/com/orbekk/discovery/SameService.java | 39 |
5 files changed, 125 insertions, 9 deletions
diff --git a/same-android/AndroidManifest.xml b/same-android/AndroidManifest.xml index a839f8c..88b2208 100644 --- a/same-android/AndroidManifest.xml +++ b/same-android/AndroidManifest.xml @@ -14,6 +14,7 @@ <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> + <action android:name="com.orbekk.same.SameService.action.AVAILABLE_NETWORKS_UPDATE" /> </intent-filter> </activity> diff --git a/same-android/res/layout/controller.xml b/same-android/res/layout/controller.xml index 6cfea80..a85cacd 100644 --- a/same-android/res/layout/controller.xml +++ b/same-android/res/layout/controller.xml @@ -17,6 +17,18 @@ android:onClick="createNetwork" android:text="Create" /> + <ListView + android:id="@+id/network_list" + android:layout_width="match_parent" + android:layout_height="wrap_content"> + </ListView> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:onClick="searchNetworks" + android:text="Search" /> + <EditText android:id="@+id/master_service_url" android:layout_width="match_parent" @@ -28,12 +40,6 @@ android:onClick="joinNetwork" android:text="Join" /> - <ListView - android:id="@+id/network_list" - android:layout_width="match_parent" - android:layout_height="wrap_content" > - </ListView> - <Button android:layout_width="wrap_content" android:layout_height="wrap_content" diff --git a/same-android/res/layout/list_text_item.xml b/same-android/res/layout/list_text_item.xml new file mode 100644 index 0000000..3aa90dd --- /dev/null +++ b/same-android/res/layout/list_text_item.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:padding="10dp" + android:textSize="16sp"> +</TextView>
\ No newline at end of file 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 |