1010import java .nio .ByteBuffer ;
1111import java .util .concurrent .Semaphore ;
1212
13+ import android .app .Activity ;
1314import android .os .Bundle ;
1415import android .support .v4 .app .FragmentActivity ;
16+ import android .text .Editable ;
17+ import android .text .TextWatcher ;
1518import android .view .View ;
1619import android .view .View .OnClickListener ;
20+ import android .view .inputmethod .InputMethodManager ;
1721import android .widget .Button ;
1822import android .widget .CheckBox ;
1923import android .widget .TextView ;
3236
3337public class EchoActivity extends FragmentActivity {
3438
35- private TextView location ;
36- private TextView message ;
37- private TextView log ;
39+ private TextView locationText ;
40+ private TextView messageText ;
41+ private TextView logTextView ;
3842 private Button sendBtn ;
3943 private Button connectBtn ;
4044 private Button disconnectBtn ;
@@ -47,43 +51,75 @@ public class EchoActivity extends FragmentActivity {
4751 private boolean closedExplicitly = false ;
4852 private LoginDialogFragment loginDialog ;
4953
50- /** Called when the activity is first created. */
54+ private void hideKeyboard () {
55+ InputMethodManager imm = (InputMethodManager ) getSystemService (Activity .INPUT_METHOD_SERVICE );
56+ imm .toggleSoftInput (InputMethodManager .HIDE_IMPLICIT_ONLY , 0 );
57+ }
58+
59+
60+ /** Called when the activity is first created. */
5161 @ Override
5262 public void onCreate (Bundle savedInstanceState ) {
5363 super .onCreate (savedInstanceState );
5464 setContentView (R .layout .main );
55- location = (TextView )findViewById (R .id .location );
56- message = (TextView )findViewById ( R .id .message );
57- log = (TextView )findViewById (R .id .log );
65+ locationText = (TextView )findViewById (R .id .locationText );
66+ messageText = (TextView )findViewById ( R .id .messageText );
67+ logTextView = (TextView )findViewById (R .id .logView );
5868 sendBtn = (Button )findViewById (R .id .send );
59- connectBtn = (Button )findViewById (R .id .connect );
60- disconnectBtn = (Button )findViewById (R .id .disconnect );
61- clearBtn = (Button )findViewById (R .id .clear );
69+ connectBtn = (Button )findViewById (R .id .connectBtn );
70+ disconnectBtn = (Button )findViewById (R .id .disconnectBtn );
71+ clearBtn = (Button )findViewById (R .id .clearBtn );
6272 sendBinaryCheckBox = (CheckBox )findViewById (R .id .sendBinaryCheckBox );
6373
74+
75+ locationText .addTextChangedListener (new TextWatcher () {
76+ @ Override
77+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
78+
79+ }
80+
81+ @ Override
82+ public void onTextChanged (CharSequence s , int start , int before , int count ) {
83+ String url = locationText .getText ().toString ();
84+ if (url .length ()>0 ){
85+ connectBtn .setEnabled (true );
86+ }
87+ else {
88+ connectBtn .setEnabled (false );
89+ }
90+ }
91+
92+ @ Override
93+ public void afterTextChanged (Editable s ) {
94+
95+ }
96+ });
97+
6498 connectBtn .setOnClickListener (new OnClickListener () {
6599 public void onClick (View v ) {
100+ hideKeyboard ();
66101 connectBtn .setEnabled (false );
67102 connect ();
68103 }
69104 });
70105
71106 sendBtn .setOnClickListener (new OnClickListener () {
72107 public void onClick (View v ) {
108+ hideKeyboard ();
73109 final boolean sendBinary = sendBinaryCheckBox .isChecked ();
74110 dispatchQueue .dispatchAsync (new Runnable () {
75111 public void run () {
76112 try {
77113 WebSocketMessageWriter messageWriter = webSocket .getMessageWriter ();
78114 if (sendBinary ) {
79- String messageToSend = message .getText ().toString ();
115+ String messageToSend = messageText .getText ().toString ();
80116 ByteBuffer payload = ByteBuffer .wrap (messageToSend .getBytes ());
81117 logMessage ("SEND BINARY:" + getHexDump (payload ));
82118 messageWriter .writeBinary (payload );
83119 }
84120 else {
85- logMessage ("SEND: " + message .getText ());
86- messageWriter .writeText (message .getText ());
121+ logMessage ("SEND: " + messageText .getText ());
122+ messageWriter .writeText (messageText .getText ());
87123 }
88124 } catch (Exception e ) {
89125 e .printStackTrace ();
@@ -102,7 +138,7 @@ public void onClick(View v) {
102138
103139 clearBtn .setOnClickListener (new OnClickListener () {
104140 public void onClick (View v ) {
105- log .setText ("" );
141+ logTextView .setText ("" );
106142 }
107143 });
108144
@@ -147,7 +183,7 @@ private void connect() {
147183 public void run () {
148184 try {
149185 WebSocketFactory webSocketFactory = WebSocketFactory .createWebSocketFactory ();
150- webSocket = webSocketFactory .createWebSocket (URI .create (location .getText ().toString ()));
186+ webSocket = webSocketFactory .createWebSocket (URI .create (locationText .getText ().toString ()));
151187 webSocket .setChallengeHandler (createChallengehandler ());
152188 webSocket .connect ();
153189 logMessage ("CONNECTED" );
@@ -211,7 +247,7 @@ public void run() {
211247 private void logMessage (final String logMessage ) {
212248 runOnUiThread (new Runnable () {
213249 public void run () {
214- log .setText (logMessage + "\n " + log .getText ());
250+ logTextView .setText (logMessage + "\n " + logTextView .getText ());
215251 }
216252 });
217253 }
0 commit comments