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;
61 // Do IRC Colors - only partly works
62 String fg = "<font color=\"<$1>\">";
64 .replaceAll("&", "&")
65 .replaceAll("<", "<")
66 .replaceAll(">", ">");
68 .replaceAll("\\002", "<b>") // bold
69 .replaceAll("\\011", "<i>") // italic
70 .replaceAll("\\025", "<u>"); // underline
72 .replaceAll("\\003(\\d+)(,\\d+)?", fg) // color
73 .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color
75 .replaceAll("<0?0>", "#000000") // White
76 .replaceAll("<0?1>", "#000000") // Black
77 .replaceAll("<0?2>", "#000080") // Navy Blue
78 .replaceAll("<0?3>", "#008000") // Green
79 .replaceAll("<0?4>", "#FF0000") // Red
80 .replaceAll("<0?5>", "#804040") // Brown
81 .replaceAll("<0?6>", "#8000FF") // Purple
82 .replaceAll("<0?7>", "#808000") // Olive
83 .replaceAll("<0?8>", "#FFFF00") // Yellow
84 .replaceAll("<0?9>", "#00FF00") // Lime Green
85 .replaceAll("<10>", "#008080") // Teal
86 .replaceAll("<11>", "#00FFFF") // Aqua Light
87 .replaceAll("<12>", "#0000FF") // Royal Blue
88 .replaceAll("<13>", "#FF00FF") // Hot Pink
89 .replaceAll("<14>", "#808080") // Dark Gray
90 .replaceAll("<15>", "#C0C0C0"); // Light Gray
97 fmt = "<b>(%s) %s: %s</b>";
100 fmt = "(%s) <b>%s</b>: %s";
107 String html = String.format(fmt, when, from, text);
108 this.log.append(Html.fromHtml(html + "<br />"));
111 /* Private handler methods */
112 private void onRegister(Object obj)
114 Os.debug("Main: onRegister");
115 this.task = (Task)obj;
118 private void onMessage(Object obj)
120 Message msg = (Message)obj;
123 this.debug.append("> " + msg.line + "\n");
124 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
132 if (!msg.txt.equals(this.topic))
133 this.notice("Topic for " + msg.arg + ": " + msg.txt);
134 this.topic = msg.txt;
137 if (!msg.txt.equals(this.names))
138 this.notice("Users in " + msg.arg + ": " + msg.txt);
139 this.names = msg.txt;
142 this.notice("Error: " + msg.txt);
145 this.notice("Authentication succeeded: " + msg.txt);
148 this.notice("Authentication failed: " + msg.txt);
151 this.lscroll.smoothScrollTo(0, this.log.getBottom());
154 private void onNotify(String text)
156 Os.debug("Main: onNotify - " + text);
158 this.toast.setText(text);
162 /* Private service methods */
163 private void startService()
165 Os.debug("Main: startService");
166 startService(new Intent(this, Task.class)
167 .putExtra("Messenger", this.messenger));
170 private void stopService()
172 Os.debug("Main: stopService");
173 stopService(new Intent(this, Task.class));
176 /* Widget callback functions */
177 public void onSend(View btn)
179 if (this.task == null)
181 String txt = this.input.getText().toString();
182 Message msg = this.task.send(txt);
185 this.input.setText("");
188 /* Activity Methods */
190 public void onCreate(Bundle savedInstanceState)
193 super.onCreate(savedInstanceState);
194 Os.debug("Main: onCreate");
197 this.setContentView(R.layout.main);
200 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
202 // Setup communication
203 this.handler = new MainHandler();
204 this.messenger = new Messenger(this.handler);
207 this.window = (TabHost) findViewById(android.R.id.tabhost);
208 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
209 this.chat = (LinearLayout) findViewById(R.id.chat);
210 this.log = (TextView) findViewById(R.id.log);
211 this.input = (EditText) findViewById(R.id.input);
212 this.send = (Button) findViewById(R.id.send);
213 this.spades = (TextView) findViewById(R.id.spades);
214 this.debug = (TextView) findViewById(R.id.debug);
216 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
217 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
222 this.window.addTab(this.window
224 .setIndicator("Chat")
225 .setContent(R.id.chat));
226 this.window.addTab(this.window
227 .newTabSpec("spades")
228 .setIndicator("Spades")
229 .setContent(R.id.spades));
230 this.window.addTab(this.window
232 .setIndicator("Debug")
233 .setContent(R.id.debug));
234 } catch (Exception e) {
235 Os.debug("Error setting content view", e);
241 public void onStart()
244 Os.debug("Main: onStart");
248 public void onResume()
251 Os.debug("Main: onResume");
255 public void onPause()
258 Os.debug("Main: onPause");
265 Os.debug("Main: onStop");
269 public void onRestart()
272 Os.debug("Main: onRestart");
276 public void onDestroy()
279 Os.debug("Main: onDestroy");
283 public boolean onCreateOptionsMenu(Menu menu)
285 MenuInflater inflater = getMenuInflater();
286 inflater.inflate(R.menu.main, menu);
291 public boolean onPrepareOptionsMenu(Menu menu)
293 menu.findItem(R.id.connect).setVisible(!this.ready);
294 menu.findItem(R.id.disconnect).setVisible(this.ready);
299 public boolean onOptionsItemSelected(MenuItem item)
301 switch (item.getItemId()) {
305 case R.id.disconnect:
309 this.startActivity(new Intent(this, Prefs.class));
321 class MainHandler extends Handler
323 public void handleMessage(android.os.Message msg)
327 Main.this.onRegister(msg.obj);
330 Main.this.onMessage(msg.obj);
333 Main.this.ready = true;
335 case Task.DISCONNECT:
336 Main.this.ready = false;
339 Main.this.onNotify((String)msg.obj);
342 Os.debug("Main: unknown message - " + msg.what);