moves xmpp url linkify to TextViewUtil
This commit is contained in:
parent
fb9817091a
commit
144a82ada2
2 changed files with 56 additions and 49 deletions
|
|
@ -8,9 +8,7 @@ import android.text.SpannableString;
|
|||
import android.text.style.ForegroundColorSpan;
|
||||
|
||||
import android.text.style.StyleSpan;
|
||||
import android.text.util.Linkify;
|
||||
|
||||
import android.util.Patterns;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.View.OnLongClickListener;
|
||||
|
|
@ -22,8 +20,6 @@ import android.widget.LinearLayout;
|
|||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import de.thedevstack.conversationsplus.ConversationsPlusColors;
|
||||
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
|
||||
|
|
@ -58,11 +54,6 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
private static final int RECEIVED = 1;
|
||||
private static final int STATUS = 2;
|
||||
private static final int ME_COMMAND = 3;
|
||||
private static final Pattern XMPP_PATTERN = Pattern
|
||||
.compile("xmpp\\:(?:(?:["
|
||||
+ Patterns.GOOD_IRI_CHAR
|
||||
+ "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])"
|
||||
+ "|(?:\\%[a-fA-F0-9]{2}))+");
|
||||
|
||||
private ConversationActivity activity;
|
||||
|
||||
|
|
@ -304,6 +295,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
|
||||
viewHolder.messageBody.setVisibility(View.VISIBLE);
|
||||
viewHolder.messageBody.setIncludeFontPadding(true);
|
||||
|
||||
if (message.getBody() != null) {
|
||||
final String nick = UIHelper.getMessageDisplayName(message);
|
||||
String body;
|
||||
|
|
@ -350,30 +342,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
}
|
||||
viewHolder.messageBody.setText(span);
|
||||
}
|
||||
int patternMatchCount = 0;
|
||||
int oldAutoLinkMask = viewHolder.messageBody.getAutoLinkMask();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
viewHolder.messageBody.setTextIsSelectable(patternMatchCount <= 1);
|
||||
viewHolder.messageBody.setAutoLinkMask(0);
|
||||
Linkify.addLinks(viewHolder.messageBody, XMPP_PATTERN, "xmpp");
|
||||
viewHolder.messageBody.setAutoLinkMask(oldAutoLinkMask);
|
||||
TextViewUtil.linkifyXmpp(viewHolder.messageBody);
|
||||
} else {
|
||||
viewHolder.messageBody.setText("");
|
||||
viewHolder.messageBody.setTextIsSelectable(false);
|
||||
|
|
@ -383,22 +352,6 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
viewHolder.messageBody.setOnLongClickListener(openContextMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int countMatches(Pattern pattern, String body) {
|
||||
Matcher matcher = pattern.matcher(body);
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private void displayDownloadButton(ViewHolder viewHolder, String btnText, OnClickListener onClickListener) {
|
||||
viewHolder.download_button.setVisibility(View.VISIBLE);
|
||||
viewHolder.download_button.setText(btnText);
|
||||
|
|
|
|||
|
|
@ -2,15 +2,47 @@ package de.thedevstack.conversationsplus.utils.ui;
|
|||
|
||||
import android.support.annotation.ColorRes;
|
||||
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();
|
||||
|
|
@ -103,6 +135,28 @@ public final class TextViewUtil extends ViewUtil {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue