Java tutorial
//package com.java2s; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.HashMap; import android.graphics.Color; import android.text.Html; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.ForegroundColorSpan; public class Main { private static HashMap<String, String[]> auths = new HashMap<String, String[]>(); public static CharSequence displayPlayer(String playerName, String playerGID, boolean linkGID) { SpannableString name = colourize(playerName); if (playerGID == null) { return name; } else { SpannableString gid = linkifyGid(playerGID); return TextUtils.concat(name, " (", gid, ")"); } } public static SpannableString colourize(String raw) { String uncoloredName = uncolourize(raw); SpannableString str = new SpannableString(uncoloredName); char c, cc; String color = "RESETT"; int start = 0; int end = 0; for (int i = 0; i < raw.length() - 1; i++) { c = raw.charAt(i); cc = raw.charAt(i + 1); // if we detect a new color tag if (c == '0' && cc == 'x') { if (start != end) { str.setSpan(new ForegroundColorSpan(getColorInt(color)), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } start = end; i += 2; color = ""; for (int k = i; k < i + 6; k++) { try { color += raw.charAt(k); } catch (IndexOutOfBoundsException e) { color = ""; break; } } i += 5; } else { end++; } } str.setSpan(new ForegroundColorSpan(getColorInt(color)), start, uncoloredName.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); return str; } public static SpannableString linkifyGid(String gid) { if (gid == null) { return null; } SpannableString ret = new SpannableString(gid); String name = gid.substring(0, gid.lastIndexOf("@")); String authority = gid.substring(gid.lastIndexOf("@") + 1); String mainAuth = authority.contains("/") ? authority.substring(0, authority.indexOf("/")) : authority; if (auths.containsKey(mainAuth)) { String link; try { if (auths.get(mainAuth).length == 1) { link = auths.get(mainAuth)[0]; } else if (auths.get(mainAuth).length == 2) { link = auths.get(mainAuth)[0] + URLEncoder.encode(name, "UTF-8") + auths.get(mainAuth)[1]; } else { return ret; } } catch (UnsupportedEncodingException e) { return ret; } ret = new SpannableString(Html.fromHtml("<a href=\"" + link + "\">" + gid + "</a>")); } else if (mainAuth.matches(".*\\..*")) { // if it contains a dot // we try a link to the supposed webpage of this auth ret = new SpannableString(Html.fromHtml("<a href=\"http://" + mainAuth + "\">" + gid + "</a>")); } return ret; } public static final String uncolourize(String str) { return str.replaceAll("0x.{6}", ""); } private static int getColorInt(String color) { color = color.toUpperCase(); // following the game's convention. see // http://bazaar.launchpad.net/~armagetronad-dev/armagetronad/trunk-armagetronad-work/view/head:/src/render/rFont.cpp#L775 color = (color == "RESETT") ? "F8F8F8" : color; // we make sure the value is valid. (maybe unexpected color for people not using right codes) color = color.replaceAll("[^0-9A-F]", "F"); return Color.parseColor("#" + color); } }