diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-25 21:52:03 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-25 21:52:03 +0100 |
commit | 28ccfd5c38df380fc87461238c9b717ad7fd7dce (patch) | |
tree | f3bc162d8495468e11cf6b57929bab7e1623ce2d | |
parent | a5ae972e46ccaccc0b60d4ff218d3b7ece1108ba (diff) |
Begin implementing a client bridge.
SameInterfaceBridge should implement ClientInterface and communicate
with same through a SameService.
Add replyTo support in SameService.
6 files changed, 157 insertions, 10 deletions
diff --git a/same-android/AndroidManifest.xml b/same-android/AndroidManifest.xml index 6cbe06c..bc28dd1 100644 --- a/same-android/AndroidManifest.xml +++ b/same-android/AndroidManifest.xml @@ -19,7 +19,8 @@ </activity> <activity android:name=".SameControllerActivity"/> - + <activity android:name=".VariableTestActivity"/> + <service android:name=".SameService" /> </application> diff --git a/same-android/res/menu/main_menu.xml b/same-android/res/menu/main_menu.xml index 6d63008..49e584a 100644 --- a/same-android/res/menu/main_menu.xml +++ b/same-android/res/menu/main_menu.xml @@ -4,5 +4,8 @@ <item android:id="@+id/same_settings" android:title="Same settings"/> + <item + android:id="@+id/variable_test" + android:title="Variable Test"/> </menu>
\ No newline at end of file diff --git a/same-android/src/main/java/com/orbekk/same/MainActivity.java b/same-android/src/main/java/com/orbekk/same/MainActivity.java index 08b0962..ea6e315 100644 --- a/same-android/src/main/java/com/orbekk/same/MainActivity.java +++ b/same-android/src/main/java/com/orbekk/same/MainActivity.java @@ -1,6 +1,7 @@ package com.orbekk.same; -import com.orbekk.same.R; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import android.app.Activity; import android.content.Intent; @@ -8,9 +9,10 @@ import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.widget.Toast; public class MainActivity extends Activity { + Logger logger = LoggerFactory.getLogger(getClass()); + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); @@ -23,8 +25,16 @@ public class MainActivity extends Activity { } @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == R.id.same_settings) { + int id = item.getItemId(); + switch (id) { + case R.id.same_settings: startActivity(new Intent(this, SameControllerActivity.class)); + break; + case R.id.variable_test: + startActivity(new Intent(this, VariableTestActivity.class)); + break; + default: + logger.error("Unknown menu entry: {}", item); } return true; } 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 a960065..a58b0ec 100644 --- a/same-android/src/main/java/com/orbekk/same/SameService.java +++ b/same-android/src/main/java/com/orbekk/same/SameService.java @@ -3,19 +3,18 @@ package com.orbekk.same; import java.util.ArrayList; import java.util.Properties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; +import android.os.RemoteException; import android.widget.Toast; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.orbekk.same.NetworkNotificationListener; -import com.orbekk.same.SameController; import com.orbekk.same.android.net.AndroidBroadcasterFactory; import com.orbekk.same.android.net.Broadcaster; import com.orbekk.same.config.Configuration; @@ -24,7 +23,7 @@ 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 int JOIN_NETWORK = 4; public final static String AVAILABLE_NETWORKS_UPDATE = "com.orbekk.same.SameService.action.AVAILABLE_NETWORKS_UPDATE"; @@ -66,6 +65,16 @@ public class SameService extends Service { Toast.makeText(SameService.this, (String)message.obj, Toast.LENGTH_SHORT) .show(); + Messenger responseService = message.replyTo; + if (responseService != null) { + Message response = Message.obtain(null, DISPLAY_MESSAGE); + response.obj = "Response from SameService"; + try { + responseService.send(response); + } catch (RemoteException e) { + logger.error("Failed to respond.", e); + } + } break; case SEARCH_NETWORKS: logger.info("SEARCH_NETWORKS"); diff --git a/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java b/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java new file mode 100644 index 0000000..1420f2e --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java @@ -0,0 +1,29 @@ +package com.orbekk.same; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import android.app.Activity; +import android.os.Bundle; + +import com.orbekk.same.android.SameInterfaceBridge; + +public class VariableTestActivity extends Activity { + private Logger logger = LoggerFactory.getLogger(getClass()); + private SameInterfaceBridge client; + + @Override public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override public void onResume() { + super.onResume(); + client = new SameInterfaceBridge(this); + client.connect(); + } + + @Override public void onStop() { + super.onStop(); + client.disconnect(); + } +} diff --git a/same-android/src/main/java/com/orbekk/same/android/SameInterfaceBridge.java b/same-android/src/main/java/com/orbekk/same/android/SameInterfaceBridge.java new file mode 100644 index 0000000..48e38cd --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/android/SameInterfaceBridge.java @@ -0,0 +1,95 @@ +package com.orbekk.same.android; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; + +import com.orbekk.same.ClientInterface; +import com.orbekk.same.SameService; +import com.orbekk.same.State; +import com.orbekk.same.StateChangedListener; +import com.orbekk.same.UpdateConflict; + +public class SameInterfaceBridge implements ClientInterface { + class ResponseHandler extends Handler { + @Override public void handleMessage(Message message) { + switch (message.what) { + default: + logger.warn("Received unknown message from service: {}", + message); + } + } + } + + private Logger logger = LoggerFactory.getLogger(getClass()); + private Messenger serviceMessenger = null; + private Messenger responseMessenger = new Messenger(new ResponseHandler()); + private Context context; + + private ServiceConnection serviceConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + serviceMessenger = new Messenger(service); + Message display_message = Message.obtain(null, + SameService.DISPLAY_MESSAGE); + display_message.obj = "Message from bridge."; + display_message.replyTo = responseMessenger; + try { + serviceMessenger.send(display_message); + } catch (RemoteException e) { + logger.error("Failed to send message to service."); + } + } + + @Override + public void onServiceDisconnected(ComponentName name) { + serviceMessenger = null; + } + }; + + public SameInterfaceBridge(Context context) { + this.context = context; + } + + public void connect() { + Intent intent = new Intent(context, SameService.class); + context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); + } + + public void disconnect() { + if (serviceMessenger != null) { + context.unbindService(serviceConnection); + } + } + + @Override + public State getState() { + return null; + } + + @Override + public void set(String name, String data, long revision) throws UpdateConflict { + logger.info("set({}, {}, {}", + new Object[]{name, data, revision}); + } + + @Override + public void addStateListener(StateChangedListener listener) { + logger.info("addStateListener()"); + } + + @Override + public void removeStateListener(StateChangedListener listener) { + logger.info("removeStateListener()"); + } + +} |