2017-04-28 13:29:42 +10:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
package com.keylesspalace.tusky.util;
|
2017-04-25 21:30:57 +10:00
|
|
|
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
import android.text.style.ClickableSpan;
|
|
|
|
import android.text.style.URLSpan;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.interfaces.LinkListener;
|
2017-04-25 21:30:57 +10:00
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
public class LinkHelper {
|
|
|
|
public static void setClickableText(TextView view, Spanned content,
|
|
|
|
@Nullable Status.Mention[] mentions, boolean useCustomTabs,
|
|
|
|
final LinkListener listener) {
|
2017-04-25 21:30:57 +10:00
|
|
|
SpannableStringBuilder builder = new SpannableStringBuilder(content);
|
|
|
|
URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
|
|
|
|
for (URLSpan span : urlSpans) {
|
|
|
|
int start = builder.getSpanStart(span);
|
|
|
|
int end = builder.getSpanEnd(span);
|
|
|
|
int flags = builder.getSpanFlags(span);
|
|
|
|
CharSequence text = builder.subSequence(start, end);
|
|
|
|
if (text.charAt(0) == '#') {
|
|
|
|
final String tag = text.subSequence(1, text.length()).toString();
|
|
|
|
ClickableSpan newSpan = new ClickableSpan() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View widget) {
|
|
|
|
listener.onViewTag(tag);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
builder.removeSpan(span);
|
|
|
|
builder.setSpan(newSpan, start, end, flags);
|
|
|
|
} else if (text.charAt(0) == '@' && mentions != null) {
|
|
|
|
final String accountUsername = text.subSequence(1, text.length()).toString();
|
|
|
|
String id = null;
|
|
|
|
for (Status.Mention mention : mentions) {
|
|
|
|
if (mention.localUsername.equals(accountUsername)) {
|
|
|
|
id = mention.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (id != null) {
|
|
|
|
final String accountId = id;
|
|
|
|
ClickableSpan newSpan = new ClickableSpan() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View widget) {
|
|
|
|
listener.onViewAccount(accountId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
builder.removeSpan(span);
|
|
|
|
builder.setSpan(newSpan, start, end, flags);
|
|
|
|
}
|
|
|
|
} else if (useCustomTabs) {
|
|
|
|
ClickableSpan newSpan = new CustomTabURLSpan(span.getURL());
|
|
|
|
builder.removeSpan(span);
|
|
|
|
builder.setSpan(newSpan, start, end, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view.setText(builder);
|
|
|
|
view.setLinksClickable(true);
|
|
|
|
view.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
}
|
|
|
|
}
|