From a461dc41fde93a4853203bd57b9359edbe4c1c24 Mon Sep 17 00:00:00 2001 From: Tobias Oberstein Date: Thu, 2 Feb 2012 15:29:14 +0100 Subject: [PATCH] add Android echo and broadcast demo clients --- Demo/BroadcastClient/.classpath | 3 + Demo/BroadcastClient/.project | 7 + Demo/BroadcastClient/AndroidManifest.xml | 1 + Demo/BroadcastClient/default.properties | 1 + Demo/BroadcastClient/res/layout/main.xml | 41 ++++- Demo/BroadcastClient/res/values/colors.xml | 6 + Demo/BroadcastClient/res/values/strings.xml | 3 +- .../BroadcastClientActivity.java | 169 +++++++++++++++++- 8 files changed, 215 insertions(+), 16 deletions(-) create mode 100644 Demo/BroadcastClient/res/values/colors.xml diff --git a/Demo/BroadcastClient/.classpath b/Demo/BroadcastClient/.classpath index 609aa00e..d319fa2c 100644 --- a/Demo/BroadcastClient/.classpath +++ b/Demo/BroadcastClient/.classpath @@ -3,5 +3,8 @@ + + + diff --git a/Demo/BroadcastClient/.project b/Demo/BroadcastClient/.project index 8d86e9ca..027a7c72 100644 --- a/Demo/BroadcastClient/.project +++ b/Demo/BroadcastClient/.project @@ -30,4 +30,11 @@ com.android.ide.eclipse.adt.AndroidNature org.eclipse.jdt.core.javanature + + + Autobahn_src + 2 + _android_Autobahn_59a1d7d4/src + + diff --git a/Demo/BroadcastClient/AndroidManifest.xml b/Demo/BroadcastClient/AndroidManifest.xml index f9e9a8bf..854365d1 100644 --- a/Demo/BroadcastClient/AndroidManifest.xml +++ b/Demo/BroadcastClient/AndroidManifest.xml @@ -4,6 +4,7 @@ android:versionCode="1" android:versionName="1.0"> + - - + android:weightSum="1" android:background="#028ec9" android:padding="12dip"> + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Demo/BroadcastClient/res/values/colors.xml b/Demo/BroadcastClient/res/values/colors.xml new file mode 100644 index 00000000..0cc87fad --- /dev/null +++ b/Demo/BroadcastClient/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #028ec9 + #cccccc + #333333 + diff --git a/Demo/BroadcastClient/res/values/strings.xml b/Demo/BroadcastClient/res/values/strings.xml index c298ae22..5bd00a37 100644 --- a/Demo/BroadcastClient/res/values/strings.xml +++ b/Demo/BroadcastClient/res/values/strings.xml @@ -1,5 +1,4 @@ - Hello World, BroadcastClientActivity! - BroadcastClient + Autobahn Broadcast Client diff --git a/Demo/BroadcastClient/src/de/tavendo/autobahn/broadcastclient/BroadcastClientActivity.java b/Demo/BroadcastClient/src/de/tavendo/autobahn/broadcastclient/BroadcastClientActivity.java index 1e6a3520..51976ad8 100644 --- a/Demo/BroadcastClient/src/de/tavendo/autobahn/broadcastclient/BroadcastClientActivity.java +++ b/Demo/BroadcastClient/src/de/tavendo/autobahn/broadcastclient/BroadcastClientActivity.java @@ -1,13 +1,168 @@ +/****************************************************************************** +* +* Copyright 2011 Tavendo GmbH +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +******************************************************************************/ + package de.tavendo.autobahn.broadcastclient; import android.app.Activity; +import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ScrollView; +import android.widget.TextView; +import android.widget.Toast; +import de.tavendo.autobahn.WebSocketConnection; +import de.tavendo.autobahn.WebSocketException; +import de.tavendo.autobahn.WebSocketHandler; public class BroadcastClientActivity extends Activity { - /** Called when the activity is first created. */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - } -} \ No newline at end of file + + static final String TAG = "de.tavendo.autobahn.broadcast"; + private static final String PREFS_NAME = "AutobahnAndroidBroadcast"; + + static EditText mHostname; + static EditText mPort; + static TextView mStatusline; + static Button mStart; + + static EditText mMessage; + static Button mSendMessage; + + static TextView mLog; + static ScrollView mLogScroller; + + private SharedPreferences mSettings; + + private void alert(String message) { + Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); + } + + private void loadPrefs() { + + mHostname.setText(mSettings.getString("hostname", "")); + mPort.setText(mSettings.getString("port", "9000")); + } + + private void savePrefs() { + + SharedPreferences.Editor editor = mSettings.edit(); + editor.putString("hostname", mHostname.getText().toString()); + editor.putString("port", mPort.getText().toString()); + editor.commit(); + } + + private void setButtonConnect() { + mHostname.setEnabled(true); + mPort.setEnabled(true); + mStart.setText("Connect"); + mStart.setOnClickListener(new Button.OnClickListener() { + public void onClick(View v) { + start(); + } + }); + } + + private void setButtonDisconnect() { + mHostname.setEnabled(false); + mPort.setEnabled(false); + mStart.setText("Disconnect"); + mStart.setOnClickListener(new Button.OnClickListener() { + public void onClick(View v) { + mConnection.disconnect(); + } + }); + } + + private final WebSocketConnection mConnection = new WebSocketConnection(); + + private void start() { + + final String wsuri = "ws://" + mHostname.getText() + ":" + mPort.getText(); + + mStatusline.setText("Status: Connecting to " + wsuri + " .."); + + setButtonDisconnect(); + + try { + mConnection.connect(wsuri, new WebSocketHandler() { + @Override + public void onOpen() { + mStatusline.setText("Status: Connected to " + wsuri); + savePrefs(); + mSendMessage.setEnabled(true); + mMessage.setEnabled(true); + } + + @Override + public void onTextMessage(String payload) { + mLog.setText(mLog.getText() + "\n" + payload); + mLogScroller.post(new Runnable() + { + public void run() + { + mLogScroller.smoothScrollTo(0, mLog.getBottom()); + } + }); + } + + @Override + public void onClose(int code, String reason) { + alert("Connection lost."); + mStatusline.setText("Status: Ready."); + setButtonConnect(); + mSendMessage.setEnabled(false); + mMessage.setEnabled(false); + } + }); + } catch (WebSocketException e) { + + Log.d(TAG, e.toString()); + } + } + + @Override + public void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + mHostname = (EditText) findViewById(R.id.hostname); + mPort = (EditText) findViewById(R.id.port); + mStatusline = (TextView) findViewById(R.id.statusline); + mStart = (Button) findViewById(R.id.start); + mMessage = (EditText) findViewById(R.id.msg); + mSendMessage = (Button) findViewById(R.id.sendMsg); + mLog = (TextView) findViewById(R.id.log); + mLogScroller = (ScrollView) findViewById(R.id.logscroller); + + mSettings = getSharedPreferences(PREFS_NAME, 0); + loadPrefs(); + + setButtonConnect(); + mSendMessage.setEnabled(false); + mMessage.setEnabled(false); + + mSendMessage.setOnClickListener(new Button.OnClickListener() { + public void onClick(View v) { + mConnection.sendTextMessage(mMessage.getText().toString()); + } + }); + } +}