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;
38 private TabHost window;
39 private TabWidget tabs;
40 private LinearLayout chat;
42 private EditText input;
44 private LinearLayout 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(Html.fromHtml("<b>*** " + text + "</b><br />"));
56 private void display(Message msg)
58 String when = DateFormat.format("hh:mm:ss", msg.time).toString();
59 String from = String.format("<font color=\"#ff88ff\">%s</font>", msg.from);
60 String text = msg.msg;
63 // Do IRC Colors - only partly works
64 String fg = "<font color=\"<$1>\">";
66 .replaceAll("&", "&")
67 .replaceAll("<", "<")
68 .replaceAll(">", ">");
70 .replaceAll("\\002", "<b>") // bold
71 .replaceAll("\\011", "<i>") // italic
72 .replaceAll("\\025", "<u>"); // underline
74 .replaceAll("\\003(\\d+)(,\\d+)?", fg) // color
75 .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color
77 .replaceAll("<0?0>", "#000000") // White
78 .replaceAll("<0?1>", "#000000") // Black
79 .replaceAll("<0?2>", "#000080") // Navy Blue
80 .replaceAll("<0?3>", "#008000") // Green
81 .replaceAll("<0?4>", "#FF0000") // Red
82 .replaceAll("<0?5>", "#804040") // Brown
83 .replaceAll("<0?6>", "#8000FF") // Purple
84 .replaceAll("<0?7>", "#808000") // Olive
85 .replaceAll("<0?8>", "#FFFF00") // Yellow
86 .replaceAll("<0?9>", "#00FF00") // Lime Green
87 .replaceAll("<10>", "#008080") // Teal
88 .replaceAll("<11>", "#00FFFF") // Aqua Light
89 .replaceAll("<12>", "#0000FF") // Royal Blue
90 .replaceAll("<13>", "#FF00FF") // Hot Pink
91 .replaceAll("<14>", "#808080") // Dark Gray
92 .replaceAll("<15>", "#C0C0C0"); // Light Gray
99 fmt = "<b>(%s) %s: %s</b>";
102 fmt = "(%s) <b>%s</b>: %s";
109 String html = String.format(fmt, when, from, text);
110 this.log.append(Html.fromHtml(html + "<br />"));
113 /* Private handler methods */
114 private void onRegister(Task task)
116 Os.debug("Main: onRegister");
118 this.game.task = task;
119 this.running = this.task.isRunning();
120 for (Object obj : this.task.getLog()) {
121 if (String.class.isInstance(obj))
122 this.notice((String)obj);
123 if (Message.class.isInstance(obj))
124 this.onMessage((Message)obj);
128 private void onMessage(Message msg)
131 this.debug.append("> " + msg.line + "\n");
132 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
138 this.game.onMessage(msg);
141 if (!msg.txt.equals(this.topic))
142 this.notice("Topic for " + msg.arg + ": " + msg.txt);
143 this.topic = msg.txt;
146 if (!msg.txt.equals(this.names))
147 this.notice("Users in " + msg.arg + ": " + msg.txt);
148 this.names = msg.txt;
151 this.notice("Error: " + msg.txt);
154 this.notice("Authentication succeeded: " + msg.txt);
157 this.notice("Authentication failed: " + msg.txt);
160 this.lscroll.smoothScrollTo(0, this.log.getBottom());
163 private void onNotify(String text)
165 Os.debug("Main: onNotify - " + text);
167 this.toast.setText(text);
171 /* Private service methods */
172 private void register()
174 Os.debug("Main: register");
175 startService(new Intent(this, Task.class)
176 .putExtra("Command", Task.REGISTER)
177 .putExtra("Messenger", this.messenger));
180 private void connect()
182 Os.debug("Main: connect");
183 startService(new Intent(this, Task.class)
184 .putExtra("Command", Task.CONNECT));
188 private void disconnect()
190 Os.debug("Main: disconnect");
191 startService(new Intent(this, Task.class)
192 .putExtra("Command", Task.DISCONNECT));
197 stopService(new Intent(this, Task.class));
200 /* Widget callback functions */
201 public void onSend(View btn)
203 if (this.task == null)
205 String txt = this.input.getText().toString();
206 Message msg = this.task.send(txt);
209 this.input.setText("");
212 /* Activity Methods */
214 public void onCreate(Bundle savedInstanceState)
217 super.onCreate(savedInstanceState);
218 Os.debug("Main: onCreate");
221 this.setContentView(R.layout.main);
224 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
226 // Setup communication
227 this.handler = new MainHandler();
228 this.messenger = new Messenger(this.handler);
231 this.window = (TabHost) findViewById(android.R.id.tabhost);
232 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
233 this.chat = (LinearLayout) findViewById(R.id.chat);
234 this.log = (TextView) findViewById(R.id.log);
235 this.input = (EditText) findViewById(R.id.input);
236 this.send = (Button) findViewById(R.id.send);
237 this.spades = (LinearLayout) findViewById(R.id.spades);
238 this.debug = (TextView) findViewById(R.id.debug);
240 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
241 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
246 this.window.addTab(this.window
248 .setIndicator("Chat")
249 .setContent(R.id.chat));
250 this.window.addTab(this.window
251 .newTabSpec("spades")
252 .setIndicator("Spades")
253 .setContent(R.id.spades));
254 this.window.addTab(this.window
256 .setIndicator("Debug")
257 .setContent(R.id.debug));
259 // Setup Spades game and cards view
260 this.game = new Spades("rhawk");
261 this.cards = new Cards(this);
263 this.game.cards = this.cards;
264 this.cards.game = this.game;
266 this.spades.addView(cards);
268 // Attach to background service
271 } catch (Exception e) {
272 Os.debug("Error setting content view", e);
278 public void onStart()
281 Os.debug("Main: onStart");
285 public void onResume()
288 Os.debug("Main: onResume");
292 public void onPause()
295 Os.debug("Main: onPause");
302 Os.debug("Main: onStop");
306 public void onRestart()
309 Os.debug("Main: onRestart");
313 public void onDestroy()
316 Os.debug("Main: onDestroy");
320 public boolean onCreateOptionsMenu(Menu menu)
322 MenuInflater inflater = getMenuInflater();
323 inflater.inflate(R.menu.main, menu);
328 public boolean onPrepareOptionsMenu(Menu menu)
330 menu.findItem(R.id.connect).setVisible(!this.running);
331 menu.findItem(R.id.disconnect).setVisible(this.running);
336 public boolean onOptionsItemSelected(MenuItem item)
338 switch (item.getItemId()) {
342 case R.id.disconnect:
346 this.startActivity(new Intent(this, Prefs.class));
358 class MainHandler extends Handler
360 public void handleMessage(android.os.Message msg)
364 Main.this.onRegister((Task)msg.obj);
367 Main.this.onMessage((Message)msg.obj);
370 Main.this.running = true;
372 case Task.DISCONNECT:
373 Main.this.running = false;
376 Main.this.onNotify((String)msg.obj);
379 Os.debug("Main: unknown message - " + msg.what);