]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Main.java
Clear logs on quit
[~andy/spades] / src / org / pileus / spades / Main.java
index f5118c13f1793f860428616dec2d59cc49390f14..e4bac7fc96d83284d25c7aea8df8254dd6572c07 100644 (file)
@@ -2,10 +2,20 @@ package org.pileus.spades;
 
 import android.app.Activity;
 import android.content.Intent;
+import android.graphics.Color;
+import android.graphics.Typeface;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Messenger;
-import android.text.method.ScrollingMovementMethod;
+import android.preference.PreferenceManager;
+import android.text.Spannable;
+import android.text.SpannableString;
+import android.text.format.DateFormat;
+import android.text.style.BackgroundColorSpan;
+import android.text.style.ForegroundColorSpan;
+import android.text.style.StrikethroughSpan;
+import android.text.style.StyleSpan;
+import android.text.style.UnderlineSpan;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -19,20 +29,18 @@ import android.widget.TabWidget;
 import android.widget.TextView;
 import android.widget.Toast;
 
-import android.preference.PreferenceActivity;
-
 public class Main extends Activity
 {
-       /* Static data */
+       /* Private data */
        private Handler      handler;
        private Messenger    messenger;
-
-       /* Private data */
        private Task         task;
        private Toast        toast;
-       private boolean      ready;
+       private boolean      running;
        private String       topic;
        private String       names;
+       private Cards        cards;
+       private Spades       game;
 
        /* Widgets */
        private TabHost      window;
@@ -41,29 +49,111 @@ public class Main extends Activity
        private TextView     log;
        private EditText     input;
        private Button       send;
-       private TextView     spades;
+       private LinearLayout spades;
        private TextView     debug;
 
        private ScrollView   lscroll;
        private ScrollView   dscroll;
 
        /* Private helper methods */
+       private int hsv2rgb(int hsv)
+       {
+               int h  = (hsv & 0xff0000) >> 16;
+               int s  = (hsv & 0x00ff00) >>  8;
+               int v  = (hsv & 0x0000ff) >>  0;
+
+               int c  = (v * s) / 256;
+               int h1 = (h * 6) / 256;
+               int x  = c * (1 - Math.abs((h1%2)-1));
+               int m  = v - c;
+
+               int rgb = 0;
+
+               if (0 <= h1 && h1 <= 1) rgb = (c << 16) | (x << 8) | 0;
+               if (1 <= h1 && h1 <= 2) rgb = (x << 16) | (c << 8) | 0;
+               if (2 <= h1 && h1 <= 3) rgb = (0 << 16) | (c << 8) | x;
+               if (3 <= h1 && h1 <= 4) rgb = (0 << 16) | (x << 8) | c;
+               if (4 <= h1 && h1 <= 5) rgb = (x << 16) | (0 << 8) | c;
+               if (5 <= h1 && h1 <= 6) rgb = (c << 16) | (0 << 8) | x;
+
+               return rgb + (m << 16) + (m << 8) + m;
+       }
+
        private void notice(String text)
        {
-               this.log.append("*** " + text + "\n");
+               String    msg  = "*** " + text + "\n";
+               Spannable span = new SpannableString(msg);
+               span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
+               this.log.append(span);
+       }
+
+       private void display(Message msg)
+       {
+               String date = DateFormat.format("hh:mm:ss", msg.time).toString();
+               String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
+               Spannable span = new SpannableString(text);
+
+               // Determin positions
+               int de  = 1 + date.length() + 1;
+               int ne  = de + 1 + msg.from.length() + 1;
+               int pos = ne + 1;
+
+               // Get user color
+               int hash  = msg.from.hashCode();
+               int color = this.hsv2rgb(hash | 0x8080) | 0xff000000;
+
+               // Format date and name
+               span.setSpan(new ForegroundColorSpan(0xffffff88), 0,    de, 0);
+               span.setSpan(new ForegroundColorSpan(color),      de+1, ne, 0);
+
+               // Format IRC Colors
+               for (Message.Format fmt : msg.parts) {
+                       int len = fmt.txt.length();
+
+                       // Bold/italics
+                       if (fmt.bold && fmt.italic)
+                               span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
+                       else if (fmt.bold)
+                               span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
+                       else if (fmt.italic)
+                               span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
+
+                       // Striketrough / underline
+                       if (fmt.strike)
+                               span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
+                       if (fmt.underline)
+                               span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
+
+                       // Colors (reverse not supported)
+                       if (fmt.fg!=null)
+                               span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
+                       if (fmt.bg!=null)
+                               span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
+
+                       pos += len;
+               }
+
+               // Append the message
+               this.log.append(span);
        }
 
        /* Private handler methods */
-       private void onRegister(Object obj)
+       private void onRegister(Task task)
        {
                Os.debug("Main: onRegister");
-               this.task = (Task)obj;
+               this.task      = task;
+               this.game.task = task;
+               this.running = this.task.isRunning();
+               for (Object obj : this.task.getLog()) {
+                       if (String.class.isInstance(obj))
+                               this.notice((String)obj);
+                       if (Message.class.isInstance(obj))
+                               this.onMessage((Message)obj);
+               }
        }
 
-       private void onMessage(Object obj)
+       private void onMessage(Message msg)
        {
-               Message msg = (Message)obj;
-
                // Debug
                this.debug.append("> " + msg.line + "\n");
                this.dscroll.smoothScrollTo(0, this.debug.getBottom());
@@ -71,7 +161,8 @@ public class Main extends Activity
                // Chat
                switch (msg.type) {
                        case PRIVMSG:
-                               this.log.append(msg.from + ": " + msg.msg + "\n");
+                               this.display(msg);
+                               this.game.onMessage(msg);
                                break;
                        case TOPIC:
                                if (!msg.txt.equals(this.topic))
@@ -105,17 +196,39 @@ public class Main extends Activity
        }
 
        /* Private service methods */
-       private void startService()
+       private void register()
        {
-               Os.debug("Main: startService");
+               Os.debug("Main: register");
                startService(new Intent(this, Task.class)
+                               .putExtra("Command",   Task.REGISTER)
                                .putExtra("Messenger", this.messenger));
        }
 
-       private void stopService()
+       private void connect()
+       {
+               Os.debug("Main: connect");
+               startService(new Intent(this, Task.class)
+                               .putExtra("Command", Task.CONNECT));
+               this.running = true;
+       }
+
+       private void disconnect()
+       {
+               Os.debug("Main: disconnect");
+               startService(new Intent(this, Task.class)
+                               .putExtra("Command", Task.DISCONNECT));
+               this.running = false;
+       }
+
+       private void quit()
        {
-               Os.debug("Main: stopService");
+               this.log.setText("");
+               this.debug.setText("");
                stopService(new Intent(this, Task.class));
+               Intent intent = new Intent(Intent.ACTION_MAIN);
+               intent.addCategory(Intent.CATEGORY_HOME);
+               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+               startActivity(intent);
        }
 
        /* Widget callback functions */
@@ -138,6 +251,9 @@ public class Main extends Activity
                        super.onCreate(savedInstanceState);
                        Os.debug("Main: onCreate");
 
+                       // Setup preferences
+                       PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
+
                        // Setup main layout
                        this.setContentView(R.layout.main);
 
@@ -155,7 +271,7 @@ public class Main extends Activity
                        this.log       = (TextView)     findViewById(R.id.log);
                        this.input     = (EditText)     findViewById(R.id.input);
                        this.send      = (Button)       findViewById(R.id.send);
-                       this.spades    = (TextView)     findViewById(R.id.spades);
+                       this.spades    = (LinearLayout) findViewById(R.id.spades);
                        this.debug     = (TextView)     findViewById(R.id.debug);
 
                        this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
@@ -176,6 +292,21 @@ public class Main extends Activity
                                        .newTabSpec("debug")
                                        .setIndicator("Debug")
                                        .setContent(R.id.debug));
+
+                       // Setup Spades game and cards view
+                       this.game  = new Spades(PreferenceManager
+                                       .getDefaultSharedPreferences(this)
+                                       .getString("pref_referee", "rhawk"));
+                       this.cards = new Cards(this);
+
+                       this.game.cards = this.cards;
+                       this.cards.game = this.game;
+
+                       this.spades.addView(cards);
+
+                       // Attach to background service
+                       this.register();
+
                } catch (Exception e) {
                        Os.debug("Error setting content view", e);
                        return;
@@ -186,6 +317,7 @@ public class Main extends Activity
        public void onStart()
        {
                super.onStart();
+               this.register();
                Os.debug("Main: onStart");
        }
 
@@ -235,8 +367,8 @@ public class Main extends Activity
        @Override
        public boolean onPrepareOptionsMenu(Menu menu)
        {
-               menu.findItem(R.id.connect).setVisible(!this.ready);
-               menu.findItem(R.id.disconnect).setVisible(this.ready);
+               menu.findItem(R.id.connect).setVisible(!this.running);
+               menu.findItem(R.id.disconnect).setVisible(this.running);
                return true;
        }
 
@@ -245,17 +377,16 @@ public class Main extends Activity
        {
                switch (item.getItemId()) {
                        case R.id.connect:
-                               this.startService();
+                               this.connect();
                                return true;
                        case R.id.disconnect:
-                               this.stopService();
+                               this.disconnect();
                                return true;
                        case R.id.settings:
                                this.startActivity(new Intent(this, Prefs.class));
                                return true;
-                       case R.id.exit:
-                               this.stopService();
-                               this.finish();
+                       case R.id.quit:
+                               this.quit();
                                return true;
                        default:
                                return false;
@@ -269,16 +400,16 @@ public class Main extends Activity
                {
                        switch (msg.what) {
                                case Task.REGISTER:
-                                       Main.this.onRegister(msg.obj);
+                                       Main.this.onRegister((Task)msg.obj);
                                        break;
                                case Task.MESSAGE:
-                                       Main.this.onMessage(msg.obj);
+                                       Main.this.onMessage((Message)msg.obj);
                                        break;
                                case Task.CONNECT:
-                                       Main.this.ready = true;
+                                       Main.this.running = true;
                                        break;
                                case Task.DISCONNECT:
-                                       Main.this.ready = false;
+                                       Main.this.running = false;
                                        break;
                                case Task.NOTIFY:
                                        Main.this.onNotify((String)msg.obj);