From 6c3356d2bec53979ec7f9391de73621bad7d0480 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 18 Jul 2022 17:46:55 +0200 Subject: [PATCH] Some fixes --- .../fedilab/android/helper/CustomEmoji.java | 5 ---- .../app/fedilab/android/helper/Helper.java | 9 ++++++++ .../android/helper/SpannableHelper.java | 23 ++++++++++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/helper/CustomEmoji.java b/app/src/main/java/app/fedilab/android/helper/CustomEmoji.java index 8b860a691..d32304750 100644 --- a/app/src/main/java/app/fedilab/android/helper/CustomEmoji.java +++ b/app/src/main/java/app/fedilab/android/helper/CustomEmoji.java @@ -7,7 +7,6 @@ import android.graphics.Paint; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.text.style.ReplacementSpan; -import android.util.Log; import android.view.View; import androidx.annotation.NonNull; @@ -24,7 +23,6 @@ import app.fedilab.android.R; public class CustomEmoji extends ReplacementSpan { - private final View view; private final float scale; private final WeakReference viewWeakReference; private Drawable imageDrawable; @@ -33,7 +31,6 @@ public class CustomEmoji extends ReplacementSpan { CustomEmoji(WeakReference viewWeakReference) { Context mContext = viewWeakReference.get().getContext(); this.viewWeakReference = viewWeakReference; - view = viewWeakReference.get(); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(mContext); scale = sharedpreferences.getFloat(mContext.getString(R.string.SET_FONT_SCALE), 1.0f); } @@ -69,8 +66,6 @@ public class CustomEmoji extends ReplacementSpan { return new CustomTarget() { @Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { - Log.v(Helper.TAG, "resource: " + resource); - Log.v(Helper.TAG, "instanceof: " + (resource instanceof Animatable)); View view = viewWeakReference.get(); if (animate && resource instanceof Animatable) { Drawable.Callback callback = resource.getCallback(); diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index b85d10fe6..99be394d8 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -1739,4 +1739,13 @@ public class Helper { }).start(); } + + public static T getKeyByValue(Map map, E value) { + for (Map.Entry entry : map.entrySet()) { + if (Objects.equals(value, entry.getValue())) { + return entry.getKey(); + } + } + return null; + } } diff --git a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java index 56ab2f4e6..da408c1df 100644 --- a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java @@ -37,6 +37,7 @@ import android.text.Spanned; import android.text.TextPaint; import android.text.style.ClickableSpan; import android.text.style.URLSpan; +import android.util.Log; import android.util.Patterns; import android.view.LayoutInflater; import android.view.View; @@ -84,6 +85,7 @@ public class SpannableHelper { boolean convertHtml, WeakReference viewWeakReference) { + SpannableString initialContent; if (text == null) { return null; @@ -97,6 +99,8 @@ public class SpannableHelper { emojiList = status.emojis; } else if (account != null) { emojiList = account.emojis; + } else if (announcement != null) { + emojiList = announcement.emojis; } HashMap urlDetails = new HashMap<>(); if (convertHtml) { @@ -110,6 +114,7 @@ public class SpannableHelper { } if (url != null && urlText != null && !url.equals(urlText) && !urlText.contains(" " + url); } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) @@ -162,10 +167,20 @@ public class SpannableHelper { if (content.toString().length() < matchEnd || matchStart < 0 || matchStart > matchEnd) { continue; } - final String url = content.toString().substring(matchStart, matchEnd); - if (!url.startsWith("http")) { - continue; + String url_temp = content.toString().substring(matchStart, matchEnd).replace("…", ""); + if (urlDetails.containsValue(url_temp + "…")) { + String originalURL = Helper.getKeyByValue(urlDetails, url_temp + "…"); + if (originalURL != null) { + content.replace(matchStart, matchEnd, originalURL); + offSetTruncate += (originalURL.length() - (url_temp.length() + 1)); + matchEnd = matchStart + originalURL.length(); + url_temp = originalURL; + } } + final String url = url_temp; + /* if (!url.startsWith("http")) { + continue; + }*/ String newURL = Helper.transformURL(context, url); //If URL has been transformed if (newURL.compareTo(url) != 0) { @@ -173,10 +188,12 @@ public class SpannableHelper { offSetTruncate += (newURL.length() - url.length()); matchEnd = matchStart + newURL.length(); //The transformed URL was in the list of URLs having a different names + if (urlDetails.containsKey(url)) { urlDetails.put(newURL, urlDetails.get(url)); } } + //Truncate URL if needed //TODO: add an option to disable truncated URLs String urlText = newURL;