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 running;
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(Task task)
115 Os.debug("Main: onRegister");
117 this.running = this.task.isRunning();
118 for (Object obj : this.task.getLog()) {
119 if (String.class.isInstance(obj))
120 this.notice((String)obj);
121 if (Message.class.isInstance(obj))
122 this.onMessage((Message)obj);
126 private void onMessage(Message msg)
129 this.debug.append("> " + msg.line + "\n");
130 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
138 if (!msg.txt.equals(this.topic))
139 this.notice("Topic for " + msg.arg + ": " + msg.txt);
140 this.topic = msg.txt;
143 if (!msg.txt.equals(this.names))
144 this.notice("Users in " + msg.arg + ": " + msg.txt);
145 this.names = msg.txt;
148 this.notice("Error: " + msg.txt);
151 this.notice("Authentication succeeded: " + msg.txt);
154 this.notice("Authentication failed: " + msg.txt);
157 this.lscroll.smoothScrollTo(0, this.log.getBottom());
160 private void onNotify(String text)
162 Os.debug("Main: onNotify - " + text);
164 this.toast.setText(text);
168 /* Private service methods */
169 private void register()
171 Os.debug("Main: register");
172 startService(new Intent(this, Task.class)
173 .putExtra("Command", Task.REGISTER)
174 .putExtra("Messenger", this.messenger));
177 private void connect()
179 Os.debug("Main: connect");
180 startService(new Intent(this, Task.class)
181 .putExtra("Command", Task.CONNECT));
185 private void disconnect()
187 Os.debug("Main: disconnect");
188 startService(new Intent(this, Task.class)
189 .putExtra("Command", Task.DISCONNECT));
194 stopService(new Intent(this, Task.class));
197 /* Widget callback functions */
198 public void onSend(View btn)
200 if (this.task == null)
202 String txt = this.input.getText().toString();
203 Message msg = this.task.send(txt);
206 this.input.setText("");
209 /* Activity Methods */
211 public void onCreate(Bundle savedInstanceState)
214 super.onCreate(savedInstanceState);
215 Os.debug("Main: onCreate");
218 this.setContentView(R.layout.main);
221 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
223 // Setup communication
224 this.handler = new MainHandler();
225 this.messenger = new Messenger(this.handler);
228 this.window = (TabHost) findViewById(android.R.id.tabhost);
229 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
230 this.chat = (LinearLayout) findViewById(R.id.chat);
231 this.log = (TextView) findViewById(R.id.log);
232 this.input = (EditText) findViewById(R.id.input);
233 this.send = (Button) findViewById(R.id.send);
234 this.spades = (LinearLayout) findViewById(R.id.spades);
235 this.debug = (TextView) findViewById(R.id.debug);
237 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
238 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
243 this.window.addTab(this.window
245 .setIndicator("Chat")
246 .setContent(R.id.chat));
247 this.window.addTab(this.window
248 .newTabSpec("spades")
249 .setIndicator("Spades")
250 .setContent(R.id.spades));
251 this.window.addTab(this.window
253 .setIndicator("Debug")
254 .setContent(R.id.debug));
257 this.cards = new Cards(this);
258 this.spades.addView(cards);
260 // Attach to background service
263 } catch (Exception e) {
264 Os.debug("Error setting content view", e);
270 public void onStart()
273 Os.debug("Main: onStart");
277 public void onResume()
280 Os.debug("Main: onResume");
284 public void onPause()
287 Os.debug("Main: onPause");
294 Os.debug("Main: onStop");
298 public void onRestart()
301 Os.debug("Main: onRestart");
305 public void onDestroy()
308 Os.debug("Main: onDestroy");
312 public boolean onCreateOptionsMenu(Menu menu)
314 MenuInflater inflater = getMenuInflater();
315 inflater.inflate(R.menu.main, menu);
320 public boolean onPrepareOptionsMenu(Menu menu)
322 menu.findItem(R.id.connect).setVisible(!this.running);
323 menu.findItem(R.id.disconnect).setVisible(this.running);
328 public boolean onOptionsItemSelected(MenuItem item)
330 switch (item.getItemId()) {
334 case R.id.disconnect:
338 this.startActivity(new Intent(this, Prefs.class));
350 class MainHandler extends Handler
352 public void handleMessage(android.os.Message msg)
356 Main.this.onRegister((Task)msg.obj);
359 Main.this.onMessage((Message)msg.obj);
362 Main.this.running = true;
364 case Task.DISCONNECT:
365 Main.this.running = false;
368 Main.this.onNotify((String)msg.obj);
371 Os.debug("Main: unknown message - " + msg.what);