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;
37 private TabHost window;
38 private TabWidget tabs;
39 private LinearLayout chat;
41 private EditText input;
43 private LinearLayout spades;
44 private TextView debug;
46 private ScrollView lscroll;
47 private ScrollView dscroll;
49 /* Private helper methods */
50 private void notice(String text)
52 this.log.append(Html.fromHtml("<b>*** " + text + "</b><br />"));
55 private void display(Message msg)
57 String when = DateFormat.format("hh:mm:ss", msg.time).toString();
58 String from = String.format("<font color=\"#ff88ff\">%s</font>", msg.from);
59 String text = msg.msg;
62 // Do IRC Colors - only partly works
63 String fg = "<font color=\"<$1>\">";
65 .replaceAll("&", "&")
66 .replaceAll("<", "<")
67 .replaceAll(">", ">");
69 .replaceAll("\\002", "<b>") // bold
70 .replaceAll("\\011", "<i>") // italic
71 .replaceAll("\\025", "<u>"); // underline
73 .replaceAll("\\003(\\d+)(,\\d+)?", fg) // color
74 .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color
76 .replaceAll("<0?0>", "#000000") // White
77 .replaceAll("<0?1>", "#000000") // Black
78 .replaceAll("<0?2>", "#000080") // Navy Blue
79 .replaceAll("<0?3>", "#008000") // Green
80 .replaceAll("<0?4>", "#FF0000") // Red
81 .replaceAll("<0?5>", "#804040") // Brown
82 .replaceAll("<0?6>", "#8000FF") // Purple
83 .replaceAll("<0?7>", "#808000") // Olive
84 .replaceAll("<0?8>", "#FFFF00") // Yellow
85 .replaceAll("<0?9>", "#00FF00") // Lime Green
86 .replaceAll("<10>", "#008080") // Teal
87 .replaceAll("<11>", "#00FFFF") // Aqua Light
88 .replaceAll("<12>", "#0000FF") // Royal Blue
89 .replaceAll("<13>", "#FF00FF") // Hot Pink
90 .replaceAll("<14>", "#808080") // Dark Gray
91 .replaceAll("<15>", "#C0C0C0"); // Light Gray
98 fmt = "<b>(%s) %s: %s</b>";
101 fmt = "(%s) <b>%s</b>: %s";
108 String html = String.format(fmt, when, from, text);
109 this.log.append(Html.fromHtml(html + "<br />"));
112 /* Private handler methods */
113 private void onRegister(Object obj)
115 Os.debug("Main: onRegister");
116 this.task = (Task)obj;
119 private void onMessage(Object obj)
121 Message msg = (Message)obj;
124 this.debug.append("> " + msg.line + "\n");
125 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
133 if (!msg.txt.equals(this.topic))
134 this.notice("Topic for " + msg.arg + ": " + msg.txt);
135 this.topic = msg.txt;
138 if (!msg.txt.equals(this.names))
139 this.notice("Users in " + msg.arg + ": " + msg.txt);
140 this.names = msg.txt;
143 this.notice("Error: " + msg.txt);
146 this.notice("Authentication succeeded: " + msg.txt);
149 this.notice("Authentication failed: " + msg.txt);
152 this.lscroll.smoothScrollTo(0, this.log.getBottom());
155 private void onNotify(String text)
157 Os.debug("Main: onNotify - " + text);
159 this.toast.setText(text);
163 /* Private service methods */
164 private void startService()
166 Os.debug("Main: startService");
167 startService(new Intent(this, Task.class)
168 .putExtra("Messenger", this.messenger));
171 private void stopService()
173 Os.debug("Main: stopService");
174 stopService(new Intent(this, Task.class));
177 /* Widget callback functions */
178 public void onSend(View btn)
180 if (this.task == null)
182 String txt = this.input.getText().toString();
183 Message msg = this.task.send(txt);
186 this.input.setText("");
189 /* Activity Methods */
191 public void onCreate(Bundle savedInstanceState)
194 super.onCreate(savedInstanceState);
195 Os.debug("Main: onCreate");
198 this.setContentView(R.layout.main);
201 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
203 // Setup communication
204 this.handler = new MainHandler();
205 this.messenger = new Messenger(this.handler);
208 this.window = (TabHost) findViewById(android.R.id.tabhost);
209 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
210 this.chat = (LinearLayout) findViewById(R.id.chat);
211 this.log = (TextView) findViewById(R.id.log);
212 this.input = (EditText) findViewById(R.id.input);
213 this.send = (Button) findViewById(R.id.send);
214 this.spades = (LinearLayout) findViewById(R.id.spades);
215 this.debug = (TextView) findViewById(R.id.debug);
217 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
218 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
223 this.window.addTab(this.window
225 .setIndicator("Chat")
226 .setContent(R.id.chat));
227 this.window.addTab(this.window
228 .newTabSpec("spades")
229 .setIndicator("Spades")
230 .setContent(R.id.spades));
231 this.window.addTab(this.window
233 .setIndicator("Debug")
234 .setContent(R.id.debug));
237 this.cards = new Cards(this);
238 this.spades.addView(cards);
240 } catch (Exception e) {
241 Os.debug("Error setting content view", e);
247 public void onStart()
250 Os.debug("Main: onStart");
254 public void onResume()
257 Os.debug("Main: onResume");
261 public void onPause()
264 Os.debug("Main: onPause");
271 Os.debug("Main: onStop");
275 public void onRestart()
278 Os.debug("Main: onRestart");
282 public void onDestroy()
285 Os.debug("Main: onDestroy");
289 public boolean onCreateOptionsMenu(Menu menu)
291 MenuInflater inflater = getMenuInflater();
292 inflater.inflate(R.menu.main, menu);
297 public boolean onPrepareOptionsMenu(Menu menu)
299 menu.findItem(R.id.connect).setVisible(!this.ready);
300 menu.findItem(R.id.disconnect).setVisible(this.ready);
305 public boolean onOptionsItemSelected(MenuItem item)
307 switch (item.getItemId()) {
311 case R.id.disconnect:
315 this.startActivity(new Intent(this, Prefs.class));
327 class MainHandler extends Handler
329 public void handleMessage(android.os.Message msg)
333 Main.this.onRegister(msg.obj);
336 Main.this.onMessage(msg.obj);
339 Main.this.ready = true;
341 case Task.DISCONNECT:
342 Main.this.ready = false;
345 Main.this.onNotify((String)msg.obj);
348 Os.debug("Main: unknown message - " + msg.what);