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;
51 private Button connect;
53 private LinearLayout spades;
54 private TextView debug;
56 private ScrollView lscroll;
57 private ScrollView dscroll;
59 /* Private helper methods */
60 private int hsv2rgb(int hsv)
62 int h = (hsv & 0xff0000) >> 16;
63 int s = (hsv & 0x00ff00) >> 8;
64 int v = (hsv & 0x0000ff) >> 0;
66 int c = (v * s) / 256;
67 int h1 = (h * 6) / 256;
68 int x = c * (1 - Math.abs((h1%2)-1));
73 if (0 <= h1 && h1 <= 1) rgb = (c << 16) | (x << 8) | 0;
74 if (1 <= h1 && h1 <= 2) rgb = (x << 16) | (c << 8) | 0;
75 if (2 <= h1 && h1 <= 3) rgb = (0 << 16) | (c << 8) | x;
76 if (3 <= h1 && h1 <= 4) rgb = (0 << 16) | (x << 8) | c;
77 if (4 <= h1 && h1 <= 5) rgb = (x << 16) | (0 << 8) | c;
78 if (5 <= h1 && h1 <= 6) rgb = (c << 16) | (0 << 8) | x;
80 return rgb + (m << 16) + (m << 8) + m;
83 private void notice(String text)
85 String msg = "*** " + text + "\n";
86 Spannable span = new SpannableString(msg);
87 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
88 this.log.append(span);
91 private void display(Message msg)
93 String date = DateFormat.format("hh:mm:ss", msg.time).toString();
94 String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
95 Spannable span = new SpannableString(text);
98 int de = 1 + date.length() + 1;
99 int ne = de + 1 + msg.from.length() + 1;
103 int hash = msg.from.hashCode();
104 int color = this.hsv2rgb(hash | 0x8080) | 0xff000000;
106 // Format date and name
107 span.setSpan(new ForegroundColorSpan(0xffffff88), 0, de, 0);
108 span.setSpan(new ForegroundColorSpan(color), de+1, ne, 0);
111 for (Message.Format fmt : msg.parts) {
112 int len = fmt.txt.length();
115 if (fmt.bold && fmt.italic)
116 span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
118 span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
120 span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
122 // Striketrough / underline
124 span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
126 span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
128 // Colors (reverse not supported)
130 span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
132 span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
137 // Append the message
138 this.log.append(span);
141 private void update(boolean running)
143 this.running = running;
144 this.connect.setVisibility(running ? View.GONE : View.VISIBLE);
145 this.send.setVisibility(running ? View.VISIBLE : View.GONE);
148 /* Private handler methods */
149 private void onRegister(Task task)
151 Os.debug("Main: onRegister");
153 this.game.task = task;
154 this.update(this.task.isRunning());
155 this.log.setText("");
156 this.debug.setText("");
157 for (Object obj : this.task.getLog()) {
158 if (String.class.isInstance(obj))
159 this.notice((String)obj);
160 if (Message.class.isInstance(obj))
161 this.onMessage((Message)obj);
165 private void onMessage(Message msg)
168 this.debug.append("> " + msg.line + "\n");
169 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
175 this.game.onMessage(msg);
178 if (!msg.txt.equals(this.topic))
179 this.notice("Topic for " + msg.arg + ": " + msg.txt);
180 this.topic = msg.txt;
183 if (!msg.txt.equals(this.names))
184 this.notice("Users in " + msg.arg + ": " + msg.txt);
185 this.names = msg.txt;
188 this.notice("Error: " + msg.txt);
191 this.notice("Authentication succeeded: " + msg.txt);
194 this.notice("Authentication failed: " + msg.txt);
197 this.lscroll.smoothScrollTo(0, this.log.getBottom());
200 private void onNotify(String text)
202 Os.debug("Main: onNotify - " + text);
204 this.toast.setText(text);
208 /* Private service methods */
209 private void register()
211 Os.debug("Main: register");
212 startService(new Intent(this, Task.class)
213 .putExtra("Command", Task.REGISTER)
214 .putExtra("Messenger", this.messenger));
217 private void connect()
219 Os.debug("Main: connect");
220 startService(new Intent(this, Task.class)
221 .putExtra("Command", Task.CONNECT));
225 private void disconnect()
227 Os.debug("Main: disconnect");
228 startService(new Intent(this, Task.class)
229 .putExtra("Command", Task.DISCONNECT));
235 this.log.setText("");
236 this.debug.setText("");
237 stopService(new Intent(this, Task.class));
238 Intent intent = new Intent(Intent.ACTION_MAIN);
239 intent.addCategory(Intent.CATEGORY_HOME);
240 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
241 startActivity(intent);
244 /* Widget callback functions */
245 public void onConnect(View btn)
250 public void onSend(View btn)
252 if (this.task == null)
254 String txt = this.input.getText().toString();
255 Message msg = this.task.send(txt);
258 this.input.setText("");
261 /* Activity Methods */
263 public void onCreate(Bundle savedInstanceState)
266 super.onCreate(savedInstanceState);
267 Os.debug("Main: onCreate");
270 PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
273 this.setContentView(R.layout.main);
276 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
278 // Setup communication
279 this.handler = new MainHandler();
280 this.messenger = new Messenger(this.handler);
283 this.window = (TabHost) findViewById(android.R.id.tabhost);
284 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
285 this.chat = (LinearLayout) findViewById(R.id.chat);
286 this.log = (TextView) findViewById(R.id.log);
287 this.input = (EditText) findViewById(R.id.input);
288 this.connect = (Button) findViewById(R.id.connect);
289 this.send = (Button) findViewById(R.id.send);
290 this.spades = (LinearLayout) findViewById(R.id.spades);
291 this.debug = (TextView) findViewById(R.id.debug);
293 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
294 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
299 this.window.addTab(this.window
301 .setIndicator("Chat")
302 .setContent(R.id.chat));
303 this.window.addTab(this.window
304 .newTabSpec("spades")
305 .setIndicator("Spades")
306 .setContent(R.id.spades));
307 this.window.addTab(this.window
309 .setIndicator("Debug")
310 .setContent(R.id.debug));
312 // Setup Spades game and cards view
313 this.game = new Spades(PreferenceManager
314 .getDefaultSharedPreferences(this)
315 .getString("pref_referee", "rhawk"));
316 this.cards = new Cards(this);
318 this.game.cards = this.cards;
319 this.cards.game = this.game;
321 this.spades.addView(cards);
323 // Attach to background service
326 } catch (Exception e) {
327 Os.debug("Error setting content view", e);
333 public void onStart()
337 Os.debug("Main: onStart");
341 public void onResume()
344 Os.debug("Main: onResume");
348 public void onPause()
351 Os.debug("Main: onPause");
358 Os.debug("Main: onStop");
362 public void onRestart()
365 Os.debug("Main: onRestart");
369 public void onDestroy()
372 Os.debug("Main: onDestroy");
376 public boolean onCreateOptionsMenu(Menu menu)
378 MenuInflater inflater = getMenuInflater();
379 inflater.inflate(R.menu.main, menu);
384 public boolean onPrepareOptionsMenu(Menu menu)
386 menu.findItem(R.id.connect).setVisible(!this.running);
387 menu.findItem(R.id.disconnect).setVisible(this.running);
392 public boolean onOptionsItemSelected(MenuItem item)
394 switch (item.getItemId()) {
398 case R.id.disconnect:
402 this.startActivity(new Intent(this, Prefs.class));
413 class MainHandler extends Handler
415 public void handleMessage(android.os.Message msg)
419 Main.this.onRegister((Task)msg.obj);
422 Main.this.onMessage((Message)msg.obj);
425 Main.this.update(true);
427 case Task.DISCONNECT:
428 Main.this.update(false);
431 Main.this.onNotify((String)msg.obj);
434 Os.debug("Main: unknown message - " + msg.what);