]> Pileus Git - ~andy/spades/commitdiff
Display IRC colors in text view
authorAndy Spencer <andy753421@gmail.com>
Wed, 24 Apr 2013 08:28:21 +0000 (08:28 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 24 Apr 2013 08:38:50 +0000 (08:38 +0000)
src/org/pileus/spades/Main.java
src/org/pileus/spades/Message.java
src/org/pileus/spades/Os.java

index e186078efe810bdefe3ca8353a95db127db326f6..201bdd29eca67af0db75fac8543f70b654210c85 100644 (file)
@@ -2,13 +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.preference.PreferenceManager;
-import android.text.Html;
-import android.text.method.ScrollingMovementMethod;
+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;
@@ -51,64 +58,56 @@ public class Main extends Activity
        /* Private helper methods */
        private void notice(String text)
        {
-               this.log.append(Html.fromHtml("<b>*** " + text + "</b><br />"));
+               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 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 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;
+
+               // Format date and name
+               span.setSpan(new ForegroundColorSpan(0xffffff88), 0,    de, 0);
+               span.setSpan(new ForegroundColorSpan(0xffff88ff), 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;
                }
 
-               String html = String.format(fmt, when, from, text);
-               this.log.append(Html.fromHtml(html + "<br />"));
+               // Append the message
+               this.log.append(span);
        }
 
        /* Private handler methods */
index 68a78f0797cc0479edfca73893f7a8e87eb5fbc3..61bf2bbed9654bf2035c0f33cba303059e14b438 100644 (file)
@@ -37,12 +37,14 @@ public class Message
                public int     code;
                public String  hex;
                public String  name;
+               public int     color;
 
                public Color(int code, String hex, String name)
                {
                        this.code  = code;
                        this.hex   = hex;
                        this.name  = name;
+                       this.color = Os.getColor(hex);
                }
        };
 
index 4f9e0fb4ce0d62d70d2adefd9231f643d5ffcf65..39cbcb62bdf80197cfabe978da48a03730661b7a 100644 (file)
@@ -2,6 +2,7 @@ package org.pileus.spades;
 
 import android.util.Log;
 import android.util.Base64;
+import android.graphics.Color;
 
 public class Os
 {
@@ -20,4 +21,9 @@ public class Os
        {
                return Base64.encodeToString(txt.getBytes(), 0);
        }
+
+       public static int getColor(String hex)
+       {
+               return Color.parseColor("#"+hex);
+       }
 }