Allow comment links (if any) to gain focus

This commit is contained in:
Alexander 2019-10-09 17:09:07 +07:00 committed by Alexander--
parent 79c962fc88
commit 6da2b399e8

View file

@ -1,6 +1,9 @@
package org.schabi.newpipe.info_list.holder; package org.schabi.newpipe.info_list.holder;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
@ -122,15 +125,35 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
}); });
} }
private void allowLinkFocus() {
if (itemView.isInTouchMode()) {
return;
}
URLSpan[] urls = itemContentView.getUrls();
if (urls != null && urls.length != 0) {
itemContentView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
private void ellipsize() { private void ellipsize() {
boolean hasEllipsis = false;
if (itemContentView.getLineCount() > commentDefaultLines){ if (itemContentView.getLineCount() > commentDefaultLines){
int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1); int endOfLastLine = itemContentView.getLayout().getLineEnd(commentDefaultLines - 1);
int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine -2); int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine -2);
if(end == -1) end = Math.max(endOfLastLine -2, 0); if(end == -1) end = Math.max(endOfLastLine -2, 0);
String newVal = itemContentView.getText().subSequence(0, end) + ""; String newVal = itemContentView.getText().subSequence(0, end) + "";
itemContentView.setText(newVal); itemContentView.setText(newVal);
hasEllipsis = true;
} }
linkify(); linkify();
if (!hasEllipsis) {
allowLinkFocus();
}
} }
private void toggleEllipsize() { private void toggleEllipsize() {
@ -145,11 +168,13 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
itemContentView.setMaxLines(commentExpandedLines); itemContentView.setMaxLines(commentExpandedLines);
itemContentView.setText(commentText); itemContentView.setText(commentText);
linkify(); linkify();
allowLinkFocus();
} }
private void linkify(){ private void linkify(){
Linkify.addLinks(itemContentView, Linkify.WEB_URLS); Linkify.addLinks(itemContentView, Linkify.WEB_URLS);
Linkify.addLinks(itemContentView, pattern, null, null, timestampLink); Linkify.addLinks(itemContentView, pattern, null, null, timestampLink);
itemContentView.setMovementMethod(null); itemContentView.setMovementMethod(null);
} }
} }