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));
199 Intent intent = new Intent(Intent.ACTION_MAIN);
200 intent.addCategory(Intent.CATEGORY_HOME);
201 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
202 startActivity(intent);
205 /* Widget callback functions */
206 public void onSend(View btn)
208 if (this.task == null)
210 String txt = this.input.getText().toString();
211 Message msg = this.task.send(txt);
214 this.input.setText("");
217 /* Activity Methods */
219 public void onCreate(Bundle savedInstanceState)
222 super.onCreate(savedInstanceState);
223 Os.debug("Main: onCreate");
226 PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
229 this.setContentView(R.layout.main);
232 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
234 // Setup communication
235 this.handler = new MainHandler();
236 this.messenger = new Messenger(this.handler);
239 this.window = (TabHost) findViewById(android.R.id.tabhost);
240 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
241 this.chat = (LinearLayout) findViewById(R.id.chat);
242 this.log = (TextView) findViewById(R.id.log);
243 this.input = (EditText) findViewById(R.id.input);
244 this.send = (Button) findViewById(R.id.send);
245 this.spades = (LinearLayout) findViewById(R.id.spades);
246 this.debug = (TextView) findViewById(R.id.debug);
248 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
249 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
254 this.window.addTab(this.window
256 .setIndicator("Chat")
257 .setContent(R.id.chat));
258 this.window.addTab(this.window
259 .newTabSpec("spades")
260 .setIndicator("Spades")
261 .setContent(R.id.spades));
262 this.window.addTab(this.window
264 .setIndicator("Debug")
265 .setContent(R.id.debug));
267 // Setup Spades game and cards view
268 this.game = new Spades(PreferenceManager
269 .getDefaultSharedPreferences(this)
270 .getString("pref_referee", "rhawk"));
271 this.cards = new Cards(this);
273 this.game.cards = this.cards;
274 this.cards.game = this.game;
276 this.spades.addView(cards);
278 // Attach to background service
281 } catch (Exception e) {
282 Os.debug("Error setting content view", e);
288 public void onStart()
291 Os.debug("Main: onStart");
295 public void onResume()
298 Os.debug("Main: onResume");
302 public void onPause()
305 Os.debug("Main: onPause");
312 Os.debug("Main: onStop");
316 public void onRestart()
319 Os.debug("Main: onRestart");
323 public void onDestroy()
326 Os.debug("Main: onDestroy");
330 public boolean onCreateOptionsMenu(Menu menu)
332 MenuInflater inflater = getMenuInflater();
333 inflater.inflate(R.menu.main, menu);
338 public boolean onPrepareOptionsMenu(Menu menu)
340 menu.findItem(R.id.connect).setVisible(!this.running);
341 menu.findItem(R.id.disconnect).setVisible(this.running);
346 public boolean onOptionsItemSelected(MenuItem item)
348 switch (item.getItemId()) {
352 case R.id.disconnect:
356 this.startActivity(new Intent(this, Prefs.class));
367 class MainHandler extends Handler
369 public void handleMessage(android.os.Message msg)
373 Main.this.onRegister((Task)msg.obj);
376 Main.this.onMessage((Message)msg.obj);
379 Main.this.running = true;
381 case Task.DISCONNECT:
382 Main.this.running = false;
385 Main.this.onNotify((String)msg.obj);
388 Os.debug("Main: unknown message - " + msg.what);