diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-28 11:57:48 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-28 11:57:48 +0100 |
commit | 6a030381607aaf7b44529c4d5a6552749305c3d2 (patch) | |
tree | 366ac55e7a96af649d1640dd80607fee0a40bf1f /same-android/src/main/java/com | |
parent | bc2d8fa1da02037d0b9a2361f394f19e494c8cec (diff) |
Run SameService in a separate process.
Diffstat (limited to 'same-android/src/main/java/com')
3 files changed, 45 insertions, 24 deletions
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 3a487d0..b7ba0ca 100644 --- a/same-android/src/main/java/com/orbekk/same/SameService.java +++ b/same-android/src/main/java/com/orbekk/same/SameService.java @@ -17,6 +17,7 @@ import android.os.RemoteException; import android.widget.Toast; import com.orbekk.same.State.Component; +import com.orbekk.same.android.ComponentBundle; import com.orbekk.same.android.net.AndroidBroadcasterFactory; import com.orbekk.same.android.net.Broadcaster; import com.orbekk.same.config.Configuration; @@ -32,14 +33,19 @@ public class SameService extends Service { /** * arg1: Operation number. - * obj: Updated component. + * bundle: A Bundle created with ComponentBundle */ public final static int SET_STATE = 7; + + /** + * bundle: A Bundle created with ComponentBundle. + */ public final static int UPDATED_STATE_CALLBACK = 8; /** * arg1: Operation number. - * obj: Operation status. + * arg2: Status code. + * obj: Status message. */ public final static int OPERATION_STATUS_CALLBACK = 9; @@ -127,9 +133,9 @@ public class SameService extends Service { case SET_STATE: logger.info("SET_STATE: oId: {}, comp: {}", message.arg1, message.obj); State.Component updatedComponent = - (State.Component)message.obj; + new ComponentBundle(message.getData()).getComponent(); int id = message.arg1; - logger.info("Running operation."); + logger.info("Running operation. Component: " + updatedComponent); DelayedOperation op = sameController.getClient().getInterface() .set(updatedComponent); logger.info("Operation finished. Sending callback."); @@ -152,7 +158,7 @@ public class SameService extends Service { ArrayList<Messenger> dropped = new ArrayList<Messenger>(); for (Messenger messenger : stateReceivers) { Message message = Message.obtain(null, UPDATED_STATE_CALLBACK); - message.obj = component; + message.setData(new ComponentBundle(component).getBundle()); try { messenger.send(message); } catch (RemoteException e) { @@ -170,8 +176,10 @@ public class SameService extends Service { op.waitFor(); synchronized (stateReceivers) { Message message = Message.obtain(null, - OPERATION_STATUS_CALLBACK, id); - message.obj = op.getStatus(); + OPERATION_STATUS_CALLBACK); + message.arg1 = id; + message.arg2 = op.getStatus().getStatusCode(); + message.obj = op.getStatus().getMessage(); try { messenger.send(message); } catch (RemoteException e) { @@ -186,7 +194,7 @@ public class SameService extends Service { State state = sameController.getClient().getInterface().getState(); for (Component c : state.getComponents()) { Message message = Message.obtain(null, UPDATED_STATE_CALLBACK); - message.obj = c; + message.setData(new ComponentBundle(c).getBundle()); try { messenger.send(message); } catch (RemoteException e) { @@ -232,7 +240,7 @@ public class SameService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { logger.info("onStartCommand()"); - return START_STICKY; + return START_NOT_STICKY; } @Override diff --git a/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java b/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java index 1fffbad..3ae1a87 100644 --- a/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java +++ b/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import android.app.Activity; +import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.EditText; @@ -27,6 +28,22 @@ public class VariableTestActivity extends Activity { } }; + private class UpdateVariableTask + extends AsyncTask<String, Void, DelayedOperation.Status> { + @Override protected DelayedOperation.Status doInBackground(String... values) { + String value = values[0]; + return variable.set(value).getStatus(); + } + + @Override protected void onPostExecute(DelayedOperation.Status status) { + if (!status.isOk()) { + Toast.makeText(VariableTestActivity.this, + "Update failed: " + status, Toast.LENGTH_SHORT) + .show(); + } + } + } + private void displayVariable() { TextView tv = (TextView)findViewById(R.id.variable_text); if (variable.get() != null) { @@ -37,14 +54,7 @@ public class VariableTestActivity extends Activity { public void setVariable(View unused) { EditText et = (EditText)findViewById(R.id.set_variable_text); String newValue = et.getText().toString(); - logger.info("Setting variable."); - DelayedOperation op = variable.set(newValue); - logger.info("Waiting for delayed operation."); - if (!op.getStatus().isOk()) { - Toast.makeText(this, "Failed to update: " + op.getStatus(), - Toast.LENGTH_SHORT) - .show(); - } + new UpdateVariableTask().execute(newValue); } @Override public void onCreate(Bundle savedInstanceState) { diff --git a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java index 3b490bc..dfab773 100644 --- a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java +++ b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java @@ -42,13 +42,17 @@ public class ClientInterfaceBridge implements ClientInterface { } switch (message.what) { case SameService.UPDATED_STATE_CALLBACK: - State.Component component = (State.Component)message.obj; + State.Component component = + new ComponentBundle(message.getData()).getComponent(); updateState(component); break; case SameService.OPERATION_STATUS_CALLBACK: int operationNumber = message.arg1; + logger.info("Received callback for operation {}", operationNumber); + int statusCode = message.getData().getInt("statusCode"); + String statusMessage = message.getData().getString("statusMessage"); DelayedOperation.Status status = - (DelayedOperation.Status)message.obj; + new DelayedOperation.Status(statusCode, statusMessage); completeOperation(operationNumber, status); break; default: @@ -153,13 +157,12 @@ public class ClientInterfaceBridge implements ClientInterface { return op; } - Message message = Message.obtain(null, SameService.SET_STATE, - op.getIdentifier()); - // this has to be Parcelable. -// message.obj = component; + Message message = Message.obtain(null, SameService.SET_STATE); + message.arg1 = op.getIdentifier(); + message.setData(new ComponentBundle(component).getBundle()); message.replyTo = responseMessenger; try { - logger.info("Sending update to service."); + logger.info("Sending update to service. No state."); serviceMessenger.send(message); logger.info("Service finished update."); } catch (RemoteException e) { |