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) { if (action == MotionEvent.ACTION_UP) {
boolean handled = false; boolean handled = false;
if (link[0] instanceof URLSpan) { if (link[0] instanceof URLSpan) {
handled = URLHandler.canHandleUrl(v.getContext(), handled = URLHandler.handleUrl(v.getContext(),
((URLSpan) link[0]).getURL(), 1); ((URLSpan) link[0]).getURL(), 1);
} }
if (!handled) { if (!handled) {

View file

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

View file

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