1 package org.pileus.spades;
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.os.Handler;
7 import android.os.Messenger;
8 import android.text.method.ScrollingMovementMethod;
9 import android.view.Menu;
10 import android.view.MenuInflater;
11 import android.view.MenuItem;
12 import android.view.View;
13 import android.widget.Button;
14 import android.widget.EditText;
15 import android.widget.LinearLayout;
16 import android.widget.ScrollView;
17 import android.widget.TabHost;
18 import android.widget.TabWidget;
19 import android.widget.TextView;
20 import android.widget.Toast;
22 import android.preference.PreferenceActivity;
24 public class Main extends Activity
27 private Handler handler;
28 private Messenger messenger;
33 private boolean ready;
38 private TabHost window;
39 private TabWidget tabs;
40 private LinearLayout chat;
42 private EditText input;
44 private TextView spades;
45 private TextView debug;
47 private ScrollView lscroll;
48 private ScrollView dscroll;
50 /* Private helper methods */
51 private void notice(String text)
53 this.log.append("*** " + text + "\n");
56 /* Private handler methods */
57 private void onRegister(Object obj)
59 Os.debug("Main: onRegister");
60 this.task = (Task)obj;
63 private void onMessage(Object obj)
65 Message msg = (Message)obj;
68 this.debug.append("> " + msg.line + "\n");
69 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
74 this.log.append(msg.from + ": " + msg.msg + "\n");
77 if (!msg.txt.equals(this.topic))
78 this.notice("Topic for " + msg.arg + ": " + msg.txt);
82 if (!msg.txt.equals(this.names))
83 this.notice("Users in " + msg.arg + ": " + msg.txt);
87 this.notice("Error: " + msg.txt);
90 this.notice("Authentication succeeded: " + msg.txt);
93 this.notice("Authentication failed: " + msg.txt);
96 this.lscroll.smoothScrollTo(0, this.log.getBottom());
99 private void onNotify(String text)
101 Os.debug("Main: onNotify - " + text);
103 this.toast.setText(text);
107 /* Private service methods */
108 private void startService()
110 Os.debug("Main: startService");
111 startService(new Intent(this, Task.class)
112 .putExtra("Messenger", this.messenger));
115 private void stopService()
117 Os.debug("Main: stopService");
118 stopService(new Intent(this, Task.class));
121 /* Widget callback functions */
122 public void onSend(View btn)
124 if (this.task == null)
126 String txt = this.input.getText().toString();
127 Message msg = this.task.send(txt);
130 this.input.setText("");
133 /* Activity Methods */
135 public void onCreate(Bundle savedInstanceState)
138 super.onCreate(savedInstanceState);
139 Os.debug("Main: onCreate");
142 this.setContentView(R.layout.main);
145 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
147 // Setup communication
148 this.handler = new MainHandler();
149 this.messenger = new Messenger(this.handler);
152 this.window = (TabHost) findViewById(android.R.id.tabhost);
153 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
154 this.chat = (LinearLayout) findViewById(R.id.chat);
155 this.log = (TextView) findViewById(R.id.log);
156 this.input = (EditText) findViewById(R.id.input);
157 this.send = (Button) findViewById(R.id.send);
158 this.spades = (TextView) findViewById(R.id.spades);
159 this.debug = (TextView) findViewById(R.id.debug);
161 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
162 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
167 this.window.addTab(this.window
169 .setIndicator("Chat")
170 .setContent(R.id.chat));
171 this.window.addTab(this.window
172 .newTabSpec("spades")
173 .setIndicator("Spades")
174 .setContent(R.id.spades));
175 this.window.addTab(this.window
177 .setIndicator("Debug")
178 .setContent(R.id.debug));
179 } catch (Exception e) {
180 Os.debug("Error setting content view", e);
186 public void onStart()
189 Os.debug("Main: onStart");
193 public void onResume()
196 Os.debug("Main: onResume");
200 public void onPause()
203 Os.debug("Main: onPause");
210 Os.debug("Main: onStop");
214 public void onRestart()
217 Os.debug("Main: onRestart");
221 public void onDestroy()
224 Os.debug("Main: onDestroy");
228 public boolean onCreateOptionsMenu(Menu menu)
230 MenuInflater inflater = getMenuInflater();
231 inflater.inflate(R.menu.main, menu);
236 public boolean onPrepareOptionsMenu(Menu menu)
238 menu.findItem(R.id.connect).setVisible(!this.ready);
239 menu.findItem(R.id.disconnect).setVisible(this.ready);
244 public boolean onOptionsItemSelected(MenuItem item)
246 switch (item.getItemId()) {
250 case R.id.disconnect:
254 this.startActivity(new Intent(this, Prefs.class));
266 class MainHandler extends Handler
268 public void handleMessage(android.os.Message msg)
272 Main.this.onRegister(msg.obj);
275 Main.this.onMessage(msg.obj);
278 Main.this.ready = true;
280 case Task.DISCONNECT:
281 Main.this.ready = false;
284 Main.this.onNotify((String)msg.obj);
287 Os.debug("Main: unknown message - " + msg.what);