Adress requested changes and try some cleanup in handleUrl method of InternalUrlsHandler class

This commit is contained in:
TiA4f8R 2021-04-03 14:46:30 +02:00
parent 2702700d10
commit a79badd783
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
4 changed files with 34 additions and 34 deletions

View File

@ -48,14 +48,11 @@ public class CommentTextOnTouchListener implements View.OnTouchListener {
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
boolean handled = false;
if (link[0] instanceof URLSpan) {
handled = InternalUrlsHandler.handleUrl(v.getContext(),
((URLSpan) link[0]).getURL(), 1);
final String url = ((URLSpan) link[0]).getURL();
if (!InternalUrlsHandler.handleUrl(v.getContext(), url, 1)) {
ShareUtils.openUrlInBrowser(v.getContext(), url, false);
}
if (!handled) {
ShareUtils.openUrlInBrowser(v.getContext(),
((URLSpan) link[0]).getURL(), false);
}
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,

View File

@ -49,22 +49,20 @@ public final class InternalUrlsHandler {
final int timestampType) {
String matchedUrl = "";
int seconds = -1;
final Pattern timestampPattern;
final Matcher matcher;
if (timestampType == 0) {
timestampPattern = AMPERSAND_TIMESTAMP_PATTERN;
matcher = AMPERSAND_TIMESTAMP_PATTERN.matcher(url);
} else if (timestampType == 1) {
timestampPattern = HASHTAG_TIMESTAMP_PATTERN;
matcher = HASHTAG_TIMESTAMP_PATTERN.matcher(url);
} else {
return false;
}
final Matcher matcher = timestampPattern.matcher(url);
if (matcher.matches()) {
matchedUrl = matcher.group(1);
seconds = Integer.parseInt(matcher.group(2));
}
if (matchedUrl == null || matchedUrl.isEmpty()) {
return false;
}
@ -78,7 +76,6 @@ public final class InternalUrlsHandler {
} catch (final ExtractionException e) {
return false;
}
if (linkType == StreamingService.LinkType.NONE) {
return false;
}

View File

@ -144,6 +144,10 @@ public final class TextLinkifier {
final int hashtagEnd = hashtagsMatches.end(1);
final String parsedHashtag = descriptionText.substring(hashtagStart, hashtagEnd);
// don't add a ClickableSpan if there is already one, which should be a part of an URL,
// already parsed before
if (spannableDescription.getSpans(hashtagStart, hashtagEnd,
ClickableSpan.class).length == 0) {
spannableDescription.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull final View view) {
@ -153,6 +157,7 @@ public final class TextLinkifier {
}, hashtagStart, hashtagEnd, 0);
}
}
}
/**
* Add click listeners which opens the popup player on timestamps in a plain text.
@ -181,19 +186,18 @@ public final class TextLinkifier {
final String parsedTimestamp = descriptionText.substring(timestampStart, timestampEnd);
final String[] timestampParts = parsedTimestamp.split(":");
final int time;
if (timestampParts.length == 3) { // timestamp format: XX:XX:XX
time = Integer.parseInt(timestampParts[0]) * 3600 // hours
+ Integer.parseInt(timestampParts[1]) * 60 // minutes
+ Integer.parseInt(timestampParts[2]); // seconds
spannableDescription.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull final View view) {
playOnPopup(context, contentUrl, streamingService, time);
}
}, timestampStart, timestampEnd, 0);
} else if (timestampParts.length == 2) { // timestamp format: XX:XX
time = Integer.parseInt(timestampParts[0]) * 60 // minutes
+ Integer.parseInt(timestampParts[1]); // seconds
} else {
continue;
}
spannableDescription.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull final View view) {
@ -202,7 +206,6 @@ public final class TextLinkifier {
}, timestampStart, timestampEnd, 0);
}
}
}
/**
* Change links generated by libraries in the description of a content to a custom link action
@ -239,10 +242,11 @@ public final class TextLinkifier {
final URLSpan[] urls = textBlockLinked.getSpans(0, chars.length(), URLSpan.class);
for (final URLSpan span : urls) {
final String url = span.getURL();
final ClickableSpan clickableSpan = new ClickableSpan() {
public void onClick(@NonNull final View view) {
if (!InternalUrlsHandler.handleUrl(context, span.getURL(), 0)) {
ShareUtils.openUrlInBrowser(context, span.getURL(), false);
if (!InternalUrlsHandler.handleUrl(context, url, 0)) {
ShareUtils.openUrlInBrowser(context, url, false);
}
}
};

View File

@ -375,6 +375,8 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
final Intent intent = new Intent(Intent.ACTION_CHOOSER);
intent.putExtra(Intent.EXTRA_INTENT, shareIntent);
// unneeded to set a title to the chooser on Android P and higher because the system
// ignores this title on these versions
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
intent.putExtra(Intent.EXTRA_TITLE, mContext.getString(R.string.share_dialog_title));
}