1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-02-03 04:39:15 +00:00

Split handleUrl method into two methods

Split handleURL method, now private, into two methods:
handleUrlCommentsTimestamp and handleUrlDescriptionTimestamp. Code is
now more proper.
This commit is contained in:
TiA4f8R 2021-04-03 18:57:16 +02:00
parent a79badd783
commit f13f4cc5d2
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
3 changed files with 53 additions and 30 deletions

View File

@ -50,7 +50,8 @@ public class CommentTextOnTouchListener implements View.OnTouchListener {
if (action == MotionEvent.ACTION_UP) { if (action == MotionEvent.ACTION_UP) {
if (link[0] instanceof URLSpan) { if (link[0] instanceof URLSpan) {
final String url = ((URLSpan) link[0]).getURL(); final String url = ((URLSpan) link[0]).getURL();
if (!InternalUrlsHandler.handleUrl(v.getContext(), url, 1)) { if (!InternalUrlsHandler.handleUrlCommentsTimestamp(v.getContext(),
url)) {
ShareUtils.openUrlInBrowser(v.getContext(), url, false); ShareUtils.openUrlInBrowser(v.getContext(), url, false);
} }
} }

View File

@ -20,6 +20,8 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.schedulers.Schedulers;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public final class InternalUrlsHandler { public final class InternalUrlsHandler {
private static final Pattern AMPERSAND_TIMESTAMP_PATTERN = Pattern.compile("(.*)&t=(\\d+)"); private static final Pattern AMPERSAND_TIMESTAMP_PATTERN = Pattern.compile("(.*)&t=(\\d+)");
private static final Pattern HASHTAG_TIMESTAMP_PATTERN = private static final Pattern HASHTAG_TIMESTAMP_PATTERN =
@ -28,48 +30,68 @@ public final class InternalUrlsHandler {
private InternalUrlsHandler() { private InternalUrlsHandler() {
} }
/**
* Handle a YouTube timestamp comment URL in NewPipe.
* <p>
* This method will check if the provided url is a YouTube comment description URL ({@code
* https://www.youtube.com/watch?v=}video_id{@code #timestamp=}time_in_seconds). If yes, the
* popup player will be opened when the user will click on the timestamp in the comment,
* at the time and for the video indicated in the timestamp.
*
* @param context the context to use
* @param url the URL to check if it can be handled
* @return true if the URL can be handled by NewPipe, false if it cannot
*/
public static boolean handleUrlCommentsTimestamp(final Context context, final String url) {
return handleUrl(context, url, HASHTAG_TIMESTAMP_PATTERN);
}
/**
* Handle a YouTube timestamp description URL in NewPipe.
* <p>
* This method will check if the provided url is a YouTube timestamp description URL ({@code
* https://www.youtube.com/watch?v=}video_id{@code &t=}time_in_seconds). If yes, the popup
* player will be opened when the user will click on the timestamp in the video description,
* at the time and for the video indicated in the timestamp.
*
* @param context the context to use
* @param url the URL to check if it can be handled
* @return true if the URL can be handled by NewPipe, false if it cannot
*/
public static boolean handleUrlDescriptionTimestamp(final Context context, final String url) {
return handleUrl(context, url, AMPERSAND_TIMESTAMP_PATTERN);
}
/** /**
* Handle an URL 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 and true will be returned;
* <p> * else, false will be returned.
* The timestamp param accepts two integers, corresponding to two timestamps types:
* 0 for {@code &t=} (used for timestamps in descriptions),
* 1 for {@code #timestamp=} (used for timestamps in comments).
* Any other value of this integer will return false.
* *
* @param context the context to be used * @param context the context to use
* @param url the URL to check if it can be handled * @param url the URL to check if it can be handled
* @param timestampType the type of timestamp * @param pattern the pattern
* @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 handleUrl(final Context context, private static boolean handleUrl(final Context context,
final String url, final String url,
final int timestampType) { final Pattern pattern) {
String matchedUrl = ""; final String matchedUrl;
int seconds = -1; final StreamingService service;
final Matcher matcher; final StreamingService.LinkType linkType;
final int seconds;
if (timestampType == 0) { final Matcher matcher = pattern.matcher(url);
matcher = AMPERSAND_TIMESTAMP_PATTERN.matcher(url); if (matcher.matches()) {
} else if (timestampType == 1) { matchedUrl = matcher.group(1);
matcher = HASHTAG_TIMESTAMP_PATTERN.matcher(url); seconds = Integer.parseInt(matcher.group(2));
} else { } else {
return false; return false;
} }
if (matcher.matches()) { if (isNullOrEmpty(matchedUrl)) {
matchedUrl = matcher.group(1);
seconds = Integer.parseInt(matcher.group(2));
}
if (matchedUrl == null || matchedUrl.isEmpty()) {
return false; return false;
} }
final StreamingService service;
final StreamingService.LinkType linkType;
try { try {
service = NewPipe.getServiceByUrl(matchedUrl); service = NewPipe.getServiceByUrl(matchedUrl);
linkType = service.getLinkTypeByUrl(matchedUrl); linkType = service.getLinkTypeByUrl(matchedUrl);

View File

@ -245,7 +245,7 @@ public final class TextLinkifier {
final String url = span.getURL(); final String url = span.getURL();
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 (!InternalUrlsHandler.handleUrl(context, url, 0)) { if (!InternalUrlsHandler.handleUrlDescriptionTimestamp(context, url)) {
ShareUtils.openUrlInBrowser(context, url, false); ShareUtils.openUrlInBrowser(context, url, false);
} }
} }