From: Andy Spencer Date: Wed, 24 Apr 2013 08:28:21 +0000 (+0000) Subject: Display IRC colors in text view X-Git-Url: http://pileus.org/git/?p=~andy%2Fspades;a=commitdiff_plain;h=7c538be264da4cdd35827febfe3ed2cd43626963 Display IRC colors in text view --- diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index e186078..201bdd2 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -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("*** " + text + "
")); + 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("%s", msg.from); - String text = msg.msg; - String fmt = null; - - // Do IRC Colors - only partly works - String fg = "\">"; - text = text - .replaceAll("&", "&") - .replaceAll("<", "<") - .replaceAll(">", ">"); - text = text - .replaceAll("\\002", "") // bold - .replaceAll("\\011", "") // italic - .replaceAll("\\025", ""); // 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 = "(%s) %s: %s"; - break; - case SENT: - fmt = "(%s) %s: %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 + "
")); + // Append the message + this.log.append(span); } /* Private handler methods */ diff --git a/src/org/pileus/spades/Message.java b/src/org/pileus/spades/Message.java index 68a78f0..61bf2bb 100644 --- a/src/org/pileus/spades/Message.java +++ b/src/org/pileus/spades/Message.java @@ -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); } }; diff --git a/src/org/pileus/spades/Os.java b/src/org/pileus/spades/Os.java index 4f9e0fb..39cbcb6 100644 --- a/src/org/pileus/spades/Os.java +++ b/src/org/pileus/spades/Os.java @@ -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); + } }