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 int hsv2rgb(int hsv)
61 int h = (hsv & 0xff0000) >> 16;
62 int s = (hsv & 0x00ff00) >> 8;
63 int v = (hsv & 0x0000ff) >> 0;
65 int c = (v * s) / 256;
66 int h1 = (h * 6) / 256;
67 int x = c * (1 - Math.abs((h1%2)-1));
72 if (0 <= h1 && h1 <= 1) rgb = (c << 16) | (x << 8) | 0;
73 if (1 <= h1 && h1 <= 2) rgb = (x << 16) | (c << 8) | 0;
74 if (2 <= h1 && h1 <= 3) rgb = (0 << 16) | (c << 8) | x;
75 if (3 <= h1 && h1 <= 4) rgb = (0 << 16) | (x << 8) | c;
76 if (4 <= h1 && h1 <= 5) rgb = (x << 16) | (0 << 8) | c;
77 if (5 <= h1 && h1 <= 6) rgb = (c << 16) | (0 << 8) | x;
79 return rgb + (m << 16) + (m << 8) + m;
82 private void notice(String text)
84 String msg = "*** " + text + "\n";
85 Spannable span = new SpannableString(msg);
86 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
87 this.log.append(span);
90 private void display(Message msg)
92 String date = DateFormat.format("hh:mm:ss", msg.time).toString();
93 String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
94 Spannable span = new SpannableString(text);
97 int de = 1 + date.length() + 1;
98 int ne = de + 1 + msg.from.length() + 1;
102 int hash = msg.from.hashCode();
103 int color = this.hsv2rgb(hash | 0x8080) | 0xff000000;
105 // Format date and name
106 span.setSpan(new ForegroundColorSpan(0xffffff88), 0, de, 0);
107 span.setSpan(new ForegroundColorSpan(color), de+1, ne, 0);
110 for (Message.Format fmt : msg.parts) {
111 int len = fmt.txt.length();
114 if (fmt.bold && fmt.italic)
115 span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
117 span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
119 span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
121 // Striketrough / underline
123 span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
125 span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
127 // Colors (reverse not supported)
129 span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
131 span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
136 // Append the message
137 this.log.append(span);
140 /* Private handler methods */
141 private void onRegister(Task task)
143 Os.debug("Main: onRegister");
145 this.game.task = task;
146 this.running = this.task.isRunning();
147 for (Object obj : this.task.getLog()) {
148 if (String.class.isInstance(obj))
149 this.notice((String)obj);
150 if (Message.class.isInstance(obj))
151 this.onMessage((Message)obj);
155 private void onMessage(Message msg)
158 this.debug.append("> " + msg.line + "\n");
159 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
165 this.game.onMessage(msg);
168 if (!msg.txt.equals(this.topic))
169 this.notice("Topic for " + msg.arg + ": " + msg.txt);
170 this.topic = msg.txt;
173 if (!msg.txt.equals(this.names))
174 this.notice("Users in " + msg.arg + ": " + msg.txt);
175 this.names = msg.txt;
178 this.notice("Error: " + msg.txt);
181 this.notice("Authentication succeeded: " + msg.txt);
184 this.notice("Authentication failed: " + msg.txt);
187 this.lscroll.smoothScrollTo(0, this.log.getBottom());
190 private void onNotify(String text)
192 Os.debug("Main: onNotify - " + text);
194 this.toast.setText(text);
198 /* Private service methods */
199 private void register()
201 Os.debug("Main: register");
202 startService(new Intent(this, Task.class)
203 .putExtra("Command", Task.REGISTER)
204 .putExtra("Messenger", this.messenger));
207 private void connect()
209 Os.debug("Main: connect");
210 startService(new Intent(this, Task.class)
211 .putExtra("Command", Task.CONNECT));
215 private void disconnect()
217 Os.debug("Main: disconnect");
218 startService(new Intent(this, Task.class)
219 .putExtra("Command", Task.DISCONNECT));
220 this.running = false;
225 stopService(new Intent(this, Task.class));
226 Intent intent = new Intent(Intent.ACTION_MAIN);
227 intent.addCategory(Intent.CATEGORY_HOME);
228 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
229 startActivity(intent);
232 /* Widget callback functions */
233 public void onSend(View btn)
235 if (this.task == null)
237 String txt = this.input.getText().toString();
238 Message msg = this.task.send(txt);
241 this.input.setText("");
244 /* Activity Methods */
246 public void onCreate(Bundle savedInstanceState)
249 super.onCreate(savedInstanceState);
250 Os.debug("Main: onCreate");
253 PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
256 this.setContentView(R.layout.main);
259 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
261 // Setup communication
262 this.handler = new MainHandler();
263 this.messenger = new Messenger(this.handler);
266 this.window = (TabHost) findViewById(android.R.id.tabhost);
267 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
268 this.chat = (LinearLayout) findViewById(R.id.chat);
269 this.log = (TextView) findViewById(R.id.log);
270 this.input = (EditText) findViewById(R.id.input);
271 this.send = (Button) findViewById(R.id.send);
272 this.spades = (LinearLayout) findViewById(R.id.spades);
273 this.debug = (TextView) findViewById(R.id.debug);
275 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
276 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
281 this.window.addTab(this.window
283 .setIndicator("Chat")
284 .setContent(R.id.chat));
285 this.window.addTab(this.window
286 .newTabSpec("spades")
287 .setIndicator("Spades")
288 .setContent(R.id.spades));
289 this.window.addTab(this.window
291 .setIndicator("Debug")
292 .setContent(R.id.debug));
294 // Setup Spades game and cards view
295 this.game = new Spades(PreferenceManager
296 .getDefaultSharedPreferences(this)
297 .getString("pref_referee", "rhawk"));
298 this.cards = new Cards(this);
300 this.game.cards = this.cards;
301 this.cards.game = this.game;
303 this.spades.addView(cards);
305 // Attach to background service
308 } catch (Exception e) {
309 Os.debug("Error setting content view", e);
315 public void onStart()
318 Os.debug("Main: onStart");
322 public void onResume()
325 Os.debug("Main: onResume");
329 public void onPause()
332 Os.debug("Main: onPause");
339 Os.debug("Main: onStop");
343 public void onRestart()
346 Os.debug("Main: onRestart");
350 public void onDestroy()
353 Os.debug("Main: onDestroy");
357 public boolean onCreateOptionsMenu(Menu menu)
359 MenuInflater inflater = getMenuInflater();
360 inflater.inflate(R.menu.main, menu);
365 public boolean onPrepareOptionsMenu(Menu menu)
367 menu.findItem(R.id.connect).setVisible(!this.running);
368 menu.findItem(R.id.disconnect).setVisible(this.running);
373 public boolean onOptionsItemSelected(MenuItem item)
375 switch (item.getItemId()) {
379 case R.id.disconnect:
383 this.startActivity(new Intent(this, Prefs.class));
394 class MainHandler extends Handler
396 public void handleMessage(android.os.Message msg)
400 Main.this.onRegister((Task)msg.obj);
403 Main.this.onMessage((Message)msg.obj);
406 Main.this.running = true;
408 case Task.DISCONNECT:
409 Main.this.running = false;
412 Main.this.onNotify((String)msg.obj);
415 Os.debug("Main: unknown message - " + msg.what);