package de.thedevstack.conversationsplus.utils.ui; import android.support.annotation.ColorRes; import android.support.annotation.IdRes; import android.support.annotation.StringRes; import android.text.util.Linkify; import android.util.Patterns; import android.view.View; import android.widget.TextView; import java.util.regex.Matcher; import java.util.regex.Pattern; import de.thedevstack.conversationsplus.ConversationsPlusColors; /** * */ public final class TextViewUtil extends ViewUtil { public static void linkifyXmpp(TextView tv) { int patternMatchCount = 0; int oldAutoLinkMask = tv.getAutoLinkMask(); CharSequence body = tv.getText(); // first check if we have a match on XMPP_PATTERN so we do not have to check for EMAIL_ADDRESSES patternMatchCount += countMatches(XMPP_PATTERN, body); if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0 && patternMatchCount > 0) { oldAutoLinkMask -= Linkify.EMAIL_ADDRESSES; } // count matches for all patterns if ((Linkify.WEB_URLS & oldAutoLinkMask) != 0) { patternMatchCount += countMatches(Patterns.WEB_URL, body); } if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0) { patternMatchCount += countMatches(Patterns.EMAIL_ADDRESS, body); } if ((Linkify.PHONE_NUMBERS & oldAutoLinkMask) != 0) { patternMatchCount += countMatches(Patterns.PHONE, body); } tv.setTextIsSelectable(patternMatchCount <= 1); tv.setAutoLinkMask(0); Linkify.addLinks(tv, XMPP_PATTERN, "xmpp"); tv.setAutoLinkMask(oldAutoLinkMask); } public static void setTextWithoutAutoLink(TextView tv, CharSequence text) { int oldAutoLinkMask = tv.getAutoLinkMask(); tv.setAutoLinkMask(0); tv.setText(text); tv.setAutoLinkMask(oldAutoLinkMask); } public static TextView showAndSetText(TextView tv, CharSequence text) { visible(tv); return setText(tv, text); } public static TextView showAndSetText(TextView tv, int textResId) { visible(tv); return setText(tv, textResId); } public static TextView setText(TextView tv, CharSequence text) { if (null != tv) { tv.setText(text); } return tv; } public static TextView setText(TextView tv, int textResId) { if (null != tv) { tv.setText(textResId); } return tv; } public static TextView setText(View parentView, int textViewId, CharSequence text) { return setText((TextView) parentView.findViewById(textViewId), text); } public static TextView setText(View parentView, int textViewId, int textResId) { return setText((TextView) parentView.findViewById(textViewId), textResId); } public static void enable(TextView tv) { setColorEnabledAndTextResId(tv, null, true, null); } public static void enable(TextView tv, String text) { setColorEnabledAndText(tv, null, true, text); } public static void enable(TextView tv, Integer color) { setColorEnabledAndTextResId(tv, color, true, null); } public static void enable(TextView tv, Integer color, @StringRes Integer resid) { setColorEnabledAndTextResId(tv, color, true, resid); } public static void disable(TextView tv) { setColorEnabledAndTextResId(tv, null, false, null); } public static void disable(TextView tv, String text) { setColorEnabledAndText(tv, null, false, text); } public static void disable(TextView tv, Integer color) { setColorEnabledAndTextResId(tv, color, false, null); } public static void disable(TextView tv, Integer color, @StringRes Integer resid) { setColorEnabledAndTextResId(tv, color, false, resid); } public static void setColor(TextView tv, Integer color) { setColorByIdEnabledAndTextResId(tv, color, null, null); } private static void setColorByIdEnabledAndTextResId(TextView tv, @ColorRes Integer colorResId, Boolean enabled, @StringRes Integer resid) { Integer color = null; if (null != colorResId) { color = ConversationsPlusColors.byId(colorResId); } setColorEnabledAndTextResId(tv, color, enabled, resid); } public static void setColorEnabledAndTextResId(TextView tv, Integer color, Boolean enabled, @StringRes Integer resid) { if (null != color) { tv.setTextColor(color); } if (enabled != null) { tv.setEnabled(enabled); } if (resid != null) { setText(tv, resid); } } public static void setColorEnabledAndText(TextView tv, Integer color, Boolean enabled, String text) { if (null != color) { tv.setTextColor(color); } if (enabled != null) { tv.setEnabled(enabled); } if (text != null) { setText(tv, text); } } /** * Counts the number of occurrences of the pattern in body. * * @param pattern the pattern to match * @param body the body to find the pattern * @return the number of occurrences */ private static int countMatches(Pattern pattern, CharSequence body) { Matcher matcher = pattern.matcher(body); int count = 0; while (matcher.find()) { count++; } return count; } private static final Pattern XMPP_PATTERN = Pattern .compile("xmpp\\:(?:(?:[" + Patterns.GOOD_IRI_CHAR + "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])" + "|(?:\\%[a-fA-F0-9]{2}))+"); private TextViewUtil() { // avoid instantiation - helper class } }