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 this.log.setText("");
148 this.debug.setText("");
149 for (Object obj : this.task.getLog()) {
150 if (String.class.isInstance(obj))
151 this.notice((String)obj);
152 if (Message.class.isInstance(obj))
153 this.onMessage((Message)obj);
157 private void onMessage(Message msg)
160 this.debug.append("> " + msg.line + "\n");
161 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
167 this.game.onMessage(msg);
170 if (!msg.txt.equals(this.topic))
171 this.notice("Topic for " + msg.arg + ": " + msg.txt);
172 this.topic = msg.txt;
175 if (!msg.txt.equals(this.names))
176 this.notice("Users in " + msg.arg + ": " + msg.txt);
177 this.names = msg.txt;
180 this.notice("Error: " + msg.txt);
183 this.notice("Authentication succeeded: " + msg.txt);
186 this.notice("Authentication failed: " + msg.txt);
189 this.lscroll.smoothScrollTo(0, this.log.getBottom());
192 private void onNotify(String text)
194 Os.debug("Main: onNotify - " + text);
196 this.toast.setText(text);
200 /* Private service methods */
201 private void register()
203 Os.debug("Main: register");
204 startService(new Intent(this, Task.class)
205 .putExtra("Command", Task.REGISTER)
206 .putExtra("Messenger", this.messenger));
209 private void connect()
211 Os.debug("Main: connect");
212 startService(new Intent(this, Task.class)
213 .putExtra("Command", Task.CONNECT));
217 private void disconnect()
219 Os.debug("Main: disconnect");
220 startService(new Intent(this, Task.class)
221 .putExtra("Command", Task.DISCONNECT));
222 this.running = false;
227 this.log.setText("");
228 this.debug.setText("");
229 stopService(new Intent(this, Task.class));
230 Intent intent = new Intent(Intent.ACTION_MAIN);
231 intent.addCategory(Intent.CATEGORY_HOME);
232 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
233 startActivity(intent);
236 /* Widget callback functions */
237 public void onSend(View btn)
239 if (this.task == null)
241 String txt = this.input.getText().toString();
242 Message msg = this.task.send(txt);
245 this.input.setText("");
248 /* Activity Methods */
250 public void onCreate(Bundle savedInstanceState)
253 super.onCreate(savedInstanceState);
254 Os.debug("Main: onCreate");
257 PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
260 this.setContentView(R.layout.main);
263 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
265 // Setup communication
266 this.handler = new MainHandler();
267 this.messenger = new Messenger(this.handler);
270 this.window = (TabHost) findViewById(android.R.id.tabhost);
271 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
272 this.chat = (LinearLayout) findViewById(R.id.chat);
273 this.log = (TextView) findViewById(R.id.log);
274 this.input = (EditText) findViewById(R.id.input);
275 this.send = (Button) findViewById(R.id.send);
276 this.spades = (LinearLayout) findViewById(R.id.spades);
277 this.debug = (TextView) findViewById(R.id.debug);
279 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
280 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
285 this.window.addTab(this.window
287 .setIndicator("Chat")
288 .setContent(R.id.chat));
289 this.window.addTab(this.window
290 .newTabSpec("spades")
291 .setIndicator("Spades")
292 .setContent(R.id.spades));
293 this.window.addTab(this.window
295 .setIndicator("Debug")
296 .setContent(R.id.debug));
298 // Setup Spades game and cards view
299 this.game = new Spades(PreferenceManager
300 .getDefaultSharedPreferences(this)
301 .getString("pref_referee", "rhawk"));
302 this.cards = new Cards(this);
304 this.game.cards = this.cards;
305 this.cards.game = this.game;
307 this.spades.addView(cards);
309 // Attach to background service
312 } catch (Exception e) {
313 Os.debug("Error setting content view", e);
319 public void onStart()
323 Os.debug("Main: onStart");
327 public void onResume()
330 Os.debug("Main: onResume");
334 public void onPause()
337 Os.debug("Main: onPause");
344 Os.debug("Main: onStop");
348 public void onRestart()
351 Os.debug("Main: onRestart");
355 public void onDestroy()
358 Os.debug("Main: onDestroy");
362 public boolean onCreateOptionsMenu(Menu menu)
364 MenuInflater inflater = getMenuInflater();
365 inflater.inflate(R.menu.main, menu);
370 public boolean onPrepareOptionsMenu(Menu menu)
372 menu.findItem(R.id.connect).setVisible(!this.running);
373 menu.findItem(R.id.disconnect).setVisible(this.running);
378 public boolean onOptionsItemSelected(MenuItem item)
380 switch (item.getItemId()) {
384 case R.id.disconnect:
388 this.startActivity(new Intent(this, Prefs.class));
399 class MainHandler extends Handler
401 public void handleMessage(android.os.Message msg)
405 Main.this.onRegister((Task)msg.obj);
408 Main.this.onMessage((Message)msg.obj);
411 Main.this.running = true;
413 case Task.DISCONNECT:
414 Main.this.running = false;
417 Main.this.onNotify((String)msg.obj);
420 Os.debug("Main: unknown message - " + msg.what);