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.Html;
9 import android.text.method.ScrollingMovementMethod;
10 import android.text.format.DateFormat;
11 import android.view.Menu;
12 import android.view.MenuInflater;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.widget.Button;
16 import android.widget.EditText;
17 import android.widget.LinearLayout;
18 import android.widget.ScrollView;
19 import android.widget.TabHost;
20 import android.widget.TabWidget;
21 import android.widget.TextView;
22 import android.widget.Toast;
24 public class Main extends Activity
27 private Handler handler;
28 private Messenger messenger;
31 private boolean ready;
36 private TabHost window;
37 private TabWidget tabs;
38 private LinearLayout chat;
40 private EditText input;
42 private TextView spades;
43 private TextView debug;
45 private ScrollView lscroll;
46 private ScrollView dscroll;
48 /* Private helper methods */
49 private void notice(String text)
51 this.log.append(Html.fromHtml("<b>*** " + text + "</b><br />"));
54 private void display(Message msg)
56 String when = DateFormat.format("hh:mm:ss", msg.time).toString();
57 String from = String.format("<font color=\"#ff88ff\">%s</font>", msg.from);
58 String text = msg.msg;
60 Os.debug("msg: " + Os.base64(msg.msg));
63 // - This doesn't actually work
64 String fg = "<font color=\"<$1>\">";
65 String bg = "<font bgcolor=\"<$2>\">";
67 .replaceAll("&", "&")
68 .replaceAll("<", "<")
69 .replaceAll(">", ">");
71 .replaceAll("\\002", "<b>") // bold
72 .replaceAll("\\011", "<i>") // italic
73 .replaceAll("\\025", "<u>") // underline
74 .replaceAll("\\022", "<b>"); // reverse
76 .replaceAll("\\003(\\d+),(\\d+)", fg + bg) // color
77 .replaceAll("\\013(\\d+),(\\d+)", fg + bg) // color
78 .replaceAll("\\003(\\d+)", fg) // color
79 .replaceAll("\\013(\\d+)", fg); // color
81 .replaceAll("<0?0>", "#000000") // White
82 .replaceAll("<0?1>", "#000000") // Black
83 .replaceAll("<0?2>", "#000080") // Navy Blue
84 .replaceAll("<0?3>", "#008000") // Green
85 .replaceAll("<0?4>", "#FF0000") // Red
86 .replaceAll("<0?5>", "#804040") // Brown
87 .replaceAll("<0?6>", "#8000FF") // Purple
88 .replaceAll("<0?7>", "#808000") // Olive
89 .replaceAll("<0?8>", "#FFFF00") // Yellow
90 .replaceAll("<0?9>", "#00FF00") // Lime Green
91 .replaceAll("<10>", "#008080") // Teal
92 .replaceAll("<11>", "#00FFFF") // Aqua Light
93 .replaceAll("<12>", "#0000FF") // Royal Blue
94 .replaceAll("<13>", "#FF00FF") // Hot Pink
95 .replaceAll("<14>", "#808080") // Dark Gray
96 .replaceAll("<15>", "#C0C0C0"); // Light Gray
103 fmt = "<b>(%s) %s: %s</b>";
110 String html = String.format(fmt, when, from, text);
111 this.log.append(Html.fromHtml(html + "<br />"));
114 /* Private handler methods */
115 private void onRegister(Object obj)
117 Os.debug("Main: onRegister");
118 this.task = (Task)obj;
121 private void onMessage(Object obj)
123 Message msg = (Message)obj;
126 this.debug.append("> " + msg.line + "\n");
127 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
135 if (!msg.txt.equals(this.topic))
136 this.notice("Topic for " + msg.arg + ": " + msg.txt);
137 this.topic = msg.txt;
140 if (!msg.txt.equals(this.names))
141 this.notice("Users in " + msg.arg + ": " + msg.txt);
142 this.names = msg.txt;
145 this.notice("Error: " + msg.txt);
148 this.notice("Authentication succeeded: " + msg.txt);
151 this.notice("Authentication failed: " + msg.txt);
154 this.lscroll.smoothScrollTo(0, this.log.getBottom());
157 private void onNotify(String text)
159 Os.debug("Main: onNotify - " + text);
161 this.toast.setText(text);
165 /* Private service methods */
166 private void startService()
168 Os.debug("Main: startService");
169 startService(new Intent(this, Task.class)
170 .putExtra("Messenger", this.messenger));
173 private void stopService()
175 Os.debug("Main: stopService");
176 stopService(new Intent(this, Task.class));
179 /* Widget callback functions */
180 public void onSend(View btn)
182 if (this.task == null)
184 String txt = this.input.getText().toString();
185 Message msg = this.task.send(txt);
188 this.input.setText("");
191 /* Activity Methods */
193 public void onCreate(Bundle savedInstanceState)
196 super.onCreate(savedInstanceState);
197 Os.debug("Main: onCreate");
200 this.setContentView(R.layout.main);
203 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
205 // Setup communication
206 this.handler = new MainHandler();
207 this.messenger = new Messenger(this.handler);
210 this.window = (TabHost) findViewById(android.R.id.tabhost);
211 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
212 this.chat = (LinearLayout) findViewById(R.id.chat);
213 this.log = (TextView) findViewById(R.id.log);
214 this.input = (EditText) findViewById(R.id.input);
215 this.send = (Button) findViewById(R.id.send);
216 this.spades = (TextView) findViewById(R.id.spades);
217 this.debug = (TextView) findViewById(R.id.debug);
219 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
220 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
225 this.window.addTab(this.window
227 .setIndicator("Chat")
228 .setContent(R.id.chat));
229 this.window.addTab(this.window
230 .newTabSpec("spades")
231 .setIndicator("Spades")
232 .setContent(R.id.spades));
233 this.window.addTab(this.window
235 .setIndicator("Debug")
236 .setContent(R.id.debug));
237 } catch (Exception e) {
238 Os.debug("Error setting content view", e);
244 public void onStart()
247 Os.debug("Main: onStart");
251 public void onResume()
254 Os.debug("Main: onResume");
258 public void onPause()
261 Os.debug("Main: onPause");
268 Os.debug("Main: onStop");
272 public void onRestart()
275 Os.debug("Main: onRestart");
279 public void onDestroy()
282 Os.debug("Main: onDestroy");
286 public boolean onCreateOptionsMenu(Menu menu)
288 MenuInflater inflater = getMenuInflater();
289 inflater.inflate(R.menu.main, menu);
294 public boolean onPrepareOptionsMenu(Menu menu)
296 menu.findItem(R.id.connect).setVisible(!this.ready);
297 menu.findItem(R.id.disconnect).setVisible(this.ready);
302 public boolean onOptionsItemSelected(MenuItem item)
304 switch (item.getItemId()) {
308 case R.id.disconnect:
312 this.startActivity(new Intent(this, Prefs.class));
324 class MainHandler extends Handler
326 public void handleMessage(android.os.Message msg)
330 Main.this.onRegister(msg.obj);
333 Main.this.onMessage(msg.obj);
336 Main.this.ready = true;
338 case Task.DISCONNECT:
339 Main.this.ready = false;
342 Main.this.onNotify((String)msg.obj);
345 Os.debug("Main: unknown message - " + msg.what);