|
|
|
@ -5,12 +5,16 @@ import static java.util.Arrays.asList;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.text.Spannable;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
|
import android.text.style.URLSpan;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import com.google.common.base.Strings;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import me.saket.bettermovementmethod.BetterLinkMovementMethod;
|
|
|
|
|
import org.tasks.R;
|
|
|
|
|
import org.tasks.injection.ForActivity;
|
|
|
|
|
|
|
|
|
@ -26,27 +30,56 @@ public class Linkify {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void linkify(TextView textView) {
|
|
|
|
|
linkify(textView, () -> {}, () -> {});
|
|
|
|
|
linkify(textView, () -> {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void linkify(TextView textView, Runnable onClick, Runnable onLongClick) {
|
|
|
|
|
public void linkify(TextView textView, Runnable onClick) {
|
|
|
|
|
if (textView.length() == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BetterLinkMovementMethod.linkify(android.text.util.Linkify.ALL, textView)
|
|
|
|
|
.setOnLinkClickListener((tv, url) -> handleLink(url, onClick))
|
|
|
|
|
.setOnLinkLongClickListener(
|
|
|
|
|
(tv, url) -> {
|
|
|
|
|
onLongClick.run();
|
|
|
|
|
return true;
|
|
|
|
|
android.text.util.Linkify.addLinks(textView, android.text.util.Linkify.ALL);
|
|
|
|
|
|
|
|
|
|
textView.setOnClickListener(
|
|
|
|
|
v -> {
|
|
|
|
|
if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1) {
|
|
|
|
|
onClick.run();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
URLSpan[] spans;
|
|
|
|
|
Spannable spannable;
|
|
|
|
|
CharSequence text = textView.getText();
|
|
|
|
|
if (text instanceof SpannableStringBuilder || text instanceof SpannableString) {
|
|
|
|
|
spannable = (Spannable) text;
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
spans = spannable.getSpans(0, text.length(), URLSpan.class);
|
|
|
|
|
for (URLSpan span : spans) {
|
|
|
|
|
int start = spannable.getSpanStart(span);
|
|
|
|
|
int end = spannable.getSpanEnd(span);
|
|
|
|
|
|
|
|
|
|
private boolean handleLink(String url, Runnable onEdit) {
|
|
|
|
|
spannable.removeSpan(span);
|
|
|
|
|
spannable.setSpan(new ClickHandlingURLSpan(span.getURL(), onClick), start, end, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class ClickHandlingURLSpan extends URLSpan {
|
|
|
|
|
|
|
|
|
|
private final Runnable onEdit;
|
|
|
|
|
|
|
|
|
|
ClickHandlingURLSpan(String url, Runnable onEdit) {
|
|
|
|
|
super(url);
|
|
|
|
|
this.onEdit = onEdit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View widget) {
|
|
|
|
|
String title;
|
|
|
|
|
String edit = context.getString(R.string.TAd_actionEditTask);
|
|
|
|
|
String action;
|
|
|
|
|
String url = getURL();
|
|
|
|
|
Uri uri = Uri.parse(url);
|
|
|
|
|
String scheme = uri.getScheme();
|
|
|
|
|
if (Strings.isNullOrEmpty(scheme)) {
|
|
|
|
@ -88,6 +121,6 @@ public class Linkify {
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.show();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|