1 package org.pileus.spades;
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.graphics.Color;
6 import android.graphics.Typeface;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Messenger;
10 import android.preference.PreferenceManager;
11 import android.text.Spannable;
12 import android.text.SpannableString;
13 import android.text.format.DateFormat;
14 import android.text.style.BackgroundColorSpan;
15 import android.text.style.ForegroundColorSpan;
16 import android.text.style.StrikethroughSpan;
17 import android.text.style.StyleSpan;
18 import android.text.style.UnderlineSpan;
19 import android.view.Menu;
20 import android.view.MenuInflater;
21 import android.view.MenuItem;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.EditText;
25 import android.widget.LinearLayout;
26 import android.widget.ScrollView;
27 import android.widget.TabHost;
28 import android.widget.TabWidget;
29 import android.widget.TextView;
30 import android.widget.Toast;
32 public class Main extends Activity
35 private Handler handler;
36 private Messenger messenger;
39 private boolean running;
46 private TabHost window;
47 private TabWidget tabs;
48 private LinearLayout chat;
50 private EditText input;
52 private LinearLayout spades;
53 private TextView debug;
55 private ScrollView lscroll;
56 private ScrollView dscroll;
58 /* Private helper methods */
59 private void notice(String text)
61 String msg = "*** " + text + "\n";
62 Spannable span = new SpannableString(msg);
63 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
64 this.log.append(span);
67 private void display(Message msg)
69 String date = DateFormat.format("hh:mm:ss", msg.time).toString();
70 String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
71 Spannable span = new SpannableString(text);
74 int de = 1 + date.length() + 1;
75 int ne = de + 1 + msg.from.length() + 1;
78 // Format date and name
79 span.setSpan(new ForegroundColorSpan(0xffffff88), 0, de, 0);
80 span.setSpan(new ForegroundColorSpan(0xffff88ff), de+1, ne, 0);
83 for (Message.Format fmt : msg.parts) {
84 int len = fmt.txt.length();
87 if (fmt.bold && fmt.italic)
88 span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
90 span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
92 span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
94 // Striketrough / underline
96 span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
98 span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
100 // Colors (reverse not supported)
102 span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
104 span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
109 // Append the message
110 this.log.append(span);
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));
193 this.running = false;
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);