2019-03-01 07:58:32 +00:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
2019-03-01 23:42:06 +00:00
|
|
|
import android.content.Context;
|
2019-03-01 07:58:32 +00:00
|
|
|
import android.text.Layout;
|
|
|
|
import android.text.Selection;
|
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.style.ClickableSpan;
|
2019-03-01 23:42:06 +00:00
|
|
|
import android.text.style.URLSpan;
|
2019-03-01 07:58:32 +00:00
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-03-01 23:42:06 +00:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
|
|
|
import org.schabi.newpipe.player.playqueue.PlayQueue;
|
|
|
|
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
|
|
|
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import io.reactivex.Single;
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
2019-03-01 07:58:32 +00:00
|
|
|
public class CommentTextOnTouchListener implements View.OnTouchListener {
|
|
|
|
|
|
|
|
public static final CommentTextOnTouchListener INSTANCE = new CommentTextOnTouchListener();
|
|
|
|
|
2019-03-13 01:31:03 +00:00
|
|
|
private static final Pattern timestampPattern = Pattern.compile("(.*)#timestamp=(\\d+)");
|
2019-03-01 23:42:06 +00:00
|
|
|
|
2019-03-01 07:58:32 +00:00
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
if(!(v instanceof TextView)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TextView widget = (TextView) v;
|
|
|
|
Object text = widget.getText();
|
|
|
|
if (text instanceof Spanned) {
|
|
|
|
Spannable buffer = (Spannable) text;
|
|
|
|
|
|
|
|
int action = event.getAction();
|
|
|
|
|
|
|
|
if (action == MotionEvent.ACTION_UP
|
|
|
|
|| action == MotionEvent.ACTION_DOWN) {
|
|
|
|
int x = (int) event.getX();
|
|
|
|
int y = (int) event.getY();
|
|
|
|
|
|
|
|
x -= widget.getTotalPaddingLeft();
|
|
|
|
y -= widget.getTotalPaddingTop();
|
|
|
|
|
|
|
|
x += widget.getScrollX();
|
|
|
|
y += widget.getScrollY();
|
|
|
|
|
|
|
|
Layout layout = widget.getLayout();
|
|
|
|
int line = layout.getLineForVertical(y);
|
|
|
|
int off = layout.getOffsetForHorizontal(line, x);
|
|
|
|
|
|
|
|
ClickableSpan[] link = buffer.getSpans(off, off,
|
|
|
|
ClickableSpan.class);
|
|
|
|
|
|
|
|
if (link.length != 0) {
|
|
|
|
if (action == MotionEvent.ACTION_UP) {
|
2019-03-01 23:42:06 +00:00
|
|
|
boolean handled = false;
|
|
|
|
if(link[0] instanceof URLSpan){
|
|
|
|
handled = handleUrl(v.getContext(), (URLSpan) link[0]);
|
|
|
|
}
|
|
|
|
if(!handled) link[0].onClick(widget);
|
2019-03-01 07:58:32 +00:00
|
|
|
} else if (action == MotionEvent.ACTION_DOWN) {
|
|
|
|
Selection.setSelection(buffer,
|
|
|
|
buffer.getSpanStart(link[0]),
|
|
|
|
buffer.getSpanEnd(link[0]));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-01 23:42:06 +00:00
|
|
|
|
|
|
|
private boolean handleUrl(Context context, URLSpan urlSpan) {
|
|
|
|
String url = urlSpan.getURL();
|
2019-03-13 01:31:03 +00:00
|
|
|
int seconds = -1;
|
|
|
|
Matcher matcher = timestampPattern.matcher(url);
|
|
|
|
if(matcher.matches()){
|
|
|
|
url = matcher.group(1);
|
|
|
|
seconds = Integer.parseInt(matcher.group(2));
|
|
|
|
}
|
2019-03-01 23:42:06 +00:00
|
|
|
StreamingService service;
|
|
|
|
StreamingService.LinkType linkType;
|
|
|
|
try {
|
|
|
|
service = NewPipe.getServiceByUrl(url);
|
|
|
|
linkType = service.getLinkTypeByUrl(url);
|
|
|
|
} catch (ExtractionException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(linkType == StreamingService.LinkType.NONE){
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-13 01:31:03 +00:00
|
|
|
if(linkType == StreamingService.LinkType.STREAM && seconds != -1){
|
2019-03-01 23:42:06 +00:00
|
|
|
return playOnPopup(context, url, service, seconds);
|
|
|
|
}else{
|
|
|
|
NavigationHelper.openRouterActivity(context, url);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean playOnPopup(Context context, String url, StreamingService service, int seconds) {
|
|
|
|
LinkHandlerFactory factory = service.getStreamLHFactory();
|
|
|
|
String cleanUrl = null;
|
|
|
|
try {
|
|
|
|
cleanUrl = factory.getUrl(factory.getId(url));
|
|
|
|
} catch (ParsingException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Single single = ExtractorHelper.getStreamInfo(service.getServiceId(), cleanUrl, false);
|
|
|
|
single.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(info -> {
|
2019-03-13 01:31:03 +00:00
|
|
|
PlayQueue playQueue = new SinglePlayQueue((StreamInfo) info, seconds*1000);
|
2019-04-13 07:31:32 +00:00
|
|
|
NavigationHelper.playOnPopupPlayer(context, playQueue, false);
|
2019-03-01 23:42:06 +00:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-01 07:58:32 +00:00
|
|
|
}
|