From 046316d122c4f06d2f2522217525d86b7d6a84b7 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 9 Aug 2021 16:15:22 +0200 Subject: [PATCH] Use gesture detector for links, etc --- .../eu/faircode/email/AdapterMessage.java | 146 ++++++++++-------- .../main/res/layout/include_message_body.xml | 1 + 2 files changed, 80 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 29d96bcce8..edcb5ad8e5 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -67,6 +67,7 @@ import android.text.TextUtils; import android.text.format.DateUtils; import android.text.method.ArrowKeyMovementMethod; import android.text.method.LinkMovementMethod; +import android.text.method.MovementMethod; import android.text.style.DynamicDrawableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.ImageSpan; @@ -76,6 +77,7 @@ import android.text.style.StyleSpan; import android.text.style.URLSpan; import android.util.Pair; import android.util.TypedValue; +import android.view.GestureDetector; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; @@ -483,6 +485,81 @@ public class AdapterMessage extends RecyclerView.Adapter 0 && image[0].getSource() != null) { + ImageHelper.AnnotatedSource a = new ImageHelper.AnnotatedSource(image[0].getSource()); + Uri uri = Uri.parse(a.getSource()); + if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) + if (onOpenLink(uri, null, false)) + return true; + } + } + + URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); + if (link.length > 0) { + String url = link[0].getURL(); + Uri uri = Uri.parse(url); + if (uri.getScheme() == null) + uri = Uri.parse("https://" + url); + + int start = buffer.getSpanStart(link[0]); + int end = buffer.getSpanEnd(link[0]); + String title = (start < 0 || end < 0 || end <= start + ? null : buffer.subSequence(start, end).toString()); + if (url.equals(title)) + title = null; + + if (onOpenLink(uri, title, false)) + return true; + } + + ImageSpan[] image = buffer.getSpans(off, off, ImageSpan.class); + if (image.length > 0) { + ImageHelper.AnnotatedSource a = new ImageHelper.AnnotatedSource(image[0].getSource()); + String source = a.getSource(); + if (!TextUtils.isEmpty(source)) { + if (!a.isTracking()) + onOpenImage(message.id, source); + return true; + } + } + + DynamicDrawableSpan[] ddss = buffer.getSpans(off, off, DynamicDrawableSpan.class); + if (ddss.length > 0) { + properties.setValue("quotes", message.id, true); + bindBody(message, false); + return true; + } + + return false; + } + }); + + @Override + public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { + return gestureDetector.onTouchEvent(event); + } + }; + private SimpleTask taskContactInfo; ViewHolder(final View itemView, long viewType) { @@ -798,6 +875,7 @@ public class AdapterMessage extends RecyclerView.Adapter= Build.VERSION_CODES.Q) bindConversationActions(message, args.getParcelable("actions")); @@ -4583,70 +4659,6 @@ public class AdapterMessage extends RecyclerView.Adapter 0 && image[0].getSource() != null) { - ImageHelper.AnnotatedSource a = new ImageHelper.AnnotatedSource(image[0].getSource()); - Uri uri = Uri.parse(a.getSource()); - if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) - if (onOpenLink(uri, null, false)) - return true; - } - } - - URLSpan[] link = buffer.getSpans(off, off, URLSpan.class); - if (link.length > 0) { - String url = link[0].getURL(); - Uri uri = Uri.parse(url); - if (uri.getScheme() == null) - uri = Uri.parse("https://" + url); - - int start = buffer.getSpanStart(link[0]); - int end = buffer.getSpanEnd(link[0]); - String title = (start < 0 || end < 0 || end <= start - ? null : buffer.subSequence(start, end).toString()); - if (url.equals(title)) - title = null; - - if (onOpenLink(uri, title, false)) - return true; - } - - ImageSpan[] image = buffer.getSpans(off, off, ImageSpan.class); - if (image.length > 0) { - ImageHelper.AnnotatedSource a = new ImageHelper.AnnotatedSource(image[0].getSource()); - String source = a.getSource(); - if (!TextUtils.isEmpty(source)) { - if (!a.isTracking()) - onOpenImage(message.id, source); - return true; - } - } - - DynamicDrawableSpan[] ddss = buffer.getSpans(off, off, DynamicDrawableSpan.class); - if (ddss.length > 0) { - properties.setValue("quotes", message.id, true); - bindBody(message, false); - } - } - - return super.onTouchEvent(widget, buffer, event); - } - } - private boolean onOpenLink(Uri uri, String title, boolean always_confirm) { Log.i("Opening uri=" + uri + " title=" + title); uri = Uri.parse(uri.toString().replaceAll("\\s+", "")); diff --git a/app/src/main/res/layout/include_message_body.xml b/app/src/main/res/layout/include_message_body.xml index d12574fb49..6ed9a7fc5c 100644 --- a/app/src/main/res/layout/include_message_body.xml +++ b/app/src/main/res/layout/include_message_body.xml @@ -97,6 +97,7 @@ android:paddingBottom="6dp" android:text="Body" android:textAppearance="@style/TextAppearance.AppCompat.Small" + android:textIsSelectable="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvDownloading" />