]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Main.java
Store Task state so we can restart Main
[~andy/spades] / src / org / pileus / spades / Main.java
index 05955f1e56376f7078076cf0f7d02ca8089d3f3b..502d5ae9b735e8da076d26461fe84be0c66cc0fd 100644 (file)
@@ -5,7 +5,9 @@ import android.content.Intent;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Messenger;
+import android.text.Html;
 import android.text.method.ScrollingMovementMethod;
+import android.text.format.DateFormat;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -19,20 +21,17 @@ 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;
 
        /* Widgets */
        private TabHost      window;
@@ -41,23 +40,91 @@ 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 void notice(String text)
+       {
+               this.log.append(Html.fromHtml("<b>*** " + text + "</b><br />"));
+       }
+
+       private void display(Message msg)
+       {
+               String when = DateFormat.format("hh:mm:ss", msg.time).toString();
+               String from = String.format("<font color=\"#ff88ff\">%s</font>", msg.from);
+               String text = msg.msg;
+               String fmt  = null;
+
+               // Do IRC Colors - only partly works
+               String fg = "<font color=\"<$1>\">";
+               text = text
+                       .replaceAll("&", "&amp;")
+                       .replaceAll("<", "&lt;")
+                       .replaceAll(">", "&gt;");
+               text = text
+                       .replaceAll("\\002", "<b>")             // bold
+                       .replaceAll("\\011", "<i>")             // italic
+                       .replaceAll("\\025", "<u>");            // underline
+               text = text
+                       .replaceAll("\\003(\\d+)(,\\d+)?", fg)  // color
+                       .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color
+               text = text
+                       .replaceAll("<0?0>", "#000000")         // White
+                       .replaceAll("<0?1>", "#000000")         // Black
+                       .replaceAll("<0?2>", "#000080")         // Navy Blue
+                       .replaceAll("<0?3>", "#008000")         // Green
+                       .replaceAll("<0?4>", "#FF0000")         // Red
+                       .replaceAll("<0?5>", "#804040")         // Brown
+                       .replaceAll("<0?6>", "#8000FF")         // Purple
+                       .replaceAll("<0?7>", "#808000")         // Olive
+                       .replaceAll("<0?8>", "#FFFF00")         // Yellow
+                       .replaceAll("<0?9>", "#00FF00")         // Lime Green
+                       .replaceAll("<10>",  "#008080")         // Teal
+                       .replaceAll("<11>",  "#00FFFF")         // Aqua Light
+                       .replaceAll("<12>",  "#0000FF")         // Royal Blue
+                       .replaceAll("<13>",  "#FF00FF")         // Hot Pink
+                       .replaceAll("<14>",  "#808080")         // Dark Gray
+                       .replaceAll("<15>",  "#C0C0C0");        // Light Gray
+
+               // Message formatting
+               switch (msg.how) {
+                       case DIRECT:
+                       case MENTION:
+                       case PRIVMSG:
+                               fmt  = "<b>(%s) %s: %s</b>";
+                               break;
+                       case SENT:
+                               fmt  = "(%s) <b>%s</b>: %s";
+                               break;
+                       default:
+                               fmt  = "(%s) %s: %s";
+                               break;
+               }
+
+               String html = String.format(fmt, when, from, text);
+               this.log.append(Html.fromHtml(html + "<br />"));
+       }
+
        /* 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.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());
@@ -65,18 +132,27 @@ public class Main extends Activity
                // Chat
                switch (msg.type) {
                        case PRIVMSG:
-                               this.log.append(msg.from + ": " + msg.msg + "\n");
+                               this.display(msg);
                                break;
                        case TOPIC:
                                if (!msg.txt.equals(this.topic))
-                                       this.log.append("** Topic for " + msg.arg + ": " + msg.txt + " **\n");
+                                       this.notice("Topic for " + msg.arg + ": " + msg.txt);
                                this.topic = msg.txt;
                                break;
                        case NAMES:
                                if (!msg.txt.equals(this.names))
-                                       this.log.append("** Users in " + msg.arg + ": " + msg.txt + " **\n");
+                                       this.notice("Users in " + msg.arg + ": " + msg.txt);
                                this.names = msg.txt;
                                break;
+                       case ERROR:
+                               this.notice("Error: " + msg.txt);
+                               break;
+                       case AUTHOK:
+                               this.notice("Authentication succeeded: " + msg.txt);
+                               break;
+                       case AUTHFAIL:
+                               this.notice("Authentication failed: " + msg.txt);
+                               break;
                }
                this.lscroll.smoothScrollTo(0, this.log.getBottom());
        }
@@ -84,22 +160,37 @@ public class Main extends Activity
        private void onNotify(String text)
        {
                Os.debug("Main: onNotify - " + text);
-               this.log.append("** " + text + " **\n");
+               this.notice(text);
                this.toast.setText(text);
                this.toast.show();
        }
 
        /* 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));
+       }
+
+       private void exit()
        {
-               Os.debug("Main: stopService");
                stopService(new Intent(this, Task.class));
        }
 
@@ -140,7 +231,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);
@@ -161,6 +252,14 @@ public class Main extends Activity
                                        .newTabSpec("debug")
                                        .setIndicator("Debug")
                                        .setContent(R.id.debug));
+
+                       // Setup OpenGL view
+                       this.cards = new Cards(this);
+                       this.spades.addView(cards);
+                       
+                       // Attach to background service
+                       this.register();
+
                } catch (Exception e) {
                        Os.debug("Error setting content view", e);
                        return;
@@ -220,8 +319,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;
        }
 
@@ -230,16 +329,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.exit();
                                this.finish();
                                return true;
                        default:
@@ -254,16 +353,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);