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.preference.PreferenceManager;
9 import android.text.Html;
10 import android.text.method.ScrollingMovementMethod;
11 import android.text.format.DateFormat;
12 import android.view.Menu;
13 import android.view.MenuInflater;
14 import android.view.MenuItem;
15 import android.view.View;
16 import android.widget.Button;
17 import android.widget.EditText;
18 import android.widget.LinearLayout;
19 import android.widget.ScrollView;
20 import android.widget.TabHost;
21 import android.widget.TabWidget;
22 import android.widget.TextView;
23 import android.widget.Toast;
25 public class Main extends Activity
28 private Handler handler;
29 private Messenger messenger;
32 private boolean running;
39 private TabHost window;
40 private TabWidget tabs;
41 private LinearLayout chat;
43 private EditText input;
45 private LinearLayout spades;
46 private TextView debug;
48 private ScrollView lscroll;
49 private ScrollView dscroll;
51 /* Private helper methods */
52 private void notice(String text)
54 this.log.append(Html.fromHtml("<b>*** " + text + "</b><br />"));
57 private void display(Message msg)
59 String when = DateFormat.format("hh:mm:ss", msg.time).toString();
60 String from = String.format("<font color=\"#ff88ff\">%s</font>", msg.from);
61 String text = msg.msg;
64 // Do IRC Colors - only partly works
65 String fg = "<font color=\"<$1>\">";
67 .replaceAll("&", "&")
68 .replaceAll("<", "<")
69 .replaceAll(">", ">");
71 .replaceAll("\\002", "<b>") // bold
72 .replaceAll("\\011", "<i>") // italic
73 .replaceAll("\\025", "<u>"); // underline
75 .replaceAll("\\003(\\d+)(,\\d+)?", fg) // color
76 .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color
78 .replaceAll("<0?0>", "#000000") // White
79 .replaceAll("<0?1>", "#000000") // Black
80 .replaceAll("<0?2>", "#000080") // Navy Blue
81 .replaceAll("<0?3>", "#008000") // Green
82 .replaceAll("<0?4>", "#FF0000") // Red
83 .replaceAll("<0?5>", "#804040") // Brown
84 .replaceAll("<0?6>", "#8000FF") // Purple
85 .replaceAll("<0?7>", "#808000") // Olive
86 .replaceAll("<0?8>", "#FFFF00") // Yellow
87 .replaceAll("<0?9>", "#00FF00") // Lime Green
88 .replaceAll("<10>", "#008080") // Teal
89 .replaceAll("<11>", "#00FFFF") // Aqua Light
90 .replaceAll("<12>", "#0000FF") // Royal Blue
91 .replaceAll("<13>", "#FF00FF") // Hot Pink
92 .replaceAll("<14>", "#808080") // Dark Gray
93 .replaceAll("<15>", "#C0C0C0"); // Light Gray
100 fmt = "<b>(%s) %s: %s</b>";
103 fmt = "(%s) <b>%s</b>: %s";
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(Task task)
117 Os.debug("Main: onRegister");
119 this.game.task = task;
120 this.running = this.task.isRunning();
121 for (Object obj : this.task.getLog()) {
122 if (String.class.isInstance(obj))
123 this.notice((String)obj);
124 if (Message.class.isInstance(obj))
125 this.onMessage((Message)obj);
129 private void onMessage(Message msg)
132 this.debug.append("> " + msg.line + "\n");
133 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
139 this.game.onMessage(msg);
142 if (!msg.txt.equals(this.topic))
143 this.notice("Topic for " + msg.arg + ": " + msg.txt);
144 this.topic = msg.txt;
147 if (!msg.txt.equals(this.names))
148 this.notice("Users in " + msg.arg + ": " + msg.txt);
149 this.names = msg.txt;
152 this.notice("Error: " + msg.txt);
155 this.notice("Authentication succeeded: " + msg.txt);
158 this.notice("Authentication failed: " + msg.txt);
161 this.lscroll.smoothScrollTo(0, this.log.getBottom());
164 private void onNotify(String text)
166 Os.debug("Main: onNotify - " + text);
168 this.toast.setText(text);
172 /* Private service methods */
173 private void register()
175 Os.debug("Main: register");
176 startService(new Intent(this, Task.class)
177 .putExtra("Command", Task.REGISTER)
178 .putExtra("Messenger", this.messenger));
181 private void connect()
183 Os.debug("Main: connect");
184 startService(new Intent(this, Task.class)
185 .putExtra("Command", Task.CONNECT));
189 private void disconnect()
191 Os.debug("Main: disconnect");
192 startService(new Intent(this, Task.class)
193 .putExtra("Command", Task.DISCONNECT));
198 stopService(new Intent(this, Task.class));
201 /* Widget callback functions */
202 public void onSend(View btn)
204 if (this.task == null)
206 String txt = this.input.getText().toString();
207 Message msg = this.task.send(txt);
210 this.input.setText("");
213 /* Activity Methods */
215 public void onCreate(Bundle savedInstanceState)
218 super.onCreate(savedInstanceState);
219 Os.debug("Main: onCreate");
222 PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
225 this.setContentView(R.layout.main);
228 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
230 // Setup communication
231 this.handler = new MainHandler();
232 this.messenger = new Messenger(this.handler);
235 this.window = (TabHost) findViewById(android.R.id.tabhost);
236 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
237 this.chat = (LinearLayout) findViewById(R.id.chat);
238 this.log = (TextView) findViewById(R.id.log);
239 this.input = (EditText) findViewById(R.id.input);
240 this.send = (Button) findViewById(R.id.send);
241 this.spades = (LinearLayout) findViewById(R.id.spades);
242 this.debug = (TextView) findViewById(R.id.debug);
244 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
245 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
250 this.window.addTab(this.window
252 .setIndicator("Chat")
253 .setContent(R.id.chat));
254 this.window.addTab(this.window
255 .newTabSpec("spades")
256 .setIndicator("Spades")
257 .setContent(R.id.spades));
258 this.window.addTab(this.window
260 .setIndicator("Debug")
261 .setContent(R.id.debug));
263 // Setup Spades game and cards view
264 this.game = new Spades(PreferenceManager
265 .getDefaultSharedPreferences(this)
266 .getString("pref_referee", "rhawk"));
267 this.cards = new Cards(this);
269 this.game.cards = this.cards;
270 this.cards.game = this.game;
272 this.spades.addView(cards);
274 // Attach to background service
277 } catch (Exception e) {
278 Os.debug("Error setting content view", e);
284 public void onStart()
287 Os.debug("Main: onStart");
291 public void onResume()
294 Os.debug("Main: onResume");
298 public void onPause()
301 Os.debug("Main: onPause");
308 Os.debug("Main: onStop");
312 public void onRestart()
315 Os.debug("Main: onRestart");
319 public void onDestroy()
322 Os.debug("Main: onDestroy");
326 public boolean onCreateOptionsMenu(Menu menu)
328 MenuInflater inflater = getMenuInflater();
329 inflater.inflate(R.menu.main, menu);
334 public boolean onPrepareOptionsMenu(Menu menu)
336 menu.findItem(R.id.connect).setVisible(!this.running);
337 menu.findItem(R.id.disconnect).setVisible(this.running);
342 public boolean onOptionsItemSelected(MenuItem item)
344 switch (item.getItemId()) {
348 case R.id.disconnect:
352 this.startActivity(new Intent(this, Prefs.class));
364 class MainHandler extends Handler
366 public void handleMessage(android.os.Message msg)
370 Main.this.onRegister((Task)msg.obj);
373 Main.this.onMessage((Message)msg.obj);
376 Main.this.running = true;
378 case Task.DISCONNECT:
379 Main.this.running = false;
382 Main.this.onNotify((String)msg.obj);
385 Os.debug("Main: unknown message - " + msg.what);