From eba2c6f06713e498168c4ef2705eb7255c18cab6 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 26 Aug 2020 15:00:37 +0300 Subject: [PATCH] Copy YouTube videos links onLongClick --- .../mthmmy/utils/parsing/ParseHelpers.java | 20 ++++++++++++------- .../thmmy/mthmmy/views/ReactiveWebView.java | 19 ++++++++++++++++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java index f602dfb2..a4acb3ec 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java @@ -128,6 +128,8 @@ public class ParseHelpers { } } + public static final String VIDEO_ID_PARAMETER = "videoId"; + /** * This method fixes html so that embedded videos will render properly and be lightweight. * @@ -146,24 +148,28 @@ public class ParseHelpers { } String fixed = html.outerHtml(); - int tmp_counter = 0; + int counter = 0; while (fixed.contains(" embededVideosUrls.size()) + if (counter > embededVideosUrls.size()) break; + final String videoId = embededVideosUrls.get(counter); fixed = fixed.replace( fixed.substring(fixed.indexOf("") + 9) , "
" + "" + "
" ); - ++tmp_counter; + ++counter; } return fixed; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/views/ReactiveWebView.java b/app/src/main/java/gr/thmmy/mthmmy/views/ReactiveWebView.java index 7c9efdb8..eddd6781 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/views/ReactiveWebView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/views/ReactiveWebView.java @@ -3,6 +3,7 @@ package gr.thmmy.mthmmy.views; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.net.Uri; import android.util.AttributeSet; import android.view.MotionEvent; import android.webkit.WebView; @@ -13,6 +14,7 @@ import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.utils.ui.ImageDownloadDialogBuilder; import static android.content.Context.CLIPBOARD_SERVICE; +import static gr.thmmy.mthmmy.utils.parsing.ParseHelpers.VIDEO_ID_PARAMETER; import static gr.thmmy.mthmmy.utils.ui.PhotoViewUtils.displayPhotoViewImage; public class ReactiveWebView extends WebView { @@ -77,8 +79,16 @@ public class ReactiveWebView extends WebView { copyUrlToClipboard(result.getExtra()); else if(result.getType() == WebView.HitTestResult.IMAGE_TYPE) { String imageURL = result.getExtra(); - ImageDownloadDialogBuilder builder = new ImageDownloadDialogBuilder(context,imageURL); - builder.show(); + showImageDownloadDialog(imageURL); + } + else if(result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { + final String imageURL = result.getExtra(); + Uri uri = Uri.parse(imageURL); + String videoId = uri.getQueryParameter(VIDEO_ID_PARAMETER); + if (videoId!=null) + copyUrlToClipboard("https://www.youtube.com/watch?v=" + videoId); + else + showImageDownloadDialog(imageURL); } return false; }); @@ -90,4 +100,9 @@ public class ReactiveWebView extends WebView { clipboard.setPrimaryClip(clip); Toast.makeText(BaseApplication.getInstance().getApplicationContext(),context.getString(R.string.link_copied_msg),Toast.LENGTH_SHORT).show(); } + + private void showImageDownloadDialog(String imageURL){ + ImageDownloadDialogBuilder builder = new ImageDownloadDialogBuilder(context, imageURL); + builder.show(); + } }