Apply suggested changes and fix some warnings

This commit is contained in:
TiA4f8R 2021-03-18 18:42:25 +01:00
parent 6abdd2a6d8
commit c972940338
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
3 changed files with 28 additions and 25 deletions

View File

@ -47,7 +47,7 @@ public class CommentTextOnTouchListener implements View.OnTouchListener {
if (action == MotionEvent.ACTION_UP) {
boolean handled = false;
if (link[0] instanceof URLSpan) {
handled = URLHandler.canHandleUrl(v.getContext(),
handled = URLHandler.handleUrl(v.getContext(),
((URLSpan) link[0]).getURL(), 1);
}
if (!handled) {

View File

@ -29,6 +29,8 @@ import static org.schabi.newpipe.util.URLHandler.playOnPopup;
public final class TextLinkifier {
public static final String TAG = TextLinkifier.class.getSimpleName();
private static final Pattern TIMESTAMPS_PATTERN_IN_PLAIN_TEXT =
Pattern.compile("(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])");
private TextLinkifier() {
}
@ -115,9 +117,6 @@ public final class TextLinkifier {
streamingService, contentUrl);
}
private static final Pattern TIMESTAMPS_PATTERN_IN_PLAIN_TEXT =
Pattern.compile("(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])");
/**
* Add click listeners which opens the popup player on timestamps in a plain text.
* <p>
@ -125,11 +124,11 @@ public final class TextLinkifier {
* using a regular expression, adds for each a {@link ClickableSpan} which opens the popup
* player at the time indicated in the timestamps.
*
* @param context the context to use
* @param spannableDescription the SpannableStringBuilder with the text of the
* content description
* @param contentUrl the URL of the content
* @param streamingService the {@link StreamingService} of the content
* @param context the context to use
* @param spannableDescription the SpannableStringBuilder with the text of the
* content description
* @param contentUrl the URL of the content
* @param streamingService the {@link StreamingService} of the content
*/
private static void addClickListenersOnTimestamps(final Context context,
final SpannableStringBuilder
@ -144,23 +143,24 @@ public final class TextLinkifier {
final int timestampEnd = timestampMatches.end(0);
final String parsedTimestamp = descriptionText.substring(timestampStart, timestampEnd);
final String[] timestampParts = parsedTimestamp.split(":");
final int seconds;
final int time;
if (timestampParts.length == 3) { // timestamp format: XX:XX:XX
seconds = Integer.parseInt(timestampParts[0]) * 3600 + Integer.parseInt(
timestampParts[1]) * 60 + Integer.parseInt(timestampParts[2]);
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, seconds);
playOnPopup(context, contentUrl, streamingService, time);
}
}, timestampStart, timestampEnd, 0);
} else if (timestampParts.length == 2) { // timestamp format: XX:XX
seconds = Integer.parseInt(timestampParts[0]) * 60 + Integer.parseInt(
timestampParts[1]);
time = Integer.parseInt(timestampParts[0]) * 60 // minutes
+ Integer.parseInt(timestampParts[1]); // seconds
spannableDescription.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull final View view) {
playOnPopup(context, contentUrl, streamingService, seconds);
playOnPopup(context, contentUrl, streamingService, time);
}
}, timestampStart, timestampEnd, 0);
}
@ -203,7 +203,7 @@ public final class TextLinkifier {
for (final URLSpan span : urls) {
final ClickableSpan clickableSpan = new ClickableSpan() {
public void onClick(@NonNull final View view) {
if (!URLHandler.canHandleUrl(context, span.getURL(), 0)) {
if (!URLHandler.handleUrl(context, span.getURL(), 0)) {
ShareUtils.openUrlInBrowser(context, span.getURL(), false);
}
}

View File

@ -19,12 +19,15 @@ import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;
public final class URLHandler {
private static final Pattern AMPERSAND_TIMESTAMP_PATTERN = Pattern.compile("(.*)&t=(\\d+)");
private static final Pattern HASHTAG_TIMESTAMP_PATTERN =
Pattern.compile("(.*)#timestamp=(\\d+)");
private URLHandler() {
}
/**
* Check if an URL can be handled in NewPipe.
* Handle an URL in NewPipe.
* <p>
* This method will check if the provided url can be handled in NewPipe or not. If this is a
* service URL with a timestamp, the popup player will be opened.
@ -39,17 +42,17 @@ public final class URLHandler {
* @param timestampType the type of timestamp
* @return true if the URL can be handled by NewPipe, false if it cannot
*/
public static boolean canHandleUrl(final Context context,
final String url,
final int timestampType) {
public static boolean handleUrl(final Context context,
final String url,
final int timestampType) {
String matchedUrl = "";
int seconds = -1;
final Pattern timestampPattern;
if (timestampType == 0) {
timestampPattern = Pattern.compile("(.*)&t=(\\d+)");
timestampPattern = AMPERSAND_TIMESTAMP_PATTERN;
} else if (timestampType == 1) {
timestampPattern = Pattern.compile("(.*)#timestamp=(\\d+)");
timestampPattern = HASHTAG_TIMESTAMP_PATTERN;
} else {
return false;
}
@ -107,13 +110,13 @@ public final class URLHandler {
return false;
}
final Single single
final Single<StreamInfo> single
= ExtractorHelper.getStreamInfo(service.getServiceId(), cleanUrl, false);
single.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(info -> {
final PlayQueue playQueue
= new SinglePlayQueue((StreamInfo) info, seconds * 1000);
= new SinglePlayQueue(info, seconds * 1000);
NavigationHelper.playOnPopupPlayer(context, playQueue, false);
});
return true;