- Improves hashtag detection in toots

- Clickable elements (hashtag + urls) in biography
This commit is contained in:
tom79 2017-07-24 10:50:40 +02:00
parent d3e43f1902
commit 0a56c6496e
3 changed files with 91 additions and 33 deletions

View File

@ -298,11 +298,12 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
account_ac.setVisibility(View.GONE); account_ac.setVisibility(View.GONE);
else else
account_ac.setText(account.getAcct()); account_ac.setText(account.getAcct());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
account_note.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getNote(),true), Html.FROM_HTML_MODE_COMPACT)); account_note.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getNote(),true), Html.FROM_HTML_MODE_COMPACT));
else else
//noinspection deprecation //noinspection deprecation
account_note.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getNote(), true))); account_note.setText(Html.fromHtml(Helper.shortnameToUnicode(account.getNote(), true)));*/
account_note = Helper.clickableElementsDescription(ShowAccountActivity.this, account_note,account.getNote());
tabLayout.getTabAt(0).setText(getString(R.string.status_cnt, account.getStatuses_count())); tabLayout.getTabAt(0).setText(getString(R.string.status_cnt, account.getStatuses_count()));
tabLayout.getTabAt(1).setText(getString(R.string.following_cnt, account.getFollowing_count())); tabLayout.getTabAt(1).setText(getString(R.string.following_cnt, account.getFollowing_count()));
tabLayout.getTabAt(2).setText(getString(R.string.followers_cnt, account.getFollowers_count())); tabLayout.getTabAt(2).setText(getString(R.string.followers_cnt, account.getFollowers_count()));

View File

@ -352,13 +352,11 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
holder.status_content = Helper.clickableElements(context, holder.status_content,content, holder.status_content = Helper.clickableElements(context, holder.status_content,content,
status.getReblog() != null?status.getReblog().getMentions():status.getMentions(), status.getReblog() != null?status.getReblog().getMentions():status.getMentions());
status.getReblog() != null?status.getReblog().getTags():status.getTags());
if( status.getContent_translated() != null && status.getContent_translated().length() > 0){ if( status.getContent_translated() != null && status.getContent_translated().length() > 0){
holder.status_content_translated = Helper.clickableElements(context, holder.status_content_translated,status.getContent_translated(), holder.status_content_translated = Helper.clickableElements(context, holder.status_content_translated,status.getContent_translated(),
status.getReblog() != null?status.getReblog().getMentions():status.getMentions(), status.getReblog() != null?status.getReblog().getMentions():status.getMentions());
status.getReblog() != null?status.getReblog().getTags():status.getTags());
} }
holder.status_favorite_count.setText(String.valueOf(status.getFavourites_count())); holder.status_favorite_count.setText(String.valueOf(status.getFavourites_count()));
holder.status_reblog_count.setText(String.valueOf(status.getReblogs_count())); holder.status_reblog_count.setText(String.valueOf(status.getReblogs_count()));

View File

@ -230,6 +230,7 @@ public class Helper {
"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))", "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
private static final Pattern hashtagPattern = Pattern.compile("(#[\\w_À-ú-]{1,})");
/** /**
* Converts emojis in input to unicode * Converts emojis in input to unicode
* @param input String * @param input String
@ -1011,7 +1012,7 @@ public class Helper {
* @param mentions List<Mention> * @param mentions List<Mention>
* @return TextView * @return TextView
*/ */
public static TextView clickableElements(final Context context, TextView statusTV, String fullContent, List<Mention> mentions, List<Tag> tags) { public static TextView clickableElements(final Context context, TextView statusTV, String fullContent, List<Mention> mentions) {
SpannableString spannableString; SpannableString spannableString;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
@ -1076,36 +1077,94 @@ public class Helper {
} }
} }
//Deals with tags to make them clickable Matcher matcher = hashtagPattern.matcher(spannableString);
if( tags != null && tags.size() > 0 ) { while (matcher.find()){
//Looping through tags which are attached to the toot int matchStart = matcher.start(1);
for (final Tag tag : tags) { int matchEnd = matcher.end();
String targetedTag = "#" + tag.getName(); final String tag = spannableString.toString().substring(matchStart, matchEnd);
if (spannableString.toString().contains(targetedTag)) { spannableString.setSpan(new ClickableSpan() {
@Override
int startPosition = spannableString.toString().indexOf(targetedTag); public void onClick(View textView) {
int endPosition = spannableString.toString().lastIndexOf(targetedTag) + targetedTag.length(); Intent intent = new Intent(context, HashTagActivity.class);
spannableString.setSpan(new ClickableSpan() { Bundle b = new Bundle();
@Override b.putString("tag", tag.substring(1));
public void onClick(View textView) { intent.putExtras(b);
Intent intent = new Intent(context, HashTagActivity.class); context.startActivity(intent);
Bundle b = new Bundle();
b.putString("tag", tag.getName());
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
},
startPosition, endPosition,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
} }
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
}, matchStart, matchEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
statusTV.setText(spannableString, TextView.BufferType.SPANNABLE);
statusTV.setMovementMethod(LinkMovementMethod.getInstance());
return statusTV;
}
/**
* Check if the account bio contents urls & tags and fills the content with ClickableSpan
* Click on url => webview or external app
* Click on tag => HashTagActivity
* @param context Context
* @param statusTV Textview
* @param fullContent String, should be the st
* @return TextView
*/
public static TextView clickableElementsDescription(final Context context, TextView statusTV, String fullContent) {
SpannableString spannableString;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableString = new SpannableString(Html.fromHtml(fullContent, Html.FROM_HTML_MODE_COMPACT));
else
//noinspection deprecation
spannableString = new SpannableString(Html.fromHtml(fullContent));
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true);
if( embedded_browser){
Matcher matcher = urlPattern.matcher(spannableString);
while (matcher.find()){
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
final String url = spannableString.toString().substring(matchStart, matchEnd);
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View textView) {
Intent intent = new Intent(context, WebviewActivity.class);
Bundle b = new Bundle();
b.putString("url", url);
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
}, matchStart, matchEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
} }
} }
Matcher matcher = hashtagPattern.matcher(spannableString);
while (matcher.find()){
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
final String tag = spannableString.toString().substring(matchStart, matchEnd);
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View textView) {
Intent intent = new Intent(context, HashTagActivity.class);
Bundle b = new Bundle();
b.putString("tag", tag.substring(1));
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
}, matchStart, matchEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
statusTV.setText(spannableString, TextView.BufferType.SPANNABLE); statusTV.setText(spannableString, TextView.BufferType.SPANNABLE);
statusTV.setMovementMethod(LinkMovementMethod.getInstance()); statusTV.setMovementMethod(LinkMovementMethod.getInstance());
return statusTV; return statusTV;