From 464a646671d08eef6b21ee5300053cccd0bb42c6 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Tue, 6 Sep 2022 09:20:52 +0530 Subject: [PATCH] Use LinkifyCompat. --- .../holder/CommentsMiniInfoItemHolder.java | 51 +++++++------------ 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java index b900750a8..fd03fcc3a 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java @@ -12,6 +12,7 @@ import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.text.util.LinkifyCompat; import org.schabi.newpipe.R; import org.schabi.newpipe.error.ErrorUtil; @@ -27,7 +28,7 @@ import org.schabi.newpipe.util.PicassoHelper; import org.schabi.newpipe.util.external_communication.ShareUtils; import org.schabi.newpipe.util.external_communication.TimestampExtractor; -import java.util.regex.Matcher; +import java.util.Objects; public class CommentsMiniInfoItemHolder extends InfoItemHolder { private static final String TAG = "CommentsMiniIIHolder"; @@ -39,7 +40,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { private final int commentVerticalPadding; private final RelativeLayout itemRoot; - public final ImageView itemThumbnailView; + private final ImageView itemThumbnailView; private final TextView itemContentView; private final TextView itemLikesCountView; private final TextView itemPublishedTime; @@ -47,27 +48,6 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { private String commentText; private String streamUrl; - private final Linkify.TransformFilter timestampLink = new Linkify.TransformFilter() { - @Override - public String transformUrl(final Matcher match, final String url) { - try { - final TimestampExtractor.TimestampMatchDTO timestampMatchDTO = - TimestampExtractor.getTimestampFromMatcher(match, commentText); - - if (timestampMatchDTO == null) { - return url; - } - - return streamUrl + url.replace( - match.group(0), - "#timestamp=" + timestampMatchDTO.seconds()); - } catch (final Exception ex) { - Log.e(TAG, "Unable to process url='" + url + "' as timestampLink", ex); - return url; - } - } - }; - CommentsMiniInfoItemHolder(final InfoItemBuilder infoItemBuilder, final int layoutId, final ViewGroup parent) { super(infoItemBuilder, layoutId, parent); @@ -243,14 +223,21 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { } private void linkify() { - Linkify.addLinks( - itemContentView, - Linkify.WEB_URLS); - Linkify.addLinks( - itemContentView, - TimestampExtractor.TIMESTAMPS_PATTERN, - null, - null, - timestampLink); + LinkifyCompat.addLinks(itemContentView, Linkify.WEB_URLS); + LinkifyCompat.addLinks(itemContentView, TimestampExtractor.TIMESTAMPS_PATTERN, null, null, + (match, url) -> { + try { + final var timestampMatch = TimestampExtractor + .getTimestampFromMatcher(match, commentText); + if (timestampMatch == null) { + return url; + } + return streamUrl + url.replace(Objects.requireNonNull(match.group(0)), + "#timestamp=" + timestampMatch.seconds()); + } catch (final Exception ex) { + Log.e(TAG, "Unable to process url='" + url + "' as timestampLink", ex); + return url; + } + }); } }