diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-26 12:55:34 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-26 12:55:34 +0100 |
commit | 734ebfad68ab5a93055c002eb073c6a694ef4436 (patch) | |
tree | 872f96fc1e94c844628c0696a5b82448b6ce7f59 /same-android/src | |
parent | 402efe234b8e2fefa6ea5f135d27c5575ade34d9 (diff) |
Begin support for variables on Android.
Diffstat (limited to 'same-android/src')
3 files changed, 43 insertions, 0 deletions
diff --git a/same-android/src/main/java/com/orbekk/same/StateViewerActivity.java b/same-android/src/main/java/com/orbekk/same/StateViewerActivity.java index 9dfb61e..5b8fd41 100644 --- a/same-android/src/main/java/com/orbekk/same/StateViewerActivity.java +++ b/same-android/src/main/java/com/orbekk/same/StateViewerActivity.java @@ -52,4 +52,5 @@ public class StateViewerActivity extends Activity { client.removeStateListener(stateListener); client.disconnect(); } + } 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 f98e204..ba33058 100644 --- a/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java +++ b/same-android/src/main/java/com/orbekk/same/VariableTestActivity.java @@ -5,21 +5,59 @@ import org.slf4j.LoggerFactory; import android.app.Activity; import android.os.Bundle; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; import com.orbekk.same.android.ClientInterfaceBridge; public class VariableTestActivity extends Activity { private Logger logger = LoggerFactory.getLogger(getClass()); private ClientInterfaceBridge client; + private Variable<String> variable; + + private Variable.OnChangeListener<String> onChangeListener = + new Variable.OnChangeListener<String>() { + @Override + public void valueChanged(Variable<String> unused) { + variable.update(); + displayVariable(); + } + }; + + private void displayVariable() { + TextView tv = (TextView)findViewById(R.id.variable_text); + if (variable.get() != null) { + tv.setText(variable.get()); + } + } + + public void setVariable(View unused) { + EditText et = (EditText)findViewById(R.id.set_variable_text); + String newValue = et.getText().toString(); + try { + variable.set(newValue); + } catch (UpdateConflict e) { + Toast.makeText(this, "Failed to update.", Toast.LENGTH_SHORT) + .show(); + e.printStackTrace(); + } + } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.variable_test); } @Override public void onResume() { super.onResume(); client = new ClientInterfaceBridge(this); client.connect(); + variable = client.createVariableFactory() + .createString("TestVariable"); + variable.setOnChangeListener(onChangeListener); + displayVariable(); } @Override public void onStop() { 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 f885b5a..a38f16f 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 @@ -20,6 +20,7 @@ import com.orbekk.same.SameService; import com.orbekk.same.State; import com.orbekk.same.StateChangedListener; import com.orbekk.same.UpdateConflict; +import com.orbekk.same.VariableFactory; public class ClientInterfaceBridge implements ClientInterface { private State state; @@ -126,4 +127,7 @@ public class ClientInterfaceBridge implements ClientInterface { listeners.remove(listener); } + public VariableFactory createVariableFactory() { + return VariableFactory.create(this); + } } |